处理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};
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<Message>{
self.update(Message::AddLog(msg))
}
fn update(&mut self, message: Message){
fn update(&mut self, message: Message)-> Task<Message>{
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<Message> {
event::listen().map(Message::WindowEvent)
}
}