diff --git a/demos/Context Menu/main.blp b/demos/Context Menu/main.blp new file mode 100644 index 00000000..59e0460f --- /dev/null +++ b/demos/Context Menu/main.blp @@ -0,0 +1,82 @@ +using Gtk 4.0; +using Adw 1; + +Adw.StatusPage demo { + title: _("Context Menu"); + description: _("Offer contextual actions"); + + Box { + orientation: vertical; + + Box box_menu_parent { + orientation: vertical; + halign: center; + width-request: 300; + height-request: 200; + + styles [ + "card", + ] + + Label label_emoji { + label: "😀"; + vexpand: true; + margin-top: 24; + + styles [ + "title-1", + ] + } + + Label { + label: _("Right click on me"); + margin-bottom: 24; + + styles [ + "title-2", + ] + } + + GestureClick gesture_click { + button: 3; + } + + PopoverMenu popover_menu { + menu-model: context_menu; + has-arrow: false; + halign: start; + } + } + + LinkButton { + margin-top: 12; + label: _("GJS Guide"); + uri: "https://gjs.guide/guides/gio/actions-and-menus.html#gmenu"; + } + + LinkButton { + label: _("Human Interface Guidelines"); + uri: "https://developer.gnome.org/hig/patterns/controls/menus.html"; + } + } +} + +menu context_menu { + item { + label: _("Happy"); + action: "mood.emoji"; + target: "😀"; + } + + item { + label: _("Start Struck"); + action: "mood.emoji"; + target: "🤩"; + } + + item { + label: _("Partying"); + action: "mood.emoji"; + target: "🥳"; + } +} diff --git a/demos/Context Menu/main.js b/demos/Context Menu/main.js new file mode 100644 index 00000000..fcabf945 --- /dev/null +++ b/demos/Context Menu/main.js @@ -0,0 +1,27 @@ +import GLib from "gi://GLib"; +import Gdk from "gi://Gdk"; +import Gio from "gi://Gio"; + +const box_menu_parent = workbench.builder.get_object("box_menu_parent"); +const label_emoji = workbench.builder.get_object("label_emoji"); +const gesture_click = workbench.builder.get_object("gesture_click"); +const popover_menu = workbench.builder.get_object("popover_menu"); + +gesture_click.connect("pressed", (_self, _n_press, x, y) => { + const position = new Gdk.Rectangle({ x: x, y: y }); + popover_menu.pointing_to = position; + popover_menu.popup(); +}); + +const mood_group = new Gio.SimpleActionGroup(); +box_menu_parent.insert_action_group("mood", mood_group); + +const emoji_action = new Gio.SimpleAction({ + name: "emoji", + parameter_type: new GLib.VariantType("s"), +}); + +emoji_action.connect("activate", (_action, parameter) => { + label_emoji.label = parameter.deepUnpack().toString(); +}); +mood_group.add_action(emoji_action); diff --git a/demos/Context Menu/main.json b/demos/Context Menu/main.json new file mode 100644 index 00000000..84aebb15 --- /dev/null +++ b/demos/Context Menu/main.json @@ -0,0 +1,6 @@ +{ + "category": "controls", + "description": "Offer contextual actions", + "panels": ["ui", "preview"], + "autorun": true +}