2021-05-15 04:00:49 +08:00
|
|
|
#include <stddef.h> // for size_t
|
|
|
|
#include <algorithm> // for max, min
|
|
|
|
#include <memory> // for shared_ptr, allocator_traits<>::value_type
|
|
|
|
#include <utility> // for move
|
2021-05-10 02:32:27 +08:00
|
|
|
|
|
|
|
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
|
|
|
|
#include "ftxui/component/event.hpp" // for Event, Event::ArrowDown, Event::ArrowUp, Event::Return, Event::Tab, Event::TabReverse
|
2018-10-10 01:06:03 +08:00
|
|
|
#include "ftxui/component/menu.hpp"
|
2021-05-10 02:32:27 +08:00
|
|
|
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Released
|
|
|
|
#include "ftxui/component/screen_interactive.hpp" // for Component
|
2020-03-24 04:26:00 +08:00
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
namespace ftxui {
|
2021-05-02 02:40:35 +08:00
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
/// @brief A list of text. The focused element is selected.
|
|
|
|
/// @param entries The list of entries in the menu.
|
|
|
|
/// @param selected The index of the currently selected element.
|
|
|
|
/// @ingroup component
|
|
|
|
/// @see MenuBase
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// auto screen = ScreenInteractive::TerminalOutput();
|
|
|
|
/// std::vector<std::wstring> entries = {
|
|
|
|
/// L"entry 1",
|
|
|
|
/// L"entry 2",
|
|
|
|
/// L"entry 3",
|
|
|
|
/// };
|
|
|
|
/// int selected = 0;
|
|
|
|
/// auto menu = Menu(&entries, &selected);
|
|
|
|
/// screen.Loop(menu);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// > entry 1
|
|
|
|
/// entry 2
|
|
|
|
/// entry 3
|
|
|
|
/// ```
|
2021-07-08 04:13:33 +08:00
|
|
|
Component Menu(const std::vector<std::wstring>* entries,
|
|
|
|
int* selected,
|
2021-07-10 18:29:39 +08:00
|
|
|
Ref<MenuOption> option) {
|
2021-07-08 04:13:33 +08:00
|
|
|
return Make<MenuBase>(entries, selected, std::move(option));
|
2021-05-10 02:32:27 +08:00
|
|
|
}
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
// static
|
|
|
|
MenuBase* MenuBase::From(Component component) {
|
|
|
|
return static_cast<MenuBase*>(component.get());
|
|
|
|
}
|
|
|
|
|
2021-07-08 04:13:33 +08:00
|
|
|
MenuBase::MenuBase(const std::vector<std::wstring>* entries,
|
|
|
|
int* selected,
|
2021-07-10 18:29:39 +08:00
|
|
|
Ref<MenuOption> option)
|
2021-07-08 04:13:33 +08:00
|
|
|
: entries_(entries), selected_(selected), option_(option) {}
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
Element MenuBase::Render() {
|
2021-04-25 00:16:13 +08:00
|
|
|
Elements elements;
|
|
|
|
bool is_menu_focused = Focused();
|
2021-05-10 02:32:27 +08:00
|
|
|
boxes_.resize(entries_->size());
|
|
|
|
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;
|
2021-05-10 02:32:27 +08:00
|
|
|
bool is_selected = (*selected_ == int(i));
|
2021-04-25 00:16:13 +08:00
|
|
|
|
2021-07-10 17:50:17 +08:00
|
|
|
auto style = is_selected ? (is_focused ? option_->style_selected_focused
|
|
|
|
: option_->style_selected)
|
|
|
|
: (is_focused ? option_->style_focused
|
|
|
|
: option_->style_normal);
|
2021-04-25 00:16:13 +08:00
|
|
|
auto focus_management = !is_selected ? nothing
|
|
|
|
: is_menu_focused ? focus
|
|
|
|
: select;
|
|
|
|
auto icon = is_selected ? L"> " : L" ";
|
2021-05-10 02:32:27 +08:00
|
|
|
elements.push_back(text(icon + entries_->at(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));
|
|
|
|
}
|
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
bool MenuBase::OnEvent(Event event) {
|
2021-05-02 02:40:35 +08:00
|
|
|
if (!CaptureMouse(event))
|
2021-05-02 00:13:56 +08:00
|
|
|
return false;
|
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;
|
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
int old_selected = *selected_;
|
2018-10-19 04:58:38 +08:00
|
|
|
if (event == Event::ArrowUp || event == Event::Character('k'))
|
2021-05-10 02:32:27 +08:00
|
|
|
(*selected_)--;
|
2018-10-19 04:58:38 +08:00
|
|
|
if (event == Event::ArrowDown || event == Event::Character('j'))
|
2021-05-10 02:32:27 +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
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
*selected_ = std::max(0, std::min(int(entries_->size()) - 1, *selected_));
|
2019-11-02 02:52:41 +08:00
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
if (*selected_ != old_selected) {
|
|
|
|
focused = *selected_;
|
2021-07-08 04:13:33 +08:00
|
|
|
option_->on_change();
|
2018-10-10 01:06:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-19 04:58:38 +08:00
|
|
|
if (event == Event::Return) {
|
2021-07-08 04:13:33 +08:00
|
|
|
option_->on_enter();
|
2018-10-10 01:06:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
bool MenuBase::OnMouseEvent(Event event) {
|
2021-05-02 02:40:35 +08:00
|
|
|
if (!CaptureMouse(event))
|
2021-05-02 00:13:56 +08:00
|
|
|
return false;
|
2021-05-16 15:38:24 +08:00
|
|
|
for (int i = 0; i < int(boxes_.size()); ++i) {
|
2021-04-25 21:22:38 +08:00
|
|
|
if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
|
2021-04-19 04:33:41 +08:00
|
|
|
continue;
|
|
|
|
|
2021-04-25 21:22:38 +08:00
|
|
|
TakeFocus();
|
2021-04-25 00:16:13 +08:00
|
|
|
focused = i;
|
2021-04-25 21:22:38 +08:00
|
|
|
if (event.mouse().button == Mouse::Left &&
|
|
|
|
event.mouse().motion == Mouse::Released) {
|
2021-05-10 02:32:27 +08:00
|
|
|
if (*selected_ != i) {
|
|
|
|
*selected_ = i;
|
2021-07-08 04:13:33 +08:00
|
|
|
option_->on_change();
|
2021-04-19 04:33:41 +08:00
|
|
|
}
|
|
|
|
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.
|