@@ -52,13 +52,15 @@ def __init__(self, application, context, on_skipped, on_postponed):
52
52
self .enable_postpone = False
53
53
self .enable_shortcut = False
54
54
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
57
57
self .on_postponed = on_postponed
58
58
self .on_skipped = on_skipped
59
59
self .shortcut_disable_time = 2
60
60
self .strict_break = False
61
61
self .windows = []
62
+ self .show_skip_button = False
63
+ self .show_postpone_button = False
62
64
63
65
if not self .context ["is_wayland" ]:
64
66
self .x11_display = Display ()
@@ -69,6 +71,17 @@ def initialize(self, config):
69
71
self .enable_postpone = config .get ("allow_postpone" , False )
70
72
self .keycode_shortcut_postpone = config .get ("shortcut_postpone" , 65 )
71
73
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
+
72
85
self .shortcut_disable_time = config .get ("shortcut_disable_time" , 2 )
73
86
self .strict_break = config .get ("strict_break" , False )
74
87
@@ -146,7 +159,12 @@ def __show_break_screen(self, message, image_path, widget, tray_actions):
146
159
logging .info ("Show break screens in %d display(s)" , len (monitors ))
147
160
148
161
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
+
149
164
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
+ )
150
168
151
169
i = 0
152
170
@@ -157,6 +175,15 @@ def __show_break_screen(self, message, image_path, widget, tray_actions):
157
175
window = builder .get_object ("window_main" )
158
176
window .set_application (self .application )
159
177
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
+
160
187
window .set_title ("SafeEyes-" + str (i ))
161
188
lbl_message = builder .get_object ("lbl_message" )
162
189
lbl_count = builder .get_object ("lbl_count" )
@@ -182,15 +209,15 @@ def __show_break_screen(self, message, image_path, widget, tray_actions):
182
209
toolbar_button .show ()
183
210
184
211
# Add the buttons
185
- if self .enable_postpone and not postpone_button_disabled :
212
+ if self .show_postpone_button :
186
213
# Add postpone button
187
214
btn_postpone = Gtk .Button .new_with_label (_ ("Postpone" ))
188
215
btn_postpone .get_style_context ().add_class ("btn_postpone" )
189
216
btn_postpone .connect ("clicked" , self .on_postpone_clicked )
190
217
btn_postpone .set_visible (True )
191
218
box_buttons .append (btn_postpone )
192
219
193
- if not self .strict_break and not skip_button_disabled :
220
+ if self .show_skip_button :
194
221
# Add the skip button
195
222
btn_skip = Gtk .Button .new_with_label (_ ("Skip" ))
196
223
btn_skip .get_style_context ().add_class ("btn_skip" )
@@ -214,6 +241,11 @@ def __show_break_screen(self, message, image_path, widget, tray_actions):
214
241
window .fullscreen_on_monitor (monitor )
215
242
window .present ()
216
243
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
+
217
249
if not self .context ["is_wayland" ]:
218
250
self .__window_set_keep_above_x11 (window )
219
251
@@ -284,20 +316,31 @@ def __lock_keyboard_x11(self):
284
316
if self .enable_shortcut and event .type == X .KeyPress :
285
317
if (
286
318
event .detail == self .keycode_shortcut_skip
287
- and not self .strict_break
319
+ and self .show_skip_button
288
320
):
289
321
self .skip_break ()
290
322
break
291
323
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
294
326
):
295
327
self .postpone_break ()
296
328
break
297
329
else :
298
330
# Reduce the CPU usage by sleeping for a second
299
331
time .sleep (1 )
300
332
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
+
301
344
def __release_keyboard_x11 (self ):
302
345
"""Release the locked keyboard."""
303
346
logging .info ("Unlock the keyboard" )
0 commit comments