Files
k-os/crates/apps/tally/src/main.rs

62 lines
1.6 KiB
Rust

//! A count, and buttons to change it: the first K-UI app. Tab and the
//! arrows move between the buttons, Enter presses, Escape leaves.
#![no_std]
#![forbid(unsafe_code)]
#![no_main]
extern crate alloc;
extern crate libkos;
use alloc::format;
use k_ui::{App, ButtonVariant, Element, Spacing, TextColour, TextStyle, button, column, row, text};
#[derive(Clone, Copy)]
enum Event {
Add,
Subtract,
Reset,
}
struct Tally(i64);
impl App for Tally {
type Event = Event;
fn init() -> Self {
Tally(0)
}
fn update(&mut self, event: Event) {
match event {
Event::Add => self.0 += 1,
Event::Subtract => self.0 -= 1,
Event::Reset => self.0 = 0,
}
}
fn view(&self) -> Element<Event> {
let mut buttons = row()
.gap(Spacing::Md)
.child(button("Subtract", Event::Subtract))
.child(button("Add", Event::Add).variant(ButtonVariant::Primary));
if self.0 != 0 {
buttons = buttons.child(button("Reset", Event::Reset).variant(ButtonVariant::Destructive));
}
column()
.padding(Spacing::Xl)
.gap(Spacing::Lg)
.child(text("Tally").style(TextStyle::Heading))
.child(text(format!("{}", self.0)).style(TextStyle::Subheading).colour(TextColour::Accent))
.child(buttons)
.child(text("Tab moves, Enter presses, Esc leaves").style(TextStyle::Caption).colour(TextColour::Secondary))
.into()
}
}
libkos::entry!(main);
fn main() {
k_ui::run::<Tally>();
}