Skip to content

Commit

Permalink
Merge pull request #53 from londospark/buttons
Browse files Browse the repository at this point in the history
Buttons Example
  • Loading branch information
togglebyte authored Sep 8, 2024
2 parents d30da1f + 3fadc4c commit f6c9d2e
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target/
Cargo.lock
.gdb_history
.idea
104 changes: 104 additions & 0 deletions examples/buttons.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use anathema_backend::tui::TuiBackend;
use anathema_runtime::Runtime;
use anathema_state::{CommonVal, State, Value};
use anathema_templates::Document;
use anathema_widgets::components::events::{KeyCode, KeyEvent, KeyState};
use anathema_widgets::components::{Component, Context};
use anathema_widgets::Elements;

struct App;

#[derive(State)]
struct AppState {
number: Value<i32>,
}

impl Component for App {
type Message = ();
type State = AppState;

fn receive(
&mut self,
ident: &str,
_value: CommonVal<'_>,
state: &mut Self::State,
_elements: Elements<'_, '_>,
_context: Context<'_, Self::State>,
) {
if ident == "increment" {
*state.number.to_mut() += 1;
} else if ident == "decrement" {
*state.number.to_mut() -= 1;
}
}
}

struct Button;

#[derive(State)]
struct ButtonState {
caption: Value<String>,
in_focus: Value<bool>,
}

impl Component for Button {
type Message = ();
type State = ButtonState;

fn on_blur(&mut self, state: &mut Self::State, _elements: Elements<'_, '_>, _context: Context<'_, Self::State>) {
state.in_focus.set(false);
}

fn on_focus(&mut self, state: &mut Self::State, _elements: Elements<'_, '_>, _context: Context<'_, Self::State>) {
state.in_focus.set(true);
}

fn on_key(
&mut self,
key: KeyEvent,
_state: &mut Self::State,
_elements: Elements<'_, '_>,
mut context: Context<'_, Self::State>,
) {
if matches!(key.state, KeyState::Press) {
if let KeyCode::Enter = key.code {
context.publish("click", |state| &state.caption)
}
}
}
}

fn main() {
let doc = Document::new("@main");

let backend = TuiBackend::builder()
.enable_alt_screen()
.enable_raw_mode()
.hide_cursor()
.finish()
.unwrap();

let mut runtime = Runtime::builder(doc, backend);
runtime
.register_component(
"main",
"examples/templates/buttons/buttons.aml",
App,
AppState { number: 0.into() },
)
.unwrap();
runtime
.register_prototype(
"button",
"examples/templates/buttons/button.aml",
move || Button,
|| ButtonState {
caption: String::from("lark").into(),
in_focus: false.into(),
},
)
.unwrap();

let mut runtime = runtime.finish().unwrap();
runtime.run();
}
6 changes: 6 additions & 0 deletions examples/templates/buttons/button.aml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
border
row
if in_focus
text [background: "grey", foreground: "black"] caption
else
text [background: "black"] caption
7 changes: 7 additions & 0 deletions examples/templates/buttons/buttons.aml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
align [alignment: "centre"]
border
column
text "Simple counter app"
@button (click->increment) {"caption": "Increment"}
text number
@button (click->decrement) {"caption": "Decrement"}

0 comments on commit f6c9d2e

Please sign in to comment.