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
// Example of immediate mode UI on top of RAUI.
// It's goal is to bring more ergonomics to RAUI by hiding
// declarative interface under simple nested function calls.
// As with retained mode, immediate mode UI can be mixed with
// declarative mode and retained mode widgets.
use raui_app::app::immediate::ImmediateApp;
use raui_core::{
Scalar,
widget::{
component::{
containers::{
horizontal_box::HorizontalBoxProps, vertical_box::VerticalBoxProps,
wrap_box::WrapBoxProps,
},
image_box::ImageBoxProps,
interactive::{
input_field::{TextInputMode, input_text_with_cursor},
navigation::NavItemActive,
},
text_box::TextBoxProps,
},
unit::{flex::FlexBoxItemLayout, text::TextBoxFont},
utils::Color,
},
};
use raui_immediate::{ImProps, apply};
use raui_immediate_widgets::core::{
containers::{content_box, horizontal_box, nav_vertical_box, wrap_box},
image_box,
interactive::{ImmediateButton, button, input_field, self_tracking},
text_box,
};
const FONT: &str = "./demos/hello-world/resources/verdana.ttf";
// app function widget, we pass application state there.
pub fn app(value: &mut usize) {
let props = WrapBoxProps {
margin: 20.0.into(),
..Default::default()
};
wrap_box(props, || {
let props = VerticalBoxProps {
separation: 50.0,
..Default::default()
};
// we can use any "immedietified" RAUI widget we want.
// we can pass Props to parameterize RAUI widget in first param.
// BTW. we should make sure to use any `nav_*` container widget
// somewhere in the app root to make app interactive.
nav_vertical_box(props, || {
let layout = FlexBoxItemLayout {
basis: Some(48.0),
grow: 0.0,
shrink: 0.0,
..Default::default()
};
// we can also apply props on all produced widgets in the scope.
apply(ImProps(layout), || {
counter(value);
let props = HorizontalBoxProps {
separation: 50.0,
..Default::default()
};
horizontal_box(props, || {
// we can react to button-like behavior by reading what
// button-like widgets return of their tracked state.
if text_button("Increment").trigger_start() {
*value = value.saturating_add(1);
}
if text_button("Decrement").trigger_start() {
*value = value.saturating_sub(1);
}
});
});
self_tracking((), |tracking| {
image_box(ImageBoxProps::colored(Color {
r: tracking.state.factor.x,
g: 0.0,
b: tracking.state.factor.y,
a: 1.0,
}));
});
});
});
}
fn text_button(text: &str) -> ImmediateButton {
// buttons use `use_state` hook under the hood to track
// declarative mode button state, that's copy of being
// returned from button function and passed into its
// group closure for children widgets to use.
// BTW. don't forget to apply `NavItemActive` props on
// button if you want to have it enabled for navigation.
button(NavItemActive, |state| {
content_box((), || {
image_box(ImageBoxProps::colored(Color {
r: if state.state.selected { 1.0 } else { 0.75 },
g: if state.state.trigger { 1.0 } else { 0.75 },
b: if state.state.context { 1.0 } else { 0.75 },
a: 1.0,
}));
text_box(TextBoxProps {
text: text.to_string(),
font: TextBoxFont {
name: FONT.to_owned(),
size: 32.0,
},
color: Color {
r: 0.0,
g: 0.0,
b: 0.0,
a: 1.0,
},
..Default::default()
});
});
})
}
fn counter(value: &mut usize) {
// counter widget is a text box wrapped in an input field.
// it works like combination of button (can be focused by
// selection/navigation) and text field (collects keyboard
// text characters when focused).
let props = (NavItemActive, TextInputMode::UnsignedInteger);
let (result, ..) = input_field(value, props, |text, state, button| {
text_box(TextBoxProps {
text: if state.focused {
input_text_with_cursor(text, state.cursor_position, '|')
} else if text.is_empty() {
"...".to_owned()
} else {
text.to_owned()
},
font: TextBoxFont {
name: FONT.to_owned(),
size: 32.0,
},
color: Color {
r: Scalar::from(button.state.trigger),
g: Scalar::from(button.state.selected),
b: Scalar::from(state.focused),
a: 1.0,
},
..Default::default()
});
});
if let Some(result) = result {
*value = result;
}
}
fn main() {
// some applciation state.
let mut counter = 0usize;
ImmediateApp::simple("Immediate mode UI", move |_| {
app(&mut counter);
});
}