Skip to content

Commit

Permalink
book: cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Hofer-Julian committed Jul 12, 2024
1 parent 0d0b5f4 commit 05b6778
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 42 deletions.
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
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
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

0 comments on commit 05b6778

Please sign in to comment.