use appy::{components::*, hooks::*, types::*, *}; use rand::{distributions::Alphanumeric, Rng}; use std::rc::Rc; #[derive_component(ComponentBuilder, Default, SnakeFactory)] pub struct FlowButton { text: String, on_click: Option>, } #[function_component] fn _flow_button(p: FlowButton) -> Elements { let hover_state = use_hover_state_ref(); let app_context = use_context::(); let w = app_context.default_font.get_str_width(&*p.text, 24.0); let c = match *hover_state { HoverState::Normal => 0x808080, HoverState::Active => 0x404040, HoverState::Hover => 0xa0a0a0, }; apx! { } } #[derive_component(ComponentBuilder, Default, SnakeFactory)] pub struct ListItem { text: String, } #[function_component] fn _list_item(p: ListItem) -> Elements { let selected = use_state(|| false); apx! { 0x0000ff, false=>0xffffff}/> 0xffffff, false=>0x000000}/> } } #[derive(Clone)] enum AppAction { AddStart, AddEnd, RemoveStart, RemoveEnd, } #[derive(Clone)] struct AppState { items: Vec, } fn random_string() -> String { let num = rand::thread_rng().gen_range(8..16); rand::thread_rng() .sample_iter(&Alphanumeric) .take(num) .map(char::from) .collect() } impl AppState { pub fn new() -> Self { Self { items: vec![] } } pub fn action(&self, action: AppAction) -> Self { let mut new_self = self.clone(); new_self.action_mut(action); new_self } pub fn action_mut(&mut self, action: AppAction) { match action { AppAction::AddStart => { self.items.insert(0, random_string()); } AppAction::RemoveStart => { if self.items.len() > 0 { self.items.remove(0); } } AppAction::AddEnd => { self.items.push(random_string()); } AppAction::RemoveEnd => { if self.items.len() > 0 { self.items.remove(self.items.len() - 1); } } } } } #[main_window] fn app() -> Elements { let app = use_reducer(AppState::action, AppState::new); apx! { {app.items.iter().flat_map(|item|{ apx!{ } }).collect()} } }