Skip to content

Commit bd3105d

Browse files
authored
book: Use new API spawn_future_local (#1533)
1 parent aaab21c commit bd3105d

File tree

9 files changed

+190
-157
lines changed

9 files changed

+190
-157
lines changed

book/listings/Cargo.lock

Lines changed: 166 additions & 124 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

book/listings/main_event_loop/3/main.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::thread;
22
use std::time::Duration;
33

4-
use glib::{clone, MainContext};
4+
use glib::clone;
55
use gtk::prelude::*;
66
use gtk::{gio, glib, Application, ApplicationWindow, Button};
77

@@ -40,18 +40,17 @@ fn build_ui(app: &Application) {
4040
sender
4141
.send_blocking(false)
4242
.expect("The channel needs to be open.");
43-
let ten_seconds = Duration::from_secs(10);
44-
thread::sleep(ten_seconds);
43+
let five_seconds = Duration::from_secs(5);
44+
thread::sleep(five_seconds);
4545
// Activate the button again
4646
sender
4747
.send_blocking(true)
4848
.expect("The channel needs to be open.");
4949
});
5050
});
5151

52-
let main_context = MainContext::default();
5352
// The main loop executes the asynchronous block
54-
main_context.spawn_local(clone!(@weak button => async move {
53+
glib::spawn_future_local(clone!(@weak button => async move {
5554
while let Ok(enable_button) = receiver.recv().await {
5655
button.set_sensitive(enable_button);
5756
}

book/listings/main_event_loop/4/main.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use glib::{clone, MainContext};
1+
use glib::clone;
22
use gtk::prelude::*;
33
use gtk::{glib, Application, ApplicationWindow, Button};
44

@@ -30,8 +30,7 @@ fn build_ui(app: &Application) {
3030
let (sender, receiver) = async_channel::bounded(1);
3131
// Connect to "clicked" signal of `button`
3232
button.connect_clicked(move |_| {
33-
let main_context = MainContext::default();
34-
main_context.spawn_local(clone!(@strong sender => async move {
33+
glib::spawn_future_local(clone!(@strong sender => async move {
3534
// Deactivate the button until the operation is done
3635
sender.send(false).await.expect("The channel needs to be open.");
3736
glib::timeout_future_seconds(5).await;
@@ -40,9 +39,8 @@ fn build_ui(app: &Application) {
4039
}));
4140
});
4241

43-
let main_context = MainContext::default();
4442
// The main loop executes the asynchronous block
45-
main_context.spawn_local(clone!(@weak button => async move {
43+
glib::spawn_future_local(clone!(@weak button => async move {
4644
while let Ok(enable_button) = receiver.recv().await {
4745
button.set_sensitive(enable_button);
4846
}

book/listings/main_event_loop/5/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use glib::{clone, MainContext};
1+
use glib::clone;
22
use gtk::prelude::*;
33
use gtk::{glib, Application, ApplicationWindow, Button};
44

@@ -28,8 +28,7 @@ fn build_ui(app: &Application) {
2828
// ANCHOR: callback
2929
// Connect to "clicked" signal of `button`
3030
button.connect_clicked(move |button| {
31-
let main_context = MainContext::default();
32-
main_context.spawn_local(clone!(@weak button => async move {
31+
glib::spawn_future_local(clone!(@weak button => async move {
3332
// Deactivate the button until the operation is done
3433
button.set_sensitive(false);
3534
glib::timeout_future_seconds(5).await;

book/listings/main_event_loop/6/main.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use glib::{clone, MainContext};
1+
use glib::clone;
22
use gtk::prelude::*;
33
use gtk::{gio, glib};
44
use gtk::{Application, ApplicationWindow, Button};
@@ -31,14 +31,13 @@ fn build_ui(app: &Application) {
3131
// ANCHOR: callback
3232
// Connect to "clicked" signal of `button`
3333
button.connect_clicked(move |button| {
34-
let main_context = MainContext::default();
3534
// The main loop executes the asynchronous block
36-
main_context.spawn_local(clone!(@weak button => async move {
35+
glib::spawn_future_local(clone!(@weak button => async move {
3736
// Deactivate the button until the operation is done
3837
button.set_sensitive(false);
3938
let enable_button = gio::spawn_blocking(move || {
40-
let ten_seconds = Duration::from_secs(10);
41-
thread::sleep(ten_seconds);
39+
let five_seconds = Duration::from_secs(5);
40+
thread::sleep(five_seconds);
4241
true
4342
})
4443
.await

book/listings/main_event_loop/7/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ashpd::desktop::account::UserInformation;
22
use ashpd::WindowIdentifier;
3-
use glib::{clone, MainContext};
3+
use glib::clone;
44
use gtk::prelude::*;
55
use gtk::{glib, Application, ApplicationWindow, Button};
66

@@ -30,9 +30,8 @@ fn build_ui(app: &Application) {
3030
// ANCHOR: callback
3131
// Connect to "clicked" signal of `button`
3232
button.connect_clicked(move |button| {
33-
let main_context = MainContext::default();
3433
// The main loop executes the asynchronous block
35-
main_context.spawn_local(clone!(@weak button => async move {
34+
glib::spawn_future_local(clone!(@weak button => async move {
3635
// Get native of button for window identifier
3736
let native = button.native().expect("Need to be able to get native.");
3837
// Get window identifier so that the dialog will be modal to the main window

book/listings/main_event_loop/8/main.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use glib::{clone, MainContext};
1+
use glib::clone;
22
use gtk::glib;
33
use gtk::prelude::*;
44
use gtk::{Application, ApplicationWindow, Button};
@@ -30,25 +30,23 @@ fn build_ui(app: &Application) {
3030
let (sender, receiver) = async_channel::bounded(1);
3131
// Connect to "clicked" signal of `button`
3232
button.connect_clicked(move |_| {
33-
let main_context = MainContext::default();
3433
// The main loop executes the asynchronous block
35-
main_context.spawn_local(clone!(@strong sender => async move {
34+
glib::spawn_future_local(clone!(@strong sender => async move {
3635
let response = reqwest::get("https://www.gtk-rs.org").await;
3736
sender.send(response).await.expect("The channel needs to be open.");
3837
}));
3938
});
4039

41-
let main_context = MainContext::default();
4240
// The main loop executes the asynchronous block
43-
main_context.spawn_local(clone!(@weak button => async move {
41+
glib::spawn_future_local(async move {
4442
while let Ok(response) = receiver.recv().await {
4543
if let Ok(response) = response {
4644
println!("Status: {}", response.status());
4745
} else {
4846
println!("Could not make a `GET` request.");
4947
}
5048
}
51-
}));
49+
});
5250
// ANCHOR_END: callback
5351

5452
// Create a window

book/listings/main_event_loop/9/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use glib::{clone, MainContext};
1+
use glib::clone;
22
use gtk::glib;
33
use gtk::prelude::*;
44
use gtk::{Application, ApplicationWindow, Button};
@@ -42,17 +42,16 @@ fn build_ui(app: &Application) {
4242
}));
4343
});
4444

45-
let main_context = MainContext::default();
4645
// The main loop executes the asynchronous block
47-
main_context.spawn_local(clone!(@weak button => async move {
46+
glib::spawn_future_local(async move {
4847
while let Ok(response) = receiver.recv().await {
4948
if let Ok(response) = response {
5049
println!("Status: {}", response.status());
5150
} else {
5251
println!("Could not make a `GET` request.");
5352
}
5453
}
55-
}));
54+
});
5655
// ANCHOR_END: callback
5756

5857
// Create a window

book/src/main_event_loop.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ However, we don't want to block the main loop while waiting for a message to rec
7979
That is the whole point of the exercise after all!
8080

8181
We solve that problem by waiting for messages in an [`async`](https://rust-lang.github.io/async-book/) block.
82-
This `async` block is spawned on the `glib` main loop with [`spawn_local`](https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/struct.MainContext.html#method.spawn_local)
82+
This `async` block is spawned on the `glib` main loop with [`spawn_future_local`](https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/fn.spawn_future_local.html)
8383

84-
> See also [`spawn`](https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/struct.MainContext.html#method.spawn) for spawning async blocks on the main loop from outside the main thread.
84+
> See also [`spawn_future`](https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/fn.spawn_future.html) for spawning async blocks on the main loop from outside the main thread.
8585
8686
Filename: <a class=file-link href="https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/main_event_loop/3/main.rs">listings/main_event_loop/3/main.rs</a>
8787

0 commit comments

Comments
 (0)