kkx/kkx/src/main.rs

86 lines
2.2 KiB
Rust

use kkdisp::{
component::{Plan, Widget},
theme::Color,
token::Token,
view::Event,
View,
};
#[tokio::main]
async fn main() {
kkdisp::run(App::default()).await.unwrap()
}
pub struct App {
states: Vec<Plan>,
index: usize,
}
impl Default for App {
fn default() -> Self {
Self {
index: 0,
states: vec![
Plan::start().fill(vec![]).fixed(
4,
vec![Widget::new(
100,
vec![
Token::End,
Token::text("click me")
.bg(Color::BLUE)
.link("click"),
],
)],
),
Plan::start()
.fixed(4, vec![])
.fill(vec![Widget::scrolling(
100,
(1..=100)
.into_iter()
.map(|num| {
Token::text(format!(
"this is {}",
num
))
.padded(30)
.bg("#330033".try_into().unwrap())
.link(format!("num{}", num))
.centered()
})
.collect(),
)])
.fixed(4, vec![]),
],
}
}
}
impl View for App {
type Message = ();
fn init(
&mut self,
) -> std::result::Result<kkdisp::PlanLayers, anyhow::Error> {
Ok(vec![self.states[self.index].clone()])
}
fn update(
&mut self,
event: Event,
) -> std::result::Result<
(Self::Message, Option<kkdisp::PlanLayers>),
anyhow::Error,
> {
match event {
Event::Link(lnk) => {
eprintln!("recieved link: {}", lnk);
self.index ^= 1;
Ok(((), Some(vec![self.states[self.index].clone()])))
}
_ => Ok(((), None)),
}
}
}