Skip to content

Commit 37548f9

Browse files
authored
Merge pull request slgobinath#716 from deltragon/gtk4-shortcuts
break screen shortcuts: implement on wayland
2 parents e49e420 + 001e83a commit 37548f9

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

safeeyes/config/locale/safeeyes.pot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,3 +561,6 @@ msgstr ""
561561
#, python-format
562562
msgid "Old stylesheet found at '%(old)s', ignoring. For custom styles, create a new stylesheet in '%(new)s' instead."
563563
msgstr ""
564+
565+
msgid "Customizing the postpone and skip shortcuts does not work on Wayland."
566+
msgstr ""

safeeyes/ui/break_screen.py

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ def __init__(self, application, context, on_skipped, on_postponed):
5252
self.enable_postpone = False
5353
self.enable_shortcut = False
5454
self.is_pretified = False
55-
self.keycode_shortcut_postpone = 65
56-
self.keycode_shortcut_skip = 9
55+
self.keycode_shortcut_postpone = 65 # Space
56+
self.keycode_shortcut_skip = 9 # Escape
5757
self.on_postponed = on_postponed
5858
self.on_skipped = on_skipped
5959
self.shortcut_disable_time = 2
6060
self.strict_break = False
6161
self.windows = []
62+
self.show_skip_button = False
63+
self.show_postpone_button = False
6264

6365
if not self.context["is_wayland"]:
6466
self.x11_display = Display()
@@ -69,6 +71,17 @@ def initialize(self, config):
6971
self.enable_postpone = config.get("allow_postpone", False)
7072
self.keycode_shortcut_postpone = config.get("shortcut_postpone", 65)
7173
self.keycode_shortcut_skip = config.get("shortcut_skip", 9)
74+
75+
if self.context["is_wayland"] and (
76+
self.keycode_shortcut_postpone != 65 or self.keycode_shortcut_skip != 9
77+
):
78+
logging.warning(
79+
_(
80+
"Customizing the postpone and skip shortcuts does not work on "
81+
"Wayland."
82+
)
83+
)
84+
7285
self.shortcut_disable_time = config.get("shortcut_disable_time", 2)
7386
self.strict_break = config.get("strict_break", False)
7487

@@ -146,7 +159,12 @@ def __show_break_screen(self, message, image_path, widget, tray_actions):
146159
logging.info("Show break screens in %d display(s)", len(monitors))
147160

148161
skip_button_disabled = self.context.get("skip_button_disabled", False)
162+
self.show_skip_button = not self.strict_break and not skip_button_disabled
163+
149164
postpone_button_disabled = self.context.get("postpone_button_disabled", False)
165+
self.show_postpone_button = (
166+
self.enable_postpone and not postpone_button_disabled
167+
)
150168

151169
i = 0
152170

@@ -157,6 +175,15 @@ def __show_break_screen(self, message, image_path, widget, tray_actions):
157175
window = builder.get_object("window_main")
158176
window.set_application(self.application)
159177
window.connect("close-request", self.on_window_delete)
178+
179+
if self.context["is_wayland"]:
180+
# Note: in theory, this could also be used on X11
181+
# however, that already has its own implementation below
182+
controller = Gtk.EventControllerKey()
183+
controller.connect("key_pressed", self.on_key_pressed_wayland)
184+
controller.set_propagation_phase(Gtk.PropagationPhase.CAPTURE)
185+
window.add_controller(controller)
186+
160187
window.set_title("SafeEyes-" + str(i))
161188
lbl_message = builder.get_object("lbl_message")
162189
lbl_count = builder.get_object("lbl_count")
@@ -182,15 +209,15 @@ def __show_break_screen(self, message, image_path, widget, tray_actions):
182209
toolbar_button.show()
183210

184211
# Add the buttons
185-
if self.enable_postpone and not postpone_button_disabled:
212+
if self.show_postpone_button:
186213
# Add postpone button
187214
btn_postpone = Gtk.Button.new_with_label(_("Postpone"))
188215
btn_postpone.get_style_context().add_class("btn_postpone")
189216
btn_postpone.connect("clicked", self.on_postpone_clicked)
190217
btn_postpone.set_visible(True)
191218
box_buttons.append(btn_postpone)
192219

193-
if not self.strict_break and not skip_button_disabled:
220+
if self.show_skip_button:
194221
# Add the skip button
195222
btn_skip = Gtk.Button.new_with_label(_("Skip"))
196223
btn_skip.get_style_context().add_class("btn_skip")
@@ -214,6 +241,11 @@ def __show_break_screen(self, message, image_path, widget, tray_actions):
214241
window.fullscreen_on_monitor(monitor)
215242
window.present()
216243

244+
# this ensures that none of the buttons is in focus immediately
245+
# otherwise, pressing space presses that button instead of triggering the
246+
# shortcut
247+
window.set_focus(None)
248+
217249
if not self.context["is_wayland"]:
218250
self.__window_set_keep_above_x11(window)
219251

@@ -284,20 +316,31 @@ def __lock_keyboard_x11(self):
284316
if self.enable_shortcut and event.type == X.KeyPress:
285317
if (
286318
event.detail == self.keycode_shortcut_skip
287-
and not self.strict_break
319+
and self.show_skip_button
288320
):
289321
self.skip_break()
290322
break
291323
elif (
292-
self.enable_postpone
293-
and event.detail == self.keycode_shortcut_postpone
324+
event.detail == self.keycode_shortcut_postpone
325+
and self.show_postpone_button
294326
):
295327
self.postpone_break()
296328
break
297329
else:
298330
# Reduce the CPU usage by sleeping for a second
299331
time.sleep(1)
300332

333+
def on_key_pressed_wayland(self, event_controller_key, keyval, keycode, state):
334+
if self.enable_shortcut:
335+
if keyval == Gdk.KEY_space and self.show_postpone_button:
336+
self.postpone_break()
337+
return True
338+
elif keyval == Gdk.KEY_Escape and self.show_skip_button:
339+
self.skip_break()
340+
return True
341+
342+
return False
343+
301344
def __release_keyboard_x11(self):
302345
"""Release the locked keyboard."""
303346
logging.info("Unlock the keyboard")

0 commit comments

Comments
 (0)