From f40f2be0a0b019a4fecd881f8e6d8cba8b0d4563 Mon Sep 17 00:00:00 2001 From: Guangzong Chen Date: Tue, 5 Oct 2021 12:10:05 -0400 Subject: [PATCH] update status bar --- Cargo.lock | 22 ++++++------- Cargo.toml | 7 +++- src/main.css | 0 src/main.rs | 92 +++++++++++++++++++++++++++------------------------- src/port.rs | 9 +++-- src/test.ui | 41 +++++++++++++++++++++++ 6 files changed, 112 insertions(+), 59 deletions(-) create mode 100644 src/main.css diff --git a/Cargo.lock b/Cargo.lock index 93411b1..9a11c1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -392,17 +392,6 @@ dependencies = [ "syn", ] -[[package]] -name = "gtk_ui" -version = "0.1.0" -dependencies = [ - "closure", - "gio", - "glib", - "gtk", - "serialport", -] - [[package]] name = "heck" version = "0.3.3" @@ -653,6 +642,17 @@ version = "1.0.130" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913" +[[package]] +name = "serial_communication_tool_v2" +version = "0.1.0" +dependencies = [ + "closure", + "gio", + "glib", + "gtk", + "serialport", +] + [[package]] name = "serialport" version = "4.0.1" diff --git a/Cargo.toml b/Cargo.toml index 10c666c..0a9745f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,4 +15,9 @@ glib = "0.14.5" #gtk4_sys = "0.3.0" #gtk4-sys = "0.3.0" serialport = "4.0.1" -closure = "0.3.0" \ No newline at end of file +closure = "0.3.0" +[profile.release] +opt-level = 'z' +lto = true +codegen-units = 1 +panic = 'abort' \ No newline at end of file diff --git a/src/main.css b/src/main.css new file mode 100644 index 0000000..e69de29 diff --git a/src/main.rs b/src/main.rs index cdef62a..c1b34e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,17 @@ +#![windows_subsystem = "windows"] mod port; + use closure::closure; -use gtk::{AboutDialog, Inhibit, TextView}; +use gtk::{Inhibit, TextView, Statusbar, Label}; use gtk::{gio, glib}; -use gtk::prelude::{GtkApplicationExt, GtkWindowExt, BuilderExtManual, GtkMenuItemExt, WidgetExt, WidgetExtManual, EntryExt, TextBufferExt}; +use gtk::prelude::{GtkWindowExt, BuilderExtManual, GtkMenuItemExt, WidgetExt, EntryExt, TextBufferExt}; use gio::prelude::{ApplicationExt, ApplicationExtManual}; use std::sync::mpsc::{Sender, Receiver, channel}; use serialport::{StopBits, FlowControl, Parity, DataBits}; use crate::port::*; use gtk::prelude::TextViewExt; +use gtk::prelude::LabelExt; fn main() { let application = gtk::Application::new( @@ -38,64 +41,51 @@ fn build_ui(application: >k::Application) { } }); let log_window: gtk::Window = builder.object("log_window").expect("Can not get log view"); - let menu_log_command:gtk::MenuItem = builder.object("log_command").expect("Can not get log command"); - let ui_receive_view:TextView = builder.object("receive_view").expect("can not get receive view"); + let menu_log_command: gtk::MenuItem = builder.object("log_command").expect("Can not get log command"); + let ui_receive_view: TextView = builder.object("receive_view").expect("can not get receive view"); + let ui_baudrate: Label = builder.object("ui_baudrate").unwrap(); + let ui_databit: Label = builder.object("ui_databit").unwrap(); + let ui_parity: Label = builder.object("ui_parity").unwrap(); + let ui_stopbit: Label = builder.object("ui_stopbit").unwrap(); + let ui_statusbar: Statusbar = builder.object("statusbar").unwrap(); + let ui_log_view: TextView = builder.object("log_view").unwrap(); log_window.show(); - let ui_log_view:TextView = builder.object("log_view").unwrap(); - log_window.connect_delete_event(|a,_|{ + log_window.connect_delete_event(|a, _| { a.hide(); Inhibit(true) }); - let tmp = main_window.clone(); + let tmp = log_window.clone(); menu_log_command.connect_activate(move |_| { - if log_window.is_visible() { - log_window.hide(); - } - else { - log_window.show(); - // tmp.show(); - // tmp.set_has_focus(true); - tmp.present(); - tmp.grab_focus(); + if tmp.is_visible() { + tmp.hide(); + } else { + tmp.show(); } }); let (sender_ui_upd, receiver_ui_upd) = glib::MainContext::channel(glib::PRIORITY_DEFAULT); - receiver_ui_upd.attach(None, move |msg|{ - + receiver_ui_upd.attach(None, move |msg| { match msg { Message::UiUpdateLog(text) => { let buf = ui_log_view.buffer().unwrap(); - // buf.insert() buf.insert(&mut buf.end_iter(), text.as_str()); - - // ui_log_view.scroll_to_mark(&buf.get_insert(),0.0, true,0.5,0.5); } Message::UiUpdateReceiveMsg(text) => { let buf = ui_receive_view.buffer().unwrap(); buf.insert(&mut buf.end_iter(), text.as_str()); ui_receive_view.scroll_to_mark(&buf.get_insert().unwrap(), 0.0, true, 0.5, 0.5); } - Message::UiCleanReceiveMsg => { - let buf = ui_receive_view.buffer().unwrap(); - // buf.set_text(Option::from("")); - buf.set_text(""); - } - Message::UiCleanLog => { - let buf = ui_log_view.buffer().unwrap(); - buf.set_text(""); - // buf.set_text(Option::from("")); - } - Message::UiShowReceive => { - // ui_receive_windows.show(); - } - Message::UiHideReceive => { - // ui_receive_windows.hide(); - } + Message::UiCleanReceiveMsg => ui_receive_view.buffer().unwrap().set_text(""), + Message::UiCleanLog => ui_log_view.buffer().unwrap().set_text(""), + Message::UiShowLog => log_window.show(), + Message::UiHideLog => log_window.hide(), + Message::UiUpdateStatusBarBaudrate(value) => ui_baudrate.set_text(value.to_string().as_str()), + Message::UiUpdateStatusBarDatabit(value) => ui_databit.set_text(value.to_string().as_str()), + Message::UiUpdateStatusBarParity(value) => ui_parity.set_text(value.to_string().as_str()), + Message::UiUpdateStatusBarStopbit(value) => ui_stopbit.set_text(value.to_string().as_str()), _ => {} } glib::Continue(true) - }); let (gui2main_tx, gui2main_rx) = channel::(); @@ -153,7 +143,7 @@ fn process_cmd(command: String, to_ui: glib::Sender, gui2main_tx: Sende to_ui.send(Message::UiUpdateLog("No enough argument\n".to_string())).unwrap(); } else { match command_split[1] { - "rec" => { to_ui.send(Message::UiShowReceive).unwrap(); } + "log" => { to_ui.send(Message::UiShowLog).unwrap(); } _ => {} } } @@ -163,7 +153,7 @@ fn process_cmd(command: String, to_ui: glib::Sender, gui2main_tx: Sende to_ui.send(Message::UiUpdateLog("No enough argument\n".to_string())).unwrap(); } else { match command_split[1] { - "rec" => { to_ui.send(Message::UiHideReceive).unwrap(); } + "log" => { to_ui.send(Message::UiHideLog).unwrap(); } _ => {} } } @@ -218,6 +208,7 @@ fn main_proc(to_main_tx: Sender, rx: Receiver, ui_upd: glib::S } else { if let Ok(value) = args[1].parse::() { port_default_config.baud_rate = value; + ui_upd.send(Message::UiUpdateStatusBarBaudrate(value)).unwrap(); } else { ui_upd.send(Message::UiUpdateLog("Invalid command!\n".to_string())).unwrap(); } @@ -228,6 +219,7 @@ fn main_proc(to_main_tx: Sender, rx: Receiver, ui_upd: glib::S ui_upd.send(Message::UiUpdateLog("Invalid command!\n".to_string())).unwrap(); } else { if let Ok(value) = args[1].parse::() { + ui_upd.send(Message::UiUpdateStatusBarDatabit(value)).unwrap(); match value { 8 => port_default_config.data_bits = DataBits::Eight, 7 => port_default_config.data_bits = DataBits::Seven, @@ -246,9 +238,18 @@ fn main_proc(to_main_tx: Sender, rx: Receiver, ui_upd: glib::S } else { if let Ok(value) = args[1].parse::() { match value { - 0 => port_default_config.parity = Parity::None, - 1 => port_default_config.parity = Parity::Odd, - 2 => port_default_config.parity = Parity::Even, + 0 => { + port_default_config.parity = Parity::None; + ui_upd.send(Message::UiUpdateStatusBarParity("None".to_string())).unwrap(); + } + 1 => { + port_default_config.parity = Parity::Odd; + ui_upd.send(Message::UiUpdateStatusBarParity("Odd".to_string())).unwrap(); + } + 2 => { + port_default_config.parity = Parity::Even; + ui_upd.send(Message::UiUpdateStatusBarParity("Even".to_string())).unwrap(); + } _ => { ui_upd.send(Message::UiUpdateLog("Invalid command!\n".to_string())).unwrap(); } } } else { @@ -261,9 +262,10 @@ fn main_proc(to_main_tx: Sender, rx: Receiver, ui_upd: glib::S ui_upd.send(Message::UiUpdateLog("Invalid command!\n".to_string())).unwrap(); } else { if let Ok(value) = args[1].parse::() { + ui_upd.send(Message::UiUpdateStatusBarStopbit(value)).unwrap(); match value { - 0 => port_default_config.stopbit = StopBits::One, - 1 => port_default_config.stopbit = StopBits::Two, + 1 => port_default_config.stopbit = StopBits::One, + 2 => port_default_config.stopbit = StopBits::Two, _ => { ui_upd.send(Message::UiUpdateLog("Invalid command!\n".to_string())).unwrap(); } } } else { diff --git a/src/port.rs b/src/port.rs index 2d9f090..8e380a4 100644 --- a/src/port.rs +++ b/src/port.rs @@ -9,8 +9,13 @@ pub enum Message { UiUpdateReceiveMsg(String), UiCleanReceiveMsg, UiCleanLog, - UiShowReceive, - UiHideReceive, + UiShowLog, + UiHideLog, + + UiUpdateStatusBarBaudrate(u32), + UiUpdateStatusBarParity(String), + UiUpdateStatusBarStopbit(u32), + UiUpdateStatusBarDatabit(u32), MainCmdOpen(String), MainCmdClose, diff --git a/src/test.ui b/src/test.ui index 331258f..85ace61 100644 --- a/src/test.ui +++ b/src/test.ui @@ -236,6 +236,47 @@ 2 + + + True + False + True + 5 + + + + True + False + True + 115200 + + + + + True + False + True + 8 + + + + + True + False + True + None + + + + + True + False + True + 1 + + + +