1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use raui_app::app::{App, AppConfig, declarative::DeclarativeApp};
use raui_core::{
Managed, make_widget, pre_hooks,
view_model::{ViewModel, ViewModelValue},
widget::{
WidgetRef,
component::{
containers::{
anchor_box::PivotBoxProps, content_box::content_box, portal_box::PortalsContainer,
size_box::SizeBoxProps, vertical_box::vertical_box,
},
image_box::{ImageBoxProps, image_box},
interactive::{
button::ButtonProps,
navigation::{NavItemActive, use_nav_container_active},
options_view::{OptionsViewMode, OptionsViewProps, options_view},
},
text_box::{TextBoxProps, text_box},
},
context::WidgetContext,
node::WidgetNode,
unit::{
content::ContentBoxItemLayout,
size::SizeBoxSizeValue,
text::{TextBoxFont, TextBoxHorizontalAlign, TextBoxVerticalAlign},
},
utils::{Color, Rect},
},
};
const DATA: &str = "data";
const INDEX: &str = "index";
struct AppData {
index: Managed<ViewModelValue<usize>>,
}
fn use_app(ctx: &mut WidgetContext) {
ctx.life_cycle.mount(|mut ctx| {
ctx.view_models
.bindings(DATA, INDEX)
.unwrap()
.bind(ctx.id.to_owned());
});
}
#[pre_hooks(use_nav_container_active, use_app)]
fn app(mut ctx: WidgetContext) -> WidgetNode {
let idref = WidgetRef::default();
let mut app_data = ctx
.view_models
.view_model_mut(DATA)
.unwrap()
.write::<AppData>()
.unwrap();
// We use content box marked with portals container as root to provide space
// for option views to anchor thier content into.
make_widget!(content_box)
.idref(idref.clone())
.with_shared_props(PortalsContainer(idref))
.listed_slot(
// Options view is basically a button that toggles its content anchored
// to itself. You can think of dropdown/context menus, but actually it
// can present any user widgets, not only in a list - content widget can
// be anything that takes listed slots and layouts them in some fashion.
make_widget!(options_view)
.with_props(ContentBoxItemLayout {
anchors: 0.25.into(),
margin: Rect {
left: -150.0,
right: -150.0,
top: -30.0,
bottom: -30.0,
},
..Default::default()
})
// Here we provide options view index source, which tells which option
// has to be shown.
.with_props(OptionsViewProps {
input: Some(app_data.index.lazy().into()),
})
.with_props(NavItemActive)
// Here we tell how to anchor content relatively to options box button.
.with_props(PivotBoxProps {
pivot: [0.0, 1.0].into(),
align: 0.0.into(),
})
// Additionally we might want to provide size of the content.
.with_props(SizeBoxProps {
width: SizeBoxSizeValue::Exact(300.0),
height: SizeBoxSizeValue::Exact(400.0),
..Default::default()
})
// Here we provide content widget. Preferably without existing children,
// because options will be appended, not replacing old children.
// Lists are obvious choice but you could also put slots into a grid,
// or even freeform content box to for example make a map with city
// icons to select!
.named_slot(
"content",
// Since this list will be injected into portal container, which is
// content box, we can make that list kept in bounds of the container.
make_widget!(vertical_box).with_props(ContentBoxItemLayout {
keep_in_bounds: true.into(),
..Default::default()
}),
)
// And last but not least, we provide items as listed slots.
// Each provided widget will be wrapped in button that will notify
// options view about selected option.
.listed_slot(
make_widget!(option)
.with_props("Hello".to_owned())
.with_props(NavItemActive),
)
.listed_slot(
make_widget!(option)
.with_props("World".to_owned())
.with_props(NavItemActive),
)
.listed_slot(
make_widget!(option)
.with_props("this".to_owned())
.with_props(NavItemActive),
)
.listed_slot(
make_widget!(option)
.with_props("is".to_owned())
.with_props(NavItemActive),
)
.listed_slot(
make_widget!(option)
.with_props("dropdown".to_owned())
.with_props(NavItemActive),
),
)
.into()
}
fn option(ctx: WidgetContext) -> WidgetNode {
// Since options are wrapped in buttons, we can read their button state and use it.
let ButtonProps {
selected, trigger, ..
} = ctx.props.read_cloned_or_default();
let color = if trigger {
Color {
r: 1.0,
g: 0.0,
b: 0.0,
a: 1.0,
}
} else if selected {
Color {
r: 0.0,
g: 0.0,
b: 1.0,
a: 1.0,
}
} else {
Color {
r: 0.0,
g: 0.0,
b: 0.0,
a: 1.0,
}
};
let text = ctx.props.read_cloned_or_default::<String>();
// We can also read options view mode property to render our option widget
// diferently, depending if option is shown as selected or as content item.
let text = match ctx.props.read_cloned_or_default::<OptionsViewMode>() {
OptionsViewMode::Selected => format!("> {text}"),
OptionsViewMode::Option => format!("# {text}"),
};
make_widget!(content_box)
.listed_slot(make_widget!(image_box).with_props(ImageBoxProps::colored(color)))
.listed_slot(make_widget!(text_box).with_props(TextBoxProps {
text,
font: TextBoxFont {
name: "./demos/hello-world/resources/verdana.ttf".to_owned(),
size: 32.0,
},
horizontal_align: TextBoxHorizontalAlign::Center,
vertical_align: TextBoxVerticalAlign::Middle,
color: Color {
r: 1.0,
g: 1.0,
b: 1.0,
a: 1.0,
},
..Default::default()
}))
.into()
}
fn main() {
let app = DeclarativeApp::default()
.tree(make_widget!(app))
.view_model(
DATA,
ViewModel::produce(|properties| AppData {
index: Managed::new(ViewModelValue::new(
Default::default(),
properties.notifier(INDEX),
)),
}),
);
App::new(AppConfig::default().title("Options View")).run(app);
}