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

Update book to new clone! syntax #1795

Merged
merged 7 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
File renamed without changes.
980 changes: 438 additions & 542 deletions book/listings/Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions book/listings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ edition = "2021"


[dependencies]
adw = { version = "0.6", package = "libadwaita", features = ["v1_5"] }
adw = { version = "0.7", package = "libadwaita", features = ["v1_5"] }
anyhow = "1.0"
ashpd = { version = "0.8", features = ["gtk4"] }
ashpd = { version = "0.9", features = ["gtk4"] }
async-channel = "2.0"
dirs = "5.0"
gtk = { version = "0.8", package = "gtk4", features = ["v4_12"] }
gtk = { version = "0.9", package = "gtk4", features = ["v4_12"] }
reqwest = { version = "0.12", default-features = false, features = [
"rustls-tls",
] }
Expand All @@ -21,7 +21,7 @@ walkdir = "2.3"
xshell = "0.2"

[build-dependencies]
glib-build-tools = "0.19"
glib-build-tools = "0.20"

# actions
[[bin]]
Expand Down
10 changes: 7 additions & 3 deletions book/listings/actions/2/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ fn build_ui(app: &Application) {

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

// ANCHOR: action_group
Expand Down
10 changes: 7 additions & 3 deletions book/listings/g_object_memory_management/3/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ fn build_ui(app: &Application) {
// Connect callbacks
// When a button is clicked, `number` will be changed
// ANCHOR: callback
button_increase.connect_clicked(clone!(@strong number => move |_| {
number.set(number.get() + 1);
}));
button_increase.connect_clicked(clone!(
#[strong]
number,
move |_| {
number.set(number.get() + 1);
}
));
button_decrease.connect_clicked(move |_| {
number.set(number.get() - 1);
});
Expand Down
16 changes: 12 additions & 4 deletions book/listings/g_object_memory_management/4/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,24 @@ fn build_ui(app: &Application) {
// ANCHOR: callback
// Connect callbacks
// When a button is clicked, `number` and label of the other button will be changed
button_increase.connect_clicked(clone!(@weak number, @strong button_decrease =>
button_increase.connect_clicked(clone!(
#[weak]
number,
#[strong]
button_decrease,
move |_| {
number.set(number.get() + 1);
button_decrease.set_label(&number.get().to_string());
}));
button_decrease.connect_clicked(clone!(@strong button_increase =>
}
));
button_decrease.connect_clicked(clone!(
#[strong]
button_increase,
move |_| {
number.set(number.get() - 1);
button_increase.set_label(&number.get().to_string());
}));
}
));
// ANCHOR_END: callback

// Add buttons to `gtk_box`
Expand Down
16 changes: 12 additions & 4 deletions book/listings/g_object_memory_management/5/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,24 @@ fn build_ui(app: &Application) {
// ANCHOR: callback
// Connect callbacks
// When a button is clicked, `number` and label of the other button will be changed
button_increase.connect_clicked(clone!(@weak number, @weak button_decrease =>
button_increase.connect_clicked(clone!(
#[weak]
number,
#[weak]
button_decrease,
move |_| {
number.set(number.get() + 1);
button_decrease.set_label(&number.get().to_string());
}));
button_decrease.connect_clicked(clone!(@weak button_increase =>
}
));
button_decrease.connect_clicked(clone!(
#[weak]
button_increase,
move |_| {
number.set(number.get() - 1);
button_increase.set_label(&number.get().to_string());
}));
}
));
// ANCHOR_END: callback

// ANCHOR: box_append
Expand Down
12 changes: 8 additions & 4 deletions book/listings/main_event_loop/3/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ fn build_ui(app: &Application) {
});

