From 5306cf8abc18b7a3a55ffc99edafaeeb23f5aca5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20L=C3=B6tscher?= <40485433+rolandlo@users.noreply.github.com> Date: Sun, 29 Oct 2023 12:36:55 +0100 Subject: [PATCH 1/2] library: Port Actions demo to Python (#705) * library: Port Actions demo to Python * Add Link to PyGObjects docs in Actions demo --- src/Library/demos/Actions/main.blp | 5 ++ src/Library/demos/Actions/main.py | 79 ++++++++++++++++++++++++++++++ src/about.js | 1 + 3 files changed, 85 insertions(+) create mode 100644 src/Library/demos/Actions/main.py diff --git a/src/Library/demos/Actions/main.blp b/src/Library/demos/Actions/main.blp index 7bb594353..1846acc3d 100644 --- a/src/Library/demos/Actions/main.blp +++ b/src/Library/demos/Actions/main.blp @@ -162,6 +162,11 @@ Adw.StatusPage demo { uri: "https://gjs.guide/guides/gio/actions-and-menus.html"; } + LinkButton { + label: _("PyGObjects Documentation"); + uri: "https://pygobject.readthedocs.io"; + } + LinkButton { label: _("GTK Documentation"); uri: "https://docs.gtk.org/gtk4/actions.html"; diff --git a/src/Library/demos/Actions/main.py b/src/Library/demos/Actions/main.py new file mode 100644 index 000000000..f7e9eb4c3 --- /dev/null +++ b/src/Library/demos/Actions/main.py @@ -0,0 +1,79 @@ +import gi + +gi.require_version("Gtk", "4.0") +gi.require_version("Adw", "1") +from gi.repository import Gtk, Adw, GLib, Gio +import workbench + +demo: Adw.StatusPage = workbench.builder.get_object("demo") + +demo_group = Gio.SimpleActionGroup() +demo.insert_action_group("demo", demo_group) + +# Action with no state or parameters +simple_action = Gio.SimpleAction(name="simple") + +simple_action.connect( + "activate", lambda action, _: print( + f"{action.get_name()} action activated" + ), +) + +demo_group.add_action(simple_action) + +# Action with parameter +bookmarks_action = Gio.SimpleAction( + name="open-bookmarks", + parameter_type=GLib.VariantType("s"), +) + +bookmarks_action.connect( + "activate", + lambda action, parameter: print( + f"{action.get_name()} activated with {parameter.unpack()}" + ), +) + +demo_group.add_action(bookmarks_action) + +# Action with state +toggle_action = Gio.SimpleAction( + name="toggle", + # Boolean actions dont need parameters for activation + state=GLib.Variant.new_boolean(False), +) + +toggle_action.connect( + "notify::state", + lambda action, _: print( + f"{action.get_name()} action set to {action.get_state().unpack()}" + ), +) + +demo_group.add_action(toggle_action) + +# Action with state and parameter +scale_action = Gio.SimpleAction( + name="scale", + state=GLib.Variant.new_string("100%"), + parameter_type=GLib.VariantType("s"), +) + +scale_action.connect( + "notify::state", + lambda action, _: print( + f"{action.get_name()} action set to {action.get_state().unpack()}" + ), +) + +demo_group.add_action(scale_action) + +text: Gtk.Label = workbench.builder.get_object("text") + +alignment_action = Gio.PropertyAction( + name="text-align", + object=text, + property_name="halign", +) + +demo_group.add_action(alignment_action) diff --git a/src/about.js b/src/about.js index ee49c15fa..d73403c59 100644 --- a/src/about.js +++ b/src/about.js @@ -82,6 +82,7 @@ ${getBlueprintVersion()} "Onkar https://github.com/onkarrai06", "Sabrina Meindlhumer https://github.com/m-sabrina", "Urtsi Santsi ", + "Roland Lötscher https://github.com/rolandlo", // Add yourself as // "John Doe", // or From 78326d74782f277f08be7671ab752fb2e218f431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Capypara=20K=C3=B6pcke?= Date: Sun, 29 Oct 2023 19:28:07 +0100 Subject: [PATCH 2/2] Apply black formatting to Actions Python demo --- src/Library/demos/Actions/main.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Library/demos/Actions/main.py b/src/Library/demos/Actions/main.py index f7e9eb4c3..2faf6997d 100644 --- a/src/Library/demos/Actions/main.py +++ b/src/Library/demos/Actions/main.py @@ -14,9 +14,8 @@ simple_action = Gio.SimpleAction(name="simple") simple_action.connect( - "activate", lambda action, _: print( - f"{action.get_name()} action activated" - ), + "activate", + lambda action, _: print(f"{action.get_name()} action activated"), ) demo_group.add_action(simple_action)