Skip to content

Commit 35c2e81

Browse files
committed
book: Remove last usage of SimpleAction
1 parent da47b1f commit 35c2e81

File tree

13 files changed

+177
-224
lines changed

13 files changed

+177
-224
lines changed

book/listings/actions/3/main.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use gio::SimpleAction;
2-
use glib::clone;
1+
use gio::ActionEntry;
32
use gtk::prelude::*;
43
use gtk::{
54
gio, glib, Align, Application, ApplicationWindow, Button, Label, Orientation,
@@ -59,33 +58,32 @@ fn build_ui(app: &Application) {
5958
.build();
6059

6160
// Add action "count" to `window` taking an integer as parameter
62-
let action_count = SimpleAction::new_stateful(
63-
"count",
64-
Some(&i32::static_variant_type()),
65-
&original_state.to_variant(),
66-
);
67-
action_count.connect_activate(clone!(@weak label => move |action, parameter| {
68-
// Get state
69-
let mut state = action
70-
.state()
71-
.expect("Could not get state.")
72-
.get::<i32>()
73-
.expect("The variant needs to be of type `i32`.");
61+
let action_count = ActionEntry::builder("count")
62+
.parameter_type(Some(&i32::static_variant_type()))
63+
.state(original_state.to_variant())
64+
.activate(move |_, action, parameter| {
65+
// Get state
66+
let mut state = action
67+
.state()
68+
.expect("Could not get state.")
69+
.get::<i32>()
70+
.expect("The variant needs to be of type `i32`.");
7471

75-
// Get parameter
76-
let parameter = parameter
77-
.expect("Could not get parameter.")
78-
.get::<i32>()
79-
.expect("The variant needs to be of type `i32`.");
72+
// Get parameter
73+
let parameter = parameter
74+
.expect("Could not get parameter.")
75+
.get::<i32>()
76+
.expect("The variant needs to be of type `i32`.");
8077

81-
// Increase state by parameter and store state
82-
state += parameter;
83-
action.set_state(&state.to_variant());
78+
// Increase state by parameter and store state
79+
state += parameter;
80+
action.set_state(&state.to_variant());
8481

85-
// Update label with new state
86-
label.set_label(&format!("Counter: {state}"));
87-
}));
88-
window.add_action(&action_count);
82+
// Update label with new state
83+
label.set_label(&format!("Counter: {state}"));
84+
})
85+
.build();
86+
window.add_action_entries([action_count]);
8987

9088
// Present window
9189
window.present();

book/listings/actions/4/main.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use gio::SimpleAction;
2-
use glib::clone;
1+
use gio::ActionEntry;
32
use gtk::prelude::*;
43
use gtk::{
54
gio, glib, Align, Application, ApplicationWindow, Button, Label, Orientation,
@@ -55,34 +54,32 @@ fn build_ui(app: &Application) {
5554
.build();
5655

5756
// Add action "count" to `window` taking an integer as parameter
58-
let action_count = SimpleAction::new_stateful(
59-
"count",
60-
Some(&i32::static_variant_type()),
61-
&original_state.to_variant(),
62-
);
63-
action_count.connect_activate(clone!(@weak label => move |action, parameter| {
64-
// Get state
65-
let mut state = action
66-
.state()
67-
.expect("Could not get state.")
68-
.get::<i32>()
69-
.expect("The value needs to be of type `i32`.");
57+
let action_count = ActionEntry::builder("count")
58+
.parameter_type(Some(&i32::static_variant_type()))
59+
.state(original_state.to_variant())
60+
.activate(move |_, action, parameter| {
61+
// Get state
62+
let mut state = action
63+
.state()
64+
.expect("Could not get state.")
65+
.get::<i32>()
66+
.expect("The variant needs to be of type `i32`.");
7067

71-
// Get parameter
72-
let parameter = parameter
73-
.expect("Could not get parameter.")
74-
.get::<i32>()
75-
.expect("The value needs to be of type `i32`.");
68+
// Get parameter
69+
let parameter = parameter
70+
.expect("Could not get parameter.")
71+
.get::<i32>()
72+
.expect("The variant needs to be of type `i32`.");
7673

77-
// Increase state by parameter and save state
78-
state += parameter;
79-
action.set_state(&state.to_variant());
74+
// Increase state by parameter and store state
75+
state += parameter;
76+
action.set_state(&state.to_variant());
8077

81-
// Update label with new state
82-
label.set_label(&format!("Counter: {state}"));
83-
}));
84-
85-
window.add_action(&action_count);
78+
// Update label with new state
79+
label.set_label(&format!("Counter: {state}"));
80+
})
81+
.build();
82+
window.add_action_entries([action_count]);
8683

8784
// Present window
8885
window.present();

book/listings/actions/5/window/mod.rs

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
mod imp;
22

3-
use gio::SimpleAction;
4-
use glib::{clone, Object};
3+
use gio::ActionEntry;
4+
use glib::Object;
55
use gtk::prelude::*;
66
use gtk::subclass::prelude::*;
77
use gtk::{gio, glib, Application};
@@ -21,38 +21,34 @@ impl Window {
2121
}
2222

2323
fn setup_actions(&self) {
24-
let label = self.imp().label.get();
25-
2624
// Add stateful action "count" to `window` taking an integer as parameter
2725
let original_state = 0;
28-
let action_count = SimpleAction::new_stateful(
29-
"count",
30-
Some(&i32::static_variant_type()),
31-
&original_state.to_variant(),
32-
);
33-
34-
action_count.connect_activate(clone!(@weak label => move |action, parameter| {
35-
// Get state
36-
let mut state = action
37-
.state()
38-
.expect("Could not get state.")
39-
.get::<i32>()
40-
.expect("The value needs to be of type `i32`.");
41-
42-
// Get parameter
43-
let parameter = parameter
44-
.expect("Could not get parameter.")
45-
.get::<i32>()
46-
.expect("The value needs to be of type `i32`.");
47-
48-
// Increase state by parameter and save state
49-
state += parameter;
50-
action.set_state(&state.to_variant());
51-
52-
// Update label with new state
53-
label.set_label(&format!("Counter: {state}"));
54-
}));
55-
self.add_action(&action_count);
26+
let action_count = ActionEntry::builder("count")
27+
.parameter_type(Some(&i32::static_variant_type()))
28+
.state(original_state.to_variant())
29+
.activate(move |window: &Self, action, parameter| {
30+
// Get state
31+
let mut state = action
32+
.state()
33+
.expect("Could not get state.")
34+
.get::<i32>()
35+
.expect("The variant needs to be of type `i32`.");
36+
37+
// Get parameter
38+
let parameter = parameter
39+
.expect("Could not get parameter.")
40+
.get::<i32>()
41+
.expect("The variant needs to be of type `i32`.");
42+
43+
// Increase state by parameter and store state
44+
state += parameter;
45+
action.set_state(&state.to_variant());
46+
47+
// Update label with new state
48+
window.imp().label.set_label(&format!("Counter: {state}"));
49+
})
50+
.build();
51+
self.add_action_entries([action_count]);
5652
}
5753
}
5854
// ANCHOR_END: impl_window

book/listings/actions/6/window/mod.rs

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
mod imp;
22

3-
use gio::{PropertyAction, SimpleAction};
4-
use glib::{clone, Object};
3+
use gio::{ActionEntry, PropertyAction};
4+
use glib::Object;
55
use gtk::prelude::*;
66
use gtk::subclass::prelude::*;
77
use gtk::{gio, glib, Application, Orientation};
@@ -21,40 +21,33 @@ impl Window {
2121

2222
// ANCHOR: setup_actions
2323
fn setup_actions(&self) {
24-
// Get state
25-
let label = self.imp().label.get();
26-
2724
// Add stateful action "count" to `window` taking an integer as parameter
2825
let original_state = 0;
29-
let action_count = SimpleAction::new_stateful(
30-
"count",
31-
Some(&i32::static_variant_type()),
32-
&original_state.to_variant(),
33-
);
34-
35-
action_count.connect_activate(clone!(@weak label => move |action, parameter| {
36-
// Get state
37-
let mut state = action
38-
.state()
39-
.expect("Could not get state.")
40-
.get::<i32>()
41-
.expect("The value needs to be of type `i32`.");
26+
let action_count = ActionEntry::builder("count")
27+
.parameter_type(Some(&i32::static_variant_type()))
28+
.state(original_state.to_variant())
29+
.activate(move |window: &Self, action, parameter| {
30+
// Get state
31+
let mut state = action
32+
.state()
33+
.expect("Could not get state.")
34+
.get::<i32>()
35+
.expect("The variant needs to be of type `i32`.");
4236

43-
// Get parameter
44-
let parameter = parameter
45-
.expect("Could not get parameter.")
46-
.get::<i32>()
47-
.expect("The value needs to be of type `i32`.");
48-
49-
// Increase state by parameter and save state
50-
state += parameter;
51-
action.set_state(&state.to_variant());
37+
// Get parameter
38+
let parameter = parameter
39+
.expect("Could not get parameter.")
40+
.get::<i32>()
41+
.expect("The variant needs to be of type `i32`.");
5242

53-
// Update label with new state
54-
label.set_label(&format!("Counter: {state}"));
55-
}));
56-
self.add_action(&action_count);
43+
// Increase state by parameter and store state
44+
state += parameter;
45+
action.set_state(&state.to_variant());
5746

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

6659
// ANCHOR: action_orientation
67-
6860
// Add stateful action "orientation" to `window` taking a string as parameter
69-
let gtk_box = self.imp().gtk_box.get();
70-
let action_orientation = SimpleAction::new_stateful(
71-
"orientation",
72-
Some(&String::static_variant_type()),
73-
&"Vertical".to_variant(),
74-
);
75-
76-
action_orientation.connect_activate(clone!(@weak gtk_box =>
77-
move |action, parameter| {
61+
let action_orientation = ActionEntry::builder("orientation")
62+
.parameter_type(Some(&String::static_variant_type()))
63+
.state("Vertical".to_variant())
64+
.activate(move |window: &Self, action, parameter| {
7865
// Get parameter
7966
let parameter = parameter
8067
.expect("Could not get parameter.")
@@ -84,14 +71,15 @@ impl Window {
8471
let orientation = match parameter.as_str() {
8572
"Horizontal" => Orientation::Horizontal,
8673
"Vertical" => Orientation::Vertical,
87-
_ => unreachable!()
74+
_ => unreachable!(),
8875
};
8976

9077
// Set orientation and save state
91-
gtk_box.set_orientation(orientation);
78+
window.imp().gtk_box.set_orientation(orientation);
9279
action.set_state(&parameter.to_variant());
93-
}));
94-
self.add_action(&action_orientation);
80+
})
81+
.build();
82+
self.add_action_entries([action_count, action_orientation]);
9583
//ANCHOR_END: action_orientation
9684
}
9785
// ANCHOR_END: setup_actions

book/listings/actions/7/resources/window.ui

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<menu id="main-menu">
44
<item>
55
<attribute name="label" translatable="yes">_Close window</attribute>
6-
<attribute name="action">win.close</attribute>
6+
<attribute name="action">window.close</attribute>
77
</item>
88
<item>
99
<attribute name="label" translatable="yes">_Toggle button frame</attribute>

0 commit comments

Comments
 (0)