|
| 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) |
0 commit comments