Skip to content
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
159 changes: 159 additions & 0 deletions src/Library/demos/Menu/main.blp
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
using Gtk 4.0;
using Adw 1;

Adw.StatusPage demo {
title: _("Menu");
description: _("Display structured menus with items, sections and submenus");

Box {
halign: center;
orientation: vertical;

Box {
orientation: vertical;
spacing: 42;

Adw.PreferencesGroup {
title: _("Primary and Secondary Menus");
description: _("Used with controls such as menu buttons");

MenuButton {
halign: center;
label: _("Menu Button");
menu-model: demo_menu;
}
}

Adw.PreferencesGroup {
title: _("Context Menus");
description: _("Menus that popup on right-clicking a widget");

Adw.Clamp {
maximum-size: 500;

Frame {
Label label {
margin-top: 6;
margin-bottom: 6;
margin-start: 6;
margin-end: 6;
wrap: true;
wrap-mode: char;
selectable: true;
label: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Vel elit scelerisque mauris pellentesque pulvinar. Molestie nunc non blandit massa enim nec dui nunc.";
extra-menu: context_menu;
}
}
}
}

Box {
halign: center;

LinkButton {
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 demo_menu {
item {
label: _("New File");
}

section {
item {
label: _("New Terminal");
}

item {
label: _("New Build Terminal");
}

item {
label: _("New Runtime Terminal");
}
}

section {
label: _("Settings");

submenu {
label: _("Appearance");

item {
label: _("System Default");
}

item {
label: _("Force Light Mode");
}

item {
label: _("Force Dark Mode");
}
}

submenu {
label: _("Accessibility");

item {
label: _("High Contrast");
}

section {
label: _("Text Direction");

item {
label: _("Left-to-Right");
}

item {
label: _("Right-to-Left");
}
}
}
}
}

menu context_menu {
item {
label: _("Italics");
action: "text.italic";
}

item {
label: _("Bold");
action: "text.bold";
}

submenu {
label: _("Font Color");

item {
label: _("Green");
action: "text.color";
target: "green";
}

item {
label: _("Blue");
action: "text.color";
target: "blue";
}

item {
label: _("Red");
action: "text.color";
target: "red";
}
}
}
57 changes: 57 additions & 0 deletions src/Library/demos/Menu/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import GLib from "gi://GLib";
import Gio from "gi://Gio";
import Pango from "gi://Pango";

const label = workbench.builder.get_object("label");

const text_group = new Gio.SimpleActionGroup();
label.insert_action_group("text", text_group);

const text_state = { italic: false, bold: false, foreground: "green" };

const italic_action = new Gio.SimpleAction({
name: "italic",
state: GLib.Variant.new_boolean(false),
});

italic_action.connect("notify::state", (action) => {
if (action.state.unpack()) text_state["italic"] = true;
else text_state["italic"] = false;
label.attributes = stateToAttr(text_state);
});
text_group.add_action(italic_action);

const bold_action = new Gio.SimpleAction({
name: "bold",
state: GLib.Variant.new_boolean(false),
});

bold_action.connect("notify::state", (action) => {
if (action.state.unpack()) text_state["bold"] = true;
else text_state["bold"] = false;
label.attributes = stateToAttr(text_state);
});
text_group.add_action(bold_action);

const color_action = new Gio.SimpleAction({
name: "color",
state: GLib.Variant.new_string("green"),
parameter_type: new GLib.VariantType("s"),
});

color_action.connect("notify::state", (action) => {
text_state["foreground"] = action.state.unpack();
label.attributes = stateToAttr(text_state);
});

text_group.add_action(color_action);

// Helper function to create a PangoAttrList from text_state
function stateToAttr(state) {
const attrs = [];
if (state["bold"]) attrs.push("0 -1 weight bold");
if (state["italic"]) attrs.push("0 -1 style italic");
attrs.push(`0 -1 foreground ${state["foreground"]}`);
const attr_string = attrs.join(", ");
return Pango.attr_list_from_string(attr_string);
}
7 changes: 7 additions & 0 deletions src/Library/demos/Menu/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "Menu",
"category": "controls",
"description": "Display structured menus with items, sections and submenus",
"panels": ["ui", "preview"],
"autorun": true
}