48 lines
1.6 KiB
Python
48 lines
1.6 KiB
Python
"""Adw.Application with single-instance nxm:// forwarding."""
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
import gi
|
|
|
|
gi.require_version("Gtk", "4.0")
|
|
gi.require_version("Adw", "1")
|
|
from gi.repository import Adw, Gio, GLib
|
|
|
|
from .. import APP_ID
|
|
from ..core.manager import Manager
|
|
from .window import MainWindow
|
|
|
|
|
|
class App(Adw.Application):
|
|
def __init__(self):
|
|
# HANDLES_COMMAND_LINE makes a second launch (e.g. the browser opening nxm://) forward its
|
|
# arguments to this already-running instance over D-Bus instead of opening a second window.
|
|
super().__init__(application_id=APP_ID, flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE)
|
|
self.manager: Manager | None = None
|
|
self.win: MainWindow | None = None
|
|
quit_action = Gio.SimpleAction.new("quit", None)
|
|
quit_action.connect("activate", lambda *_: self.quit())
|
|
self.add_action(quit_action)
|
|
self.set_accels_for_action("app.quit", ["<Primary>q"])
|
|
|
|
def _window(self) -> MainWindow:
|
|
if self.win is None:
|
|
self.manager = Manager()
|
|
self.win = MainWindow(self, self.manager)
|
|
return self.win
|
|
|
|
def do_activate(self):
|
|
self._window().present()
|
|
|
|
def do_command_line(self, cmdline: Gio.ApplicationCommandLine) -> int:
|
|
args = cmdline.get_arguments()[1:]
|
|
win = self._window()
|
|
win.present()
|
|
for a in args:
|
|
if a.startswith("nxm://"):
|
|
GLib.idle_add(lambda url=a: (win.handle_nxm(url), False)[1])
|
|
elif Path(a).exists():
|
|
win.import_paths([Path(a)])
|
|
return 0
|