Skip to content

Commit 5306cf8

Browse files
authored
library: Port Actions demo to Python (#705)
* library: Port Actions demo to Python * Add Link to PyGObjects docs in Actions demo
1 parent 1b04512 commit 5306cf8

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-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: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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", lambda action, _: print(
18+
f"{action.get_name()} action activated"
19+
),
20+
)
21+
22+
demo_group.add_action(simple_action)
23+
24+
# Action with parameter
25+
bookmarks_action = Gio.SimpleAction(
26+
name="open-bookmarks",
27+
parameter_type=GLib.VariantType("s"),
28+
)
29+
30+
bookmarks_action.connect(
31+
"activate",
32+
lambda action, parameter: print(
33+
f"{action.get_name()} activated with {parameter.unpack()}"
34+
),
35+
)
36+
37+
demo_group.add_action(bookmarks_action)
38+
39+
# Action with state
40+
toggle_action = Gio.SimpleAction(
41+
name="toggle",
42+
# Boolean actions dont need parameters for activation
43+
state=GLib.Variant.new_boolean(False),
44+
)
45+
46+
toggle_action.connect(
47+
"notify::state",
48+
lambda action, _: print(
49+
f"{action.get_name()} action set to {action.get_state().unpack()}"
50+
),
51+
)
52+
53+
demo_group.add_action(toggle_action)
54+
55+
# Action with state and parameter
56+
scale_action = Gio.SimpleAction(
57+
name="scale",
58+
state=GLib.Variant.new_string("100%"),
59+
parameter_type=GLib.VariantType("s"),
60+
)
61+
62+
scale_action.connect(
63+
"notify::state",
64+
lambda action, _: print(
65+
f"{action.get_name()} action set to {action.get_state().unpack()}"
66+
),
67+
)
68+
69+
demo_group.add_action(scale_action)
70+
71+
text: Gtk.Label = workbench.builder.get_object("text")
72+
73+
alignment_action = Gio.PropertyAction(
74+
name="text-align",
75+
object=text,
76+
property_name="halign",
77+
)
78+
79+
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)