forked from gtk-rs/gtk3-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
56 lines (44 loc) · 1.59 KB
/
main.rs
File metadata and controls
56 lines (44 loc) · 1.59 KB
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
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) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
thread::spawn(move || {
for i in 1..100 {
// do long work
thread::sleep(Duration::from_millis(50));
// send result to channel
tx.send(format!("#{} Text from another thread.", i))
.expect("Couldn't send data to channel");
// receiver will be run on the main thread
}
});
// Attach receiver to the main context and set the text buffer text from here
let text_buffer = text_view
.buffer()
.expect("Couldn't get buffer from text_view");
rx.attach(None, move |text| {
text_buffer.set_text(&text);
glib::Continue(true)
});
window.add(&scroll);
window.show_all();
}