From 1ac05ab2469cd4b3a2ccc6d740ae4f8e38f3c558 Mon Sep 17 00:00:00 2001 From: JIe Date: Mon, 9 Dec 2024 10:33:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86request=20close=20=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6,=20update=E5=87=BD=E6=95=B0=E8=BF=94=E5=9B=9ETask?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 76 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index d3da868..ccdf635 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,19 @@ -use iced::widget::{button, center, checkbox, column, container, horizontal_rule, pick_list, progress_bar, radio, row, scrollable, slider, text, text_editor, text_input, toggler, vertical_rule, vertical_space, Checkbox, Scrollable}; -use iced::{Center, Element, Fill, Length, Size, Task, Theme}; -use iced::widget::scrollable::{scroll_to, AbsoluteOffset}; +mod download_wrapper; + + +use iced::widget::{button, checkbox, column, container, radio, row, text_editor, text_input}; +use iced::{event, window, Element, Event, Length, Subscription, Task}; use iced::widget::text_editor::Action::Scroll; use iced::widget::text_editor::Edit; pub fn main() ->iced::Result { iced::application("WisunDownload V0.1", MainWindow::update, MainWindow::view) + .subscription(MainWindow::subscription) + .exit_on_close_request(false) .window_size((830.0, 600.0)) .run() } -#[derive(Default)] struct MainWindow { is_online: bool, mysql_config: MysqlConfig, @@ -19,6 +22,19 @@ struct MainWindow { log_content: text_editor::Content, } +impl Default for MainWindow { + fn default() -> Self { + Self{ + is_online: true, + mysql_config: MysqlConfig::default(), + selection: None, + label: String::new(), + log_content: text_editor::Content::default(), + + } + } +} + #[derive(Debug, Clone, Default)] struct MysqlConfig{ ip: String, @@ -41,7 +57,9 @@ enum Message{ TypeSelected(DownloadType), OnlineChecked(bool), AddLog(String), - LogContentChanged(text_editor::Action) + LogContentChanged(text_editor::Action), + SaveConfig, + WindowEvent(Event) } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -52,11 +70,11 @@ enum DownloadType{ } impl MainWindow{ - pub fn add_log(&mut self, msg: String){ + pub fn add_log(&mut self, msg: String) -> Task{ self.update(Message::AddLog(msg)) } - fn update(&mut self, message: Message){ + fn update(&mut self, message: Message)-> Task{ match message{ Message::Start() => { println!("Start: {:?}", self.mysql_config); @@ -67,33 +85,43 @@ impl MainWindow{ self.add_log("bbbbbbbbbbbbbbbb".to_string()); self.add_log("cccccccccccccccc".to_string()); println!("{:?}", self.log_content.text()); + Task::none() } Message::IpChanged(ip) => { self.mysql_config.ip = ip; + Task::none() } Message::PortChanged(port) => { self.mysql_config.port = port; + Task::none() } Message::UsernameChanged(username)=>{ - self.mysql_config.username = username + self.mysql_config.username = username; + Task::none() } Message::PasswordChanged(password)=>{ - self.mysql_config.password = password + self.mysql_config.password = password; + Task::none() } Message::DatabaseChanged(database)=>{ - self.mysql_config.database = database + self.mysql_config.database = database; + Task::none() } Message::WorkOrderChanged(work_order)=>{ - self.mysql_config.work_order = work_order + self.mysql_config.work_order = work_order; + Task::none() } Message::TypeSelected(download_type)=>{ - self.selection = Some(download_type) + self.selection = Some(download_type); + Task::none() } Message::LabelChanged(label)=>{ - self.label = label + self.label = label; + Task::none() } Message::OnlineChecked(is_online)=>{ - self.is_online = is_online + self.is_online = is_online; + Task::none() } Message::AddLog(log)=>{ for c in log.chars(){ @@ -102,14 +130,24 @@ impl MainWindow{ self.log_content.perform(text_editor::Action::Edit(Edit::Enter)); let line_size = self.log_content.lines().count(); self.log_content.perform(Scroll{lines: line_size as i32 }); + Task::none() } Message::LogContentChanged(action)=>{ if action.is_edit(){ - return + return Task::none() } - self.log_content.perform(action) + self.log_content.perform(action); + Task::none() } - _=>{} + Message::WindowEvent(event) =>{ + println!("{:?}", event); + if let Event::Window(window::Event::CloseRequested) = event { + println!("Request Close"); + return window::get_latest().and_then(window::close) + }; + Task::none() + } + _=>{Task::none()} } } @@ -160,5 +198,7 @@ impl MainWindow{ container.into() } - + fn subscription(&self) -> Subscription { + event::listen().map(Message::WindowEvent) + } } \ No newline at end of file