Last active
February 14, 2020 07:29
-
-
Save shadiakiki1986/bd5c705b627fb82f8aba89047b050330 to your computer and use it in GitHub Desktop.
orbtk example with trait
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
// Based on https://github.com/redox-os/orbtk/blob/a9653954fed7275328bc8e893c3465506154571e/examples/clear.rs | |
// Modified to add a trait member called "summary" which is either implemented as a Book or a NewsArticle | |
// The summarize button would call the trait function "summarize" (be it from Book or NewsArticle). | |
// I couldn't get this to work yet, with the troubling sections marked with FIXME | |
use orbtk::prelude::*; | |
//--------------------------------------- | |
// https://doc.rust-lang.org/1.30.0/book/second-edition/ch10-02-traits.html | |
pub trait Summary { | |
fn summarize(&self) -> String; | |
} | |
pub struct NewsArticle { | |
pub headline: String, | |
} | |
impl Summary for NewsArticle { | |
fn summarize(&self) -> String { | |
format!("news article: {}", self.headline) | |
} | |
} | |
pub struct Book { | |
pub title: String, | |
} | |
impl Summary for Book { | |
fn summarize(&self) -> String { | |
format!("book: {}", self.title) | |
} | |
} | |
//--------------------------------------- | |
#[derive(Default, AsAny)] | |
pub struct MainViewState { | |
summarize: bool, | |
} | |
impl MainViewState { | |
// Sets an action the state | |
fn summarize(&mut self) { | |
self.summarize = true; | |
} | |
} | |
impl State for MainViewState { | |
fn update(&mut self, _: &mut Registry, ctx: &mut Context<'_>) { | |
if self.summarize { | |
// Clears the text property of MainView and because | |
// of the sharing also the text of the TextBox. | |
let summary = *ctx.widget().get::<Summary>("summary"); // trait not allowed <<<<<<<<< FIXME | |
ctx.widget().set("text", String16::from(summary.summarize())); | |
self.summarize = false; | |
} | |
} | |
} | |
widget!(MainView<MainViewState> { | |
text: String16, | |
summary: Summary // doesn't have a size known at compile-time <<<<<<<<< FIXME | |
//summary: dyn Summary // no rules expected this token in macro call | |
//summary: &Summary, | |
//summary: PropertySource<Summary>, | |
}); | |
impl Template for MainView { | |
fn template(self, id: Entity, ctx: &mut BuildContext) -> Self { | |
self.name("MainView").child( | |
Stack::create() | |
.orientation("horizontal") | |
// By injecting the id of the parent the text property | |
// is shared between the MainView and the TextBox. This | |
// means both references the same String16 object. | |
.child(TextBox::create().height(32.0).text(id).build(ctx)) | |
.child( | |
Button::create() | |
.margin((8.0, 0.0, 0.0, 0.0)) | |
// mouse click event handler | |
.on_click(move |states, _| { | |
// Calls clear of the state of MainView | |
states.get_mut::<MainViewState>(id).summarize(); | |
true | |
}) | |
.text("Summarize") | |
.build(ctx), | |
) | |
.build(ctx), | |
) | |
} | |
} | |
fn main() { | |
Application::new() | |
.window(|ctx| { | |
let main_view = MainView::create(); | |
// Allow to either pass a Book or a NewsArticle | |
main_view.summary = Some(PropertySource::Value(Book { title: "The Jungle Book" })); | |
// main_view.summary = Some(PropertySource::Value(NewsArticle { headline: "Dumbo the great!" })); | |
Window::create() | |
.title("OrbTk - minimal example") | |
.position((100.0, 100.0)) | |
.size(420.0, 730.0) | |
.child(main_view.margin(4.0).build(ctx)) | |
.build(ctx) | |
}) | |
.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment