diff --git a/src/main.rs b/src/main.rs index 99e570a..15d9b8d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,10 +5,6 @@ use std::sync::mpsc::{channel, Sender, Receiver}; use std::env; use serialport::{SerialPort, StopBits, Parity, FlowControl, DataBits}; -// use mio_serial::{StopBits, SerialStream, Parity, SerialPortBuilder, FlowControl, DataBits, SerialPort}; - -// extern crate glib; -// extern crate gio; #[derive(Copy, Clone)] struct PortConfig { @@ -29,8 +25,8 @@ enum Message { UiUpdateReceiveMsg(String), UiCleanReceiveMsg, UiCleanLog, - UiShowLog, - UiHideLog, + UiShowReceive, + UiHideReceive, MainCmdOpen(String), MainCmdClose, @@ -45,29 +41,28 @@ enum Message { PortError(String), } -// use gtk::pango::*; -// use gtk::gdk::Display; -// use gtk::{Application, ApplicationWindow, Orientation, CssProvider, StyleContext}; -// use gtk::prelude::StyleContextExt; -// use gtk::prelude::*; +enum Error { + PortOpenFailed(String), +} + use gtk4::prelude::*; -// ::prelude::*; -// use gtk::prelude::WidgetExt; -use gtk4::{Application, ApplicationWindow, CssProvider, Entry, StyleContext, STYLE_PROVIDER_PRIORITY_APPLICATION, Orientation, ScrolledWindow, ScrollablePolicy, WrapMode}; +use gtk4::{Application, ApplicationWindow, CssProvider, StyleContext, STYLE_PROVIDER_PRIORITY_APPLICATION, Orientation, ScrolledWindow, ScrollablePolicy, WrapMode}; use gtk4::gdk::Display; -// use gtk::gdk::{Display, Screen}; -// use gtk::{ -// Application, ApplicationWindow, Box as Box_, Button, ComboBoxText, CssProvider, Entry, -// Orientation, StyleContext, STYLE_PROVIDER_PRIORITY_APPLICATION, -// }; - - fn main() { let app = Application::builder() .application_id("org.example.HelloWorld") .build(); app.connect_activate(|app| { + + let provider = CssProvider::new(); + provider.load_from_data(include_bytes!("style.css")); + StyleContext::add_provider_for_display( + &Display::default().expect("Error initializing gtk css provider."), + &provider, + STYLE_PROVIDER_PRIORITY_APPLICATION, + ); + let ui_main_window = ApplicationWindow::builder() .application(app) .default_width(320) @@ -75,62 +70,53 @@ fn main() { .title("hello world!") .build(); - let provider = CssProvider::new(); - provider.load_from_data(include_bytes!("style.css")); - // StyleContext::add_providoer_for_screen(&provider, STYLE_PROVIDER_PRIORITY_APPLICATION); - StyleContext::add_provider_for_display( - &Display::default().expect("Error initializing gtk css provider."), - // &Screen::default().expect("Error"), - &provider, - STYLE_PROVIDER_PRIORITY_APPLICATION, - ); - let ui_layout_box = gtk4::Box::new(Orientation::Vertical, 1); - ui_main_window.set_child(Some(&ui_layout_box)); - - let ui_receive_view = gtk4::TextView::new(); - let ui_command_editor = gtk4::Entry::new(); - ui_command_editor.add_css_class("entry1"); - ui_receive_view.set_vexpand(true); - + let ui_layout_main_box = gtk4::Box::new(Orientation::Vertical, 1); let ui_main_scroller_window = ScrolledWindow::builder().build(); - ui_main_scroller_window.set_child(Some(&ui_receive_view)); - ui_receive_view.set_vscroll_policy(ScrollablePolicy::Natural); + let ui_command_editor = gtk4::Entry::new(); + let ui_log_view = gtk4::TextView::new(); - // ui_layout_box.append(&ui_receive_view); - ui_layout_box.append(&ui_main_scroller_window); - ui_layout_box.append(&ui_command_editor); - ui_receive_view.set_editable(false); - ui_receive_view.show(); + ui_log_view.set_editable(false); + ui_log_view.set_wrap_mode(WrapMode::Char); + ui_log_view.set_css_classes(&["textview1"]); + ui_log_view.set_vexpand(true); + ui_main_scroller_window.set_child(Some(&ui_log_view)); + + ui_main_window.set_child(Some(&ui_layout_main_box)); + + ui_layout_main_box.append(&ui_main_scroller_window); + ui_layout_main_box.append(&ui_command_editor); + + ui_command_editor.add_css_class("entry1"); ui_command_editor.show(); - ui_layout_box.show(); + ui_layout_main_box.show(); - let ui_log_window = ApplicationWindow::builder() + let ui_receive_windows = ApplicationWindow::builder() .default_width(350) .default_height(450) .title("log") .build(); - ui_log_window.set_hide_on_close(true); + ui_receive_windows.set_hide_on_close(true); + let ui_receive_view = gtk4::TextView::new(); - let ui_log_view = gtk4::TextView::new(); - ui_log_view.set_can_focus(false); - ui_log_view.set_editable(false); - // ui_log_view.set_vscroll_policy(ScrollablePolicy::Natural); - ui_log_view.set_wrap_mode(WrapMode::Char); - ui_log_view.set_css_classes(&["textview1"]); - ui_log_window.set_child(Some(&ui_log_view)); + ui_receive_view.set_vexpand(true); + ui_receive_view.set_vscroll_policy(ScrollablePolicy::Natural); + ui_receive_view.set_editable(false); + ui_receive_view.show(); + ui_receive_windows.set_child(Some(&ui_receive_view)); - ui_log_window.show(); + + ui_receive_windows.show(); ui_main_window.show(); ui_log_view.show(); let (sender_ui_upd, receiver_ui_upd) = glib::MainContext::channel(glib::PRIORITY_DEFAULT); - let tmp_command_editor = ui_command_editor.clone(); + // let tmp_command_editor = ui_command_editor.clone(); receiver_ui_upd.attach(None, move |msg| { match msg { Message::UiUpdateLog(text) => { @@ -141,22 +127,21 @@ fn main() { Message::UiUpdateReceiveMsg(text) => { let buf = ui_receive_view.buffer(); buf.insert(&mut buf.end_iter(), text.as_str()); - ui_receive_view.scroll_to_mark(&buf.get_insert(),0.0, true,0.5,0.5); + ui_receive_view.scroll_to_mark(&buf.get_insert(), 0.0, true, 0.5, 0.5); } Message::UiCleanReceiveMsg => { let buf = ui_receive_view.buffer(); - buf.set_text("") ; + buf.set_text(""); } Message::UiCleanLog => { let buf = ui_log_view.buffer(); buf.set_text(""); - } - Message::UiShowLog => { - ui_log_window.show(); + Message::UiShowReceive => { + ui_receive_windows.show(); } - Message::UiHideLog => { - ui_log_window.hide(); + Message::UiHideReceive => { + ui_receive_windows.hide(); } _ => {} } @@ -213,14 +198,13 @@ fn process_cmd(command: String, to_ui: glib::Sender, gui2main_tx: Sende "clean" => { to_ui.send(Message::UiCleanReceiveMsg).unwrap(); to_ui.send(Message::UiCleanLog).unwrap(); - } "show" => { if command_split.len() == 1 { to_ui.send(Message::UiUpdateLog("No enough argument\n".to_string())).unwrap(); } else { match command_split[1] { - "log" => { to_ui.send(Message::UiShowLog).unwrap(); } + "rec" => { to_ui.send(Message::UiShowReceive).unwrap(); } _ => {} } } @@ -230,7 +214,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] { - "log" => { to_ui.send(Message::UiHideLog).unwrap(); } + "rec" => { to_ui.send(Message::UiHideReceive).unwrap(); } _ => {} } } @@ -353,8 +337,8 @@ fn main_proc(to_main_tx: Sender, rx: Receiver, ui_upd: glib::S _ => { ui_upd.send(Message::UiUpdateLog("command does not support!\n".to_string())).unwrap(); } } } - Message::PortHaveData(data) => unsafe { - ui_upd.send(Message::UiUpdateReceiveMsg(String::from_utf8_unchecked(data))).unwrap(); + Message::PortHaveData(data) => { + ui_upd.send(Message::UiUpdateReceiveMsg(String::from_utf8_lossy(&*data).parse().unwrap())).unwrap(); } Message::PortCloseSucceed => { ui_upd.send(Message::UiUpdateLog("Close port succeed\n".to_string())).unwrap(); @@ -365,9 +349,6 @@ fn main_proc(to_main_tx: Sender, rx: Receiver, ui_upd: glib::S } } -enum Error { - PortOpenFailed(String), -} fn port_open(port: String, config: PortConfig) -> Result, Error> { let mut prelude = "";