Dotfiles/desktopenvs/hyprdrive/station-bar/main.py

93 lines
3.1 KiB
Python

#!/usr/bin/env python3
"""station-bar — the EWW top bar's replacement for hyprdrive (codename:
Voidstation Status Bar). Single-instance, same pattern as the rest of the
Cosmonaut Shell suite: the first launch builds the (visible, reserved-space)
bar and holds; later invocations forward their verb over D-Bus via
scripts/station-bar.sh instead of spawning a second python3+GTK4 process.
main.py run the resident instance (bar shown by default)
main.py --show show the bar (restores its exclusive zone)
main.py --hide hide the bar (releases its exclusive zone)
main.py --toggle whichever of the above applies
"""
from __future__ import annotations
import os
import sys
from pathlib import Path
# station-bar-start.sh LD_PRELOADs libgtk4-layer-shell (load-ordering
# requirement ahead of libwayland-client). Drop it once resident so it isn't
# inherited by anything this process launches (see orbit-menu/horizon-dock/
# astro-menu's main.py for the same rationale).
os.environ.pop("LD_PRELOAD", None)
sys.path.insert(0, str(Path(__file__).resolve().parent))
import gi # noqa: E402
gi.require_version("Gtk", "4.0")
from gi.repository import Gio, GLib, Gtk # noqa: E402
import config # noqa: E402
import theme # noqa: E402
from bar import StationBar # noqa: E402
from paths import APP_ID # noqa: E402
class StationBarApp(Gtk.Application):
def __init__(self) -> None:
super().__init__(application_id=APP_ID,
flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE)
self.window: StationBar | None = None
def do_startup(self) -> None:
Gtk.Application.do_startup(self)
theme.load_css()
self.window = StationBar(hologram_enabled=config.hologram_enabled())
self._register_actions()
self.hold() # stay alive even while the bar is briefly hidden
def _register_actions(self) -> None:
def add(name: str, callback) -> None:
action = Gio.SimpleAction.new(name, None)
action.connect("activate", callback)
self.add_action(action)
def guarded(fn):
def wrapper(*_a) -> None:
assert self.window is not None
fn(self.window)
return wrapper
add("show", guarded(lambda w: w.show_bar()))
add("hide", guarded(lambda w: w.hide_bar()))
add("toggle", guarded(lambda w: w.toggle()))
def do_command_line(self, cmdline: Gio.ApplicationCommandLine) -> int:
args = list(cmdline.get_arguments()[1:])
verb = args[0] if args else "--daemon"
if self.window is None:
return 0
if verb == "--show":
self.window.show_bar()
elif verb == "--hide":
self.window.hide_bar()
elif verb == "--toggle":
self.window.toggle()
# --daemon and anything else: no-op (bar already shown by do_startup)
return 0
def do_activate(self) -> None:
pass # resident instance: nothing to do on plain activate
def main() -> int:
GLib.set_prgname("station-bar")
return StationBarApp().run(sys.argv)
if __name__ == "__main__":
raise SystemExit(main())