Skip to content

Instantly share code, notes, and snippets.

@Autofire
Created November 26, 2020 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Autofire/895c471cef9b9c886b9d6d9f6bcb80bc to your computer and use it in GitHub Desktop.
Save Autofire/895c471cef9b9c886b9d6d9f6bcb80bc to your computer and use it in GitHub Desktop.
Playing with messages in Rust and OrbTk
use orbtk::prelude::*;
enum Message {
Increment,
}
#[derive(Default, AsAny)]
struct MainState {
count: i32,
}
impl State for MainState {
fn messages(
&mut self,
mut messages: MessageReader,
_registry: &mut Registry,
ctx: &mut Context,
) {
for message in messages.read::<Message>() {
match message {
Message::Increment => {
self.count += 1;
MainView::my_text_set(&mut ctx.widget(), self.count.to_string());
}
}
}
}
}
widget!(MainView<MainState> { my_text: String });
impl Template for MainView {
fn template(self, id: Entity, ctx: &mut BuildContext) -> Self {
self.my_text("0").child(
Stack::new()
.margin(8)
.spacing(4)
.child(
TextBlock::new()
.text("Message counter example")
.style("header")
.build(ctx),
)
.child(
TextBlock::new()
.style("body")
.margin((0, 8, 0, 0))
.text(("my_text", id))
.build(ctx),
)
.child(
Button::new()
.text("Hello")
.on_click(move |states, _| {
states.send_message(Message::Increment, id);
true
})
.build(ctx),
)
.build(ctx),
)
}
}
fn main() {
// use this only if you want to run it as web application.
orbtk::initialize();
Application::new()
.window(|ctx| {
Window::new()
.title("OrbTk - message example")
.position((100.0, 100.0))
.size(420.0, 730.0)
.child(MainView::new().build(ctx))
.build(ctx)
})
.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment