update ui and fix output issue

This commit is contained in:
Guangzong Chen 2021-09-29 13:59:35 -04:00
parent e4791d534e
commit 2eaca282bd
No known key found for this signature in database
GPG Key ID: E3E141D720E7F9A3

View File

@ -5,10 +5,6 @@ use std::sync::mpsc::{channel, Sender, Receiver};
use std::env; use std::env;
use serialport::{SerialPort, StopBits, Parity, FlowControl, DataBits}; 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)] #[derive(Copy, Clone)]
struct PortConfig { struct PortConfig {
@ -29,8 +25,8 @@ enum Message {
UiUpdateReceiveMsg(String), UiUpdateReceiveMsg(String),
UiCleanReceiveMsg, UiCleanReceiveMsg,
UiCleanLog, UiCleanLog,
UiShowLog, UiShowReceive,
UiHideLog, UiHideReceive,
MainCmdOpen(String), MainCmdOpen(String),
MainCmdClose, MainCmdClose,
@ -45,29 +41,28 @@ enum Message {
PortError(String), PortError(String),
} }
// use gtk::pango::*; enum Error {
// use gtk::gdk::Display; PortOpenFailed(String),
// use gtk::{Application, ApplicationWindow, Orientation, CssProvider, StyleContext}; }
// use gtk::prelude::StyleContextExt;
// use gtk::prelude::*;
use gtk4::prelude::*; use gtk4::prelude::*;
// ::prelude::*; use gtk4::{Application, ApplicationWindow, CssProvider, StyleContext, STYLE_PROVIDER_PRIORITY_APPLICATION, Orientation, ScrolledWindow, ScrollablePolicy, WrapMode};
// use gtk::prelude::WidgetExt;
use gtk4::{Application, ApplicationWindow, CssProvider, Entry, StyleContext, STYLE_PROVIDER_PRIORITY_APPLICATION, Orientation, ScrolledWindow, ScrollablePolicy, WrapMode};
use gtk4::gdk::Display; 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() { fn main() {
let app = Application::builder() let app = Application::builder()
.application_id("org.example.HelloWorld") .application_id("org.example.HelloWorld")
.build(); .build();
app.connect_activate(|app| { 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() let ui_main_window = ApplicationWindow::builder()
.application(app) .application(app)
.default_width(320) .default_width(320)
@ -75,62 +70,53 @@ fn main() {
.title("hello world!") .title("hello world!")
.build(); .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); let ui_layout_main_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_main_scroller_window = ScrolledWindow::builder().build(); let ui_main_scroller_window = ScrolledWindow::builder().build();
ui_main_scroller_window.set_child(Some(&ui_receive_view)); let ui_command_editor = gtk4::Entry::new();
ui_receive_view.set_vscroll_policy(ScrollablePolicy::Natural); let ui_log_view = gtk4::TextView::new();
// ui_layout_box.append(&ui_receive_view); ui_log_view.set_editable(false);
ui_layout_box.append(&ui_main_scroller_window); ui_log_view.set_wrap_mode(WrapMode::Char);
ui_layout_box.append(&ui_command_editor); ui_log_view.set_css_classes(&["textview1"]);
ui_receive_view.set_editable(false); ui_log_view.set_vexpand(true);
ui_receive_view.show(); 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_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_width(350)
.default_height(450) .default_height(450)
.title("log") .title("log")
.build(); .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_receive_view.set_vexpand(true);
ui_log_view.set_can_focus(false); ui_receive_view.set_vscroll_policy(ScrollablePolicy::Natural);
ui_log_view.set_editable(false); ui_receive_view.set_editable(false);
// ui_log_view.set_vscroll_policy(ScrollablePolicy::Natural); ui_receive_view.show();
ui_log_view.set_wrap_mode(WrapMode::Char); ui_receive_windows.set_child(Some(&ui_receive_view));
ui_log_view.set_css_classes(&["textview1"]);
ui_log_window.set_child(Some(&ui_log_view));
ui_log_window.show();
ui_receive_windows.show();
ui_main_window.show(); ui_main_window.show();
ui_log_view.show(); ui_log_view.show();
let (sender_ui_upd, receiver_ui_upd) = glib::MainContext::channel(glib::PRIORITY_DEFAULT); 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| { receiver_ui_upd.attach(None, move |msg| {
match msg { match msg {
Message::UiUpdateLog(text) => { Message::UiUpdateLog(text) => {
@ -150,13 +136,12 @@ fn main() {
Message::UiCleanLog => { Message::UiCleanLog => {
let buf = ui_log_view.buffer(); let buf = ui_log_view.buffer();
buf.set_text(""); buf.set_text("");
} }
Message::UiShowLog => { Message::UiShowReceive => {
ui_log_window.show(); ui_receive_windows.show();
} }
Message::UiHideLog => { Message::UiHideReceive => {
ui_log_window.hide(); ui_receive_windows.hide();
} }
_ => {} _ => {}
} }
@ -213,14 +198,13 @@ fn process_cmd(command: String, to_ui: glib::Sender<Message>, gui2main_tx: Sende
"clean" => { "clean" => {
to_ui.send(Message::UiCleanReceiveMsg).unwrap(); to_ui.send(Message::UiCleanReceiveMsg).unwrap();
to_ui.send(Message::UiCleanLog).unwrap(); to_ui.send(Message::UiCleanLog).unwrap();
} }
"show" => { "show" => {
if command_split.len() == 1 { if command_split.len() == 1 {
to_ui.send(Message::UiUpdateLog("No enough argument\n".to_string())).unwrap(); to_ui.send(Message::UiUpdateLog("No enough argument\n".to_string())).unwrap();
} else { } else {
match command_split[1] { 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<Message>, gui2main_tx: Sende
to_ui.send(Message::UiUpdateLog("No enough argument\n".to_string())).unwrap(); to_ui.send(Message::UiUpdateLog("No enough argument\n".to_string())).unwrap();
} else { } else {
match command_split[1] { 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<Message>, rx: Receiver<Message>, ui_upd: glib::S
_ => { ui_upd.send(Message::UiUpdateLog("command does not support!\n".to_string())).unwrap(); } _ => { ui_upd.send(Message::UiUpdateLog("command does not support!\n".to_string())).unwrap(); }
} }
} }
Message::PortHaveData(data) => unsafe { Message::PortHaveData(data) => {
ui_upd.send(Message::UiUpdateReceiveMsg(String::from_utf8_unchecked(data))).unwrap(); ui_upd.send(Message::UiUpdateReceiveMsg(String::from_utf8_lossy(&*data).parse().unwrap())).unwrap();
} }
Message::PortCloseSucceed => { Message::PortCloseSucceed => {
ui_upd.send(Message::UiUpdateLog("Close port succeed\n".to_string())).unwrap(); ui_upd.send(Message::UiUpdateLog("Close port succeed\n".to_string())).unwrap();
@ -365,9 +349,6 @@ fn main_proc(to_main_tx: Sender<Message>, rx: Receiver<Message>, ui_upd: glib::S
} }
} }
enum Error {
PortOpenFailed(String),
}
fn port_open(port: String, config: PortConfig) -> Result<Box<dyn SerialPort>, Error> { fn port_open(port: String, config: PortConfig) -> Result<Box<dyn SerialPort>, Error> {
let mut prelude = ""; let mut prelude = "";