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
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
//     https://www.apache.org/licenses/LICENSE-2.0

//! Menubar

use super::{Menu, SubMenu, SubMenuBuilder};
use kas::event::{Command, FocusSource};
use kas::layout::{self, RowPositionSolver, RowSetter, RowSolver, RulesSetter, RulesSolver};
use kas::prelude::*;
use kas::theme::FrameStyle;

impl_scope! {
    /// A menu-bar
    ///
    /// This widget houses a sequence of menu buttons, allowing input actions across
    /// menus.
    #[widget]
    pub struct MenuBar<Data, D: Directional = kas::dir::Right> {
        core: widget_core!(),
        direction: D,
        widgets: Vec<SubMenu<Data>>,
        layout_store: layout::DynRowStorage,
        delayed_open: Option<Id>,
    }

    impl Self
    where
        D: Default,
    {
        /// Construct a menubar
        pub fn new(menus: Vec<SubMenu<Data>>) -> Self {
            MenuBar::new_dir(menus, Default::default())
        }

        /// Construct a menu builder
        pub fn builder() -> MenuBuilder<Data, D> {
            MenuBuilder { menus: vec![], direction: D::default() }
        }
    }
    impl<Data> MenuBar<Data, kas::dir::Right> {
        /// Construct a menubar
        pub fn right(menus: Vec<SubMenu<Data>>) -> Self {
            MenuBar::new(menus)
        }
    }

    impl Self {
        /// Construct a menubar with explicit direction
        pub fn new_dir(mut menus: Vec<SubMenu<Data>>, direction: D) -> Self {
            for menu in menus.iter_mut() {
                menu.navigable = false;
            }
            MenuBar {
                core: Default::default(),
                direction,
                widgets: menus,
                layout_store: Default::default(),
                delayed_open: None,
            }
        }
    }
    impl Layout for Self {
        #[inline]
        fn num_children(&self) -> usize {
            self.widgets.len()
        }
        fn get_child(&self, index: usize) -> Option<&dyn Layout> {
            self.widgets.get(index).map(|w| w.as_layout())
        }

        fn size_rules(&mut self, sizer: SizeCx, mut axis: AxisInfo) -> SizeRules {
            // Unusual behaviour: children's SizeRules are padded with a frame,
            // but the frame does not adjust the children's rects.

            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 = sizer.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(sizer.re(), axis);
                    frame_rules.surround(rules).0
                });
            }
            solver.finish(&mut self.layout_store)
        }

        fn set_rect(&mut self, cx: &mut ConfigCx, 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(cx, setter.child_rect(&mut self.layout_store, n));
            }
        }

        fn find_id(&mut self, coord: Coord) -> Option<Id> {
            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: DrawCx) {
            let solver = RowPositionSolver::new(self.direction);
            solver.for_children(&mut self.widgets, self.core.rect, |w| draw.recurse(w));
        }
    }

    impl Widget for Self {
        type Data = Data;

        fn for_child_node(
            &mut self,
            data: &Data,
            index: usize,
            closure: Box<dyn FnOnce(Node<'_>) + '_>,
        ) {
            if let Some(w) = self.widgets.get_mut(index) {
                closure(w.as_node(data));
            }
        }
    }

    impl<Data, D: Directional> Events for MenuBar<Data, D> {
        fn handle_event(&mut self, cx: &mut EventCx, data: &Data, event: Event) -> IsUsed {
            match event {
                Event::Timer(id_code) => {
                    if let Some(id) = self.delayed_open.clone() {
                        if id.as_u64() == id_code {
                            self.set_menu_path(cx, data, Some(&id), false);
                        }
                    }
                    Used
                }
                Event::PressStart { press } => {
                    if press.id
                        .as_ref()
                        .map(|id| self.is_ancestor_of(id))
                        .unwrap_or(false)
                    {
                        if press.is_primary() {
                            let any_menu_open = self.widgets.iter().any(|w| w.menu_is_open());
                            let press_in_the_bar = self.rect().contains(press.coord);

                            if !press_in_the_bar || !any_menu_open {
                                press.grab(self.id()).with_cx(cx);
                            }
                            cx.set_grab_depress(*press, press.id.clone());
                            if press_in_the_bar {
                                if self
                                    .widgets
                                    .iter()
                                    .any(|w| w.eq_id(&press.id) && !w.menu_is_open())
                                {
                                    self.set_menu_path(cx, data, press.id.as_ref(), false);
                                } else {
                                    self.set_menu_path(cx, data, None, false);
                                }
                            }
                        }
                        Used
                    } else {
                        // Click happened out of the menubar or submenus,
                        // while one or more submenus are opened.
                        self.delayed_open = None;
                        self.set_menu_path(cx, data, None, false);
                        Unused
                    }
                }
                Event::CursorMove { press } | Event::PressMove { press, .. } => {
                    cx.set_grab_depress(*press, press.id.clone());

                    let id = match press.id {
                        Some(x) => x,
                        None => return Used,
                    };

                    if self.is_strict_ancestor_of(&id) {
                        // We instantly open a sub-menu on motion over the bar,
                        // but delay when over a sub-menu (most intuitive?)
                        if self.rect().contains(press.coord) {
                            cx.clear_nav_focus();
                            self.delayed_open = None;
                            self.set_menu_path(cx, data, Some(&id), false);
                        } else if id != self.delayed_open {
                            cx.set_nav_focus(id.clone(), FocusSource::Pointer);
                            let delay = cx.config().menu_delay();
                            cx.request_timer(self.id(), id.as_u64(), delay);
                            self.delayed_open = Some(id);
                        }
                    } else {
                        self.delayed_open = None;
                    }
                    Used
                }
                Event::PressEnd {
                    press,
                    success,
                    ..
                } if success => {
                    let id = match press.id {
                        Some(x) => x,
                        None => return Used,
                    };

                    if !self.rect().contains(press.coord) {
                        // not on the menubar
                        self.delayed_open = None;
                        cx.send_command(id, Command::Activate);
                    }
                    Used
                }
                Event::Command(cmd, _) => {
                    // Arrow keys can switch to the next / previous menu
                    // as well as to the first / last item of an open menu.
                    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(cx, data, None, true);
                                    let w = &mut self.widgets[usize::conv(j)];
                                    w.set_menu_path(cx, data, Some(&w.id()), true);
                                    break;
                                }
                            }
                            Used
                        }
                        Some(_) => {
                            cx.next_nav_focus(self.id(), reverse, FocusSource::Key);
                            Used
                        }
                        None => Unused,
                    }
                }
                _ => Unused,
            }
        }
    }

    impl Self {
        fn set_menu_path(
            &mut self,
            cx: &mut EventCx,
            data: &Data,
            target: Option<&Id>,
            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(cx, data, target, set_focus);
            }
        }
    }
}

/// Builder for [`MenuBar`]
///
/// Access through [`MenuBar::builder`].
pub struct MenuBuilder<Data, D: Directional> {
    menus: Vec<SubMenu<Data>>,
    direction: D,
}

impl<Data, D: Directional> MenuBuilder<Data, D> {
    /// Add a new menu
    ///
    /// The menu's direction is determined via [`Directional::Flipped`].
    pub fn menu<F>(mut self, label: impl Into<AccessString>, f: F) -> Self
    where
        F: FnOnce(SubMenuBuilder<Data>),
    {
        let mut menu = Vec::new();
        f(SubMenuBuilder { menu: &mut menu });
        let dir = self.direction.as_direction().flipped();
        self.menus.push(SubMenu::new(label, menu, dir));
        self
    }

    /// Finish, yielding a [`MenuBar`]
    pub fn build(self) -> MenuBar<Data, D> {
        MenuBar::new_dir(self.menus, self.direction)
    }
}