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
//!
//! This widget draws a popup-menu.
//!
//! It diverges from other widgets as this widget doesn't draw
//! *inside* the given area but aims to stay *outside* of it.
//!
//! You can give a [Placement] where the popup-menu should appear
//! relative to the given area.
//!
//! If you want it to appear at a mouse-click position, use a
//! `Rect::new(mouse_x, mouse_y, 0,0)` area.
//! If you want it to appear next to a given widget, use
//! the widgets drawing area.
//!
//! ## 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::event::{HandleEvent, MouseOnly};
use rat_focus::{FocusFlag, HasFocusFlag, ZRect};
use rat_input::event::Popup;
use rat_input::menuline::{MenuOutcome, MenuStyle};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::Style;
use ratatui::text::Line;
use ratatui::widgets::{Block, StatefulWidget, StatefulWidgetRef};

pub use rat_input::popup_menu::Placement;

/// Popup menu.
#[derive(Debug, Default, Clone)]
pub struct RPopupMenu<'a> {
    widget: rat_input::popup_menu::PopupMenu<'a>,
}

/// Popup menu state.
#[derive(Debug, Clone)]
pub struct RPopupMenuState {
    pub focus: FocusFlag,
    pub widget: rat_input::popup_menu::PopupMenuState,
    pub z_areas: [ZRect; 1],

    pub non_exhaustive: NonExhaustive,
}

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

    /// Add a formatted item.
    /// The navchar is optional, any markup for it is your problem.
    pub fn add(mut self, item: Line<'a>, navchar: Option<char>) -> Self {
        self.widget = self.widget.add(item, navchar);
        self
    }

    /// Add item.
    #[inline]
    #[allow(clippy::should_implement_trait)]
    pub fn add_str(mut self, menu_item: &'a str) -> Self {
        self.widget = self.widget.add_str(menu_item);
        self
    }

    /// Fixed width for the menu.
    /// If not set it uses 1.5 times the length of the longest item.
    pub fn width(mut self, width: u16) -> Self {
        self.widget = self.widget.width(width);
        self
    }

    /// Placement relative to the render-area.
    pub fn placement(mut self, placement: Placement) -> Self {
        self.widget = self.widget.placement(placement);
        self
    }

    /// Combined style.
    #[inline]
    pub fn styles(mut self, styles: MenuStyle) -> Self {
        self.widget = self.widget.styles(styles);
        self
    }

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

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

    /// Block for borders.
    pub fn block(mut self, block: Block<'a>) -> Self {
        self.widget = self.widget.block(block);
        self
    }
}

impl<'a> StatefulWidgetRef for RPopupMenu<'a> {
    type State = RPopupMenuState;

    fn render_ref(&self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        if state.active() {
            self.widget.render_ref(area, buf, &mut state.widget);
            state.z_areas[0] = ZRect::from((1, state.widget.area));
        } else {
            state.clear();
        }
    }
}

impl<'a> StatefulWidget for RPopupMenu<'a> {
    type State = RPopupMenuState;

    fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        if state.active() {
            self.widget.render(area, buf, &mut state.widget);
            state.z_areas[0] = ZRect::from((1, state.widget.area));
        } else {
            state.clear();
        }
    }
}

impl Default for RPopupMenuState {
    fn default() -> Self {
        Self {
            focus: Default::default(),
            widget: Default::default(),
            z_areas: Default::default(),
            non_exhaustive: NonExhaustive,
        }
    }
}

impl RPopupMenuState {
    /// Reset the state to defaults.
    pub fn clear(&mut self) {
        *self = Default::default();
    }

    /// Show the popup.
    pub fn flip_active(&mut self) {
        self.focus.focus.set(!self.focus.get());
    }

    /// Show the popup.
    pub fn active(&self) -> bool {
        self.is_focused()
    }

    /// Show the popup.
    pub fn set_active(&self, active: bool) {
        self.focus.focus.set(active);
    }
}

impl RPopupMenuState {
    /// New
    pub fn new() -> Self {
        Self::default()
    }

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

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

    /// Select
    #[inline]
    pub fn select(&mut self, select: Option<usize>) -> bool {
        self.widget.select(select)
    }

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

    /// Previous item.
    #[inline]
    pub fn prev(&mut self) -> bool {
        self.widget.prev()
    }

    /// Next item.
    #[inline]
    #[allow(clippy::should_implement_trait)]
    pub fn next(&mut self) -> bool {
        self.widget.next()
    }

    /// Select by hotkey
    #[inline]
    pub fn navigate(&mut self, c: char) -> MenuOutcome {
        self.widget.navigate(c)
    }

    /// Select item at position
    #[inline]
    pub fn select_at(&mut self, pos: (u16, u16)) -> bool {
        self.widget.select_at(pos)
    }

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

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

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

    /// Widget area with z index.
    fn z_areas(&self) -> &[ZRect] {
        &self.z_areas
    }

    fn navigable(&self) -> bool {
        false
    }
}

impl HandleEvent<crossterm::event::Event, Popup, MenuOutcome> for RPopupMenuState {
    fn handle(&mut self, event: &crossterm::event::Event, _keymap: Popup) -> MenuOutcome {
        if self.is_focused() {
            self.widget.handle(event, Popup)
        } else {
            self.widget.handle(event, MouseOnly)
        }
    }
}

impl HandleEvent<crossterm::event::Event, MouseOnly, MenuOutcome> for RPopupMenuState {
    fn handle(&mut self, event: &crossterm::event::Event, _keymap: MouseOnly) -> MenuOutcome {
        self.widget.handle(event, MouseOnly)
    }
}