Skip to content

Commit aaab21c

Browse files
authored
book: Move to install_action (#1529)
* book: Start with moving to `install_action` * book: Add another paragraph * book: Document remove_done_tasks * book: Explain `install_action_async`
1 parent 617bbdd commit aaab21c

File tree

18 files changed

+210
-183
lines changed

18 files changed

+210
-183
lines changed

book/listings/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2021"
66

77
[dependencies]
88
gtk = { version = "*", package = "gtk4", features = ["v4_8"] }
9-
adw = { version = ">= 0.3.1", package = "libadwaita", features = ["v1_2"] }
9+
adw = { version = ">= 0.3.1", package = "libadwaita", features = ["v1_3"] }
1010
once_cell = "1.0"
1111
serde = { version = "1.0", features = ["derive"] }
1212
serde_json = "1.0"

book/listings/todo/2/window/imp.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct Window {
2525
}
2626
// ANCHOR_END: struct_default
2727

28+
// ANCHOR: object_subclass
2829
// The central trait for subclassing a GObject
2930
#[glib::object_subclass]
3031
impl ObjectSubclass for Window {
@@ -35,12 +36,18 @@ impl ObjectSubclass for Window {
3536

3637
fn class_init(klass: &mut Self::Class) {
3738
klass.bind_template();
39+
40+
// Create action to remove done tasks and add to action group "win"
41+
klass.install_action("win.remove-done-tasks", None, |window, _, _| {
42+
window.remove_done_tasks();
43+
});
3844
}
3945

4046
fn instance_init(obj: &InitializingObject<Self>) {
4147
obj.init_template();
4248
}
4349
}
50+
// ANCHOR_END: object_subclass
4451

4552
// ANCHOR: object_impl
4653
// Trait shared by all GObjects

book/listings/todo/2/window/mod.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -222,27 +222,25 @@ impl Window {
222222
// Create action from key "filter" and add to action group "win"
223223
let action_filter = self.settings().create_action("filter");
224224
self.add_action(&action_filter);
225-
226-
// Create action to remove done tasks and add to action group "win"
227-
let action_remove_done_tasks = gio::ActionEntry::builder("remove-done-tasks")
228-
.activate(move |window: &Self, _, _| {
229-
let tasks = window.tasks();
230-
let mut position = 0;
231-
while let Some(item) = tasks.item(position) {
232-
// Get `TaskObject` from `glib::Object`
233-
let task_object = item
234-
.downcast_ref::<TaskObject>()
235-
.expect("The object needs to be of type `TaskObject`.");
236-
237-
if task_object.is_completed() {
238-
tasks.remove(position);
239-
} else {
240-
position += 1;
241-
}
242-
}
243-
})
244-
.build();
245-
self.add_action_entries([action_remove_done_tasks]);
246225
}
247226
// ANCHOR_END: setup_actions
227+
228+
// ANCHOR: remove_done_tasks
229+
fn remove_done_tasks(&self) {
230+
let tasks = self.tasks();
231+
let mut position = 0;
232+
while let Some(item) = tasks.item(position) {
233+
// Get `TaskObject` from `glib::Object`
234+
let task_object = item
235+
.downcast_ref::<TaskObject>()
236+
.expect("The object needs to be of type `TaskObject`.");
237+
238+
if task_object.is_completed() {
239+
tasks.remove(position);
240+
} else {
241+
position += 1;
242+
}
243+
}
244+
}
245+
// ANCHOR_END: remove_done_tasks
248246
}

book/listings/todo/3/window/imp.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ impl ObjectSubclass for Window {
3333

3434
fn class_init(klass: &mut Self::Class) {
3535
klass.bind_template();
36+
37+
// Create action to remove done tasks and add to action group "win"
38+
klass.install_action("win.remove-done-tasks", None, |window, _, _| {
39+
window.remove_done_tasks();
40+
});
3641
}
3742

3843
fn instance_init(obj: &InitializingObject<Self>) {

book/listings/todo/3/window/mod.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -211,26 +211,22 @@ impl Window {
211211
// Create action from key "filter" and add to action group "win"
212212
let action_filter = self.settings().create_action("filter");
213213
self.add_action(&action_filter);
214+
}
215+
216+
fn remove_done_tasks(&self) {
217+
let tasks = self.tasks();
218+
let mut position = 0;
219+
while let Some(item) = tasks.item(position) {
220+
// Get `TaskObject` from `glib::Object`
221+
let task_object = item
222+
.downcast_ref::<TaskObject>()
223+
.expect("The object needs to be of type `TaskObject`.");
214224

215-
// Create action to remove done tasks and add to action group "win"
216-
let action_remove_done_tasks = gio::ActionEntry::builder("remove-done-tasks")
217-
.activate(move |window: &Self, _, _| {
218-
let tasks = window.tasks();
219-
let mut position = 0;
220-
while let Some(item) = tasks.item(position) {
221-
// Get `TaskObject` from `glib::Object`
222-
let task_object = item
223-
.downcast_ref::<TaskObject>()
224-
.expect("The object needs to be of type `TaskObject`.");
225-
226-
if task_object.is_completed() {
227-
tasks.remove(position);
228-
} else {
229-
position += 1;
230-
}
231-
}
232-
})
233-
.build();
234-
self.add_action_entries([action_remove_done_tasks]);
225+
if task_object.is_completed() {
226+
tasks.remove(position);
227+
} else {
228+
position += 1;
229+
}
230+
}
235231
}
236232
}

book/listings/todo/4/window/imp.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ impl ObjectSubclass for Window {
3333

3434
fn class_init(klass: &mut Self::Class) {
3535
klass.bind_template();
36+
37+
// Create action to remove done tasks and add to action group "win"
38+
klass.install_action("win.remove-done-tasks", None, |window, _, _| {
39+
window.remove_done_tasks();
40+
});
3641
}
3742

3843
fn instance_init(obj: &InitializingObject<Self>) {

book/listings/todo/4/window/mod.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -210,26 +210,22 @@ impl Window {
210210
// Create action from key "filter" and add to action group "win"
211211
let action_filter = self.settings().create_action("filter");
212212
self.add_action(&action_filter);
213+
}
214+
215+
fn remove_done_tasks(&self) {
216+
let tasks = self.tasks();
217+
let mut position = 0;
218+
while let Some(item) = tasks.item(position) {
219+
// Get `TaskObject` from `glib::Object`
220+
let task_object = item
221+
.downcast_ref::<TaskObject>()
222+
.expect("The object needs to be of type `TaskObject`.");
213223

214-
// Create action to remove done tasks and add to action group "win"
215-
let action_remove_done_tasks = gio::ActionEntry::builder("remove-done-tasks")
216-
.activate(move |window: &Self, _, _| {
217-
let tasks = window.tasks();
218-
let mut position = 0;
219-
while let Some(item) = tasks.item(position) {
220-
// Get `TaskObject` from `glib::Object`
221-
let task_object = item
222-
.downcast_ref::<TaskObject>()
223-
.expect("The object needs to be of type `TaskObject`.");
224-
225-
if task_object.is_completed() {
226-
tasks.remove(position);
227-
} else {
228-
position += 1;
229-
}
230-
}
231-
})
232-
.build();
233-
self.add_action_entries([action_remove_done_tasks]);
224+
if task_object.is_completed() {
225+
tasks.remove(position);
226+
} else {
227+
position += 1;
228+
}
229+
}
234230
}
235231
}

book/listings/todo/5/window/imp.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ impl ObjectSubclass for Window {
3333

3434
fn class_init(klass: &mut Self::Class) {
3535
klass.bind_template();
36+
37+
// Create action to remove done tasks and add to action group "win"
38+
klass.install_action("win.remove-done-tasks", None, |window, _, _| {
39+
window.remove_done_tasks();
40+
});
3641
}
3742

3843
fn instance_init(obj: &InitializingObject<Self>) {

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -211,26 +211,22 @@ impl Window {
211211
// Create action from key "filter" and add to action group "win"
212212
let action_filter = self.settings().create_action("filter");
213213
self.add_action(&action_filter);
214+
}
215+
216+
fn remove_done_tasks(&self) {
217+
let tasks = self.tasks();
218+
let mut position = 0;
219+
while let Some(item) = tasks.item(position) {
220+
// Get `TaskObject` from `glib::Object`
221+
let task_object = item
222+
.downcast_ref::<TaskObject>()
223+
.expect("The object needs to be of type `TaskObject`.");
214224

215-
// Create action to remove done tasks and add to action group "win"
216-
let action_remove_done_tasks = gio::ActionEntry::builder("remove-done-tasks")
217-
.activate(move |window: &Self, _, _| {
218-
let tasks = window.tasks();
219-
let mut position = 0;
220-
while let Some(item) = tasks.item(position) {
221-
// Get `TaskObject` from `glib::Object`
222-
let task_object = item
223-
.downcast_ref::<TaskObject>()
224-
.expect("The object needs to be of type `TaskObject`.");
225-
226-
if task_object.is_completed() {
227-
tasks.remove(position);
228-
} else {
229-
position += 1;
230-
}
231-
}
232-
})
233-
.build();
234-
self.add_action_entries([action_remove_done_tasks]);
225+
if task_object.is_completed() {
226+
tasks.remove(position);
227+
} else {
228+
position += 1;
229+
}
230+
}
235231
}
236232
}

book/listings/todo/6/window/imp.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ impl ObjectSubclass for Window {
3636

3737
fn class_init(klass: &mut Self::Class) {
3838
klass.bind_template();
39+
40+
// Create action to remove done tasks and add to action group "win"
41+
klass.install_action("win.remove-done-tasks", None, |window, _, _| {
42+
window.remove_done_tasks();
43+
});
3944
}
4045

4146
fn instance_init(obj: &InitializingObject<Self>) {

0 commit comments

Comments
 (0)