2021-07-10 19:20:43 +08:00
|
|
|
#include <stddef.h> // for size_t
|
|
|
|
#include <algorithm> // for max, min
|
|
|
|
#include <functional> // for function
|
|
|
|
#include <memory> // for shared_ptr, allocator_traits<>::value_type
|
2021-08-09 05:25:20 +08:00
|
|
|
#include <string> // for operator+, string
|
2021-07-10 19:20:43 +08:00
|
|
|
#include <utility> // for move
|
|
|
|
#include <vector> // for vector, __alloc_traits<>::value_type
|
2021-05-10 02:32:27 +08:00
|
|
|
|
2021-07-10 19:20:43 +08:00
|
|
|
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
|
|
|
|
#include "ftxui/component/component.hpp" // for Make, Menu
|
|
|
|
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
|
|
|
#include "ftxui/component/component_options.hpp" // for MenuOption
|
2021-05-10 02:32:27 +08:00
|
|
|
#include "ftxui/component/event.hpp" // for Event, Event::ArrowDown, Event::ArrowUp, Event::Return, Event::Tab, Event::TabReverse
|
|
|
|
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Released
|
|
|
|
#include "ftxui/component/screen_interactive.hpp" // for Component
|
2021-07-10 19:20:43 +08:00
|
|
|
#include "ftxui/dom/elements.hpp" // for operator|, Element, reflect, text, vbox, Elements, focus, nothing, select
|
|
|
|
#include "ftxui/screen/box.hpp" // for Box
|
|
|
|
#include "ftxui/util/ref.hpp" // for Ref
|
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-07-10 18:59:36 +08:00
|
|
|
/// @brief A list of items. The user can navigate through them.
|
|
|
|
/// @ingroup component
|
|
|
|
class MenuBase : public ComponentBase {
|
|
|
|
public:
|
2021-08-09 05:25:20 +08:00
|
|
|
MenuBase(ConstStringListRef entries, int* selected, Ref<MenuOption> option)
|
2021-07-10 18:59:36 +08:00
|
|
|
: entries_(entries), selected_(selected), option_(option) {}
|
|
|
|
|
2021-09-08 15:36:37 +08:00
|
|
|
Element Render() override {
|
2021-07-10 18:59:36 +08:00
|
|
|
Elements elements;
|
|
|
|
bool is_menu_focused = Focused();
|
2021-08-09 05:25:20 +08:00
|
|
|
boxes_.resize(entries_.size());
|
|
|
|
for (size_t i = 0; i < entries_.size(); ++i) {
|
2021-07-10 18:59:36 +08:00
|
|
|
bool is_focused = (focused_entry() == int(i)) && is_menu_focused;
|
|
|
|
bool is_selected = (*selected_ == int(i));
|
|
|
|
|
|
|
|
auto style = is_selected ? (is_focused ? option_->style_selected_focused
|
|
|
|
: option_->style_selected)
|
|
|
|
: (is_focused ? option_->style_focused
|
|
|
|
: option_->style_normal);
|
|
|
|
auto focus_management = !is_selected ? nothing
|
|
|
|
: is_menu_focused ? focus
|
|
|
|
: select;
|
2021-08-09 05:25:20 +08:00
|
|
|
auto icon = is_selected ? "> " : " ";
|
|
|
|
elements.push_back(text(icon + entries_[i]) | style | focus_management |
|
|
|
|
reflect(boxes_[i]));
|
2021-07-10 18:59:36 +08:00
|
|
|
}
|
2021-09-08 15:36:37 +08:00
|
|
|
return vbox(std::move(elements)) | reflect(box_);
|
2021-07-10 18:59:36 +08:00
|
|
|
}
|
|
|
|
|
2021-09-08 15:36:37 +08:00
|
|
|
bool OnEvent(Event event) override {
|
2021-07-10 18:59:36 +08:00
|
|
|
if (!CaptureMouse(event))
|
|
|
|
return false;
|
2021-09-08 15:36:37 +08:00
|
|
|
|
2021-07-10 18:59:36 +08:00
|
|
|
if (event.is_mouse())
|
|
|
|
return OnMouseEvent(event);
|
|
|
|
|
2021-09-08 15:36:37 +08:00
|
|
|
if (Focused()) {
|
|
|
|
int old_selected = *selected_;
|
|
|
|
if (event == Event::ArrowUp || event == Event::Character('k'))
|
|
|
|
(*selected_)--;
|
|
|
|
if (event == Event::ArrowDown || event == Event::Character('j'))
|
|
|
|
(*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();
|
|
|
|
|
|
|
|
*selected_ = std::max(0, std::min(int(entries_.size()) - 1, *selected_));
|
|
|
|
|
|
|
|
if (*selected_ != old_selected) {
|
|
|
|
focused_entry() = *selected_;
|
|
|
|
option_->on_change();
|
|
|
|
return true;
|
|
|
|
}
|
2021-07-10 18:59:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (event == Event::Return) {
|
|
|
|
option_->on_enter();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OnMouseEvent(Event event) {
|
2021-09-08 15:36:37 +08:00
|
|
|
if (event.mouse().button == Mouse::WheelDown ||
|
|
|
|
event.mouse().button == Mouse::WheelUp) {
|
|
|
|
return OnMouseWheel(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.mouse().button != Mouse::None &&
|
|
|
|
event.mouse().button != Mouse::Left) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-10 18:59:36 +08:00
|
|
|
if (!CaptureMouse(event))
|
|
|
|
return false;
|
|
|
|
for (int i = 0; i < int(boxes_.size()); ++i) {
|
|
|
|
if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
TakeFocus();
|
|
|
|
focused_entry() = i;
|
|
|
|
if (event.mouse().button == Mouse::Left &&
|
|
|
|
event.mouse().motion == Mouse::Released) {
|
|
|
|
if (*selected_ != i) {
|
|
|
|
*selected_ = i;
|
|
|
|
option_->on_change();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-08 15:36:37 +08:00
|
|
|
bool OnMouseWheel(Event event) {
|
|
|
|
if (!box_.Contain(event.mouse().x, event.mouse().y))
|
|
|
|
return false;
|
|
|
|
int old_selected = *selected_;
|
|
|
|
|
|
|
|
if (event.mouse().button == Mouse::WheelUp)
|
|
|
|
(*selected_)--;
|
|
|
|
if (event.mouse().button == Mouse::WheelDown)
|
|
|
|
(*selected_)++;
|
|
|
|
|
|
|
|
*selected_ = std::max(0, std::min(int(entries_.size()) - 1, *selected_));
|
|
|
|
|
|
|
|
if (*selected_ != old_selected)
|
|
|
|
option_->on_change();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-09 05:25:20 +08:00
|
|
|
bool Focusable() const final { return entries_.size(); }
|
2021-07-10 18:59:36 +08:00
|
|
|
int& focused_entry() { return option_->focused_entry(); }
|
|
|
|
|
|
|
|
protected:
|
2021-08-09 05:25:20 +08:00
|
|
|
ConstStringListRef entries_;
|
2021-07-10 18:59:36 +08:00
|
|
|
int* selected_ = 0;
|
|
|
|
Ref<MenuOption> option_;
|
|
|
|
|
|
|
|
std::vector<Box> boxes_;
|
2021-09-08 15:36:37 +08:00
|
|
|
Box box_;
|
2021-07-10 18:59:36 +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.
|
2021-07-10 20:23:46 +08:00
|
|
|
/// @param option Additional optional parameters.
|
2021-05-10 02:32:27 +08:00
|
|
|
/// @ingroup component
|
|
|
|
/// @see MenuBase
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// auto screen = ScreenInteractive::TerminalOutput();
|
2021-08-09 05:25:20 +08:00
|
|
|
/// std::vector<std::string> entries = {
|
|
|
|
/// "entry 1",
|
|
|
|
/// "entry 2",
|
|
|
|
/// "entry 3",
|
2021-05-10 02:32:27 +08:00
|
|
|
/// };
|
|
|
|
/// int selected = 0;
|
|
|
|
/// auto menu = Menu(&entries, &selected);
|
|
|
|
/// screen.Loop(menu);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// > entry 1
|
|
|
|
/// entry 2
|
|
|
|
/// entry 3
|
|
|
|
/// ```
|
2021-08-09 05:25:20 +08:00
|
|
|
Component Menu(ConstStringListRef entries,
|
2021-07-08 04:13:33 +08:00
|
|
|
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-09-05 00:43:56 +08:00
|
|
|
Component MenuEntry(ConstStringRef label, Ref<MenuEntryOption> option) {
|
|
|
|
class Impl : public ComponentBase {
|
|
|
|
public:
|
|
|
|
Impl(ConstStringRef label, Ref<MenuEntryOption> option)
|
|
|
|
: label_(std::move(label)), option_(std::move(option)) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Element Render() override {
|
|
|
|
bool focused = Focused();
|
2021-09-17 02:45:26 +08:00
|
|
|
auto style =
|
|
|
|
hovered_ ? (focused ? option_->style_selected_focused
|
|
|
|
: option_->style_selected)
|
|
|
|
: (focused ? option_->style_focused : option_->style_normal);
|
2021-09-05 00:43:56 +08:00
|
|
|
auto focus_management = focused ? select : nothing;
|
|
|
|
auto label = focused ? "> " + (*label_) //
|
|
|
|
: " " + (*label_);
|
|
|
|
return text(label) | style | focus_management | reflect(box_);
|
|
|
|
}
|
|
|
|
bool Focusable() const override { return true; }
|
|
|
|
bool OnEvent(Event event) override {
|
|
|
|
if (!event.is_mouse())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
hovered_ = box_.Contain(event.mouse().x, event.mouse().y);
|
|
|
|
|
|
|
|
if (!hovered_)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (event.mouse().button == Mouse::Left &&
|
|
|
|
event.mouse().motion == Mouse::Released) {
|
|
|
|
TakeFocus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ConstStringRef label_;
|
|
|
|
Ref<MenuEntryOption> option_;
|
|
|
|
Box box_;
|
|
|
|
bool hovered_ = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
return Make<Impl>(std::move(label), std::move(option));
|
|
|
|
}
|
|
|
|
|
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.
|