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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//!
//! A simple line menu.
//!
//! If the render area has more than one line, this will
//! linebreak if needed.
//!
//! ## Navigation keys
//! If you give plain-text strings as items, the underscore
//! designates a navigation key. If you hit the key, the matching
//! item is selected. On the second hit, the matching item is
//! activated.
//!
use crate::_private::NonExhaustive;
use crate::util::{menu_str, next_opt, prev_opt, revert_style};
#[allow(unused_imports)]
use log::debug;
use rat_event::util::MouseFlags;
use rat_event::{ct_event, ConsumedEvent, HandleEvent, MouseOnly, Outcome, Regular};
use rat_focus::{FocusFlag, HasFocusFlag};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::{Style, Stylize};
use ratatui::text::Line;
#[cfg(feature = "unstable-widget-ref")]
use ratatui::widgets::StatefulWidgetRef;
use ratatui::widgets::{StatefulWidget, Widget};
use std::fmt::Debug;

/// One line menu widget.
#[derive(Debug, Default, Clone)]
pub struct MenuLine<'a> {
    title: Line<'a>,
    items: Vec<Line<'a>>,
    navchar: Vec<Option<char>>,

    style: Style,
    title_style: Option<Style>,
    select_style: Option<Style>,
    focus_style: Option<Style>,
}

/// Combined styles.
#[derive(Debug, Clone)]
pub struct MenuStyle {
    pub style: Style,
    pub title: Option<Style>,
    pub select: Option<Style>,
    pub focus: Option<Style>,
    pub non_exhaustive: NonExhaustive,
}

/// State & event handling.
#[derive(Debug, Clone)]
pub struct MenuLineState {
    /// Current focus state.
    pub focus: FocusFlag,
    /// Area for the whole widget.
    pub area: Rect,
    /// Areas for each item.
    pub item_areas: Vec<Rect>,
    /// Hot keys
    pub navchar: Vec<Option<char>>,

    /// Selected item.
    pub selected: Option<usize>,

    /// Flags for mouse handling.
    pub mouse: MouseFlags,

    pub non_exhaustive: NonExhaustive,
}

impl<'a> MenuLine<'a> {
    /// New
    pub fn new() -> Self {
        Default::default()
    }

    /// Title text.
    #[inline]
    pub fn title(mut self, title: impl Into<Line<'a>>) -> Self {
        self.title = title.into();
        self
    }

    /// Add a formatted item.
    /// The navchar is optional, any markup for it is your problem.
    #[allow(clippy::should_implement_trait)]
    pub fn add(mut self, item: Line<'a>, navchar: Option<char>) -> Self {
        self.items.push(item);
        self.navchar.push(navchar);
        self
    }

    /// Add item.
    #[inline]
    pub fn add_str(mut self, txt: &'a str) -> Self {
        let (item, navchar) = menu_str(txt);
        self.items.push(item);
        self.navchar.push(navchar);
        self
    }

    /// Combined style.
    #[inline]
    pub fn styles(mut self, styles: MenuStyle) -> Self {
        self.style = styles.style;
        self.title_style = styles.title;
        self.select_style = styles.select;
        self.focus_style = styles.focus;
        self
    }

    /// Base style.
    #[inline]
    pub fn style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    /// Menu-title style.
    #[inline]
    pub fn title_style(mut self, style: Style) -> Self {
        self.title_style = Some(style);
        self
    }

    /// Selection
    #[inline]
    pub fn select_style(mut self, style: Style) -> Self {
        self.select_style = Some(style);
        self
    }

    /// Selection + Focus
    #[inline]
    pub fn focus_style(mut self, style: Style) -> Self {
        self.focus_style = Some(style);
        self
    }
}

#[cfg(feature = "unstable-widget-ref")]
impl<'a> StatefulWidgetRef for MenuLine<'a> {
    type State = MenuLineState;

    fn render_ref(&self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        render_ref(self, area, buf, state);
    }
}

impl<'a> StatefulWidget for MenuLine<'a> {
    type State = MenuLineState;

    fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        render_ref(&self, area, buf, state);
    }
}

