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