Skip to content

Commit

Permalink
book: Start migrating listings to action group
Browse files Browse the repository at this point in the history
  • Loading branch information
Hofer-Julian committed Oct 25, 2023
1 parent fb3ef4d commit da47b1f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
14 changes: 7 additions & 7 deletions book/listings/actions/1/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use gio::SimpleAction;
use glib::clone;
use gio::ActionEntry;
use gtk::prelude::*;
use gtk::{gio, glib, Application, ApplicationWindow};

Expand Down Expand Up @@ -31,11 +30,12 @@ fn build_ui(app: &Application) {
.build();

// Add action "close" to `window` taking no parameter
let action_close = SimpleAction::new("close", None);
action_close.connect_activate(clone!(@weak window => move |_, _| {
window.close();
}));
window.add_action(&action_close);
let action_close = ActionEntry::builder("close")
.activate(|window: &ApplicationWindow, _, _| {
window.close();
})
.build();
window.add_action_entries([action_close]);

// Present window
window.present();
Expand Down
25 changes: 12 additions & 13 deletions book/listings/actions/2/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use gio::SimpleAction;
use gio::ActionEntry;
use glib::clone;
use gtk::gio::SimpleActionGroup;
use gtk::prelude::*;
use gtk::{gio, glib, Application, ApplicationWindow};

// ANCHOR: main
const APP_ID: &str = "org.gtk_rs.Actions2";

fn main() -> glib::ExitCode {
Expand All @@ -13,14 +12,14 @@ fn main() -> glib::ExitCode {

// Connect to "activate" signal of `app`
app.connect_activate(build_ui);

// Set keyboard accelerator to trigger "win.close".
app.set_accels_for_action("win.close", &["<Ctrl>W"]);
// ANCHOR: accel
// Set keyboard accelerator to trigger "custom-group.close".
app.set_accels_for_action("custom-group.close", &["<Ctrl>W"]);
// ANCHOR_END: accel

// Run the application
app.run()
}
// ANCHOR_END: main

// ANCHOR: build_ui
fn build_ui(app: &Application) {
Expand All @@ -32,17 +31,17 @@ fn build_ui(app: &Application) {
.build();

// Add action "close" to `window` taking no parameter
let action_close = SimpleAction::new("close", None);
action_close.connect_activate(clone!(@weak window => move |_, _| {
window.close();
}));
window.add_action(&action_close);
let action_close = ActionEntry::builder("close")
.activate(clone!(@weak window => move |_, _, _| {
window.close();
}))
.build();

// ANCHOR: action_group
// Create a new action group and add actions to it
let actions = SimpleActionGroup::new();
window.insert_action_group("win", Some(&actions));
actions.add_action(&action_close);
actions.add_action_entries([action_close]);
window.insert_action_group("custom-group", Some(&actions));
// ANCHOR_END: action_group

// Present window
Expand Down
19 changes: 15 additions & 4 deletions book/src/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ Filename: <a class=file-link href="https://github.com/gtk-rs/gtk4-rs/blob/master
{{#rustdoc_include ../listings/actions/1/main.rs:build_ui}}
```

First, we created a new [`gio::SimpleAction`](https://gtk-rs.org/gtk-rs-core/stable/latest/docs/gio/struct.SimpleAction.html) which is named "close" and takes no parameter.
We also connected a callback which closes the window.
First, we created a new [`gio::SimpleActionGroup`](https://gtk-rs.org/gtk-rs-core/stable/latest/docs/gio/struct.SimpleActionGroup.html) which is named "close" and takes no parameter.
We also connected a callback which closes the window when the action is activated.
Finally, we add the action entry to the window via [`add_action_entries`](https://gtk-rs.org/gtk-rs-core/stable/latest/docs/gio/prelude/trait.ActionMapExtManual.html#method.add_action_entries).

Filename: <a class=file-link href="https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/actions/1/main.rs">listings/actions/1/main.rs</a>

Expand All @@ -33,18 +34,28 @@ The answer is that it is so common to add actions to windows and applications th
- "app" for actions global to the application, and
- "win" for actions tied to an application window.

If that had not been the case, we would have to add the action group manually via [`gio::SimpleActionGroup`](https://gtk-rs.org/gtk-rs-core/stable/latest/docs/gio/struct.SimpleActionGroup.html).
We can add a action group to any widget via the method [`insert_action_group`](https://gtk-rs.org/gtk4-rs/stable/latest/docs/gtk4/prelude/trait.WidgetExt.html#method.insert_action_group).
Let's add our action to the action group "custom-group" and add the group then to our window.
Since the action entry isn't specific to our window anymore, the first parameter of the "activate" callback is of type `SimpleActionGroup` instead of `ApplicationWindow`.

Filename: <a class=file-link href="https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/actions/2/main.rs">listings/actions/2/main.rs</a>

```rust
{{#rustdoc_include ../listings/actions/2/main.rs:action_group}}
```

If we bind the accelerator to "custom-group.close", it works just as before.

Filename: <a class=file-link href="https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/actions/2/main.rs">listings/actions/2/main.rs</a>

```rust
{{#rustdoc_include ../listings/actions/2/main.rs:main}}
```

Also, if we had multiple instances of the same windows we would expect that only the currently focused window will be closed when activating "win.close".
And indeed, the "win.close" will be dispatched to the currently focused window.
However, that also means that we actually define one action per window instance.
If we want to have a single globally accessible action instead, we call `add_action` on our application instead.
If we want to have a single globally accessible action instead, we call `add_action_entries` on our application instead.

> Adding "win.close" was useful as a simple example.
> However, in the future we will use the pre-defined ["window.close"](https://gtk-rs.org/gtk4-rs/stable/latest/docs/gtk4/struct.Window.html#actions) action which does exactly the same thing.
Expand Down

0 comments on commit da47b1f

Please sign in to comment.