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