|
| 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( |
| 15 | + name = "simple" |
| 16 | +) |
| 17 | + |
| 18 | +simple_action.connect("activate", |
| 19 | + lambda action, _ : print(f"{action.get_name()} action activated") |
| 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("activate", |
| 31 | + lambda action, parameter : print(f"{action.get_name()} activated with {parameter.unpack()}") |
| 32 | +) |
| 33 | + |
| 34 | +demo_group.add_action(bookmarks_action) |
| 35 | + |
| 36 | +# Action with state |
| 37 | +toggle_action = Gio.SimpleAction( |
| 38 | + name = "toggle", |
| 39 | + # Boolean actions dont need parameters for activation |
| 40 | + state = GLib.Variant.new_boolean(False), |
| 41 | +) |
| 42 | + |
| 43 | +toggle_action.connect("notify::state", |
| 44 | + lambda action, _ : print(f"{action.get_name()} action set to {action.get_state().unpack()}") |
| 45 | +) |
| 46 | + |
| 47 | +demo_group.add_action(toggle_action) |
| 48 | + |
| 49 | +# Action with state and parameter |
| 50 | +scale_action = Gio.SimpleAction( |
| 51 | + name = "scale", |
| 52 | + state = GLib.Variant.new_string("100%"), |
| 53 | + parameter_type = GLib.VariantType("s"), |
| 54 | +) |
| 55 | + |
| 56 | +scale_action.connect("notify::state", |
| 57 | + lambda action, _ : print(f"{action.get_name()} action set to {action.get_state().unpack()}") |
| 58 | +) |
| 59 | + |
| 60 | +demo_group.add_action(scale_action) |
| 61 | + |
| 62 | +text : Gtk.Label = workbench.builder.get_object("text") |
| 63 | + |
| 64 | +alignment_action = Gio.PropertyAction( |
| 65 | + name = "text-align", |
| 66 | + object = text, |
| 67 | + property_name = "halign", |
| 68 | +) |
| 69 | + |
| 70 | +demo_group.add_action(alignment_action) |
0 commit comments