fn render_ref(widget: &MenuLine<'_>, area: Rect, buf: &mut Buffer, state: &mut MenuLineState) {
    state.area = area;
    state.item_areas.clear();
    state.navchar = widget.navchar.clone();

    let select_style = if state.is_focused() {
        if let Some(focus_style) = widget.focus_style {
            focus_style
        } else {
            revert_style(widget.style)
        }
    } else {
        if let Some(select_style) = widget.select_style {
            select_style
        } else {
            revert_style(widget.style)
        }
    };
    let title_style = if let Some(title_style) = widget.title_style {
        title_style
    } else {
        widget.style.underlined()
    };

    buf.set_style(area, widget.style);

    let mut item_area = Rect::new(area.x, area.y, 0, 1);
    if widget.title.width() > 0 {
        item_area.width = widget.title.width() as u16;

        buf.set_style(item_area, title_style);
        widget.title.clone().render(item_area, buf); // todo: clone

        item_area.x += item_area.width + 1;
    }

    'f: {
        for (n, item) in widget.items.iter().enumerate() {
            item_area.width = item.width() as u16;

            // line breaks
            if item_area.right() >= area.right() {
                if item_area.bottom() + 1 >= area.bottom() {
                    break 'f;
                }
                item_area.y += 1;
                item_area.x = area.x;
            }

            state.item_areas.push(item_area);

            if state.selected == Some(n) {
                buf.set_style(item_area, select_style);
            }
            item.render(item_area, buf);

            item_area.x += item_area.width + 1;
        }
    }
}

impl Default for MenuStyle {
    fn default() -> Self {
        Self {
            style: Default::default(),
            title: None,
            select: None,
            focus: None,
            non_exhaustive: NonExhaustive,
        }
    }
}

impl HasFocusFlag for MenuLineState {
    /// Focus flag.
    fn focus(&self) -> FocusFlag {
        self.focus.clone()
    }

    /// Focus area.
    fn area(&self) -> Rect {
        self.area
    }
}

#[allow(clippy::len_without_is_empty)]
impl MenuLineState {
    pub fn new() -> Self {
        Self::default()
    }

    /// New with a focus name.
    pub fn named(name: &str) -> Self {
        Self {
            focus: FocusFlag::named(name),
            ..Default::default()
        }
    }

    /// Number of items.
    #[inline]
    pub fn len(&self) -> usize {
        self.item_areas.len()
    }

    /// Any items.
    pub fn is_empty(&self) -> bool {
        self.item_areas.is_empty()
    }

    /// Select
    #[inline]
    pub fn select(&mut self, select: Option<usize>) -> bool {
        let old = self.selected;
        self.selected = select;
        old != self.selected
    }

    /// Selected index
    #[inline]
    pub fn selected(&self) -> Option<usize> {
        self.selected
    }

    /// Previous item.
    #[inline]
    pub fn prev_item(&mut self) -> bool {
        let old = self.selected;
        self.selected = prev_opt(self.selected, 1, self.len());
        old != self.selected
    }

    /// Next item.
    #[inline]
    pub fn next_item(&mut self) -> bool {
        let old = self.selected;
        self.selected = next_opt(self.selected, 1, self.len());
        old != self.selected
    }

    /// Select by hotkey
    #[inline]
    pub fn navigate(&mut self, c: char) -> MenuOutcome {
        let c = c.to_ascii_lowercase();
        for (i, cc) in self.navchar.iter().enumerate() {
            if *cc == Some(c) {
                if self.selected == Some(i) {
                    return MenuOutcome::Activated(i);
                } else {
                    self.selected = Some(i);
                    return MenuOutcome::Selected(i);
                }
            }
        }
        MenuOutcome::Continue
    }

    /// Select item at position
    #[inline]
    pub fn select_at(&mut self, pos: (u16, u16)) -> bool {
        if let Some(idx) = self.mouse.item_at(&self.item_areas, pos.0, pos.1) {
            self.selected = Some(idx);
            true
        } else {
            false
        }
    }

    /// Item at position.
    #[inline]
    pub fn item_at(&self, pos: (u16, u16)) -> Option<usize> {
        self.mouse.item_at(&self.item_areas, pos.0, pos.1)
    }
}

impl Default for MenuLineState {
    fn default() -> Self {
        Self {
            focus: Default::default(),
            navchar: Default::default(),
            mouse: Default::default(),
            selected: Default::default(),
            item_areas: Default::default(),
            area: Default::default(),
            non_exhaustive: NonExhaustive,
        }
    }
}

/// Outcome for menuline.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MenuOutcome {
    /// The given event was not handled at all.
    Continue,
    /// The event was handled, no repaint necessary.
    Unchanged,
    /// The event was handled, repaint necessary.
    Changed,
    /// The menuitem was selected.
    Selected(usize),
    /// The menuitem was selected and activated.
    Activated(usize),
    /// Selected popup-menu.
    MenuSelected(usize, usize),
    /// Activated popup-menu.
    MenuActivated(usize, usize),
}

impl ConsumedEvent for MenuOutcome {
    fn is_consumed(&self) -> bool {
        *self != MenuOutcome::Continue
    }
}

