Skip to content
This repository was archived by the owner on Mar 4, 2024. It is now read-only.

Commit e11d592

Browse files
committed
Use async-channel in the examples
glib's MainContext::channel was removed from glib-rs
1 parent 168f4e7 commit e11d592

File tree

4 files changed

+30
-30
lines changed

4 files changed

+30
-30
lines changed

examples/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ authors = ["The gtk-rs Project Developers"]
55
edition = "2021"
66

77
[dependencies]
8+
async-channel = "2.0.0"
89
chrono = "0.4"
910
futures = "0.3"
1011
futures-util = "0.3"

examples/cairo_threads/main.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn build_ui(application: &gtk::Application) {
3939

4040
// This is the channel for sending results from the worker thread to the main thread
4141
// For every received image, queue the corresponding part of the DrawingArea for redrawing
42-
let (ready_tx, ready_rx) = glib::MainContext::channel(glib::Priority::default());
42+
let (ready_tx, ready_rx) = async_channel::bounded(10);
4343

4444
let mut images = Vec::new();
4545
let mut origins = Vec::new();
@@ -92,7 +92,7 @@ fn build_ui(application: &gtk::Application) {
9292
});
9393

9494
// Send the finished image back to the GUI thread
95-
let _ = ready_tx.send((thread_num, image));
95+
let _ = ready_tx.send_blocking((thread_num, image));
9696
}
9797
}));
9898
}
@@ -117,18 +117,18 @@ fn build_ui(application: &gtk::Application) {
117117
}),
118118
);
119119

120-
ready_rx.attach(None, move |(thread_num, image)| {
121-
let (ref images, ref origins, ref workers) = *workspace;
120+
glib::MainContext::default().spawn_local(async move {
121+
while let Ok((thread_num, image)) = ready_rx.recv().await {
122+
let (ref images, ref origins, ref workers) = *workspace;
122123

123-
// Swap the newly received image with the old stored one and send the old one back to
124-
// the worker thread
125-
let tx = &workers[thread_num];
126-
let image = images[thread_num].replace(image);
127-
let _ = tx.send(image);
124+
// Swap the newly received image with the old stored one and send the old one back to
125+
// the worker thread
126+
let tx = &workers[thread_num];
127+
let image = images[thread_num].replace(image);
128+
let _ = tx.send(image);
128129

129-
area.queue_draw_area(origins[thread_num].0, origins[thread_num].1, WIDTH, HEIGHT);
130-
131-
glib::ControlFlow::Continue
130+
area.queue_draw_area(origins[thread_num].0, origins[thread_num].1, WIDTH, HEIGHT);
131+
}
132132
});
133133

134134
window.show_all();

examples/multi_threading_context/main.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,28 @@ fn build_ui(application: &gtk::Application) {
2828
scroll.set_policy(gtk::PolicyType::Automatic, gtk::PolicyType::Automatic);
2929
scroll.add(&text_view);
3030

31-
let (tx, rx) = glib::MainContext::channel(glib::Priority::default());
31+
let (tx, rx) = async_channel::bounded(10);
3232

3333
thread::spawn(move || {
3434
for i in 1..100 {
3535
// do long work
3636
thread::sleep(Duration::from_millis(50));
3737
// send result to channel
38-
tx.send(format!("#{i} Text from another thread."))
38+
tx.send_blocking(format!("#{i} Text from another thread."))
3939
.expect("Couldn't send data to channel");
4040
// receiver will be run on the main thread
4141
}
4242
});
4343

44-
// Attach receiver to the main context and set the text buffer text from here
4544
let text_buffer = text_view
4645
.buffer()
4746
.expect("Couldn't get buffer from text_view");
48-
rx.attach(None, move |text| {
49-
text_buffer.set_text(&text);
5047

51-
glib::ControlFlow::Continue
48+
// Spawn a future on main context and set the text buffer text from here
49+
glib::MainContext::default().spawn_local(async move {
50+
while let Ok(text) = rx.recv().await {
51+
text_buffer.set_text(&text);
52+
}
5253
});
5354

5455
window.add(&scroll);

examples/progress_tracker/main.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,19 @@ impl Application {
5757

5858
active.set(true);
5959

60-
let (tx, rx) = glib::MainContext::channel(glib::Priority::default());
60+
let (tx, rx) = async_channel::bounded(10);
61+
6162
thread::spawn(move || {
6263
for v in 1..=10 {
63-
let _ = tx.send(Some(v));
64+
let _ = tx.send_blocking(Some(v));
6465
thread::sleep(Duration::from_millis(500));
6566
}
66-
let _ = tx.send(None);
67+
let _ = tx.send_blocking(None);
6768
});
6869

69-
rx.attach(None, glib::clone!(@weak active, @weak widgets => @default-return glib::ControlFlow::Break, move |value| match value {
70-
Some(value) => {
70+
let active = active.clone();
71+
glib::MainContext::default().spawn_local(async move {
72+
while let Ok(Some(value)) = rx.recv().await {
7173
widgets
7274
.main_view
7375
.progress
@@ -86,14 +88,10 @@ impl Application {
8688
glib::ControlFlow::Break
8789
}));
8890
}
89-
90-
glib::ControlFlow::Continue
91-
}
92-
None => {
93-
active.set(false);
94-
glib::ControlFlow::Break
9591
}
96-
}));
92+
93+
active.set(false);
94+
});
9795
}),
9896
);
9997
}

0 commit comments

Comments
 (0)