Skip to content

GUI app for chapter2; #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]

members = ["ast_utils"]
members = ["ast_utils", "chapter2"]
10 changes: 10 additions & 0 deletions rust/chapter2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "chapter2"
version = "0.1.0"
authors = ["Timo Sulg <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
orbtk = "0.3.1-alpha1"
142 changes: 142 additions & 0 deletions rust/chapter2/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
extern crate orbtk;

use orbtk::prelude::*;
use std::cell::Cell;

#[derive(Debug, Copy, Clone)]
enum Action {
ClearText,
Convert,
InputChanged(entity),
}

#[derive(Default)]
pub struct MainViewState {
action: Cell<Option<Action>>,
}

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::Convert => {
let input = *ctx.widget().get::<f64>("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::<f64>("text");
*ctx.widget().get_mut::<f64>("input") = value;
println!("entry changed: {}", value);
}
_ => {
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<MainViewState> {
input: f64,
text_input: String16,
text_result: String16
}
);

impl Template for MainView {
fn template(self, id: Entity, ctx: &mut BuildContext) -> Self {
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(
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))
.on_changed(move |_, entity| {
change_state.action(Action::InputChanged(entity));
})
.build(ctx),
)
.child(
Button::create()
.text("Clear")
.on_click(move |_| {
clear_state.action(Action::ClearText);
true
})
.build(ctx),
)
.build(ctx),
)
.child(
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),
)
}
}

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()
}