Skip to content

Commit

Permalink
solaar: Add type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
MattHag authored and pfps committed Oct 8, 2024
1 parent 0481950 commit 128ec43
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
15 changes: 13 additions & 2 deletions lib/solaar/dbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from __future__ import annotations

import logging

from typing import Callable

logger = logging.getLogger(__name__)

try:
Expand Down Expand Up @@ -49,14 +52,22 @@ def _suspend_or_resume(suspend):
_LOGIND_INTERFACE = "org.freedesktop.login1.Manager"


def watch_suspend_resume(on_resume_callback=None, on_suspend_callback=None):
def watch_suspend_resume(
on_resume_callback: Callable[[], None] | None = None,
on_suspend_callback: Callable[[], None] | None = None,
):
"""Register callback for suspend/resume events.
They are called only if the system DBus is running, and the Login daemon is available."""
global _resume_callback, _suspend_callback
_suspend_callback = on_suspend_callback
_resume_callback = on_resume_callback
if bus is not None and on_resume_callback is not None or on_suspend_callback is not None:
bus.add_signal_receiver(_suspend_or_resume, "PrepareForSleep", dbus_interface=_LOGIND_INTERFACE, path=_LOGIND_PATH)
bus.add_signal_receiver(
_suspend_or_resume,
"PrepareForSleep",
dbus_interface=_LOGIND_INTERFACE,
path=_LOGIND_PATH,
)
if logger.isEnabledFor(logging.INFO):
logger.info("connected to system dbus, watching for suspend/resume events")

Expand Down
20 changes: 17 additions & 3 deletions lib/solaar/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import logging

from typing import Callable

import gi
import yaml

Expand All @@ -43,6 +45,9 @@
assert Gtk.get_major_version() > 2, "Solaar requires Gtk 3 python bindings"


APP_ID = "io.github.pwr_solaar.solaar"


def _startup(app, startup_hook, use_tray, show_window):
if logger.isEnabledFor(logging.DEBUG):
logger.debug("startup registered=%s, remote=%s", app.get_is_registered(), app.get_is_remote())
Expand Down Expand Up @@ -88,12 +93,21 @@ def _shutdown(app, shutdown_hook):
desktop_notifications.uninit()


def run_loop(startup_hook, shutdown_hook, use_tray, show_window):
def run_loop(
startup_hook: Callable[[], None],
shutdown_hook: Callable[[], None],
use_tray: bool,
show_window: bool,
):
assert use_tray or show_window, "need either tray or visible window"
APP_ID = "io.github.pwr_solaar.solaar"

application = Gtk.Application.new(APP_ID, Gio.ApplicationFlags.HANDLES_COMMAND_LINE)

application.connect("startup", lambda app, startup_hook: _startup(app, startup_hook, use_tray, show_window), startup_hook)
application.connect(
"startup",
lambda app, startup_hook: _startup(app, startup_hook, use_tray, show_window),
startup_hook,
)
application.connect("command-line", _command_line)
application.connect("activate", _activate)
application.connect("shutdown", _shutdown, shutdown_hook)
Expand Down

0 comments on commit 128ec43

Please sign in to comment.