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
use std::rc::Rc;
use std::cmp::min;

use unicode_width::UnicodeWidthStr;

use Cursive;
use With;
use menu::{MenuItem, MenuTree};
use Printer;
use view::{Position, ScrollBase, View};
use views::KeyEventView;
use align::Align;
use vec::Vec2;
use event::{Callback, Event, EventResult, Key};

/// Popup that shows a list of items.
pub struct MenuPopup {
    menu: Rc<MenuTree>,
    focus: usize,
    scrollbase: ScrollBase,
    align: Align,
    on_dismiss: Option<Callback>,
    on_action: Option<Callback>,
}

impl MenuPopup {
    /// Creates a new `MenuPopup` using the given menu tree.
    pub fn new(menu: Rc<MenuTree>) -> Self {
        MenuPopup {
            menu: menu,
            focus: 0,
            scrollbase: ScrollBase::new().scrollbar_offset(1).right_padding(0),
            align: Align::top_left(),
            on_dismiss: None,
            on_action: None,
        }
    }

    /// Sets the currently focused element.
    pub fn set_focus(&mut self, focus: usize) {
        self.focus = min(focus, self.menu.len());
    }

    /// Sets the currently focused element.
    ///
    /// Chainable variant.
    pub fn focus(self, focus: usize) -> Self {
        self.with(|s| s.set_focus(focus))
    }

    fn item_width(item: &MenuItem) -> usize {
        match *item {
            MenuItem::Delimiter => 1,
            MenuItem::Leaf(ref title, _) => title.width(),
            MenuItem::Subtree(ref title, _) => title.width() + 3,
        }
    }

    fn scroll_up(&mut self, mut n: usize, cycle: bool) {
        while n > 0 {
            if self.focus > 0 {
                self.focus -= 1;
            } else if cycle {
                self.focus = self.menu.children.len() - 1;
            } else {
                break;
            }

            if !self.menu.children[self.focus].is_delimiter() {
                n -= 1;
            }
        }
    }

    fn scroll_down(&mut self, mut n: usize, cycle: bool) {
        while n > 0 {
            if self.focus + 1 < self.menu.children.len() {
                self.focus += 1;
            } else if cycle {
                self.focus = 0;
            } else {
                break;
            }
            if !self.menu.children[self.focus].is_delimiter() {
                n -= 1;
            }
        }
    }


    /// Sets the alignment for this view.
    pub fn align(mut self, align: Align) -> Self {
        self.align = align;

        self
    }

    /// Sets a callback to be used when this view is actively dismissed.
    ///
    /// (When the user hits <ESC>)
    pub fn on_dismiss<F: 'static + Fn(&mut Cursive)>(mut self, f: F) -> Self {
        self.on_dismiss = Some(Callback::from_fn(f));
        self
    }

    /// Sets a callback to be used when a leaf is activated.
    ///
    /// Will also be called if a leaf from a subtree is activated.
    ///
    /// Usually used to hide the parent view.
    pub fn on_action<F: 'static + Fn(&mut Cursive)>(mut self, f: F) -> Self {
        self.on_action = Some(Callback::from_fn(f));
        self
    }

    fn make_subtree_cb(&self, tree: &Rc<MenuTree>) -> EventResult {
        let tree = tree.clone();
        let max_width = 4 +
                        self.menu
            .children
            .iter()
            .map(Self::item_width)
            .max()
            .unwrap_or(1);
        let offset = Vec2::new(max_width, self.focus);
        let action_cb = self.on_action.clone();

        EventResult::with_cb(move |s| {
            let action_cb = action_cb.clone();
            s.screen_mut()
                .add_layer_at(Position::parent(offset),
                              KeyEventView::new(MenuPopup::new(tree.clone())
                                      .on_action(move |s| {
                            // This will happen when the subtree popup
                            // activates something;
                            // First, remove ourselve.
                            s.pop_layer();
                            if let Some(ref action_cb) = action_cb {
                                action_cb.clone()(s);
                            }
                        }))
                                  .register(Key::Left, |s| s.pop_layer()));
        })
    }
}

