界面初步完成
This commit is contained in:
164
src/main.rs
Normal file
164
src/main.rs
Normal file
@@ -0,0 +1,164 @@
|
||||
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};
|
||||
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)
|
||||
.window_size((830.0, 600.0))
|
||||
.run()
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct MainWindow {
|
||||
is_online: bool,
|
||||
mysql_config: MysqlConfig,
|
||||
selection: Option<DownloadType>,
|
||||
label: String,
|
||||
log_content: text_editor::Content,
|
||||
}
|
||||
|
||||
#[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),
|
||||
LogContentChanged(text_editor::Action)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum DownloadType{
|
||||
BootLoader,
|
||||
App,
|
||||
Cal
|
||||
}
|
||||
|
||||
impl MainWindow{
|
||||
pub fn add_log(&mut self, msg: String){
|
||||
self.update(Message::AddLog(msg))
|
||||
}
|
||||
|
||||
fn update(&mut self, message: Message){
|
||||
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());
|
||||
}
|
||||
Message::IpChanged(ip) => {
|
||||
self.mysql_config.ip = ip;
|
||||
}
|
||||
Message::PortChanged(port) => {
|
||||
self.mysql_config.port = port;
|
||||
}
|
||||
Message::UsernameChanged(username)=>{
|
||||
self.mysql_config.username = username
|
||||
}
|
||||
Message::PasswordChanged(password)=>{
|
||||
self.mysql_config.password = password
|
||||
}
|
||||
Message::DatabaseChanged(database)=>{
|
||||
self.mysql_config.database = database
|
||||
}
|
||||
Message::WorkOrderChanged(work_order)=>{
|
||||
self.mysql_config.work_order = work_order
|
||||
}
|
||||
Message::TypeSelected(download_type)=>{
|
||||
self.selection = Some(download_type)
|
||||
}
|
||||
Message::LabelChanged(label)=>{
|
||||
self.label = label
|
||||
}
|
||||
Message::OnlineChecked(is_online)=>{
|
||||
self.is_online = is_online
|
||||
}
|
||||
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 });
|
||||
}
|
||||
Message::LogContentChanged(action)=>{
|
||||
if action.is_edit(){
|
||||
return
|
||||
}
|
||||
self.log_content.perform(action)
|
||||
}
|
||||
_=>{}
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user