From 8d7d5176d90a4a5cb3afb30a9accb16bc97bf738 Mon Sep 17 00:00:00 2001 From: Ralf Zerres Date: Thu, 2 Jul 2020 07:23:23 +0200 Subject: [PATCH] examples: advowidgets: update to new api syntax * when addressing widgets properties the new api syntax seems more natural. It accesses the properties via a function oriented notation: - view_name(_mut() - ctx.child("").set::("", ) - (ctx.child(() Signed-off-by: Ralf Zerres --- advotracker/examples/advowidgets.rs | 138 ++++++++++++++-------------- 1 file changed, 69 insertions(+), 69 deletions(-) diff --git a/advotracker/examples/advowidgets.rs b/advotracker/examples/advowidgets.rs index 5c4e80f..350184e 100644 --- a/advotracker/examples/advowidgets.rs +++ b/advotracker/examples/advowidgets.rs @@ -35,60 +35,58 @@ impl State for MainViewState { if let Some(action) = self.action { match action { Action::AddItem => { - let len = ctx.widget().get::("list").len(); + let len = main_view(ctx.widget()).list().len(); + if len < 5 { - ctx.widget() - .get_mut::("list") + main_view(ctx.widget()) + .list_mut() .push(format!("Item {}", len + 1)); - ctx.child("items").set("count", len + 1); - ctx.child("remove-item-button").set("enabled", true); + ctx.child("items").set::("blub", len + 1); + items_widget(ctx.child("items")).set_count(len + 1); + button(ctx.child("remove-item-button")).set_enabled(true); if len == 4 { - ctx.child("add-item-button").set("enabled", false); + button(ctx.child("add-item-button")).set_enabled(false); } } } Action::RemoveItem => { - let len = ctx.widget().get::("list").len(); + let len = main_view(ctx.widget()).list().len(); if len > 0 { - ctx.widget().get_mut::("list").remove(len - 1); - ctx.child("items").set("count", len - 1); - ctx.child("add-item-button").set("enabled", true); + main_view(ctx.widget()).list_mut().remove(len - 1); + items_widget(ctx.child("items")).set_count(len - 1); + button(ctx.child("add-item-button")).set_enabled(true); if len == 1 { - ctx.child("remove-item-button").set("enabled", false); + button(ctx.child("remove-item-button")).set_enabled(false); } } } Action::IncrementCounter => { - *ctx.widget().get_mut::("counter") += 1; + *main_view(ctx.widget()).counter_mut() += 1; - let counter = *ctx.widget().get::("counter"); + let counter = *main_view(ctx.widget()).counter(); - ctx.widget().set( - "result", - String16::from(format!("Button count: {}", counter)), - ); + main_view(ctx.widget()) + .set_result(String16::from(format!("Button count: {}", counter))); } Action::ClearText => { - ctx.widget().set("text_one", String16::from("")); - ctx.widget().set("text_two", String16::from("")); + main_view(ctx.widget()).set_text_one(String16::default()); + main_view(ctx.widget()).set_text_two(String16::default()); } Action::EntryActivated(entity) => { - let mut widget = ctx.get_widget(entity); - let text = widget.get_mut::("text"); + let mut text_box = text_box(ctx.get_widget(entity)); + let text = text_box.text_mut(); println!("submitting {}", text); text.clear(); } Action::EntryChanged(entity) => { - let widget = ctx.get_widget(entity); - let text = widget.get::("text"); - println!("entry changed: {}", text); + println!("entry changed: {}", text_box(ctx.get_widget(entity)).text()); } Action::ValueChanged(entity) => { - let val = - ((*ctx.get_widget(entity).get::("val")).floor() as i32).to_string(); - ctx.child("value_text").set("text", String16::from(val)); + let val = ((slider(ctx.get_widget(entity)).val()).floor() as i32).to_string(); + + text_block(ctx.child("value_text")).set_text(String16::from(val)); } } @@ -99,12 +97,11 @@ impl State for MainViewState { fn update_post_layout(&mut self, _: &mut Registry, ctx: &mut Context<'_>) { let mut selection_string = "Selected:".to_string(); - for index in &ctx.widget().get::("selected_indices").0 { + for index in &main_view(ctx.widget()).selected_indices().0 { selection_string = format!("{} {}", selection_string, index); } - ctx.child("selection") - .set("text", String16::from(selection_string)); + text_block(ctx.child("selection")).set_text(selection_string); } } @@ -174,14 +171,14 @@ impl Template for MainView { .combo_box_list_count(10) .child( Grid::new() - .margin(8.0) + .margin(8.) .columns( Columns::new() - .add(132.0) - .add(16.0) - .add(132.0) - .add(16.0) - .add(132.0), + .add(132.) + .add(16.) + .add(132.) + .add(16.) + .add(132.), ) .child( Stack::new() @@ -191,8 +188,8 @@ impl Template for MainView { .child( Button::new() .text("Button") - .margin((0.0, 8.0, 0.0, 0.0)) - .icon(material_font_icons::CHECK_FONT_ICON) + .margin((0., 8., 0., 0.)) + .icon(material_icons_font_ttf::MD_CHECK) .attach(Grid::column(0)) .attach(Grid::row(1)) .on_click(move |states, _| { @@ -206,8 +203,8 @@ impl Template for MainView { .text("Primary") .element("button") .class("primary") - .margin((0.0, 8.0, 0.0, 0.0)) - .icon(material_font_icons::CHECK_FONT_ICON) + .margin((0., 8., 0., 0.)) + .icon(material_icons_font_ttf::MD_360) .attach(Grid::column(0)) .attach(Grid::row(2)) .build(ctx), @@ -216,7 +213,8 @@ impl Template for MainView { ToggleButton::new() .class("single_content") .text("ToggleButton") - .margin((0.0, 8.0, 0.0, 0.0)) + .margin((0., 8., 2., 0.)) + .icon(material_icons_font_ttf::MD_ALARM_ON) .attach(Grid::column(0)) .attach(Grid::row(3)) .build(ctx), @@ -224,21 +222,21 @@ impl Template for MainView { .child( CheckBox::new() .text("CheckBox") - .margin((0.0, 8.0, 0.0, 0.0)) + .margin((0., 8., 0., 0.)) .attach(Grid::column(0)) .attach(Grid::row(4)) .build(ctx), ) .child( Switch::new() - .margin((0.0, 8.0, 0.0, 0.0)) + .margin((0., 8., 0., 0.)) .attach(Grid::column(0)) .attach(Grid::row(5)) .build(ctx), ) .child( TextBlock::new() - .margin((0.0, 8.0, 0.0, 0.0)) + .margin((0., 8., 0., 0.)) .element("h1") .id("value_text") .text("0") @@ -262,7 +260,7 @@ impl Template for MainView { TextBlock::new() .class("body") .text(("result", id)) - .margin((0.0, 8.0, 0.0, 0.0)) + .margin((0., 8., 0., 0.)) .attach(Grid::column(2)) .attach(Grid::row(1)) .build(ctx), @@ -271,7 +269,7 @@ impl Template for MainView { TextBox::new() .water_mark("TextBox...") .text(("text_one", id)) - .margin((0.0, 8.0, 0.0, 0.0)) + .margin((0., 8., 0., 0.)) .attach(Grid::column(2)) .attach(Grid::row(2)) .on_activate(move |states, entity| { @@ -286,7 +284,7 @@ impl Template for MainView { TextBox::new() .water_mark("TextBox...") .text(("text_two", id)) - .margin((0.0, 8.0, 0.0, 0.0)) + .margin((0., 8., 0., 0.)) .attach(Grid::column(2)) .attach(Grid::row(2)) .on_activate(move |states, entity| { @@ -299,8 +297,10 @@ impl Template for MainView { ) .child( Button::new() - .margin((0.0, 8.0, 0.0, 0.0)) + .margin((0., 8., 0., 0.)) .class("single_content") + .margin((0., 8., 8., 0.)) + .icon(material_icons_font_ttf::MD_CLEAR) .text("clear text") .on_click(move |states, _| { state(id, states).action(Action::ClearText); @@ -310,8 +310,8 @@ impl Template for MainView { ) .child( NumericBox::new() - .margin((0.0, 8.0, 0.0, 0.0)) - .max(123.0) + .margin((-0., 8., 0., 0.)) + .max(123.) .step(0.123) .val(0.123) .build(ctx), @@ -323,14 +323,14 @@ impl Template for MainView { .rows( Rows::new() .add("auto") - .add(32.0) - .add(16.0) - .add(204.0) + .add(32.) + .add(16.) + .add(204.) .add("auto") - .add(192.0) + .add(192.) .add("auto"), ) - .columns(Columns::new().add("*").add(4.0).add("*")) + .columns(Columns::new().add("*").add(4.).add("*")) .attach(Grid::column(4)) .child( TextBlock::new() @@ -350,7 +350,7 @@ impl Template for MainView { .get::>("combo_box_list")[index] .clone(); TextBlock::new() - .margin((0.0, 0.0, 0.0, 2.0)) + .margin((0., 0., 0., 2.)) .v_align("center") .text(text) .build(bc) @@ -359,7 +359,7 @@ impl Template for MainView { .attach(Grid::column(0)) .attach(Grid::column_span(3)) .attach(Grid::row(1)) - .margin((0.0, 8.0, 0.0, 0.0)) + .margin((0., 8., 0., 0.)) .count(("combo_box_list_count", id)) .build(ctx), ) @@ -367,18 +367,18 @@ impl Template for MainView { ItemsWidget::new() .element("items-widget") .id("items") - .padding((4.0, 4.0, 4.0, 2.0)) + .padding((4., 4., 4., 2.)) .attach(Grid::column(0)) .attach(Grid::column_span(3)) .attach(Grid::row(3)) - .margin((0.0, 0.0, 0.0, 8.0)) + .margin((0., 0., 0., 8.)) .items_builder(move |bc, index| { let text = bc.get_widget(id).get::>("list") [index] .clone(); Button::new() - .margin((0.0, 0.0, 0.0, 2.0)) + .margin((0., 0., 0., 2.)) .text(text) .build(bc) }) @@ -390,12 +390,12 @@ impl Template for MainView { .element("button") .class("single_content") .id("remove-item-button") - .icon(material_font_icons::MINUS_FONT_ICON) + .icon(material_icons_font_ttf::MD_REMOVE_CIRCLE) .on_click(move |states, _| { state(id, states).action(Action::RemoveItem); true }) - .min_width(0.0) + .min_width(0.) .attach(Grid::column(0)) .attach(Grid::row(4)) .build(ctx), @@ -405,12 +405,12 @@ impl Template for MainView { .element("button") .class("single_content") .id("add-item-button") - .icon(material_font_icons::ADD_FONT_ICON) + .icon(material_icons_font_ttf::MD_ADD_CIRCLE) .on_click(move |states, _| { state(id, states).action(Action::AddItem); true }) - .min_width(0.0) + .min_width(0.) .attach(Grid::column(2)) .attach(Grid::row(4)) .build(ctx), @@ -421,14 +421,14 @@ impl Template for MainView { .attach(Grid::column_span(3)) .attach(Grid::row(5)) .selected_indices(id) - .margin((0.0, 16.0, 0.0, 8.0)) + .margin((0., 16., 0., 8.)) .items_builder(move |bc, index| { let text = bc .get_widget(id) .get::>("selection_list")[index] .clone(); TextBlock::new() - .margin((0.0, 0.0, 0.0, 2.0)) + .margin((0., 0., 0., 2.)) .v_align("center") .text(text) .build(bc) @@ -441,7 +441,7 @@ impl Template for MainView { TextBlock::new() .element("text-block") .id("selection") - .max_width(120.0) + .max_width(120.) .attach(Grid::column(0)) .attach(Grid::column_span(3)) .attach(Grid::row(6)) @@ -463,8 +463,8 @@ fn main() { .window(|ctx| { Window::new() .title("OrbTk - widgets example") - .position((100.0, 100.0)) - .size(468.0, 730.0) + .position((100., 100.)) + .size(468., 730.) .resizeable(true) .child(MainView::new().build(ctx)) .build(ctx)