some fixes

This commit is contained in:
Piyush मिश्रः 2021-01-11 18:09:05 +05:30
parent 896b5bf70c
commit f6753f2af9
3 changed files with 11 additions and 79 deletions

View File

@ -1,64 +0,0 @@
extern crate gio;
extern crate glib;
extern crate gtk;
use gio::prelude::*;
use gtk::prelude::*;
use std::env::args;
use std::thread;
use std::time::Duration;
fn build_ui(application: &gtk::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::NONE_ADJUSTMENT, gtk::NONE_ADJUSTMENT);
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
.get_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();
}
fn main() {
let application = gtk::Application::new(
Some("com.github.gtk-rs.examples.multithreading_context"),
Default::default(),
)
.expect("Initialization failed...");
application.connect_activate(|app| {
build_ui(app);
});
application.run(&args().collect::<Vec<_>>());
}

View File

@ -32,7 +32,7 @@ impl Config {
}
}
enum Message {
enum MessageSerialThread {
Msg(String),
Status(String)
}
@ -243,7 +243,7 @@ pub fn build_ui(app: &gtk::Application, config: Arc::<Mutex::<Config>>) {
}
});
//avrodith_btn
// avrodith_btn
let avrodith_btn = builder.get_object::<gtk::ToolButton>("avrodith_btn").expect("Resource file missing!");
let tmp_bar = bar.clone();
@ -259,7 +259,7 @@ pub fn build_ui(app: &gtk::Application, config: Arc::<Mutex::<Config>>) {
}
});
//clear_log
// clear_log
let clear_log = builder.get_object::<gtk::ToolButton>("clear_log").expect("Resource file missing!");
let tmp_log_area = log_area.clone();
@ -276,7 +276,7 @@ pub fn build_ui(app: &gtk::Application, config: Arc::<Mutex::<Config>>) {
send_text(&tmp_config, ent, &tmp_bar);
});
//send_btn
// send_btn
let send_btn = builder.get_object::<gtk::Button>("send_btn").expect("Resource file missing!");
let tmp_bar = bar.clone();
@ -285,7 +285,7 @@ pub fn build_ui(app: &gtk::Application, config: Arc::<Mutex::<Config>>) {
send_text(&tmp_config, &send_entry, &tmp_bar);
});
// serial
// Serial Thread
let (sender, receiver) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
let tmp_config = Arc::clone(&config);
@ -297,14 +297,15 @@ pub fn build_ui(app: &gtk::Application, config: Arc::<Mutex::<Config>>) {
}
});
// Serial Thread Reciver
let full_log = builder.get_object::<gtk::CheckButton>("full_log").expect("Resource file missing!");
let tmp_graph = Rc::clone(&graph);
receiver.attach(None, move |msg| {
match msg {
Message::Msg(text) => {
MessageSerialThread::Msg(text) => {
receiver_for_msg(text, &tmp_graph, &full_log, &log_area);
},
Message::Status(text) => {
MessageSerialThread::Status(text) => {
bar.push(1, &text);
}
}
@ -330,7 +331,7 @@ pub fn build_ui(app: &gtk::Application, config: Arc::<Mutex::<Config>>) {
fn serial_thread_work(
config: &Arc<Mutex<Config>>,
bufread: &mut Option<BufReader<Box<dyn serialport::SerialPort>>>,
sender: &glib::Sender<Message>,
sender: &glib::Sender<MessageSerialThread>,
buf: &mut String) {
match config.lock() {
Ok(mut config) => {
@ -342,7 +343,7 @@ fn serial_thread_work(
Status::JAGRIT => {
if let Some(read) = bufread {
if let Ok(_) = read.read_line(buf) {
sender.send(Message::Msg(buf.clone())).unwrap();
sender.send(MessageSerialThread::Msg(buf.clone())).unwrap();
buf.clear();
}
}
@ -363,7 +364,7 @@ fn serial_thread_work(
}
}, Err(_) => {
sender.send(Message::Status("Faild prepare for communication!".to_owned())).unwrap();
sender.send(MessageSerialThread::Status("Faild prepare for communication!".to_owned())).unwrap();
return;
}
};

View File

@ -1,5 +0,0 @@
#[test]
fn start() {
}