Skip to content

Commit

Permalink
port to Gtk.Application
Browse files Browse the repository at this point in the history
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().
  • Loading branch information
deltragon committed Jan 2, 2024
1 parent f45b83c commit 1ebcfa2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
25 changes: 1 addition & 24 deletions safeeyes/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,14 @@
import logging
import signal
import sys
from threading import Timer

import gi
import psutil
from safeeyes import utility
from safeeyes.model import Config
from safeeyes.safeeyes import SafeEyes
from safeeyes.safeeyes import SAFE_EYES_VERSION
from safeeyes.rpc import RPCClient

gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

gettext.install('safeeyes', utility.LOCALE_PATH)


Expand Down Expand Up @@ -68,22 +63,6 @@ def __running():
return False


def __evaluate_arguments(args, safe_eyes):
"""
Evaluate the arguments and execute the operations.
"""
if args.about:
utility.execute_main_thread(safe_eyes.show_about)
elif args.disable:
utility.execute_main_thread(safe_eyes.disable_safeeyes)
elif args.enable:
utility.execute_main_thread(safe_eyes.enable_safeeyes)
elif args.settings:
utility.execute_main_thread(safe_eyes.show_settings)
elif args.take_break:
utility.execute_main_thread(safe_eyes.take_break)


def main():
"""
Start the Safe Eyes.
Expand Down Expand Up @@ -147,10 +126,8 @@ def main():
sys.exit(0)
elif not args.quit:
logging.info("Starting Safe Eyes")
safe_eyes = SafeEyes(system_locale, config)
safe_eyes = SafeEyes(system_locale, config, args)
safe_eyes.start()
Timer(1.0, lambda: __evaluate_arguments(args, safe_eyes)).start()
Gtk.main()


if __name__ == '__main__':
Expand Down
35 changes: 29 additions & 6 deletions safeeyes/safeeyes.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,21 @@
from safeeyes.ui.settings_dialog import SettingsDialog

gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gtk, Gio

SAFE_EYES_VERSION = "2.1.6"


class SafeEyes:
class SafeEyes(Gtk.Application):
"""
This class represents a runnable Safe Eyes instance.
"""

def __init__(self, system_locale, config):
def __init__(self, system_locale, config, cli_args):
super().__init__(
application_id="io.github.slgobinath.SafeEyes",
flags=Gio.ApplicationFlags.IS_SERVICE
)
self.active = False
self.break_screen = None
self.safe_eyes_core = None
Expand All @@ -58,6 +62,7 @@ def __init__(self, system_locale, config):
self.settings_dialog_active = False
self.rpc_server = None
self._status = ''
self.cli_args = cli_args

# Initialize the Safe Eyes Context
self.context['version'] = SAFE_EYES_VERSION
Expand Down Expand Up @@ -98,6 +103,9 @@ def __init__(self, system_locale, config):
self.context['api']['postpone'] = self.safe_eyes_core.postpone
self.context['api']['get_break_time'] = self.safe_eyes_core.get_break_time
self.plugins_manager.init(self.context, self.config)

self.hold()

atexit.register(self.persist_session)

def start(self):
Expand All @@ -114,6 +122,22 @@ def start(self):
self.safe_eyes_core.start()
self.handle_system_suspend()

self.run()

def do_startup(self):
Gtk.Application.do_startup(self)

if self.cli_args.about:
self.show_about()
elif self.cli_args.disable:
self.disable_safeeyes()
elif self.cli_args.enable:
self.enable_safeeyes()
elif self.cli_args.settings:
self.show_settings()
elif self.cli_args.take_break:
self.take_break()

def show_settings(self):
"""
Listen to tray icon Settings action and send the signal to Settings dialog.
Expand Down Expand Up @@ -144,9 +168,8 @@ def quit(self):
self.plugins_manager.exit()
self.__stop_rpc_server()
self.persist_session()
Gtk.main_quit()
# Exit all threads
os._exit(0)

super().quit()

def handle_suspend_callback(self, sleeping):
"""
Expand Down

0 comments on commit 1ebcfa2

Please sign in to comment.