This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
main.rs
57 lines (45 loc) · 1.62 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use gtk::glib;
use gtk::prelude::*;
use std::thread;
use std::time::Duration;
fn main() {
let application = gtk::Application::new(
Some("com.github.gtk-rs.examples.multithreading_context"),
Default::default(),
);
application.connect_activate(build_ui);
application.run();
}
fn build_ui(application: >k::Application) {
let window = gtk::ApplicationWindow::new(application);
window.set_title("Multithreading GTK+ Program");
window.set_border_width(10);
window.set_position(gtk::WindowPosition::Center);
window.set_default_size(600, 400);
let text_view = gtk::TextView::new();
let scroll = gtk::ScrolledWindow::new(gtk::Adjustment::NONE, gtk::Adjustment::NONE);
scroll.set_policy(gtk::PolicyType::Automatic, gtk::PolicyType::Automatic);
scroll.add(&text_view);
let (tx, rx) = async_channel::bounded(10);
thread::spawn(move || {
for i in 1..100 {
// do long work
thread::sleep(Duration::from_millis(50));
// send result to channel
tx.send_blocking(format!("#{i} Text from another thread."))
.expect("Couldn't send data to channel");
// receiver will be run on the main thread
}
});
let text_buffer = text_view
.buffer()
.expect("Couldn't get buffer from text_view");
// Spawn a future on main context and set the text buffer text from here
glib::MainContext::default().spawn_local(async move {
while let Ok(text) = rx.recv().await {
text_buffer.set_text(&text);
}
});
window.add(&scroll);
window.show_all();
}