2018-10-10 01:06:03 +08:00
|
|
|
#include "ftxui/component/menu.hpp"
|
2020-03-24 04:26:00 +08:00
|
|
|
|
2018-10-10 01:06:03 +08:00
|
|
|
#include <algorithm>
|
2018-10-19 04:58:38 +08:00
|
|
|
#include <iostream>
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
Element Menu::Render() {
|
2021-04-25 00:16:13 +08:00
|
|
|
Elements elements;
|
|
|
|
bool is_menu_focused = Focused();
|
2021-04-19 04:33:41 +08:00
|
|
|
boxes_.resize(entries.size());
|
2018-10-10 01:06:03 +08:00
|
|
|
for (size_t i = 0; i < entries.size(); ++i) {
|
2021-04-25 00:16:13 +08:00
|
|
|
bool is_focused = (focused == int(i)) && is_menu_focused;
|
|
|
|
bool is_selected = (selected == int(i));
|
|
|
|
|
|
|
|
auto style = is_selected
|
|
|
|
? (is_focused ? selected_focused_style : selected_style)
|
|
|
|
: (is_focused ? focused_style : normal_style);
|
|
|
|
auto focus_management = !is_selected ? nothing
|
|
|
|
: is_menu_focused ? focus
|
|
|
|
: select;
|
|
|
|
auto icon = is_selected ? L"> " : L" ";
|
|
|
|
elements.push_back(text(icon + entries[i]) | style | focus_management |
|
2021-04-19 04:33:41 +08:00
|
|
|
reflect(boxes_[i]));
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
return vbox(std::move(elements));
|
|
|
|
}
|
|
|
|
|
2018-10-19 04:58:38 +08:00
|
|
|
bool Menu::OnEvent(Event event) {
|
2021-04-19 04:33:41 +08:00
|
|
|
if (event.is_mouse())
|
|
|
|
return OnMouseEvent(event);
|
|
|
|
|
2018-10-10 01:06:03 +08:00
|
|
|
if (!Focused())
|
|
|
|
return false;
|
|
|
|
|
2019-11-02 02:52:41 +08:00
|
|
|
int old_selected = selected;
|
2018-10-19 04:58:38 +08:00
|
|
|
if (event == Event::ArrowUp || event == Event::Character('k'))
|
2019-11-02 02:52:41 +08:00
|
|
|
selected--;
|
2018-10-19 04:58:38 +08:00
|
|
|
if (event == Event::ArrowDown || event == Event::Character('j'))
|
2019-11-02 02:52:41 +08:00
|
|
|
selected++;
|
|
|
|
if (event == Event::Tab && entries.size())
|
|
|
|
selected = (selected + 1) % entries.size();
|
|
|
|
if (event == Event::TabReverse && entries.size())
|
|
|
|
selected = (selected + entries.size() - 1) % entries.size();
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-11-02 02:52:41 +08:00
|
|
|
selected = std::max(0, std::min(int(entries.size()) - 1, selected));
|
|
|
|
|
|
|
|
if (selected != old_selected) {
|
2021-04-25 00:16:13 +08:00
|
|
|
focused = selected;
|
2018-10-10 01:06:03 +08:00
|
|
|
on_change();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-19 04:58:38 +08:00
|
|
|
if (event == Event::Return) {
|
2018-10-10 01:06:03 +08:00
|
|
|
on_enter();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-19 04:33:41 +08:00
|
|
|
bool Menu::OnMouseEvent(Event event) {
|
|
|
|
for (int i = 0; i < boxes_.size(); ++i) {
|
|
|
|
if (!boxes_[i].Contain(event.mouse_x(), event.mouse_y()))
|
|
|
|
continue;
|
|
|
|
|
2021-04-25 00:16:13 +08:00
|
|
|
focused = i;
|
2021-04-19 04:33:41 +08:00
|
|
|
if (event.is_mouse_left_down()) {
|
2021-04-25 00:16:13 +08:00
|
|
|
TakeFocus();
|
2021-04-19 04:33:41 +08:00
|
|
|
if (selected != i) {
|
|
|
|
selected = i;
|
|
|
|
on_change();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
} // namespace ftxui
|
2020-08-16 06:24:18 +08:00
|
|
|
|
|
|
|
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|