Skip to content

Commit

Permalink
book: Move to install_action (#1529)
Browse files Browse the repository at this point in the history
* book: Start with moving to `install_action`

* book: Add another paragraph

* book: Document remove_done_tasks

* book: Explain `install_action_async`
  • Loading branch information
Hofer-Julian authored Nov 2, 2023
1 parent 617bbdd commit aaab21c
Show file tree
Hide file tree
Showing 18 changed files with 210 additions and 183 deletions.
2 changes: 1 addition & 1 deletion book/listings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"

[dependencies]
gtk = { version = "*", package = "gtk4", features = ["v4_8"] }
adw = { version = ">= 0.3.1", package = "libadwaita", features = ["v1_2"] }
adw = { version = ">= 0.3.1", package = "libadwaita", features = ["v1_3"] }
once_cell = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
7 changes: 7 additions & 0 deletions book/listings/todo/2/window/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct Window {
}
// ANCHOR_END: struct_default

// ANCHOR: object_subclass
// The central trait for subclassing a GObject
#[glib::object_subclass]
impl ObjectSubclass for Window {
Expand All @@ -35,12 +36,18 @@ impl ObjectSubclass for Window {

fn class_init(klass: &mut Self::Class) {
klass.bind_template();

// Create action to remove done tasks and add to action group "win"
klass.install_action("win.remove-done-tasks", None, |window, _, _| {
window.remove_done_tasks();
});
}

fn instance_init(obj: &InitializingObject<Self>) {
obj.init_template();
}
}
// ANCHOR_END: object_subclass

// ANCHOR: object_impl
// Trait shared by all GObjects
Expand Down
40 changes: 19 additions & 21 deletions book/listings/todo/2/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,27 +222,25 @@ impl Window {
// Create action from key "filter" and add to action group "win"
let action_filter = self.settings().create_action("filter");
self.add_action(&action_filter);

// Create action to remove done tasks and add to action group "win"
let action_remove_done_tasks = gio::ActionEntry::builder("remove-done-tasks")
.activate(move |window: &Self, _, _| {
let tasks = window.tasks();
let mut position = 0;
while let Some(item) = tasks.item(position) {
// Get `TaskObject` from `glib::Object`
let task_object = item
.downcast_ref::<TaskObject>()
.expect("The object needs to be of type `TaskObject`.");

if task_object.is_completed() {
tasks.remove(position);
} else {
position += 1;
}
}
})
.build();
self.add_action_entries([action_remove_done_tasks]);
}
// ANCHOR_END: setup_actions

// ANCHOR: remove_done_tasks
fn remove_done_tasks(&self) {
let tasks = self.tasks();
let mut position = 0;
while let Some(item) = tasks.item(position) {
// Get `TaskObject` from `glib::Object`
let task_object = item
.downcast_ref::<TaskObject>()
.expect("The object needs to be of type `TaskObject`.");

if task_object.is_completed() {
tasks.remove(position);
} else {
position += 1;
}
}
}
// ANCHOR_END: remove_done_tasks
}
5 changes: 5 additions & 0 deletions book/listings/todo/3/window/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ impl ObjectSubclass for Window {

fn class_init(klass: &mut Self::Class) {
klass.bind_template();

// Create action to remove done tasks and add to action group "win"
klass.install_action("win.remove-done-tasks", None, |window, _, _| {
window.remove_done_tasks();
});
}

fn instance_init(obj: &InitializingObject<Self>) {
Expand Down
36 changes: 16 additions & 20 deletions book/listings/todo/3/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,26 +211,22 @@ impl Window {
// Create action from key "filter" and add to action group "win"
let action_filter = self.settings().create_action("filter");
self.add_action(&action_filter);
}

fn remove_done_tasks(&self) {
let tasks = self.tasks();
let mut position = 0;
while let Some(item) = tasks.item(position) {
// Get `TaskObject` from `glib::Object`
let task_object = item
.downcast_ref::<TaskObject>()
.expect("The object needs to be of type `TaskObject`.");

// Create action to remove done tasks and add to action group "win"
let action_remove_done_tasks = gio::ActionEntry::builder("remove-done-tasks")
.activate(move |window: &Self, _, _| {
let tasks = window.tasks();
let mut position = 0;
while let Some(item) = tasks.item(position) {
// Get `TaskObject` from `glib::Object`
let task_object = item
.downcast_ref::<TaskObject>()
.expect("The object needs to be of type `TaskObject`.");

if task_object.is_completed() {
tasks.remove(position);
} else {
position += 1;
}
}
})
.build();
self.add_action_entries([action_remove_done_tasks]);
if task_object.is_completed() {
tasks.remove(position);
} else {
position += 1;
}
}
}
}
5 changes: 5 additions & 0 deletions book/listings/todo/4/window/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ impl ObjectSubclass for Window {

fn class_init(klass: &mut Self::Class) {
klass.bind_template();

// Create action to remove done tasks and add to action group "win"
klass.install_action("win.remove-done-tasks", None, |window, _, _| {
window.remove_done_tasks();
});
}

fn instance_init(obj: &InitializingObject<Self>) {
Expand Down
36 changes: 16 additions & 20 deletions book/listings/todo/4/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,26 +210,22 @@ impl Window {
// Create action from key "filter" and add to action group "win"
let action_filter = self.settings().create_action("filter");
self.add_action(&action_filter);
}

fn remove_done_tasks(&self) {
let tasks = self.tasks();
let mut position = 0;
while let Some(item) = tasks.item(position) {
// Get `TaskObject` from `glib::Object`
let task_object = item
.downcast_ref::<TaskObject>()
.expect("The object needs to be of type `TaskObject`.");

// Create action to remove done tasks and add to action group "win"
let action_remove_done_tasks = gio::ActionEntry::builder("remove-done-tasks")
.activate(move |window: &Self, _, _| {
let tasks = window.tasks();
let mut position = 0;
while let Some(item) = tasks.item(position) {
// Get `TaskObject` from `glib::Object`
let task_object = item
.downcast_ref::<TaskObject>()
.expect("The object needs to be of type `TaskObject`.");

if task_object.is_completed() {
tasks.remove(position);
} else {
position += 1;
}
}
})
.build();
self.add_action_entries([action_remove_done_tasks]);
if task_object.is_completed() {
tasks.remove(position);
} else {
position += 1;
}
}
}
}
5 changes: 5 additions & 0 deletions book/listings/todo/5/window/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ impl ObjectSubclass for Window {

fn class_init(klass: &mut Self::Class) {
klass.bind_template();

// Create action to remove done tasks and add to action group "win"
klass.install_action("win.remove-done-tasks", None, |window, _, _| {
window.remove_done_tasks();
});
}

fn instance_init(obj: &InitializingObject<Self>) {
Expand Down
36 changes: 16 additions & 20 deletions book/listings/todo/5/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,26 +211,22 @@ impl Window {
// Create action from key "filter" and add to action group "win"
let action_filter = self.settings().create_action("filter");
self.add_action(&action_filter);
}

fn remove_done_tasks(&self) {
let tasks = self.tasks();
let mut position = 0;
while let Some(item) = tasks.item(position) {
// Get `TaskObject` from `glib::Object`
let task_object = item
.downcast_ref::<TaskObject>()
.expect("The object needs to be of type `TaskObject`.");

// Create action to remove done tasks and add to action group "win"
let action_remove_done_tasks = gio::ActionEntry::builder("remove-done-tasks")
.activate(move |window: &Self, _, _| {
let tasks = window.tasks();
let mut position = 0;
while let Some(item) = tasks.item(position) {
// Get `TaskObject` from `glib::Object`
let task_object = item
.downcast_ref::<TaskObject>()
.expect("The object needs to be of type `TaskObject`.");

if task_object.is_completed() {
tasks.remove(position);
} else {
position += 1;
}
}
})
.build();
self.add_action_entries([action_remove_done_tasks]);
if task_object.is_completed() {
tasks.remove(position);
} else {
position += 1;
}
}
}
}
5 changes: 5 additions & 0 deletions book/listings/todo/6/window/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ impl ObjectSubclass for Window {

fn class_init(klass: &mut Self::Class) {
klass.bind_template();

// Create action to remove done tasks and add to action group "win"
klass.install_action("win.remove-done-tasks", None, |window, _, _| {
window.remove_done_tasks();
});
}

fn instance_init(obj: &InitializingObject<Self>) {
Expand Down
36 changes: 16 additions & 20 deletions book/listings/todo/6/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,26 +211,22 @@ impl Window {
// Create action from key "filter" and add to action group "win"
let action_filter = self.settings().create_action("filter");
self.add_action(&action_filter);
}

// Create action to remove done tasks and add to action group "win"
let action_remove_done_tasks = gio::ActionEntry::builder("remove-done-tasks")
.activate(move |window: &Self, _, _| {
let tasks = window.tasks();
let mut position = 0;
while let Some(item) = tasks.item(position) {
// Get `TaskObject` from `glib::Object`
let task_object = item
.downcast_ref::<TaskObject>()
.expect("The object needs to be of type `TaskObject`.");

if task_object.is_completed() {
tasks.remove(position);
} else {
position += 1;
}
}
})
.build();
self.add_action_entries([action_remove_done_tasks]);
fn remove_done_tasks(&self) {
let tasks = self.tasks();
let mut position = 0;
while let Some(item) = tasks.item(position) {
// Get `TaskObject` from `glib::Object`
let task_object = item
.downcast_ref::<TaskObject>()
.expect("The object needs to be of type `TaskObject`.");

if task_object.is_completed() {
tasks.remove(position);
} else {
position += 1;
}
}
}
}
5 changes: 5 additions & 0 deletions book/listings/todo/7/window/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ impl ObjectSubclass for Window {

fn class_init(klass: &mut Self::Class) {
klass.bind_template();

// Create action to remove done tasks and add to action group "win"
klass.install_action("win.remove-done-tasks", None, |window, _, _| {
window.remove_done_tasks();
});
}

fn instance_init(obj: &InitializingObject<Self>) {
Expand Down
36 changes: 16 additions & 20 deletions book/listings/todo/7/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,26 +214,22 @@ impl Window {
// Create action from key "filter" and add to action group "win"
let action_filter = self.settings().create_action("filter");
self.add_action(&action_filter);
}

// Create action to remove done tasks and add to action group "win"
let action_remove_done_tasks = gio::ActionEntry::builder("remove-done-tasks")
.activate(move |window: &Self, _, _| {
let tasks = window.tasks();
let mut position = 0;
while let Some(item) = tasks.item(position) {
// Get `TaskObject` from `glib::Object`
let task_object = item
.downcast_ref::<TaskObject>()
.expect("The object needs to be of type `TaskObject`.");

if task_object.is_completed() {
tasks.remove(position);
} else {
position += 1;
}
}
})
.build();
self.add_action_entries([action_remove_done_tasks]);
fn remove_done_tasks(&self) {
let tasks = self.tasks();
let mut position = 0;
while let Some(item) = tasks.item(position) {
// Get `TaskObject` from `glib::Object`
let task_object = item
.downcast_ref::<TaskObject>()
.expect("The object needs to be of type `TaskObject`.");

if task_object.is_completed() {
tasks.remove(position);
} else {
position += 1;
}
}
}
}
16 changes: 16 additions & 0 deletions book/listings/todo/8/window/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct Window {
}
// ANCHOR_END: struct

// ANCHOR: object_subclass
// The central trait for subclassing a GObject
#[glib::object_subclass]
impl ObjectSubclass for Window {
Expand All @@ -51,12 +52,27 @@ impl ObjectSubclass for Window {

fn class_init(klass: &mut Self::Class) {
klass.bind_template();

// Create action to remove done tasks and add to action group "win"
klass.install_action("win.remove-done-tasks", None, |window, _, _| {
window.remove_done_tasks();
});

// Create async action to create new collection and add to action group "win"
klass.install_action_async(
"win.new-collection",
None,
|window, _, _| async move {
window.new_collection().await;
},
);
}

fn instance_init(obj: &InitializingObject<Self>) {
obj.init_template();
}
}
// ANCHOR_END: object_subclass

// ANCHOR: object_impl
// Trait shared by all GObjects
Expand Down
Loading

0 comments on commit aaab21c

Please sign in to comment.