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

library: Add Actions entry #397

Merged
merged 6 commits into from
Jul 19, 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
208 changes: 208 additions & 0 deletions src/Library/demos/Actions/main.blp
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
using Gtk 4.0;
using Adw 1;

Adw.StatusPage demo {
title: "Actions";
description: _("A high-level interface used to describe a piece of functionality");

Adw.Clamp {
Box {
orientation: vertical;

Box actions {
orientation: vertical;
homogeneous: true;
vexpand: true;
spacing: 24;

Adw.PreferencesGroup {
title: _("Activatable Actions");
description: _("Stateless actions that optionally take parameters");

Box {
margin-top: 6;
margin-bottom: 6;
hexpand: true;
homogeneous: true;

Button button {
halign: center;
action-name: "demo.simple";
label: _("Simple Action");

styles ["pill"]
}

MenuButton menu_button {
halign: center;
menu-model: bookmarks_menu;
label: _("Bookmarks");

styles ["pill"]
}
}
}

Adw.PreferencesGroup {
title: _("Stateful Actions");
description: _("Actions with a closely-associated state");

Box {
margin-top: 6;
margin-bottom: 6;
hexpand: true;
homogeneous: true;

Box {
valign: center;
orientation: vertical;
spacing: 6;

Label {
label: _("Toggle Action");

styles ["heading"]
}

Switch switch {
halign: center;
valign: center;
action-name: "demo.toggle";
}
}

Box {
orientation: vertical;
spacing: 6;

Label {
label: _("Scale");

styles ["heading"]
}

Box {
halign: center;
valign: center;

ToggleButton {
action-name: "demo.scale";
action-target: "'100%'";
label: _("100%");
}

ToggleButton {
action-name: "demo.scale";
action-target: "'150%'";
label: _("150%");
}

ToggleButton {
action-name: "demo.scale";
action-target: "'200%'";
label: _("200%");
}

styles ["linked"]
}
}
}
}

Adw.PreferencesGroup {
title: _("Specialized Actions");
description: _("A stateful action which is bound to a GObject property");

Box {
orientation: vertical;
halign: center;
margin-top: 6;
margin-bottom: 12;
homogeneous: true;


Label text {
label: _("Text Align");
halign: center;

styles ["heading"]
}

Box {
halign: center;
spacing: 6;

CheckButton {
label: _("Start");
action-name: "demo.text-align";
action-target: "'start'";
}

CheckButton {
label: _("Center");
action-name: "demo.text-align";
action-target: "'center'";
}

CheckButton {
label: _("End");
action-name: "demo.text-align";
action-target: "'end'";
}
}
}
}
}

Box links {
halign: center;

LinkButton {
label: "GJS Guide";
uri: "https://gjs.guide/guides/gio/actions-and-menus.html";
}

LinkButton {
label: "GTK Documentation";
uri: "https://docs.gtk.org/gtk4/actions.html";
}
}
}
}
}

menu bookmarks_menu {
item {
label: _("Developer Documentation");
action: "demo.open-bookmarks";
target: _("Developer Documentation");
}

item {
label: _("Human Interface Guidelines");
action: "demo.open-bookmarks";
target: _("Human Interface Guidelines");
}

item {
label: _("GNOME Javascript");
action: "demo.open-bookmarks";
target: _("GNOME Javascript");
}
AkshayWarrier marked this conversation as resolved.
Show resolved Hide resolved

section {
label: _("Actions Documentation");

item {
label: _("GJS Guide");
action: "app.open_uri";
target: "https://gjs.guide/guides/gio/actions-and-menus.html";
}

item {
label: _("GTK Documentation");
action: "app.open_uri";
target: "https://docs.gtk.org/gtk4/actions.html";
}
}
}
68 changes: 68 additions & 0 deletions src/Library/demos/Actions/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import Gtk from "gi://Gtk";
import Gio from "gi://Gio";
import GObject from "gi://GObject";
import GLib from "gi://GLib";

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

const demo_group = new Gio.SimpleActionGroup();
demo.insert_action_group("demo", demo_group);

// Action with no state or parameters
const simple_action = new Gio.SimpleAction({
name: "simple",
});

simple_action.connect("activate", (action, parameter) => {
console.log(`${action.name} action activated`);
});

demo_group.add_action(simple_action);

// Action with parameter
const bookmarks_action = new Gio.SimpleAction({
name: "open-bookmarks",
parameter_type: new GLib.VariantType("s"),
});

bookmarks_action.connect("activate", (action, parameter) => {
console.log(`${action.name} activated with ${parameter.unpack()}`);
});

demo_group.add_action(bookmarks_action);

// Action with state
const toggle_action = new Gio.SimpleAction({
name: "toggle",
// Boolean actions dont need parameters for activation
state: GLib.Variant.new_boolean(false),
});

toggle_action.connect("notify::state", (action) => {
console.log(`${action.name} action set to ${action.state.unpack()}`);
});

demo_group.add_action(toggle_action);

// Action with state and parameter
const scale_action = new Gio.SimpleAction({
name: "scale",
state: GLib.Variant.new_string("100%"),
parameter_type: new GLib.VariantType("s"),
});

scale_action.connect("notify::state", (action) => {
console.log(`${action.name} action set to ${action.state.unpack()}`);
});

demo_group.add_action(scale_action);

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

const alignment_action = new Gio.PropertyAction({
name: "text-align",
object: text,
property_name: "halign",
});

demo_group.add_action(alignment_action);
7 changes: 7 additions & 0 deletions src/Library/demos/Actions/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "Actions",
"category": "controls",
"description": "A high-level interface used to describe a piece of functionality",
"panels": ["ui", "preview"],
"autorun": true
}