design
This commit is contained in:
parent
d321be19ad
commit
8d05978815
71
src/main.rs
71
src/main.rs
@ -1,6 +1,12 @@
|
||||
use iced::widget::{button, center, column, row, text_input, Space, Text};
|
||||
use iced::widget::{button, center, checkbox, column, row, scrollable, text_input, Space, Text};
|
||||
use iced::window::Settings;
|
||||
use iced::{Center, Element, Length, Size, Theme};
|
||||
use iced::{Element, Length, Size, Theme};
|
||||
use std::collections::HashMap;
|
||||
|
||||
struct TaskData {
|
||||
checked: bool,
|
||||
value: String,
|
||||
}
|
||||
|
||||
fn main() -> iced::Result {
|
||||
let settings = Settings {
|
||||
@ -9,7 +15,7 @@ fn main() -> iced::Result {
|
||||
..Settings::default()
|
||||
};
|
||||
|
||||
iced::application("ToDo", Todo::update, Todo::view)
|
||||
iced::application("To Do App", Todo::update, Todo::view)
|
||||
.theme(Todo::theme)
|
||||
.window(settings)
|
||||
.run()
|
||||
@ -18,45 +24,72 @@ fn main() -> iced::Result {
|
||||
#[derive(Default)]
|
||||
struct Todo {
|
||||
new_task: String,
|
||||
tasks: Vec<String>,
|
||||
tasks: HashMap<usize, TaskData>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum Message {
|
||||
AddTask,
|
||||
ContentUpdated,
|
||||
DeleteTask(usize),
|
||||
ActivityTask,
|
||||
ContentUpdated(String),
|
||||
}
|
||||
|
||||
impl Todo {
|
||||
fn update(&mut self, message: Message) {
|
||||
match message {
|
||||
Message::ContentUpdated(new_value) => self.new_task = new_value,
|
||||
Message::AddTask => {
|
||||
if self.new_task.is_empty() { return; }
|
||||
|
||||
let data = TaskData {
|
||||
checked: false,
|
||||
value: self.new_task.to_string(),
|
||||
};
|
||||
|
||||
self.tasks.insert(self.tasks.len(), data);
|
||||
|
||||
|
||||
// temp disable for testing fixme
|
||||
//self.new_task = String::from("");
|
||||
},
|
||||
Message::DeleteTask(id) => todo!(),
|
||||
Message::ActivityTask => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn view(&self) -> Element<Message> {
|
||||
|
||||
let input = text_input("Enter new task", &self.new_task)
|
||||
.width(300)
|
||||
.size(25);
|
||||
//.read_line(self.new_task)
|
||||
.size(25)
|
||||
.on_input(Message::ContentUpdated);
|
||||
|
||||
|
||||
let add_btn = button(
|
||||
Text::new("Add task")
|
||||
.size(19)
|
||||
.center()
|
||||
)
|
||||
)
|
||||
.width(Length::Shrink)
|
||||
.height(40)
|
||||
.padding(10);
|
||||
.padding(10)
|
||||
.on_press(Message::AddTask);
|
||||
|
||||
let skeleton = row![input, add_btn].spacing(10);
|
||||
let r = column![
|
||||
Space::with_height(Length::Fill),
|
||||
skeleton
|
||||
]
|
||||
.spacing(20)
|
||||
.align_x(Center);
|
||||
let new_task = row![input, add_btn].spacing(10);
|
||||
let mut saved_tasks = column![];
|
||||
|
||||
let container = center(r);
|
||||
container.into()
|
||||
for task in &self.tasks {
|
||||
let chk = checkbox("", task.1.checked).size(25);
|
||||
let label = Text::new(&task.1.value).width(200);
|
||||
let edit = button("edit").width(Length::Shrink);
|
||||
let delete = button("delete").width(Length::Shrink);
|
||||
let task_line = row![chk, label, edit, delete].spacing(10);
|
||||
|
||||
saved_tasks = saved_tasks.push(task_line.padding(10));
|
||||
}
|
||||
|
||||
let r = center(column![new_task, scrollable(saved_tasks).spacing(10), Space::with_height(Length::Fill)].spacing(20));
|
||||
r.into()
|
||||
}
|
||||
|
||||
fn theme(&self) -> Theme {
|
||||
|
Reference in New Issue
Block a user