-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from londospark/buttons
Buttons Example
- Loading branch information
Showing
4 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
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,3 +1,4 @@ | ||
target/ | ||
Cargo.lock | ||
.gdb_history | ||
.idea |
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 |
---|---|---|
@@ -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(); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
border | ||
row | ||
if in_focus | ||
text [background: "grey", foreground: "black"] caption | ||
else | ||
text [background: "black"] caption |
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 |
---|---|---|
@@ -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"} |