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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use super::{Menu, SubMenu, SubMenuBuilder};
use kas::event::Command;
use kas::layout::{self, RowPositionSolver, RowSetter, RowSolver, RulesSetter, RulesSolver};
use kas::prelude::*;
use kas::theme::FrameStyle;
impl_scope! {
#[autoimpl(Debug where D: trait)]
#[widget]
pub struct MenuBar<D: Directional = kas::dir::Right> {
core: widget_core!(),
direction: D,
widgets: Vec<SubMenu<D::Flipped>>,
layout_store: layout::DynRowStorage,
delayed_open: Option<WidgetId>,
}
impl Self
where
D: Default,
{
pub fn new(menus: Vec<SubMenu<D::Flipped>>) -> Self {
MenuBar::new_with_direction(D::default(), menus)
}
}
impl Self {
pub fn new_with_direction(direction: D, mut menus: Vec<SubMenu<D::Flipped>>) -> Self {
for menu in menus.iter_mut() {
menu.navigable = false;
}
MenuBar {
core: Default::default(),
direction,
widgets: menus,
layout_store: Default::default(),
delayed_open: None,
}
}
pub fn builder() -> MenuBuilder<D> {
MenuBuilder { menus: vec![] }
}
}
impl WidgetChildren for Self {
#[inline]
fn num_children(&self) -> usize {
self.widgets.len()
}
#[inline]
fn get_child(&self, index: usize) -> Option<&dyn Widget> {
self.widgets.get(index).map(|w| w.as_widget())
}
#[inline]
fn get_child_mut(&mut self, index: usize) -> Option<&mut dyn Widget> {
self.widgets.get_mut(index).map(|w| w.as_widget_mut())
}
}
impl Layout for Self {
fn size_rules(&mut self, mgr: SizeMgr, mut axis: AxisInfo) -> SizeRules {
axis.set_default_align(Align::Center);
let dim = (self.direction, self.widgets.len());
let mut solver = RowSolver::new(axis, dim, &mut self.layout_store);
let frame_rules = mgr.frame(FrameStyle::MenuEntry, axis);
for (n, child) in self.widgets.iter_mut().enumerate() {
solver.for_child(&mut self.layout_store, n, |axis| {
let rules = child.size_rules(mgr.re(), axis);
frame_rules.surround(rules).0
});
}
solver.finish(&mut self.layout_store)
}
fn set_rect(&mut self, mgr: &mut ConfigMgr, rect: Rect) {
self.core.rect = rect;
let dim = (self.direction, self.widgets.len());
let mut setter = RowSetter::<D, Vec<i32>, _>::new(rect, dim, &mut self.layout_store);
for (n, child) in self.widgets.iter_mut().enumerate() {
child.set_rect(mgr, setter.child_rect(&mut self.layout_store, n));
}
}
fn find_id(&mut self, coord: Coord) -> Option<WidgetId> {
if !self.rect().contains(coord) {
return None;
}
let solver = RowPositionSolver::new(self.direction);
solver
.find_child_mut(&mut self.widgets, coord)
.and_then(|child| child.find_id(coord))
.or_else(|| Some(self.id()))
}
fn draw(&mut self, mut draw: DrawMgr) {
let solver = RowPositionSolver::new(self.direction);
solver.for_children(&mut self.widgets, self.core.rect, |w| draw.recurse(w));
}
}
impl<D: Directional> Widget for MenuBar<D> {
fn handle_event(&mut self, mgr: &mut EventMgr, event: Event) -> Response {
match event {
Event::TimerUpdate(id_code) => {
if let Some(id) = self.delayed_open.clone() {
if id.as_u64() == id_code {
self.set_menu_path(mgr, Some(&id), false);
}
}
Response::Used
}
Event::PressStart {
source,
start_id,
coord,
} => {
if start_id
.as_ref()
.map(|id| self.is_ancestor_of(id))
.unwrap_or(false)
{
if source.is_primary() {
let any_menu_open = self.widgets.iter().any(|w| w.menu_is_open());
let press_in_the_bar = self.rect().contains(coord);
if !press_in_the_bar || !any_menu_open {
mgr.grab_press_unique(self.id(), source, coord, None);
}
mgr.set_grab_depress(source, start_id.clone());
if press_in_the_bar {
if self
.widgets
.iter()
.any(|w| w.eq_id(&start_id) && !w.menu_is_open())
{
self.set_menu_path(mgr, start_id.as_ref(), false);
} else {
self.set_menu_path(mgr, None, false);
}
}
}
Response::Used
} else {
self.delayed_open = None;
Response::Unused
}
}
Event::PressMove {
source,
cur_id,
coord,
..
} => {
mgr.set_grab_depress(source, cur_id.clone());
let id = match cur_id {
Some(x) => x,
None => return Response::Used,
};
if self.is_strict_ancestor_of(&id) {
if self.rect().contains(coord) {
mgr.clear_nav_focus();
self.delayed_open = None;
self.set_menu_path(mgr, Some(&id), false);
} else if id != self.delayed_open {
mgr.set_nav_focus(id.clone(), false);
let delay = mgr.config().menu_delay();
mgr.request_update(self.id(), id.as_u64(), delay, true);
self.delayed_open = Some(id);
}
} else {
self.delayed_open = None;
}
Response::Used
}
Event::PressEnd {
coord,
end_id,
success,
..
} if success => {
let id = match end_id {
Some(x) => x,
None => return Response::Used,
};
if !self.rect().contains(coord) {
self.delayed_open = None;
return mgr.send(self, id, Event::Command(Command::Activate));
}
Response::Used
}
Event::PressEnd { .. } => Response::Used,
Event::Command(cmd) => {
use Command::{Left, Up};
let is_vert = self.direction.is_vertical();
let reverse = self.direction.is_reversed() ^ matches!(cmd, Left | Up);
match cmd.as_direction().map(|d| d.is_vertical()) {
Some(v) if v == is_vert => {
for i in 0..self.widgets.len() {
if self.widgets[i].menu_is_open() {
let mut j = isize::conv(i);
j = if reverse { j - 1 } else { j + 1 };
j = j.rem_euclid(self.widgets.len().cast());
self.widgets[i].set_menu_path(mgr, None, true);
let w = &mut self.widgets[usize::conv(j)];
w.set_menu_path(mgr, Some(&w.id()), true);
break;
}
}
Response::Used
}
Some(_) => {
mgr.next_nav_focus(self, reverse, true);
Response::Used
}
None => Response::Unused,
}
}
_ => Response::Unused,
}
}
}
impl Self {
fn set_menu_path(
&mut self,
mgr: &mut EventMgr,
target: Option<&WidgetId>,
set_focus: bool,
) {
log::trace!(
"set_menu_path: self={}, target={target:?}, set_focus={set_focus}",
self.identify()
);
self.delayed_open = None;
for i in 0..self.widgets.len() {
self.widgets[i].set_menu_path(mgr, target, set_focus);
}
}
}
}
pub struct MenuBuilder<D: Directional> {
menus: Vec<SubMenu<D::Flipped>>,
}
impl<D: Directional> MenuBuilder<D> {
pub fn menu<F>(mut self, label: impl Into<AccelString>, f: F) -> Self
where
F: FnOnce(SubMenuBuilder),
D::Flipped: Default,
{
let mut menu = Vec::new();
f(SubMenuBuilder { menu: &mut menu });
self.menus.push(SubMenu::new(label, menu));
self
}
pub fn build(self) -> MenuBar<D>
where
D: Default,
{
MenuBar::new(self.menus)
}
}