处理request close 事件, update函数返回Task

This commit is contained in:
JIe 2024-12-09 10:33:01 +08:00
parent 5960a91645
commit 1ac05ab246

View File

@ -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}; mod download_wrapper;
use iced::{Center, Element, Fill, Length, Size, Task, Theme};
use iced::widget::scrollable::{scroll_to, AbsoluteOffset};
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::Action::Scroll;
use iced::widget::text_editor::Edit; use iced::widget::text_editor::Edit;
pub fn main() ->iced::Result { pub fn main() ->iced::Result {
iced::application("WisunDownload V0.1", MainWindow::update, MainWindow::view) iced::application("WisunDownload V0.1", MainWindow::update, MainWindow::view)
.subscription(MainWindow::subscription)
.exit_on_close_request(false)
.window_size((830.0, 600.0)) .window_size((830.0, 600.0))
.run() .run()
} }
#[derive(Default)]
struct MainWindow { struct MainWindow {
is_online: bool, is_online: bool,
mysql_config: MysqlConfig, mysql_config: MysqlConfig,
@ -19,6 +22,19 @@ struct MainWindow {
log_content: text_editor::Content, 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)] #[derive(Debug, Clone, Default)]
struct MysqlConfig{ struct MysqlConfig{
ip: String, ip: String,
@ -41,7 +57,9 @@ enum Message{
TypeSelected(DownloadType), TypeSelected(DownloadType),
OnlineChecked(bool), OnlineChecked(bool),
AddLog(String), AddLog(String),
LogContentChanged(text_editor::Action) LogContentChanged(text_editor::Action),
SaveConfig,
WindowEvent(Event)
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -52,11 +70,11 @@ enum DownloadType{
} }
impl MainWindow{ impl MainWindow{
pub fn add_log(&mut self, msg: String){ pub fn add_log(&mut self, msg: String) -> Task<Message>{
self.update(Message::AddLog(msg)) self.update(Message::AddLog(msg))
} }
fn update(&mut self, message: Message){ fn update(&mut self, message: Message)-> Task<Message>{
match message{ match message{
Message::Start() => { Message::Start() => {
println!("Start: {:?}", self.mysql_config); println!("Start: {:?}", self.mysql_config);
@ -67,33 +85,43 @@ impl MainWindow{
self.add_log("bbbbbbbbbbbbbbbb".to_string()); self.add_log("bbbbbbbbbbbbbbbb".to_string());
self.add_log("cccccccccccccccc".to_string()); self.add_log("cccccccccccccccc".to_string());
println!("{:?}", self.log_content.text()); println!("{:?}", self.log_content.text());
Task::none()
} }
Message::IpChanged(ip) => { Message::IpChanged(ip) => {
self.mysql_config.ip = ip; self.mysql_config.ip = ip;
Task::none()
} }
Message::PortChanged(port) => { Message::PortChanged(port) => {
self.mysql_config.port = port; self.mysql_config.port = port;
Task::none()
} }
Message::UsernameChanged(username)=>{ Message::UsernameChanged(username)=>{
self.mysql_config.username = username self.mysql_config.username = username;
Task::none()
} }
Message::PasswordChanged(password)=>{ Message::PasswordChanged(password)=>{
self.mysql_config.password = password self.mysql_config.password = password;
Task::none()
} }
Message::DatabaseChanged(database)=>{ Message::DatabaseChanged(database)=>{
self.mysql_config.database = database self.mysql_config.database = database;
Task::none()
} }
Message::WorkOrderChanged(work_order)=>{ Message::WorkOrderChanged(work_order)=>{
self.mysql_config.work_order = work_order self.mysql_config.work_order = work_order;
Task::none()
} }
Message::TypeSelected(download_type)=>{ Message::TypeSelected(download_type)=>{
self.selection = Some(download_type) self.selection = Some(download_type);
Task::none()
} }
Message::LabelChanged(label)=>{ Message::LabelChanged(label)=>{
self.label = label self.label = label;
Task::none()
} }
Message::OnlineChecked(is_online)=>{ Message::OnlineChecked(is_online)=>{
self.is_online = is_online self.is_online = is_online;
Task::none()
} }
Message::AddLog(log)=>{ Message::AddLog(log)=>{
for c in log.chars(){ for c in log.chars(){
@ -102,14 +130,24 @@ impl MainWindow{
self.log_content.perform(text_editor::Action::Edit(Edit::Enter)); self.log_content.perform(text_editor::Action::Edit(Edit::Enter));
let line_size = self.log_content.lines().count(); let line_size = self.log_content.lines().count();
self.log_content.perform(Scroll{lines: line_size as i32 }); self.log_content.perform(Scroll{lines: line_size as i32 });
Task::none()
} }
Message::LogContentChanged(action)=>{ Message::LogContentChanged(action)=>{
if action.is_edit(){ 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() container.into()
} }
fn subscription(&self) -> Subscription<Message> {
event::listen().map(Message::WindowEvent)
}
} }