-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Iced app can display the todo items (no checkboxes)
- Loading branch information
Showing
6 changed files
with
130 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,120 @@ | ||
use iced::widget::{button, column, text, Column}; | ||
use iced::{ | ||
widget::{center, column, container, keyed_column, row, scrollable, text}, | ||
Alignment::Center, | ||
Element, | ||
Length::Fill, | ||
}; | ||
|
||
#[derive(Default)] | ||
pub struct App { | ||
value: i64, | ||
use sift_persist::MemoryStore; | ||
use sift_state::State; | ||
|
||
pub enum App { | ||
Loading, | ||
Loaded(State), | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
#[derive(Debug)] | ||
pub enum Message { | ||
Increment, | ||
Decrement, | ||
Loaded(anyhow::Result<MemoryStore>), | ||
} | ||
|
||
impl App { | ||
pub fn new() -> (Self, iced::Task<Message>) { | ||
( | ||
App::Loading, | ||
iced::Task::perform(App::load(), Message::Loaded), | ||
) | ||
} | ||
|
||
async fn load() -> anyhow::Result<MemoryStore> { | ||
let path = sift_core::save_name(); | ||
MemoryStore::load(&path) | ||
// unwrap(); | ||
// let state = State::new(store); | ||
|
||
// Ok(Self { | ||
// state: State::new(MemoryStore::load(&path)?), | ||
// }) | ||
} | ||
|
||
pub fn update(&mut self, message: Message) { | ||
match message { | ||
Message::Increment => { | ||
self.value += 1; | ||
} | ||
Message::Decrement => { | ||
self.value -= 1; | ||
match self { | ||
App::Loading => { | ||
self.update_loading(message); | ||
} | ||
App::Loaded(_) => self.update_loaded(), | ||
} | ||
} | ||
|
||
fn update_loading(&mut self, message: Message) { | ||
match message { | ||
Message::Loaded(result) => match result { | ||
Ok(store) => *self = App::Loaded(State::new(store)), | ||
Err(_) => unimplemented!("report error and/or implement default state"), | ||
}, | ||
} | ||
} | ||
|
||
pub fn view(&self) -> Column<Message> { | ||
column![ | ||
button("+").on_press(Message::Increment), | ||
text(self.value), | ||
button("-").on_press(Message::Decrement), | ||
] | ||
fn update_loaded(&mut self) { | ||
todo!() | ||
} | ||
|
||
pub fn view(&self) -> Element<Message> { | ||
match self { | ||
App::Loading => self.view_loading(), | ||
App::Loaded(state) => self.view_loaded(state), | ||
} | ||
} | ||
|
||
fn view_loading(&self) -> Element<Message> { | ||
center(text("Loading...").width(Fill).align_x(Center).size(50)).into() | ||
} | ||
|
||
fn view_loaded(&self, state: &State) -> Element<Message> { | ||
let title = text("todos") | ||
.width(Fill) | ||
.size(100) | ||
.color([0.5, 0.5, 0.5]) | ||
.align_x(Center); | ||
let tasks = state.list_tasks_for_display(); | ||
let tasks: Element<_> = if tasks.is_empty() { | ||
unimplemented!("implement display of zero tasks; see https://github.com/iced-rs/iced/blob/9b99b932bced46047ec2e18c2b6ec5a6c5b3636f/examples/todos/src/main.rs#L229"); | ||
} else { | ||
keyed_column(tasks.iter().map(|task| { | ||
let text = text(task.title().to_string()); | ||
let row = row![text]; | ||
|
||
(task.id(), row.into()) | ||
})) | ||
.into() | ||
}; | ||
|
||
let content = column![title, tasks]; | ||
scrollable(container(content).center_x(Fill)).into() | ||
} | ||
} | ||
|
||
// impl eframe::App for App { | ||
// fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { | ||
// egui::CentralPanel::default().show(ctx, |ui| { | ||
// ui.heading("Todos"); | ||
|
||
// let tasks = self.state.list_tasks_for_display(); | ||
// let height = TextStyle::Body.resolve(ui.style()).size; | ||
// ScrollArea::vertical().show_rows(ui, height, tasks.len(), |ui, row_range| { | ||
// ui.allocate_space(vec2(ui.available_width(), 0.0)); | ||
// for (index, task) in tasks.iter().enumerate() { | ||
// if !row_range.contains(&index) { | ||
// continue; | ||
// } | ||
// let checked = task.completed().is_some(); | ||
// let mut checkbox_checked = checked; | ||
// ui.checkbox(&mut checkbox_checked, task.title()); | ||
// if checkbox_checked != checked { | ||
// self.state.toggle_id(task.id()); | ||
// } | ||
// } | ||
// }); | ||
// }); | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
use sift_iced::App; | ||
|
||
pub fn main() -> iced::Result { | ||
iced::run("A cool counter", App::update, App::view) | ||
iced::application("Sift", App::update, App::view).run_with(App::new) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters