summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAylur <[email protected]>2024-09-08 02:58:14 +0200
committerAylur <[email protected]>2024-09-08 02:58:14 +0200
commit8e44219b0ea108a4b33cbd1e5ea06dfb1c06f659 (patch)
tree2340f0683595b502f0ec8c562d544c2bb2cbddf4
parent81f26551103e785fa974e054b2b3357725deb419 (diff)
example: add python starter bar
-rw-r--r--docs/getting-started/supported-languages.md9
-rw-r--r--examples/py/.gitignore3
-rw-r--r--examples/py/starter-bar/README.md7
-rw-r--r--examples/py/starter-bar/__init__.py0
-rwxr-xr-xexamples/py/starter-bar/app.py27
-rw-r--r--examples/py/starter-bar/style.css4
-rw-r--r--examples/py/starter-bar/versions.py6
-rw-r--r--examples/py/starter-bar/widget/Bar.py48
-rw-r--r--examples/py/starter-bar/widget/__init__.py0
-rw-r--r--flake.nix2
10 files changed, 101 insertions, 5 deletions
diff --git a/docs/getting-started/supported-languages.md b/docs/getting-started/supported-languages.md
index 9f3831b..f69dd19 100644
--- a/docs/getting-started/supported-languages.md
+++ b/docs/getting-started/supported-languages.md
@@ -11,9 +11,9 @@ The runtime is [GJS](https://gitlab.gnome.org/GNOME/gjs) and **not** nodejs
:::
Examples:
-- [Simple Bar](https://github.com/Aylur/astal/tree/main/examples/js/simple-bar)
-![sime-bar](https://github.com/user-attachments/assets/a306c864-56b7-44c4-8820-81f424f32b9b)
+- [Simple Bar](https://github.com/Aylur/astal/tree/main/examples/js/simple-bar)
+![simple-bar](https://github.com/user-attachments/assets/a306c864-56b7-44c4-8820-81f424f32b9b)
## Lua
@@ -29,15 +29,14 @@ Examples:
## Python
-<!--TODO: move python PR to monorepo and link-->
-There is a WIP [library for python](), to make it behave similar to the above two
+There was a WIP [library for python](https://github.com/aylur/astal/tree/feat/python), to make it behave similar to the above two
but I don't plan on finishing it, because I'm not a fan of python.
If you are interested in picking it up, feel free to open a PR.
Nonetheless you can still use python the OOP way [pygobject](https://pygobject.gnome.org/tutorials/gobject/subclassing.html) intended it.
Examples:
-- TODO
+- [Starter Bar](https://github.com/Aylur/astal/tree/main/examples/py/starter-bar)
## Vala
diff --git a/examples/py/.gitignore b/examples/py/.gitignore
new file mode 100644
index 0000000..b0bff8b
--- /dev/null
+++ b/examples/py/.gitignore
@@ -0,0 +1,3 @@
+pygobject-stubs/
+*.pyi
+__pycache__/
diff --git a/examples/py/starter-bar/README.md b/examples/py/starter-bar/README.md
new file mode 100644
index 0000000..c9a7ad1
--- /dev/null
+++ b/examples/py/starter-bar/README.md
@@ -0,0 +1,7 @@
+# Starter Bar Example
+
+A starter bar that shows a label and the date.
+
+> [!NOTE]
+> This code is not ideal to work with as it requires too much boilerplate compared to AGS.
+> If you want to use python try picking up the [python library](https://github.com/aylur/astal/tree/feat/python).
diff --git a/examples/py/starter-bar/__init__.py b/examples/py/starter-bar/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/examples/py/starter-bar/__init__.py
diff --git a/examples/py/starter-bar/app.py b/examples/py/starter-bar/app.py
new file mode 100755
index 0000000..287d0f8
--- /dev/null
+++ b/examples/py/starter-bar/app.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+import versions
+from gi.repository import Astal, Gio
+from widget.Bar import Bar
+from pathlib import Path
+
+css = str(Path(__file__).parent.resolve() / "style.css")
+
+
+class App(Astal.Application):
+ def __init__(self):
+ super().__init__()
+ self.acquire_socket()
+ self.run(None)
+
+ def do_request(self, msg: str, conn: Gio.SocketConnection) -> None:
+ print(msg)
+ Astal.write_sock(conn, "hello")
+
+ def do_activate(self) -> None:
+ self.hold()
+ self.apply_css(css, True)
+ for mon in self.get_monitors():
+ self.add_window(Bar(mon))
+
+
+App()
diff --git a/examples/py/starter-bar/style.css b/examples/py/starter-bar/style.css
new file mode 100644
index 0000000..cb8c712
--- /dev/null
+++ b/examples/py/starter-bar/style.css
@@ -0,0 +1,4 @@
+window.Bar {
+ background-color: #212121;
+ color: white;
+}
diff --git a/examples/py/starter-bar/versions.py b/examples/py/starter-bar/versions.py
new file mode 100644
index 0000000..a5993be
--- /dev/null
+++ b/examples/py/starter-bar/versions.py
@@ -0,0 +1,6 @@
+import gi
+
+gi.require_version("Astal", "0.1")
+gi.require_version("Gtk", "3.0")
+gi.require_version("Gdk", "3.0")
+gi.require_version("Gio", "2.0")
diff --git a/examples/py/starter-bar/widget/Bar.py b/examples/py/starter-bar/widget/Bar.py
new file mode 100644
index 0000000..9071169
--- /dev/null
+++ b/examples/py/starter-bar/widget/Bar.py
@@ -0,0 +1,48 @@
+from gi.repository import Astal, Gtk, Gdk, GLib
+
+
+class Time(Astal.Label):
+ def __init__(self, format="%H:%M:%S"):
+ super().__init__(visible=True)
+ self.connect("destroy", self.on_destroy)
+ self.format = format
+ self.time = Astal.Time.interval(1000, self.on_tick)
+
+ def on_tick(self):
+ datetime = GLib.DateTime.new_now_local()
+ assert datetime
+ time = datetime.format(self.format)
+ assert time
+ self.set_label(time)
+
+ def on_destroy(self, *args, **kwargs):
+ self.time.cancel()
+
+
+class Bar(Astal.Window):
+ def __init__(self, monitor: Gdk.Monitor):
+ super().__init__(
+ visible=True,
+ gdkmonitor=monitor,
+ name="Bar" + str(monitor.get_model()),
+ anchor=Astal.WindowAnchor.LEFT
+ | Astal.WindowAnchor.RIGHT
+ | Astal.WindowAnchor.TOP,
+ exclusivity=Astal.Exclusivity.EXCLUSIVE,
+ )
+
+ Astal.widget_set_class_names(self, ["Bar"])
+ start_widget = Astal.Box(visible=True, hexpand=True, halign=Gtk.Align.CENTER)
+ end_widget = Astal.Box(visible=True, hexpand=True, halign=Gtk.Align.CENTER)
+
+ start_widget.set_children([Astal.Label(visible=True, label="Astal in python")])
+
+ end_widget.set_children([Time()])
+
+ self.add(
+ Astal.CenterBox(
+ visible=True,
+ start_widget=start_widget,
+ end_widget=end_widget,
+ )
+ )
diff --git a/examples/py/starter-bar/widget/__init__.py b/examples/py/starter-bar/widget/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/examples/py/starter-bar/widget/__init__.py
diff --git a/flake.nix b/flake.nix
index 7d502dc..000ded1 100644
--- a/flake.nix
+++ b/flake.nix
@@ -66,7 +66,9 @@
wireplumber
libdbusmenu-gtk3
wayland
+
(lua.withPackages (ps: [ps.lgi]))
+ (python3.withPackages (ps: [ps.pygobject3 ps.pygobject-stubs]))
gjs
];
in {