impl View for MenuPopup {
    fn draw(&self, printer: &Printer) {
        if printer.size.x < 2 || printer.size.y < 2 {
            return;
        }

        let h = self.menu.len();
        let offset = self.align.v.get_offset(h, printer.size.y);
        let printer =
            &printer.sub_printer(Vec2::new(0, offset), printer.size, true);

        // Start with a box
        printer.print_box(Vec2::new(0, 0), printer.size, false);

        // We're giving it a reduced size because of borders.
        // But we're keeping the full width,
        // to integrate horizontal delimiters in the frame.
        let size = printer.size - (0, 2);
        let printer = printer.sub_printer(Vec2::new(0, 1), size, true);
        self.scrollbase.draw(&printer, |printer, i| {
            printer.with_selection(i == self.focus, |printer| {
                let item = &self.menu.children[i];
                match *item {
                    MenuItem::Delimiter => {
                        printer.print_hdelim((0, 0), printer.size.x)
                    }
                    MenuItem::Subtree(ref label, _) => {
                        if printer.size.x < 4 {
                            return;
                        }
                        printer.print_hline((1, 0), printer.size.x - 2, " ");
                        printer.print((2, 0), label);
                        printer.print((printer.size.x - 4, 0), ">>");
                    }
                    MenuItem::Leaf(ref label, _) => {
                        if printer.size.x < 2 {
                            return;
                        }
                        printer.print_hline((1, 0), printer.size.x - 2, " ");
                        printer.print((2, 0), label);
                    }
                }

            });
        });
    }

    fn get_min_size(&mut self, req: Vec2) -> Vec2 {
        // We can't really shrink our items here, so it's not flexible.
        let w = 4 +
                self.menu
            .children
            .iter()
            .map(Self::item_width)
            .max()
            .unwrap_or(1);
        let h = 2 + self.menu.children.len();


        let scrolling = req.y < h;

        let w = if scrolling {
            w + 1
        } else {
            w
        };

        Vec2::new(w, h)
    }

    fn on_event(&mut self, event: Event) -> EventResult {
        match event {
            Event::Key(Key::Esc) => {
                let dismiss_cb = self.on_dismiss.clone();
                return EventResult::with_cb(move |s| {
                    if let Some(ref cb) = dismiss_cb {
                        cb.clone()(s);
                    }
                    s.pop_layer();
                });
            }
            Event::Key(Key::Up) => self.scroll_up(1, true),
            Event::Key(Key::PageUp) => self.scroll_up(5, false),
            Event::Key(Key::Down) => self.scroll_down(1, true),
            Event::Key(Key::PageDown) => self.scroll_down(5, false),

            Event::Key(Key::Home) => self.focus = 0,
            Event::Key(Key::End) => self.focus = self.menu.children.len() - 1,

            Event::Key(Key::Right) if self.menu.children[self.focus]
                .is_subtree() => {
                return match self.menu.children[self.focus] {
                    MenuItem::Subtree(_, ref tree) => {
                        self.make_subtree_cb(tree)
                    }
                    _ => panic!("Not a subtree???"),

                };
            }
            Event::Key(Key::Enter) if !self.menu.children[self.focus]
                .is_delimiter() => {
                return match self.menu.children[self.focus] {
                    MenuItem::Leaf(_, ref cb) => {

                        let cb = cb.clone();
                        let action_cb = self.on_action.clone();
                        EventResult::with_cb(move |s| {
                            // Remove ourselves from the face of the earth
                            s.pop_layer();
                            // If we had prior orders, do it now.
                            if let Some(ref action_cb) = action_cb {
                                action_cb.clone()(s);
                            }
                            // And transmit his last words.
                            cb.clone()(s);
                        })
                    }
                    MenuItem::Subtree(_, ref tree) => {
                        self.make_subtree_cb(tree)
                    }
                    _ => panic!("No delimiter here"),
                };
            }

            _ => return EventResult::Ignored,
        }

        self.scrollbase.scroll_to(self.focus);

        EventResult::Consumed(None)
    }

    fn layout(&mut self, size: Vec2) {
        self.scrollbase.set_heights(size.y - 2, self.menu.children.len());
    }
}