Refine config and runtime flow
This commit is contained in:
parent
85e082dd46
commit
b3be444625
16 changed files with 642 additions and 137 deletions
|
|
@ -80,7 +80,7 @@ class X11Adapter:
|
|||
def run_tray(self, state_getter: Callable[[], str], on_quit: Callable[[], None]) -> None:
|
||||
self.menu = Gtk.Menu()
|
||||
quit_item = Gtk.MenuItem(label="Quit")
|
||||
quit_item.connect("activate", lambda *_: on_quit())
|
||||
quit_item.connect("activate", lambda *_: self._handle_quit(on_quit))
|
||||
self.menu.append(quit_item)
|
||||
self.menu.show_all()
|
||||
if self.indicator is not None:
|
||||
|
|
@ -90,24 +90,39 @@ class X11Adapter:
|
|||
GLib.timeout_add(TRAY_UPDATE_MS, self._update_tray, state_getter)
|
||||
Gtk.main()
|
||||
|
||||
def _listen(self, hotkey: str, callback: Callable[[], None]) -> None:
|
||||
disp = display.Display()
|
||||
root = disp.screen().root
|
||||
mods, keysym = self._parse_hotkey(hotkey)
|
||||
keycode = self._grab_hotkey(disp, root, mods, keysym)
|
||||
def request_quit(self) -> None:
|
||||
GLib.idle_add(Gtk.main_quit)
|
||||
|
||||
def _handle_quit(self, on_quit: Callable[[], None]) -> None:
|
||||
try:
|
||||
on_quit()
|
||||
finally:
|
||||
self.request_quit()
|
||||
|
||||
def _listen(self, hotkey: str, callback: Callable[[], None]) -> None:
|
||||
disp = None
|
||||
root = None
|
||||
keycode = None
|
||||
try:
|
||||
disp = display.Display()
|
||||
root = disp.screen().root
|
||||
mods, keysym = self._parse_hotkey(hotkey)
|
||||
keycode = self._grab_hotkey(disp, root, mods, keysym)
|
||||
while True:
|
||||
ev = disp.next_event()
|
||||
if ev.type == X.KeyPress and ev.detail == keycode:
|
||||
state = ev.state & ~(X.LockMask | X.Mod2Mask)
|
||||
if state == mods:
|
||||
callback()
|
||||
except Exception as exc:
|
||||
logging.error("hotkey listener stopped: %s", exc)
|
||||
finally:
|
||||
try:
|
||||
root.ungrab_key(keycode, X.AnyModifier)
|
||||
disp.sync()
|
||||
except Exception:
|
||||
pass
|
||||
if root is not None and keycode is not None and disp is not None:
|
||||
try:
|
||||
root.ungrab_key(keycode, X.AnyModifier)
|
||||
disp.sync()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _parse_hotkey(self, hotkey: str):
|
||||
parts = [p.strip() for p in hotkey.split("+") if p.strip()]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue