Created
November 26, 2020 22:27
-
-
Save Autofire/895c471cef9b9c886b9d6d9f6bcb80bc to your computer and use it in GitHub Desktop.
Playing with messages in Rust and OrbTk
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
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