Skip to content

Commit d834af9

Browse files
authored
Remove uses of deprecated gtk3 apis (slgobinath#560)
* fix deprecations * port to Gtk.Application Gtk.main() and Gtk.main_quit() are dropped in gtk4 in favor of subclassing Gtk.Application. This commit also moves argument handling from a separate thread to GtkApplication.do_startup(). * fix deprecations in settings dialog
1 parent 05d95ad commit d834af9

File tree

4 files changed

+58
-52
lines changed

4 files changed

+58
-52
lines changed

safeeyes/__main__.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,14 @@
2525
import logging
2626
import signal
2727
import sys
28-
from threading import Timer
2928

30-
import gi
3129
import psutil
3230
from safeeyes import utility
3331
from safeeyes.model import Config
3432
from safeeyes.safeeyes import SafeEyes
3533
from safeeyes.safeeyes import SAFE_EYES_VERSION
3634
from safeeyes.rpc import RPCClient
3735

38-
gi.require_version('Gtk', '3.0')
39-
from gi.repository import Gtk
40-
4136
gettext.install('safeeyes', utility.LOCALE_PATH)
4237

4338

@@ -68,22 +63,6 @@ def __running():
6863
return False
6964

7065

71-
def __evaluate_arguments(args, safe_eyes):
72-
"""
73-
Evaluate the arguments and execute the operations.
74-
"""
75-
if args.about:
76-
utility.execute_main_thread(safe_eyes.show_about)
77-
elif args.disable:
78-
utility.execute_main_thread(safe_eyes.disable_safeeyes)
79-
elif args.enable:
80-
utility.execute_main_thread(safe_eyes.enable_safeeyes)
81-
elif args.settings:
82-
utility.execute_main_thread(safe_eyes.show_settings)
83-
elif args.take_break:
84-
utility.execute_main_thread(safe_eyes.take_break)
85-
86-
8766
def main():
8867
"""
8968
Start the Safe Eyes.
@@ -147,10 +126,8 @@ def main():
147126
sys.exit(0)
148127
elif not args.quit:
149128
logging.info("Starting Safe Eyes")
150-
safe_eyes = SafeEyes(system_locale, config)
129+
safe_eyes = SafeEyes(system_locale, config, args)
151130
safe_eyes.start()
152-
Timer(1.0, lambda: __evaluate_arguments(args, safe_eyes)).start()
153-
Gtk.main()
154131

155132

156133
if __name__ == '__main__':

safeeyes/safeeyes.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,21 @@
3838
from safeeyes.ui.settings_dialog import SettingsDialog
3939

4040
gi.require_version('Gtk', '3.0')
41-
from gi.repository import Gtk
41+
from gi.repository import Gtk, Gio
4242

4343
SAFE_EYES_VERSION = "2.1.9"
4444

4545

46-
class SafeEyes:
46+
class SafeEyes(Gtk.Application):
4747
"""
4848
This class represents a runnable Safe Eyes instance.
4949
"""
5050

51-
def __init__(self, system_locale, config):
51+
def __init__(self, system_locale, config, cli_args):
52+
super().__init__(
53+
application_id="io.github.slgobinath.SafeEyes",
54+
flags=Gio.ApplicationFlags.IS_SERVICE
55+
)
5256
self.active = False
5357
self.break_screen = None
5458
self.safe_eyes_core = None
@@ -58,6 +62,7 @@ def __init__(self, system_locale, config):
5862
self.settings_dialog_active = False
5963
self.rpc_server = None
6064
self._status = ''
65+
self.cli_args = cli_args
6166

6267
# Initialize the Safe Eyes Context
6368
self.context['version'] = SAFE_EYES_VERSION
@@ -98,6 +103,9 @@ def __init__(self, system_locale, config):
98103
self.context['api']['postpone'] = self.safe_eyes_core.postpone
99104
self.context['api']['get_break_time'] = self.safe_eyes_core.get_break_time
100105
self.plugins_manager.init(self.context, self.config)
106+
107+
self.hold()
108+
101109
atexit.register(self.persist_session)
102110

103111
def start(self):
@@ -114,6 +122,22 @@ def start(self):
114122
self.safe_eyes_core.start()
115123
self.handle_system_suspend()
116124

125+
self.run()
126+
127+
def do_startup(self):
128+
Gtk.Application.do_startup(self)
129+
130+
if self.cli_args.about:
131+
self.show_about()
132+
elif self.cli_args.disable:
133+
self.disable_safeeyes()
134+
elif self.cli_args.enable:
135+
self.enable_safeeyes()
136+
elif self.cli_args.settings:
137+
self.show_settings()
138+
elif self.cli_args.take_break:
139+
self.take_break()
140+
117141
def show_settings(self):
118142
"""
119143
Listen to tray icon Settings action and send the signal to Settings dialog.
@@ -144,9 +168,8 @@ def quit(self):
144168
self.plugins_manager.exit()
145169
self.__stop_rpc_server()
146170
self.persist_session()
147-
Gtk.main_quit()
148-
# Exit all threads
149-
os._exit(0)
171+
172+
super().quit()
150173

151174
def handle_suspend_callback(self, sleeping):
152175
"""

safeeyes/ui/break_screen.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,14 @@ def __show_break_screen(self, message, image_path, widget, tray_actions):
151151
# Lock the keyboard
152152
utility.start_thread(self.__lock_keyboard)
153153

154-
screen = Gtk.Window().get_screen()
155-
no_of_monitors = screen.get_n_monitors()
154+
display = Gdk.Display.get_default()
155+
screen = display.get_default_screen()
156+
no_of_monitors = display.get_n_monitors()
156157
logging.info("Show break screens in %d display(s)", no_of_monitors)
157158

158-
for monitor in range(no_of_monitors):
159-
monitor_gemoetry = screen.get_monitor_geometry(monitor)
159+
for monitor_num in range(no_of_monitors):
160+
monitor = display.get_monitor(monitor_num)
161+
monitor_gemoetry = monitor.get_geometry()
160162
x = monitor_gemoetry.x
161163
y = monitor_gemoetry.y
162164

@@ -165,7 +167,7 @@ def __show_break_screen(self, message, image_path, widget, tray_actions):
165167
builder.connect_signals(self)
166168

167169
window = builder.get_object("window_main")
168-
window.set_title("SafeEyes-" + str(monitor))
170+
window.set_title("SafeEyes-" + str(monitor_num))
169171
lbl_message = builder.get_object("lbl_message")
170172
lbl_count = builder.get_object("lbl_count")
171173
lbl_widget = builder.get_object("lbl_widget")
@@ -188,15 +190,15 @@ def __show_break_screen(self, message, image_path, widget, tray_actions):
188190
# Add the buttons
189191
if self.enable_postpone:
190192
# Add postpone button
191-
btn_postpone = Gtk.Button(_('Postpone'))
193+
btn_postpone = Gtk.Button.new_with_label(_('Postpone'))
192194
btn_postpone.get_style_context().add_class('btn_postpone')
193195
btn_postpone.connect('clicked', self.on_postpone_clicked)
194196
btn_postpone.set_visible(True)
195197
box_buttons.pack_start(btn_postpone, True, True, 0)
196198

197199
if not self.strict_break:
198200
# Add the skip button
199-
btn_skip = Gtk.Button(_('Skip'))
201+
btn_skip = Gtk.Button.new_with_label(_('Skip'))
200202
btn_skip.get_style_context().add_class('btn_skip')
201203
btn_skip.connect('clicked', self.on_skip_clicked)
202204
btn_skip.set_visible(True)
@@ -222,7 +224,7 @@ def __show_break_screen(self, message, image_path, widget, tray_actions):
222224
window.resize(monitor_gemoetry.width, monitor_gemoetry.height)
223225
window.stick()
224226
window.set_keep_above(True)
225-
window.fullscreen()
227+
window.fullscreen_on_monitor(screen, monitor_num)
226228
window.present()
227229
# In other desktop environments, move the window after present
228230
window.move(x, y)

safeeyes/ui/settings_dialog.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,16 @@ def __confirmation_dialog_response(widget, response_id):
170170
self.__initialize(self.config)
171171
widget.destroy()
172172

173-
messagedialog = Gtk.MessageDialog(parent=self.window,
174-
flags=Gtk.DialogFlags.MODAL,
175-
type=Gtk.MessageType.WARNING,
176-
buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
177-
_("Reset"), Gtk.ResponseType.OK),
178-
message_format=_("Are you sure you want to reset all settings to default?"))
173+
messagedialog = Gtk.MessageDialog()
174+
messagedialog.set_modal(True)
175+
messagedialog.set_transient_for(self.window)
176+
messagedialog.set_property('message_type', Gtk.MessageType.WARNING)
177+
messagedialog.set_property('text', _("Are you sure you want to reset all settings to default?"))
178+
messagedialog.set_property('secondary-text', _("You can't undo this action."))
179+
messagedialog.add_button('_Cancel', Gtk.ResponseType.CANCEL)
180+
messagedialog.add_button(_("Reset"), Gtk.ResponseType.OK)
181+
179182
messagedialog.connect("response", __confirmation_dialog_response)
180-
messagedialog.format_secondary_text(_("You can't undo this action."))
181183
messagedialog.show()
182184

183185
def __delete_break(self, break_config, is_short, on_remove):
@@ -194,14 +196,16 @@ def __confirmation_dialog_response(widget, response_id):
194196
on_remove()
195197
widget.destroy()
196198

197-
messagedialog = Gtk.MessageDialog(parent=self.window,
198-
flags=Gtk.DialogFlags.MODAL,
199-
type=Gtk.MessageType.WARNING,
200-
buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
201-
_("Delete"), Gtk.ResponseType.OK),
202-
message_format=_("Are you sure you want to delete this break?"))
199+
messagedialog = Gtk.MessageDialog()
200+
messagedialog.set_modal(True)
201+
messagedialog.set_transient_for(self.window)
202+
messagedialog.set_property('message_type', Gtk.MessageType.WARNING)
203+
messagedialog.set_property('text', _("Are you sure you want to delete this break?"))
204+
messagedialog.set_property('secondary-text', _("You can't undo this action."))
205+
messagedialog.add_button('_Cancel', Gtk.ResponseType.CANCEL)
206+
messagedialog.add_button(_("Delete"), Gtk.ResponseType.OK)
207+
203208
messagedialog.connect("response", __confirmation_dialog_response)
204-
messagedialog.format_secondary_text(_("You can't undo this action."))
205209
messagedialog.show()
206210

207211
def __create_plugin_item(self, plugin_config):

0 commit comments

Comments
 (0)