Last active
November 18, 2020 22:31
-
-
Save jcbritobr/a67604c8445f2edbb2028949bad2c1bb to your computer and use it in GitHub Desktop.
Gtk-rs multithreading with rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![windows_subsystem = "windows"] | |
use gtk::prelude::*; | |
use std::{thread, time::Duration}; | |
fn main() { | |
println!("Hello, world!"); | |
if gtk::init().is_err() { | |
eprintln!("failed to initialize"); | |
return; | |
} | |
let glade_src = include_str!("gui.glade"); | |
let builder = gtk::Builder::from_string(glade_src); | |
let button: gtk::Button = builder.get_object("cmd_clique").unwrap(); | |
let window: gtk::Window = builder.get_object("main_window").unwrap(); | |
let mut _progress: gtk::ProgressBar = builder.get_object("pb_progress").unwrap(); | |
window.connect_destroy(|_| { | |
gtk::main_quit(); | |
}); | |
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT); | |
button.connect_clicked(move |_| { | |
let ntx = tx.clone(); | |
thread::spawn(move || { | |
for i in 0..100 { | |
thread::sleep(Duration::from_millis(100)); | |
ntx.send(i).expect("cant sent data"); | |
} | |
}); | |
}); | |
let pb = _progress.clone(); | |
rx.attach(None, move |id| { | |
let data = (id as f64) / 100.0; | |
pb.set_fraction(data); | |
glib::Continue(true) | |
}); | |
window.show_all(); | |
gtk::main(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment