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

Migrate listings to action group #1525

Merged
merged 5 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 24 additions & 26 deletions book/listings/actions/3/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, Align, Application, ApplicationWindow, Button, Label, Orientation,
Expand Down Expand Up @@ -59,33 +58,32 @@ fn build_ui(app: &Application) {
.build();

// Add action "count" to `window` taking an integer as parameter
let action_count = SimpleAction::new_stateful(
"count",
Some(&i32::static_variant_type()),
&original_state.to_variant(),
);
action_count.connect_activate(clone!(@weak label => move |action, parameter| {
// Get state
let mut state = action
.state()
.expect("Could not get state.")
.get::<i32>()
.expect("The variant needs to be of type `i32`.");
let action_count = ActionEntry::builder("count")
.parameter_type(Some(&i32::static_variant_type()))
.state(original_state.to_variant())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Some? is this takes an Option??

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/gio/struct.ActionEntryBuilder.html

If you want to make me a bit happier, then please open a PR/MR at the right place 😅

.activate(move |_, action, parameter| {
// Get state
let mut state = action
.state()
.expect("Could not get state.")
.get::<i32>()
.expect("The variant needs to be of type `i32`.");

// Get parameter
let parameter = parameter
.expect("Could not get parameter.")
.get::<i32>()
.expect("The variant needs to be of type `i32`.");
// Get parameter
let parameter = parameter
.expect("Could not get parameter.")
.get::<i32>()
.expect("The variant needs to be of type `i32`.");

// Increase state by parameter and store state
state += parameter;
action.set_state(&state.to_variant());
// Increase state by parameter and store state
state += parameter;
action.set_state(&state.to_variant());

// Update label with new state
label.set_label(&format!("Counter: {state}"));
}));
window.add_action(&action_count);
// Update label with new state
label.set_label(&format!("Counter: {state}"));
})
.build();
window.add_action_entries([action_count]);

// Present window
window.present();
Expand Down
51 changes: 24 additions & 27 deletions book/listings/actions/4/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, Align, Application, ApplicationWindow, Button, Label, Orientation,
Expand Down Expand Up @@ -55,34 +54,32 @@ fn build_ui(app: &Application) {
.build();

// Add action "count" to `window` taking an integer as parameter
let action_count = SimpleAction::new_stateful(
"count",
Some(&i32::static_variant_type()),
&original_state.to_variant(),
);
action_count.connect_activate(clone!(@weak label => move |action, parameter| {
// Get state
let mut state = action
.state()
.expect("Could not get state.")
.get::<i32>()
.expect("The value needs to be of type `i32`.");
let action_count = ActionEntry::builder("count")
.parameter_type(Some(&i32::static_variant_type()))
.state(original_state.to_variant())
.activate(move |_, action, parameter| {
// Get state
let mut state = action
.state()
.expect("Could not get state.")
.get::<i32>()
.expect("The variant needs to be of type `i32`.");

// Get parameter
let parameter = parameter
.expect("Could not get parameter.")
.get::<i32>()
.expect("The value needs to be of type `i32`.");
// Get parameter
let parameter = parameter
.expect("Could not get parameter.")
.get::<i32>()
.expect("The variant needs to be of type `i32`.");

// Increase state by parameter and save state
state += parameter;
action.set_state(&state.to_variant());
// Increase state by parameter and store state
state += parameter;
action.set_state(&state.to_variant());

// Update label with new state
label.set_label(&format!("Counter: {state}"));
}));

window.add_action(&action_count);
// Update label with new state
label.set_label(&format!("Counter: {state}"));
})
.build();
window.add_action_entries([action_count]);

// Present window
window.present();
Expand Down
60 changes: 28 additions & 32 deletions book/listings/actions/5/window/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod imp;

use gio::SimpleAction;
use glib::{clone, Object};
use gio::ActionEntry;
use glib::Object;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{gio, glib, Application};
Expand All @@ -21,38 +21,34 @@ impl Window {
}

fn setup_actions(&self) {
let label = self.imp().label.get();

// Add stateful action "count" to `window` taking an integer as parameter
let original_state = 0;
let action_count = SimpleAction::new_stateful(
"count",
Some(&i32::static_variant_type()),
&original_state.to_variant(),
);

action_count.connect_activate(clone!(@weak label => move |action, parameter| {
// Get state
let mut state = action
.state()
.expect("Could not get state.")
.get::<i32>()
.expect("The value needs to be of type `i32`.");

// Get parameter
let parameter = parameter
.expect("Could not get parameter.")
.get::<i32>()
.expect("The value needs to be of type `i32`.");

// Increase state by parameter and save state
state += parameter;
action.set_state(&state.to_variant());

// Update label with new state
label.set_label(&format!("Counter: {state}"));
}));
self.add_action(&action_count);
let action_count = ActionEntry::builder("count")
.parameter_type(Some(&i32::static_variant_type()))
.state(original_state.to_variant())
.activate(move |window: &Self, action, parameter| {
// Get state
let mut state = action
.state()
.expect("Could not get state.")
.get::<i32>()
.expect("The variant needs to be of type `i32`.");

// Get parameter
let parameter = parameter
.expect("Could not get parameter.")
.get::<i32>()
.expect("The variant needs to be of type `i32`.");

// Increase state by parameter and store state
state += parameter;
action.set_state(&state.to_variant());

// Update label with new state
window.imp().label.set_label(&format!("Counter: {state}"));
})
.build();
self.add_action_entries([action_count]);
}
}
// ANCHOR_END: impl_window
78 changes: 33 additions & 45 deletions book/listings/actions/6/window/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod imp;

use gio::{PropertyAction, SimpleAction};
use glib::{clone, Object};
use gio::{ActionEntry, PropertyAction};
use glib::Object;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{gio, glib, Application, Orientation};
Expand All @@ -21,40 +21,33 @@ impl Window {

// ANCHOR: setup_actions
fn setup_actions(&self) {
// Get state
let label = self.imp().label.get();

// Add stateful action "count" to `window` taking an integer as parameter
let original_state = 0;
let action_count = SimpleAction::new_stateful(
"count",
Some(&i32::static_variant_type()),
&original_state.to_variant(),
);

action_count.connect_activate(clone!(@weak label => move |action, parameter| {
// Get state
let mut state = action
.state()
.expect("Could not get state.")
.get::<i32>()
.expect("The value needs to be of type `i32`.");
let action_count = ActionEntry::builder("count")
.parameter_type(Some(&i32::static_variant_type()))
.state(original_state.to_variant())
.activate(move |window: &Self, action, parameter| {
// Get state
let mut state = action
.state()
.expect("Could not get state.")
.get::<i32>()
.expect("The variant needs to be of type `i32`.");

// Get parameter
let parameter = parameter
.expect("Could not get parameter.")
.get::<i32>()
.expect("The value needs to be of type `i32`.");

// Increase state by parameter and save state
state += parameter;
action.set_state(&state.to_variant());
// Get parameter
let parameter = parameter
.expect("Could not get parameter.")
.get::<i32>()
.expect("The variant needs to be of type `i32`.");

// Update label with new state
label.set_label(&format!("Counter: {state}"));
}));
self.add_action(&action_count);
// Increase state by parameter and store state
state += parameter;
action.set_state(&state.to_variant());

// Update label with new state
window.imp().label.set_label(&format!("Counter: {state}"));
})
.build();
// ANCHOR: action_button_frame
// Add property action "button-frame" to `window`
let button = self.imp().button.get();
Expand All @@ -64,17 +57,11 @@ impl Window {
// ANCHOR_END: action_button_frame

// ANCHOR: action_orientation

// Add stateful action "orientation" to `window` taking a string as parameter
let gtk_box = self.imp().gtk_box.get();
let action_orientation = SimpleAction::new_stateful(
"orientation",
Some(&String::static_variant_type()),
&"Vertical".to_variant(),
);

action_orientation.connect_activate(clone!(@weak gtk_box =>
move |action, parameter| {
let action_orientation = ActionEntry::builder("orientation")
.parameter_type(Some(&String::static_variant_type()))
.state("Vertical".to_variant())
.activate(move |window: &Self, action, parameter| {
// Get parameter
let parameter = parameter
.expect("Could not get parameter.")
Expand All @@ -84,14 +71,15 @@ impl Window {
let orientation = match parameter.as_str() {
"Horizontal" => Orientation::Horizontal,
"Vertical" => Orientation::Vertical,
_ => unreachable!()
_ => unreachable!(),
};

// Set orientation and save state
gtk_box.set_orientation(orientation);
window.imp().gtk_box.set_orientation(orientation);
action.set_state(&parameter.to_variant());
}));
self.add_action(&action_orientation);
})
.build();
self.add_action_entries([action_count, action_orientation]);
//ANCHOR_END: action_orientation
}
// ANCHOR_END: setup_actions
Expand Down
2 changes: 1 addition & 1 deletion book/listings/actions/7/resources/window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<menu id="main-menu">
<item>
<attribute name="label" translatable="yes">_Close window</attribute>
<attribute name="action">win.close</attribute>
<attribute name="action">window.close</attribute>
</item>
<item>
<attribute name="label" translatable="yes">_Toggle button frame</attribute>
Expand Down
Loading
Loading