2024-12-09 10:33:01 +08:00
|
|
|
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};
|
2024-12-06 17:57:22 +08:00
|
|
|
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)
|
2024-12-09 10:33:01 +08:00
|
|
|
.subscription(MainWindow::subscription)
|
|
|
|
|
.exit_on_close_request(false)
|
2024-12-06 17:57:22 +08:00
|
|
|
.window_size((830.0, 600.0))
|
|
|
|
|
.run()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct MainWindow {
|
|
|
|
|
is_online: bool,
|
|
|
|
|
mysql_config: MysqlConfig,
|
|
|
|
|
selection: Option<DownloadType>,
|
|
|
|
|
label: String,
|
|
|
|
|
log_content: text_editor::Content,
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-09 10:33:01 +08:00
|
|
|
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(),
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-06 17:57:22 +08:00
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
|
struct MysqlConfig{
|
|
|
|
|
ip: String,
|
|
|
|
|
port: String,
|
|
|
|
|
username: String,
|
|
|
|
|
password: String,
|
|
|
|
|
database: String,
|
|
|
|
|
work_order: String,
|
|
|
|
|
}
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
enum Message{
|
|
|
|
|
Start(),
|
|
|
|
|
IpChanged(String),
|
|
|
|
|
PortChanged(String),
|
|
|
|
|
UsernameChanged(String),
|
|
|
|
|
PasswordChanged(String),
|
|
|
|
|
DatabaseChanged(String),
|
|
|
|
|
WorkOrderChanged(String),
|
|
|
|
|
LabelChanged(String),
|
|
|
|
|
TypeSelected(DownloadType),
|
|
|
|
|
OnlineChecked(bool),
|
|
|
|
|
AddLog(String),
|
2024-12-09 10:33:01 +08:00
|
|
|
LogContentChanged(text_editor::Action),
|
|
|
|
|
SaveConfig,
|
|
|
|
|
WindowEvent(Event)
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
|
enum DownloadType{
|
|
|
|
|
BootLoader,
|
|
|
|
|
App,
|
|
|
|
|
Cal
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MainWindow{
|
2024-12-09 10:33:01 +08:00
|
|
|
pub fn add_log(&mut self, msg: String) -> Task<Message>{
|
2024-12-06 17:57:22 +08:00
|
|
|
self.update(Message::AddLog(msg))
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-09 10:33:01 +08:00
|
|
|
fn update(&mut self, message: Message)-> Task<Message>{
|
2024-12-06 17:57:22 +08:00
|
|
|
match message{
|
|
|
|
|
Message::Start() => {
|
|
|
|
|
println!("Start: {:?}", self.mysql_config);
|
|
|
|
|
println!("Start: {:?}", self.is_online);
|
|
|
|
|
println!("Start: {:?}", self.selection);
|
|
|
|
|
self.add_log(self.label.clone());
|
|
|
|
|
self.add_log("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string());
|
|
|
|
|
self.add_log("bbbbbbbbbbbbbbbb".to_string());
|
|
|
|
|
self.add_log("cccccccccccccccc".to_string());
|
|
|
|
|
println!("{:?}", self.log_content.text());
|
2024-12-09 10:33:01 +08:00
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
|
|
|
|
Message::IpChanged(ip) => {
|
|
|
|
|
self.mysql_config.ip = ip;
|
2024-12-09 10:33:01 +08:00
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
|
|
|
|
Message::PortChanged(port) => {
|
|
|
|
|
self.mysql_config.port = port;
|
2024-12-09 10:33:01 +08:00
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
|
|
|
|
Message::UsernameChanged(username)=>{
|
2024-12-09 10:33:01 +08:00
|
|
|
self.mysql_config.username = username;
|
|
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
|
|
|
|
Message::PasswordChanged(password)=>{
|
2024-12-09 10:33:01 +08:00
|
|
|
self.mysql_config.password = password;
|
|
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
|
|
|
|
Message::DatabaseChanged(database)=>{
|
2024-12-09 10:33:01 +08:00
|
|
|
self.mysql_config.database = database;
|
|
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
|
|
|
|
Message::WorkOrderChanged(work_order)=>{
|
2024-12-09 10:33:01 +08:00
|
|
|
self.mysql_config.work_order = work_order;
|
|
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
|
|
|
|
Message::TypeSelected(download_type)=>{
|
2024-12-09 10:33:01 +08:00
|
|
|
self.selection = Some(download_type);
|
|
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
|
|
|
|
Message::LabelChanged(label)=>{
|
2024-12-09 10:33:01 +08:00
|
|
|
self.label = label;
|
|
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
|
|
|
|
Message::OnlineChecked(is_online)=>{
|
2024-12-09 10:33:01 +08:00
|
|
|
self.is_online = is_online;
|
|
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
|
|
|
|
Message::AddLog(log)=>{
|
|
|
|
|
for c in log.chars(){
|
|
|
|
|
self.log_content.perform(text_editor::Action::Edit(Edit::Insert(c)))
|
|
|
|
|
}
|
|
|
|
|
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 });
|
2024-12-09 10:33:01 +08:00
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
|
|
|
|
Message::LogContentChanged(action)=>{
|
|
|
|
|
if action.is_edit(){
|
2024-12-09 10:33:01 +08:00
|
|
|
return Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
2024-12-09 10:33:01 +08:00
|
|
|
self.log_content.perform(action);
|
|
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
2024-12-09 10:33:01 +08:00
|
|
|
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()}
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn view(&self) -> Element<Message>{
|
|
|
|
|
let ip_input = text_input(
|
|
|
|
|
"IP Address",
|
|
|
|
|
&self.mysql_config.ip,
|
|
|
|
|
).on_input(Message::IpChanged);
|
|
|
|
|
let port_input = text_input(
|
|
|
|
|
"Port",
|
|
|
|
|
&self.mysql_config.port,
|
|
|
|
|
).on_input(Message::PortChanged);
|
|
|
|
|
let username_input = text_input(
|
|
|
|
|
"Username",
|
|
|
|
|
&self.mysql_config.username
|
|
|
|
|
).on_input(Message::UsernameChanged);
|
|
|
|
|
let password_input = text_input(
|
|
|
|
|
"Password",
|
|
|
|
|
&self.mysql_config.password
|
|
|
|
|
).on_input(Message::PasswordChanged);
|
|
|
|
|
let database_input = text_input(
|
|
|
|
|
"Database",
|
|
|
|
|
&self.mysql_config.database
|
|
|
|
|
).on_input(Message::DatabaseChanged);
|
|
|
|
|
let work_order_input = text_input(
|
|
|
|
|
"WorkOrder",
|
|
|
|
|
&self.mysql_config.work_order
|
|
|
|
|
).on_input(Message::WorkOrderChanged);
|
|
|
|
|
let label_input = text_input(
|
|
|
|
|
"label",
|
|
|
|
|
&self.label
|
|
|
|
|
).on_input(Message::LabelChanged).on_submit(Message::Start()).width(Length::Fill);
|
|
|
|
|
let content = column![
|
|
|
|
|
row![ip_input, port_input].spacing(5),
|
|
|
|
|
row![username_input, password_input].spacing(5),
|
|
|
|
|
row![database_input, work_order_input].spacing(5),
|
|
|
|
|
row![
|
|
|
|
|
radio("BootLoader", DownloadType::BootLoader,self.selection,Message::TypeSelected),
|
|
|
|
|
radio("App", DownloadType::App,self.selection,Message::TypeSelected),
|
|
|
|
|
radio("Cal", DownloadType::Cal,self.selection,Message::TypeSelected),
|
|
|
|
|
checkbox("online", self.is_online).on_toggle(Message::OnlineChecked)
|
|
|
|
|
].spacing(10),
|
|
|
|
|
row![label_input,button("Start").on_press(Message::Start())].spacing(10),
|
|
|
|
|
text_editor(&self.log_content).on_action(Message::LogContentChanged).height(Length::Fill)
|
|
|
|
|
].spacing(10).padding(5);
|
|
|
|
|
|
|
|
|
|
let container = container(content).padding(20);
|
|
|
|
|
container.into()
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-09 10:33:01 +08:00
|
|
|
fn subscription(&self) -> Subscription<Message> {
|
|
|
|
|
event::listen().map(Message::WindowEvent)
|
|
|
|
|
}
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|