Skip to content

Commit 4891bd5

Browse files
library: Port Actions demo to Python (#717)
--------- Co-authored-by: Marco Capypara Köpcke <[email protected]>
1 parent 72685e3 commit 4891bd5

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

src/Library/demos/Actions/main.blp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ Adw.StatusPage demo {
162162
uri: "https://gjs.guide/guides/gio/actions-and-menus.html";
163163
}
164164

165+
LinkButton {
166+
label: _("PyGObjects Documentation");
167+
uri: "https://pygobject.readthedocs.io";
168+
}
169+
165170
LinkButton {
166171
label: _("GTK Documentation");
167172
uri: "https://docs.gtk.org/gtk4/actions.html";

src/Library/demos/Actions/main.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import gi
2+
3+
gi.require_version("Gtk", "4.0")
4+
gi.require_version("Adw", "1")
5+
from gi.repository import Gtk, Adw, GLib, Gio
6+
import workbench
7+
8+
demo: Adw.StatusPage = workbench.builder.get_object("demo")
9+
10+
demo_group = Gio.SimpleActionGroup()
11+
demo.insert_action_group("demo", demo_group)
12+
13+
# Action with no state or parameters
14+
simple_action = Gio.SimpleAction(name="simple")
15+
16+
simple_action.connect(
17+
"activate",
18+
lambda action, _: print(f"{action.get_name()} action activated"),
19+
)
20+
21+
demo_group.add_action(simple_action)
22+
23+
# Action with parameter
24+
bookmarks_action = Gio.SimpleAction(
25+
name="open-bookmarks",
26+
parameter_type=GLib.VariantType("s"),
27+
)
28+
29+
bookmarks_action.connect(
30+
"activate",
31+
lambda action, parameter: print(
32+
f"{action.get_name()} activated with {parameter.unpack()}"
33+
),
34+
)
35+
36+
demo_group.add_action(bookmarks_action)
37+
38+
# Action with state
39+
toggle_action = Gio.SimpleAction(
40+
name="toggle",
41+
# Boolean actions dont need parameters for activation
42+
state=GLib.Variant.new_boolean(False),
43+
)
44+
45+
toggle_action.connect(
46+
"notify::state",
47+
lambda action, _: print(
48+
f"{action.get_name()} action set to {action.get_state().unpack()}"
49+
),
50+
)
51+
52+
demo_group.add_action(toggle_action)
53+
54+
# Action with state and parameter
55+
scale_action = Gio.SimpleAction(
56+
name="scale",
57+
state=GLib.Variant.new_string("100%"),
58+
parameter_type=GLib.VariantType("s"),
59+
)
60+
61+
scale_action.connect(
62+
"notify::state",
63+
lambda action, _: print(
64+
f"{action.get_name()} action set to {action.get_state().unpack()}"
65+
),
66+
)
67+
68+
demo_group.add_action(scale_action)
69+
70+
text: Gtk.Label = workbench.builder.get_object("text")
71+
72+
alignment_action = Gio.PropertyAction(
73+
name="text-align",
74+
object=text,
75+
property_name="halign",
76+
)
77+
78+
demo_group.add_action(alignment_action)

src/about.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ ${getBlueprintVersion()}
8282
"Onkar https://github.com/onkarrai06",
8383
"Sabrina Meindlhumer https://github.com/m-sabrina",
8484
"Urtsi Santsi <[email protected]>",
85+
"Roland Lötscher https://github.com/rolandlo",
8586
// Add yourself as
8687
// "John Doe",
8788
// or

0 commit comments

Comments
 (0)