Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context menu demo #30

Merged
merged 6 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions demos/Context Menu/main.blp
Original file line number Diff line number Diff line change
@@ -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: "🥳";
}
}
27 changes: 27 additions & 0 deletions demos/Context Menu/main.js
Original file line number Diff line number Diff line change
@@ -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);
6 changes: 6 additions & 0 deletions demos/Context Menu/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"category": "controls",
"description": "Offer contextual actions",
"panels": ["ui", "preview"],
"autorun": true
}