frontend: update example widgets and linked in resources

Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
This commit is contained in:
2020-06-16 02:48:37 +02:00
parent 43257dee99
commit d5f48dfed7
16 changed files with 978 additions and 422 deletions

View File

@@ -1,240 +1,478 @@
use std::{cell::Cell, collections::HashSet};
use std::collections::HashSet;
use orbtk::prelude::*;
use orbtk::theme::DEFAULT_THEME_CSS;
use orbtk::*;
static DARK_EXT: &'static str = include_str!("../resources/stylesheets/advotracker-dark.css");
//#[cfg(feature = "light-theme")]
static LIGHT_EXT: &'static str = include_str!("../resources/stylesheets/advotracker-light.css");
static CSS_EXT: &'static str = include_str!("../resources/stylesheets/grid.css");
//#[cfg(not(feature = "light-theme"))]
//fn get_theme() -> ThemeValue {
// ThemeValue::create_from_css(DEFAULT_THEME_CSS)
// .extension_css(CSS_EXT)
// .build()
//}
fn get_theme() -> ThemeValue {
ThemeValue::create()
//.extension_css(DARK_EXT)
.extension_css(LIGHT_EXT)
//.extension_css(CSS_EXT)
.build()
}
#[derive(Debug, Copy, Clone)]
enum Action {
AddItem,
ClearText,
EntryActivated(Entity),
EntryChanged(Entity),
ValueChanged(Entity),
IncrementCounter,
RemoveItem,
}
#[derive(AsAny)]
pub struct MainViewState {
counter: Cell<i32>,
action: Cell<Option<Action>>,
}
impl MainViewState {
fn action(&self, action: impl Into<Option<Action>>) {
self.action.set(action.into());
}
action: Option<Action>,
}
impl Default for MainViewState {
fn default() -> Self {
MainViewState {
counter: Cell::new(0),
action: Cell::new(None),
}
MainViewState { action: None }
}
}
impl MainViewState {
fn action(&mut self, action: impl Into<Option<Action>>) {
self.action = action.into();
}
}
impl State for MainViewState {
fn update(&self, ctx: &mut Context<'_>) {
if let Some(action) = self.action.get() {
fn update(&mut self, _: &mut Registry, ctx: &mut Context<'_>) {
if let Some(action) = self.action {
match action {
Action::AddItem => {
let len = ctx.widget().get::<List>("list").len();
if len < 5 {
ctx.widget()
.get_mut::<List>("list")
.push(format!("Item {}", len + 1));
ctx.child("items").set("count", len + 1);
ctx.child("remove-item-button").set("enabled", true);
if len == 4 {
ctx.child("add-item-button").set("enabled", false);
}
}
}
Action::RemoveItem => {
let len = ctx.widget().get::<List>("list").len();
if len > 0 {
ctx.widget().get_mut::<List>("list").remove(len - 1);
ctx.child("items").set("count", len - 1);
ctx.child("add-item-button").set("enabled", true);
if len == 1 {
ctx.child("remove-item-button").set("enabled", false);
}
}
}
Action::IncrementCounter => {
self.counter.set(self.counter.get() + 1);
*ctx.widget().get_mut::<usize>("counter") += 1;
let counter = *ctx.widget().get::<usize>("counter");
ctx.widget().set(
"result",
String16::from(format!("Button count: {}", self.counter.get())),
String16::from(format!("Button count: {}", counter)),
);
}
Action::ClearText => {
ctx.widget().set("text_one", String16::from(""));
ctx.widget().set("text_two", String16::from(""));
}
Action::EntryActivated(entity) => {
let mut widget = ctx.get_widget(entity);
let text = widget.get_mut::<String16>("text");
println!("submitting {}", text);
text.clear();
}
Action::EntryChanged(entity) => {
let widget = ctx.get_widget(entity);
let text = widget.get::<String16>("text");
println!("entry changed: {}", text);
}
Action::ValueChanged(entity) => {
let val =
((*ctx.get_widget(entity).get::<f64>("val")).floor() as i32).to_string();
ctx.child("value_text").set("text", String16::from(val));
}
}
self.action.set(None);
self.action = None;
}
}
fn update_post_layout(&mut self, _: &mut Registry, ctx: &mut Context<'_>) {
let mut selection_string = "Selected:".to_string();
for index in &ctx.widget().get::<SelectedIndices>("selected_indices").0 {
selection_string = format!("{} {}", selection_string, index);
}
ctx.child("selection")
.set("text", String16::from(selection_string));
}
}
fn create_header(ctx: &mut BuildContext, text: &str) -> Entity {
TextBlock::create()
TextBlock::new()
.text(text)
.selector(Selector::new().with("text-block").class("h1"))
.element("text-block")
.class("h1")
.build(ctx)
}
type List = Vec<String>;
widget!(
MainView<MainViewState> {
selected_indices: SelectedIndices,
text_harm: String16,
counter: usize,
list_count: usize,
combo_box_list_count: usize,
list: List,
selection_list: List,
combo_box_list: List,
selection_list_count: usize,
text_one: String16,
text_two: String16,
result: String16
}
);
impl Template for MainView {
fn template(self, id: Entity, ctx: &mut BuildContext) -> Self {
let state = self.clone_state();
self.name("MainView")
.result("Button count: 0")
.counter(0)
.selected_indices(HashSet::new())
.list(vec![
"Item 1".to_string(),
"Item 2".to_string(),
"Item 3".to_string(),
])
.list_count(3)
.selection_list(vec![
"Item 1".to_string(),
"Item 2".to_string(),
"Item 3".to_string(),
"Item 4".to_string(),
"Item 5".to_string(),
"Item 6".to_string(),
"Item 7".to_string(),
"Item 8".to_string(),
"Item 9".to_string(),
"Item 10".to_string(),
])
.combo_box_list(vec![
"CB 1".to_string(),
"CB 2".to_string(),
"CB 3".to_string(),
"CB 4".to_string(),
"CB 5".to_string(),
"CB 6".to_string(),
"CB 7".to_string(),
"CB 8".to_string(),
"CB 9".to_string(),
"CB 10".to_string(),
])
.selection_list_count(10)
.combo_box_list_count(10)
.child(
Grid::new()
.margin(8.0)
.columns(
Columns::new()
.add(132.0)
.add(16.0)
.add(132.0)
.add(16.0)
.add(132.0),
)
.child(
Stack::new()
.attach(Grid::column(0))
// Column 0
.child(create_header(ctx, "Buttons"))
.child(
Button::new()
.text("Button")
.margin((0.0, 8.0, 0.0, 0.0))
.icon(material_font_icons::CHECK_FONT_ICON)
.attach(Grid::column(0))
.attach(Grid::row(1))
.on_click(move |states, _| {
state(id, states).action(Action::IncrementCounter);
true
})
.build(ctx),
)
.child(
Button::new()
.text("Primary")
.element("button")
.class("primary")
.margin((0.0, 8.0, 0.0, 0.0))
.icon(material_font_icons::CHECK_FONT_ICON)
.attach(Grid::column(0))
.attach(Grid::row(2))
.build(ctx),
)
.child(
ToggleButton::new()
.class("single_content")
.text("ToggleButton")
.margin((0.0, 8.0, 0.0, 0.0))
.attach(Grid::column(0))
.attach(Grid::row(3))
.build(ctx),
)
.child(
CheckBox::new()
.text("CheckBox")
.margin((0.0, 8.0, 0.0, 0.0))
.attach(Grid::column(0))
.attach(Grid::row(4))
.build(ctx),
)
.child(
Switch::new()
.margin((0.0, 8.0, 0.0, 0.0))
.attach(Grid::column(0))
.attach(Grid::row(5))
.build(ctx),
)
.child(
TextBlock::new()
.margin((0.0, 8.0, 0.0, 0.0))
.element("h1")
.id("value_text")
.text("0")
.h_align("center")
.build(ctx),
)
.child(
Slider::new()
.on_changed(move |states, entity| {
state(id, states).action(Action::ValueChanged(entity));
})
.build(ctx),
)
.build(ctx),
)
.child(
Stack::new()
.attach(Grid::column(2))
.child(create_header(ctx, "Text"))
.child(
TextBlock::new()
.class("body")
.text(("result", id))
.margin((0.0, 8.0, 0.0, 0.0))
.attach(Grid::column(2))
.attach(Grid::row(1))
.build(ctx),
)
.child(
TextBox::new()
.water_mark("TextBox...")
.text(("text_one", id))
.margin((0.0, 8.0, 0.0, 0.0))
.attach(Grid::column(2))
.attach(Grid::row(2))
.on_activate(move |states, entity| {
state(id, states).action(Action::EntryActivated(entity));
})
.on_changed(move |states, entity| {
state(id, states).action(Action::EntryChanged(entity));
})
.build(ctx),
)
.child(
TextBox::new()
.water_mark("TextBox...")
.text(("text_two", id))
.margin((0.0, 8.0, 0.0, 0.0))
.attach(Grid::column(2))
.attach(Grid::row(2))
.on_activate(move |states, entity| {
state(id, states).action(Action::EntryActivated(entity));
})
.on_changed(move |states, entity| {
state(id, states).action(Action::EntryChanged(entity));
})
.build(ctx),
)
.child(
Button::new()
.margin((0.0, 8.0, 0.0, 0.0))
.class("single_content")
.text("clear text")
.on_click(move |states, _| {
state(id, states).action(Action::ClearText);
true
})
.build(ctx),
)
.child(
NumericBox::new()
.margin((0.0, 8.0, 0.0, 0.0))
.max(123.0)
.step(0.123)
.val(0.123)
.build(ctx),
)
.build(ctx),
)
.child(
Grid::new()
.rows(
Rows::new()
.add("auto")
.add(32.0)
.add(16.0)
.add(204.0)
.add("auto")
.add(192.0)
.add("auto"),
)
.columns(Columns::new().add("*").add(4.0).add("*"))
.attach(Grid::column(4))
.child(
TextBlock::new()
.text("Items")
.element("text-block")
.class("h1")
.attach(Grid::column(0))
.attach(Grid::column_span(3))
.attach(Grid::row(0))
.build(ctx),
)
.child(
ComboBox::new()
.items_builder(move |bc, index| {
let text = bc
.get_widget(id)
.get::<Vec<String>>("combo_box_list")[index]
.clone();
TextBlock::new()
.margin((0.0, 0.0, 0.0, 2.0))
.v_align("center")
.text(text)
.build(bc)
})
.selected_index(0)
.attach(Grid::column(0))
.attach(Grid::column_span(3))
.attach(Grid::row(1))
.margin((0.0, 8.0, 0.0, 0.0))
.count(("combo_box_list_count", id))
.build(ctx),
)
.child(
ItemsWidget::new()
.element("items-widget")
.id("items")
.padding((4.0, 4.0, 4.0, 2.0))
.attach(Grid::column(0))
.attach(Grid::column_span(3))
.attach(Grid::row(3))
.margin((0.0, 0.0, 0.0, 8.0))
.items_builder(move |bc, index| {
let text = bc.get_widget(id).get::<Vec<String>>("list")
[index]
.clone();
self.name("MainView").child(
//.result("Button count: 0")
//.selected_indices(HashSet::new())
/*
Grid::create()
.margin(8.0)
.rows(
Rows::create()
// define two rows
.row("*")
.row("*")
.build(),
)
.child(
// Container 0
Container::create()
.padding(8.0)
.selector(Selector::from("container").class("header"))
.attach(Grid::row(0))
.child(
*/
Grid::create()
.selector("lynch")
.margin(8.0)
.columns(
Columns::create()
// define three columns
.column("auto")
.column(24.0)
.column("auto")
.build(),
)
.child(
Stack::create()
// Column 0
.attach(Grid::column(0))
.child(create_header(ctx, "Buttons"))
.child(
Button::create()
.text("Button")
.selector(Selector::new().with("button").class("primary"))
.margin((0.0, 8.0, 0.0, 0.0))
.icon(material_font_icons::CHECK_FONT_ICON)
.attach(Grid::column(0))
.attach(Grid::row(1))
.on_mouse_move(move |_| {
println!("Mouse moved over Button!");
true
})
.on_click(move |_| {
println!("Mouse clicked Buttonmoved over Button!");
state.action(Action::IncrementCounter);
true
})
.build(ctx),
)
.child(
Switch::create()
.selector("bluebayoux")
.margin((0.0, 8.0, 0.0, 0.0))
.attach(Grid::column(0))
.attach(Grid::row(2))
.build(ctx),
)
.build(ctx),
)
.child(
Stack::create()
// Column 2
.attach(Grid::column(2))
.child(create_header(ctx, "Text"))
.child(
TextBlock::create()
.selector(Selector::new().class("body"))
.text(("result", id))
.margin((0.0, 8.0, 0.0, 0.0))
.attach(Grid::column(2))
.attach(Grid::row(1))
.build(ctx),
)
.child(
TextBox::create()
.water_mark("Harm...")
.text(("text_harm", id))
.margin((0.0, 8.0, 0.0, 0.0))
.attach(Grid::column(2))
.attach(Grid::row(2))
.build(ctx),
)
.build(ctx),
)
.build(ctx),
)
/*.child(
// Container 1
Container::create()
.selector(Selector::from("container").class("content"))
.padding(8.0)
.attach(Grid::row(1))
.child(
ImageWidget::create()
.image("resources/images/orbtk-space.png")
.visibility("Hidden")
.margin(8.0)
.vertical_alignment("Center")
.horizontal_alignment("Center")
.size(600.0, 600.0)
.clip(true)
.build(ctx),
)
.build(ctx),
)
.build(ctx),
)
Button::new()
.margin((0.0, 0.0, 0.0, 2.0))
.text(text)
.build(bc)
})
.count(("list_count", id))
.build(ctx),
)
.child(
Button::new()
.element("button")
.class("single_content")
.id("remove-item-button")
.icon(material_font_icons::MINUS_FONT_ICON)
.on_click(move |states, _| {
state(id, states).action(Action::RemoveItem);
true
})
.min_width(0.0)
.attach(Grid::column(0))
.attach(Grid::row(4))
.build(ctx),
)
.child(
Button::new()
.element("button")
.class("single_content")
.id("add-item-button")
.icon(material_font_icons::ADD_FONT_ICON)
.on_click(move |states, _| {
state(id, states).action(Action::AddItem);
true
})
.min_width(0.0)
.attach(Grid::column(2))
.attach(Grid::row(4))
.build(ctx),
)
.child(
ListView::new()
.attach(Grid::column(0))
.attach(Grid::column_span(3))
.attach(Grid::row(5))
.selected_indices(id)
.margin((0.0, 16.0, 0.0, 8.0))
.items_builder(move |bc, index| {
let text = bc
.get_widget(id)
.get::<Vec<String>>("selection_list")[index]
.clone();
TextBlock::new()
.margin((0.0, 0.0, 0.0, 2.0))
.v_align("center")
.text(text)
.build(bc)
})
.count(("selection_list_count", id))
.build(ctx),
)
.child(
// todo: wrong text width????
TextBlock::new()
.element("text-block")
.id("selection")
.max_width(120.0)
.attach(Grid::column(0))
.attach(Grid::column_span(3))
.attach(Grid::row(6))
.text("Selected:")
.build(ctx),
)
.build(ctx),
)
.build(ctx),
)
*/
)
}
}
fn main() {
// use this only if you want to run it as web application.
orbtk::initialize();
//orbtk::initialize();
let mut application = Application::default();
application
.create_window()
.bounds((100.0, 100.0, 420.0, 730.0))
.title("OrbTk - advotracker test GUI")
.resizeable(true)
.root(MainView::create())
.build();
application.run();
/*
Application::new()
.window(|ctx| {
Window::create()
.title("OrbTk - advotracker test GUI")
Window::new()
.title("OrbTk - widgets example")
.position((100.0, 100.0))
//.size(600.0, 800.0)
.size(640.0, 480.0)
.size(468.0, 730.0)
.resizeable(true)
//.theme(get_theme())
.child(MainView::create().build(ctx))
.child(MainView::new().build(ctx))
.build(ctx)
})
.run();
*/
}
// helper to request MainViewState
fn state<'a>(id: Entity, states: &'a mut StatesContext) -> &'a mut MainViewState {
states.get_mut(id)
}