summaryrefslogtreecommitdiff
path: root/python/astal/application.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/astal/application.py')
-rw-r--r--python/astal/application.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/python/astal/application.py b/python/astal/application.py
new file mode 100644
index 0000000..3eb1a4f
--- /dev/null
+++ b/python/astal/application.py
@@ -0,0 +1,62 @@
+from collections.abc import Callable
+from gi.repository import Astal, Gio
+
+RequestHandler = Callable[[str, Callable[[str], None]], None]
+
+
+class _Application(Astal.Application):
+ def __init__(self) -> None:
+ super().__init__()
+ self.request_handler: RequestHandler | None = None
+
+ def do_response(self, msg: str, conn: Gio.SocketConnection) -> None:
+ if self.request_handler:
+ self.request_handler(
+ msg,
+ lambda response: Astal.write_sock(
+ conn,
+ response,
+ lambda _, res: Astal.write_sock_finish(res),
+ ),
+ )
+ else:
+ super().do_response(msg, conn)
+
+ def start(
+ self,
+ instance_name: str | None = None,
+ gtk_theme: str | None = None,
+ icon_theme: str | None = None,
+ cursor_theme: str | None = None,
+ css: str | None = None,
+ hold: bool | None = True,
+ request_handler: RequestHandler | None = None,
+ callback: Callable | None = None,
+ ) -> None:
+ if request_handler:
+ self.request_handler = request_handler
+ if hold:
+ self.hold()
+ if instance_name:
+ self.instance_name = instance_name
+ if gtk_theme:
+ self.gtk_theme = gtk_theme
+ if icon_theme:
+ self.icon_theme = icon_theme
+ if cursor_theme:
+ self.cursor_theme = icon_theme
+ if css:
+ self.apply_css(css, False)
+ if not self.acquire_socket():
+ print(f"Astal instance {self.instance_name} already running")
+ return
+
+ def on_activate(app):
+ if callback:
+ callback()
+
+ self.connect("activate", on_activate)
+ self.run()
+
+
+App = _Application()