// The main loop executes the asynchronous block
glib::spawn_future_local(clone!(@weak button => async move {
while let Ok(enable_button) = receiver.recv().await {
button.set_sensitive(enable_button);
glib::spawn_future_local(clone!(
#[weak]
button,
async move {
while let Ok(enable_button) = receiver.recv().await {
button.set_sensitive(enable_button);
}
}
}));
));
// ANCHOR_END: callback

// Create a window
Expand Down
36 changes: 25 additions & 11 deletions book/listings/main_event_loop/4/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,35 @@ fn build_ui(app: &Application) {
let (sender, receiver) = async_channel::bounded(1);
// Connect to "clicked" signal of `button`
button.connect_clicked(move |_| {
glib::spawn_future_local(clone!(@strong sender => async move {
// Deactivate the button until the operation is done
sender.send(false).await.expect("The channel needs to be open.");
glib::timeout_future_seconds(5).await;
// Activate the button again
sender.send(true).await.expect("The channel needs to be open.");
}));
glib::spawn_future_local(clone!(
#[strong]
sender,
async move {
// Deactivate the button until the operation is done
sender
.send(false)
.await
.expect("The channel needs to be open.");
glib::timeout_future_seconds(5).await;
// Activate the button again
sender
.send(true)
.await
.expect("The channel needs to be open.");
}
));
});

// The main loop executes the asynchronous block
glib::spawn_future_local(clone!(@weak button => async move {
while let Ok(enable_button) = receiver.recv().await {
button.set_sensitive(enable_button);
glib::spawn_future_local(clone!(
#[weak]
button,
async move {
while let Ok(enable_button) = receiver.recv().await {
button.set_sensitive(enable_button);
}
}
}));
));
// ANCHOR_END: callback

// Create a window
Expand Down
18 changes: 11 additions & 7 deletions book/listings/main_event_loop/5/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ fn build_ui(app: &Application) {
// ANCHOR: callback
// Connect to "clicked" signal of `button`
button.connect_clicked(move |button| {
glib::spawn_future_local(clone!(@weak button => async move {
// Deactivate the button until the operation is done
button.set_sensitive(false);
glib::timeout_future_seconds(5).await;
// Activate the button again
button.set_sensitive(true);
}));
glib::spawn_future_local(clone!(
#[weak]
button,
async move {
// Deactivate the button until the operation is done
button.set_sensitive(false);
glib::timeout_future_seconds(5).await;
// Activate the button again
button.set_sensitive(true);
}
));
});
// ANCHOR_END: callback

Expand Down
30 changes: 17 additions & 13 deletions book/listings/main_event_loop/6/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,23 @@ fn build_ui(app: &Application) {
// Connect to "clicked" signal of `button`
button.connect_clicked(move |button| {
// The main loop executes the asynchronous block
glib::spawn_future_local(clone!(@weak button => async move {
// Deactivate the button until the operation is done
button.set_sensitive(false);
let enable_button = gio::spawn_blocking(move || {
let five_seconds = Duration::from_secs(5);
thread::sleep(five_seconds);
true
})
.await
.expect("Task needs to finish successfully.");
// Set sensitivity of button to `enable_button`
button.set_sensitive(enable_button);
}));
glib::spawn_future_local(clone!(
#[weak]
button,
async move {
// Deactivate the button until the operation is done
button.set_sensitive(false);
let enable_button = gio::spawn_blocking(move || {
let five_seconds = Duration::from_secs(5);
thread::sleep(five_seconds);
true
})
.await
.expect("Task needs to finish successfully.");
// Set sensitivity of button to `enable_button`
button.set_sensitive(enable_button);
}
));
});
// ANCHOR_END: callback

Expand Down
36 changes: 20 additions & 16 deletions book/listings/main_event_loop/7/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,27 @@ fn build_ui(app: &Application) {
// Connect to "clicked" signal of `button`
button.connect_clicked(move |button| {
// The main loop executes the asynchronous block
glib::spawn_future_local(clone!(@weak button => async move {
// Get native of button for window identifier
let native = button.native().expect("Need to be able to get native.");
// Get window identifier so that the dialog will be modal to the main window
let identifier = WindowIdentifier::from_native(&native).await;
let request = UserInformation::request()
.reason("App would like to access user information.")
.identifier(identifier)
.send()
.await;

if let Ok(response) = request.and_then(|r| r.response()) {
println!("User name: {}", response.name());
} else {
println!("Could not access user information.")
glib::spawn_future_local(clone!(
#[weak]
button,
async move {
// Get native of button for window identifier
let native = button.native().expect("Need to be able to get native.");
// Get window identifier so that the dialog will be modal to the main window
let identifier = WindowIdentifier::from_native(&native).await;
let request = UserInformation::request()
.reason("App would like to access user information.")
.identifier(identifier)
.send()
.await;

if let Ok(response) = request.and_then(|r| r.response()) {
println!("User name: {}", response.name());
} else {
println!("Could not access user information.")
}
}
}));
));
});
// ANCHOR_END: callback

Expand Down
15 changes: 11 additions & 4 deletions book/listings/main_event_loop/8/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ fn build_ui(app: &Application) {
// Connect to "clicked" signal of `button`
button.connect_clicked(move |_| {
// The main loop executes the asynchronous block
glib::spawn_future_local(clone!(@strong sender => async move {
let response = reqwest::get("https://www.gtk-rs.org").await;
sender.send(response).await.expect("The channel needs to be open.");
}));
glib::spawn_future_local(clone!(
#[strong]
sender,
async move {
let response = reqwest::get("https://www.gtk-rs.org").await;
sender
.send(response)
.await
.expect("The channel needs to be open.");
}
));
});

// The main loop executes the asynchronous block
Expand Down
15 changes: 11 additions & 4 deletions book/listings/main_event_loop/9/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,17 @@ fn build_ui(app: &Application) {
let (sender, receiver) = async_channel::bounded(1);
// Connect to "clicked" signal of `button`
button.connect_clicked(move |_| {
runtime().spawn(clone!(@strong sender => async move {
let response = reqwest::get("https://www.gtk-rs.org").await;
sender.send(response).await.expect("The channel needs to be open.");
}));
runtime().spawn(clone!(
#[strong]
sender,
async move {
let response = reqwest::get("https://www.gtk-rs.org").await;
sender
.send(response)
.await
.expect("The channel needs to be open.");
}
));
});

// The main loop executes the asynchronous block
Expand Down
20 changes: 12 additions & 8 deletions book/listings/todo/1/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,22 @@ impl Window {
// ANCHOR: setup_callbacks
fn setup_callbacks(&self) {
// Setup callback for activation of the entry
self.imp()
.entry
.connect_activate(clone!(@weak self as window => move |_| {
self.imp().entry.connect_activate(clone!(
#[weak(rename_to = window)]
self,
move |_| {
window.new_task();
}));
}
));

// Setup callback for clicking (and the releasing) the icon of the entry
self.imp().entry.connect_icon_release(
clone!(@weak self as window => move |_,_| {
self.imp().entry.connect_icon_release(clone!(
#[weak(rename_to = window)]
self,
move |_, _| {
window.new_task();
}),
);
}
));
}
// ANCHOR_END: setup_callbacks

Expand Down
Loading
Loading