impl From<MenuOutcome> for Outcome {
    fn from(value: MenuOutcome) -> Self {
        match value {
            MenuOutcome::Continue => Outcome::Continue,
            MenuOutcome::Unchanged => Outcome::Unchanged,
            MenuOutcome::Changed => Outcome::Changed,
            MenuOutcome::Selected(_) => Outcome::Changed,
            MenuOutcome::Activated(_) => Outcome::Changed,
            MenuOutcome::MenuSelected(_, _) => Outcome::Changed,
            MenuOutcome::MenuActivated(_, _) => Outcome::Changed,
        }
    }
}

impl HandleEvent<crossterm::event::Event, Regular, MenuOutcome> for MenuLineState {
    #[allow(clippy::redundant_closure)]
    fn handle(&mut self, event: &crossterm::event::Event, _: Regular) -> MenuOutcome {
        let res = if self.is_focused() {
            match event {
                ct_event!(key press ' ') => self
                    .selected
                    .map_or(MenuOutcome::Unchanged, |v| MenuOutcome::Selected(v)),
                ct_event!(key press ANY-c) => self.navigate(*c),
                ct_event!(keycode press Left) => {
                    if self.prev_item() {
                        MenuOutcome::Selected(self.selected.expect("selected"))
                    } else {
                        MenuOutcome::Unchanged
                    }
                }
                ct_event!(keycode press Right) => {
                    if self.next_item() {
                        MenuOutcome::Selected(self.selected.expect("selected"))
                    } else {
                        MenuOutcome::Unchanged
                    }
                }
                ct_event!(keycode press Home) => {
                    if self.select(Some(0)) {
                        MenuOutcome::Selected(self.selected.expect("selected"))
                    } else {
                        MenuOutcome::Unchanged
                    }
                }
                ct_event!(keycode press End) => {
                    if self.select(Some(self.len().saturating_sub(1))) {
                        MenuOutcome::Selected(self.selected.expect("selected"))
                    } else {
                        MenuOutcome::Unchanged
                    }
                }
                ct_event!(keycode press Enter) => {
                    if let Some(select) = self.selected {
                        MenuOutcome::Activated(select)
                    } else {
                        MenuOutcome::Continue
                    }
                }

                ct_event!(key release _)
                | ct_event!(keycode release Left)
                | ct_event!(keycode release Right)
                | ct_event!(keycode release Home)
                | ct_event!(keycode release End)
                | ct_event!(keycode release Enter) => MenuOutcome::Unchanged,

                _ => MenuOutcome::Continue,
            }
        } else {
            MenuOutcome::Continue
        };

        if res == MenuOutcome::Continue {
            self.handle(event, MouseOnly)
        } else {
            res
        }
    }
}

impl HandleEvent<crossterm::event::Event, MouseOnly, MenuOutcome> for MenuLineState {
    fn handle(&mut self, event: &crossterm::event::Event, _: MouseOnly) -> MenuOutcome {
        match event {
            ct_event!(mouse any for m) if self.mouse.doubleclick(self.area, m) => {
                let idx = self.item_at(self.mouse.pos_of(m));
                if self.selected() == idx {
                    match self.selected {
                        Some(a) => MenuOutcome::Activated(a),
                        None => MenuOutcome::Continue,
                    }
                } else {
                    MenuOutcome::Continue
                }
            }
            ct_event!(mouse any for m) if self.mouse.drag(self.area, m) => {
                let old = self.selected;
                if self.select_at(self.mouse.pos_of(m)) {
                    if old != self.selected {
                        MenuOutcome::Selected(self.selected().expect("selected"))
                    } else {
                        MenuOutcome::Unchanged
                    }
                } else {
                    MenuOutcome::Continue
                }
            }
            ct_event!(mouse down Left for col, row) if self.area.contains((*col, *row).into()) => {
                if self.select_at((*col, *row)) {
                    MenuOutcome::Selected(self.selected().expect("selected"))
                } else {
                    MenuOutcome::Continue
                }
            }
            _ => MenuOutcome::Continue,
        }
    }
}

/// Handle all events.
/// Text events are only processed if focus is true.
/// Mouse events are processed if they are in range.
pub fn handle_events(
    state: &mut MenuLineState,
    focus: bool,
    event: &crossterm::event::Event,
) -> MenuOutcome {
    state.focus.set(focus);
    state.handle(event, Regular)
}

/// Handle only mouse-events.
pub fn handle_mouse_events(
    state: &mut MenuLineState,
    event: &crossterm::event::Event,
) -> MenuOutcome {
    state.handle(event, MouseOnly)
}