From 5982153a2ce0ac5386d712a8911a6a9dfc47de9b Mon Sep 17 00:00:00 2001 From: Timo Sulg Date: Sat, 18 Jan 2020 17:13:21 +0100 Subject: [PATCH 1/2] set up GUI boilerplate; --- rust/Cargo.toml | 2 +- rust/chapter2/Cargo.toml | 10 ++++ rust/chapter2/src/main.rs | 116 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 rust/chapter2/Cargo.toml create mode 100644 rust/chapter2/src/main.rs diff --git a/rust/Cargo.toml b/rust/Cargo.toml index f5ef4cb..188887a 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -1,3 +1,3 @@ [workspace] -members = ["ast_utils"] +members = ["ast_utils", "chapter2"] diff --git a/rust/chapter2/Cargo.toml b/rust/chapter2/Cargo.toml new file mode 100644 index 0000000..1687b2d --- /dev/null +++ b/rust/chapter2/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "chapter2" +version = "0.1.0" +authors = ["Timo Sulg "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +orbtk = "0.3.1-alpha1" diff --git a/rust/chapter2/src/main.rs b/rust/chapter2/src/main.rs new file mode 100644 index 0000000..30ede7c --- /dev/null +++ b/rust/chapter2/src/main.rs @@ -0,0 +1,116 @@ +extern crate orbtk; + +use orbtk::prelude::*; +use std::cell::Cell; + +#[derive(Debug, Copy, Clone)] +enum Action { + ClearText, + EntryActivated(Entity), + EntryChanged(Entity), + ValueChanged(Entity), +} + +#[derive(Default)] +pub struct MainViewState { + action: Cell>, +} + +impl MainViewState { + fn action(&self, action: Action) { + self.action.set(Some(action)); + } +} + +impl State for MainViewState { + fn update(&self, ctx: &mut Context<'_>) { + if let Some(action) = self.action.get() { + match action { + Action::ClearText => { + ctx.widget().set("text_input", String16::from("")); + ctx.widget().set("text_result", String16::from("")); + } + Action::EntryChanged(entity) => { + let widget = ctx.get_widget(entity); + let text = widget.get::("text"); + println!("entry changed: {}", text); + } + _ => { + println!("unsupported action"); + } + } + + self.action.set(None); + } + } +} + +fn create_header(ctx: &mut BuildContext, text: &str) -> Entity { + TextBlock::create() + .text(text) + .selector(Selector::new().with("text-block").class("h1")) + .build(ctx) +} + +widget!( + MainView { + text_input: String16, + text_result: String16 + } +); + +impl Template for MainView { + fn template(self, id: Entity, ctx: &mut BuildContext) -> Self { + let state = self.clone_state(); + + self.name("Chapter.2 - Unit conversion") + .text_input("0.0") + .text_result("Result: 0") + .child( + Grid::create() + .margin(10.0) + .columns(Columns::create().column(150.0).column(150.0).build()) + .child( + Stack::create() + .attach(Grid::column(0)) + .child(create_header(ctx, "From unit")) + .child( + TextBox::create() + .water_mark("From value") + .text(("text_input", id)) + .build(ctx), + ) + .child( + Button::create() + .text("Clear") + .on_click(move |_| { + state.action(Action::ClearText); + true + }) + .build(ctx), + ) + .build(ctx), + ) + .child( + Stack::create() + .attach(Grid::column(1)) + .child(create_header(ctx, "To unit")) + .build(ctx), + ) + .build(ctx), + ) + } +} + +fn main() { + Application::new() + .window(|ctx| { + Window::create() + .title("Chapter.2 - unit conversion") + .position((100.0, 100.0)) + .size(450.0, 450.0) + .child(MainView::create().build(ctx)) + .build(ctx) + }) + .run() +} From 5d6b185306f38f787a8488385449b292c957e57b Mon Sep 17 00:00:00 2001 From: Timo Sulg Date: Sun, 19 Jan 2020 13:53:47 +0100 Subject: [PATCH 2/2] put on hold due the limitations of orbtk --- rust/chapter2/src/main.rs | 44 +++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/rust/chapter2/src/main.rs b/rust/chapter2/src/main.rs index 30ede7c..69a3ef9 100644 --- a/rust/chapter2/src/main.rs +++ b/rust/chapter2/src/main.rs @@ -6,9 +6,8 @@ use std::cell::Cell; #[derive(Debug, Copy, Clone)] enum Action { ClearText, - EntryActivated(Entity), - EntryChanged(Entity), - ValueChanged(Entity), + Convert, + InputChanged(entity), } #[derive(Default)] @@ -30,10 +29,15 @@ impl State for MainViewState { ctx.widget().set("text_input", String16::from("")); ctx.widget().set("text_result", String16::from("")); } - Action::EntryChanged(entity) => { - let widget = ctx.get_widget(entity); - let text = widget.get::("text"); - println!("entry changed: {}", text); + Action::Convert => { + let input = *ctx.widget().get::("input"); + let result_txt = format!("{:.9}", input); + ctx.widget().set("text_result", String16::from(result_txt)); + } + Action::InputChanged(entity) => { + let value = *ctx.get_widget(entity).get::("text"); + *ctx.widget().get_mut::("input") = value; + println!("entry changed: {}", value); } _ => { println!("unsupported action"); @@ -54,6 +58,7 @@ fn create_header(ctx: &mut BuildContext, text: &str) -> Entity { widget!( MainView { + input: f64, text_input: String16, text_result: String16 } @@ -61,9 +66,12 @@ widget!( impl Template for MainView { fn template(self, id: Entity, ctx: &mut BuildContext) -> Self { - let state = self.clone_state(); + let clear_state = self.clone_state(); + let convert_state = self.clone_state(); + let change_state = self.clone_state(); self.name("Chapter.2 - Unit conversion") + .input(0.0) .text_input("0.0") .text_result("Result: 0") .child( @@ -78,13 +86,16 @@ impl Template for MainView { TextBox::create() .water_mark("From value") .text(("text_input", id)) + .on_changed(move |_, entity| { + change_state.action(Action::InputChanged(entity)); + }) .build(ctx), ) .child( Button::create() .text("Clear") .on_click(move |_| { - state.action(Action::ClearText); + clear_state.action(Action::ClearText); true }) .build(ctx), @@ -95,6 +106,21 @@ impl Template for MainView { Stack::create() .attach(Grid::column(1)) .child(create_header(ctx, "To unit")) + .child( + TextBox::create() + .water_mark("Result") + .text(("text_result", id)) + .build(ctx), + ) + .child( + Button::create() + .text("Convert") + .on_click(move |_| { + convert_state.action(Action::Convert); + true + }) + .build(ctx), + ) .build(ctx), ) .build(ctx),