2023-08-19 19:56:36 +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.
|
2023-03-10 03:21:23 +08:00
|
|
|
#include <algorithm> // for max, fill_n, reverse
|
|
|
|
#include <chrono> // for milliseconds
|
|
|
|
#include <ftxui/dom/direction.hpp> // for Direction, Direction::Down, Direction::Left, Direction::Right, Direction::Up
|
|
|
|
#include <functional> // for function
|
|
|
|
#include <string> // for operator+, string
|
|
|
|
#include <utility> // for move
|
|
|
|
#include <vector> // for vector, __alloc_traits<>::value_type
|
2022-01-07 05:38:32 +08:00
|
|
|
|
2024-05-01 17:40:49 +08:00
|
|
|
#include "ftxui/component/animation.hpp" // for Animator, Linear
|
2022-03-14 01:51:46 +08:00
|
|
|
#include "ftxui/component/component.hpp" // for Make, Menu, MenuEntry, Toggle
|
|
|
|
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
2023-03-10 03:21:23 +08:00
|
|
|
#include "ftxui/component/component_options.hpp" // for MenuOption, MenuEntryOption, UnderlineOption, AnimatedColorOption, AnimatedColorsOption, EntryState
|
2022-03-14 01:51:46 +08:00
|
|
|
#include "ftxui/component/event.hpp" // for Event, Event::ArrowDown, Event::ArrowLeft, Event::ArrowRight, Event::ArrowUp, Event::End, Event::Home, Event::PageDown, Event::PageUp, Event::Return, Event::Tab, Event::TabReverse
|
2022-01-07 05:38:32 +08:00
|
|
|
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Released, Mouse::WheelDown, Mouse::WheelUp, Mouse::None
|
2021-05-10 02:32:27 +08:00
|
|
|
#include "ftxui/component/screen_interactive.hpp" // for Component
|
2022-03-14 01:51:46 +08:00
|
|
|
#include "ftxui/dom/elements.hpp" // for operator|, Element, reflect, Decorator, nothing, Elements, bgcolor, color, hbox, separatorHSelector, separatorVSelector, vbox, xflex, yflex, text, bold, focus, inverted, select
|
|
|
|
#include "ftxui/screen/box.hpp" // for Box
|
|
|
|
#include "ftxui/screen/color.hpp" // for Color
|
|
|
|
#include "ftxui/screen/util.hpp" // for clamp
|
|
|
|
#include "ftxui/util/ref.hpp" // for Ref, ConstStringListRef, ConstStringRef
|
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
|
|
|
|
2022-03-14 01:51:46 +08:00
|
|
|
namespace {
|
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
Element DefaultOptionTransform(const EntryState& state) {
|
|
|
|
std::string label = (state.active ? "> " : " ") + state.label; // NOLINT
|
2023-06-01 01:24:08 +08:00
|
|
|
Element e = text(std::move(label));
|
2022-03-31 08:17:43 +08:00
|
|
|
if (state.focused) {
|
2022-03-14 01:51:46 +08:00
|
|
|
e = e | inverted;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
if (state.active) {
|
2022-03-14 01:51:46 +08:00
|
|
|
e = e | bold;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2022-03-14 01:51:46 +08:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2023-03-10 03:21:23 +08:00
|
|
|
bool IsInverted(Direction direction) {
|
2022-03-14 01:51:46 +08:00
|
|
|
switch (direction) {
|
2023-03-10 03:21:23 +08:00
|
|
|
case Direction::Up:
|
|
|
|
case Direction::Left:
|
2022-03-14 01:51:46 +08:00
|
|
|
return true;
|
2023-03-10 03:21:23 +08:00
|
|
|
case Direction::Down:
|
|
|
|
case Direction::Right:
|
2022-03-14 01:51:46 +08:00
|
|
|
return false;
|
|
|
|
}
|
2022-03-31 08:17:43 +08:00
|
|
|
return false; // NOT_REACHED()
|
2022-03-14 01:51:46 +08:00
|
|
|
}
|
|
|
|
|
2023-03-10 03:21:23 +08:00
|
|
|
bool IsHorizontal(Direction direction) {
|
2022-03-14 01:51:46 +08:00
|
|
|
switch (direction) {
|
2023-03-10 03:21:23 +08:00
|
|
|
case Direction::Left:
|
|
|
|
case Direction::Right:
|
2022-03-14 01:51:46 +08:00
|
|
|
return true;
|
2023-03-10 03:21:23 +08:00
|
|
|
case Direction::Down:
|
|
|
|
case Direction::Up:
|
2022-03-14 01:51:46 +08:00
|
|
|
return false;
|
|
|
|
}
|
2022-03-31 08:17:43 +08:00
|
|
|
return false; // NOT_REACHED()
|
2022-03-14 01:51:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2021-07-10 18:59:36 +08:00
|
|
|
/// @brief A list of items. The user can navigate through them.
|
|
|
|
/// @ingroup component
|
2023-06-25 23:22:05 +08:00
|
|
|
class MenuBase : public ComponentBase, public MenuOption {
|
2021-07-10 18:59:36 +08:00
|
|
|
public:
|
2024-05-01 17:40:49 +08:00
|
|
|
explicit MenuBase(const MenuOption& option) : MenuOption(option) {}
|
2021-07-10 18:59:36 +08:00
|
|
|
|
2023-06-25 23:22:05 +08:00
|
|
|
bool IsHorizontal() { return ftxui::IsHorizontal(direction); }
|
2022-03-14 01:51:46 +08:00
|
|
|
void OnChange() {
|
2023-06-25 23:22:05 +08:00
|
|
|
if (on_change) {
|
|
|
|
on_change();
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2022-03-14 01:51:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnEnter() {
|
2023-06-25 23:22:05 +08:00
|
|
|
if (on_enter) {
|
|
|
|
on_enter();
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2022-03-14 01:51:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Clamp() {
|
2023-06-25 23:22:05 +08:00
|
|
|
if (selected() != selected_previous_) {
|
2022-08-21 23:23:13 +08:00
|
|
|
SelectedTakeFocus();
|
|
|
|
}
|
2022-03-14 01:51:46 +08:00
|
|
|
boxes_.resize(size());
|
2023-06-25 23:22:05 +08:00
|
|
|
selected() = util::clamp(selected(), 0, size() - 1);
|
2022-08-21 23:23:13 +08:00
|
|
|
selected_previous_ = util::clamp(selected_previous_, 0, size() - 1);
|
|
|
|
selected_focus_ = util::clamp(selected_focus_, 0, size() - 1);
|
2022-03-14 01:51:46 +08:00
|
|
|
focused_entry() = util::clamp(focused_entry(), 0, size() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnAnimation(animation::Params& params) override {
|
|
|
|
animator_first_.OnAnimation(params);
|
|
|
|
animator_second_.OnAnimation(params);
|
2022-03-31 08:17:43 +08:00
|
|
|
for (auto& animator : animator_background_) {
|
2022-03-14 01:51:46 +08:00
|
|
|
animator.OnAnimation(params);
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
for (auto& animator : animator_foreground_) {
|
2022-03-14 01:51:46 +08:00
|
|
|
animator.OnAnimation(params);
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2022-03-14 01:51:46 +08:00
|
|
|
}
|
|
|
|
|
2021-09-08 15:36:37 +08:00
|
|
|
Element Render() override {
|
2022-01-07 05:38:32 +08:00
|
|
|
Clamp();
|
2022-03-14 01:51:46 +08:00
|
|
|
UpdateAnimationTarget();
|
|
|
|
|
2021-07-10 18:59:36 +08:00
|
|
|
Elements elements;
|
2022-12-20 01:51:25 +08:00
|
|
|
const bool is_menu_focused = Focused();
|
2023-06-25 23:22:05 +08:00
|
|
|
if (elements_prefix) {
|
|
|
|
elements.push_back(elements_prefix());
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2023-06-01 01:24:08 +08:00
|
|
|
elements.reserve(size());
|
2022-01-07 05:38:32 +08:00
|
|
|
for (int i = 0; i < size(); ++i) {
|
2023-06-25 23:22:05 +08:00
|
|
|
if (i != 0 && elements_infix) {
|
|
|
|
elements.push_back(elements_infix());
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2022-12-20 01:51:25 +08:00
|
|
|
const bool is_focused = (focused_entry() == i) && is_menu_focused;
|
2023-06-25 23:22:05 +08:00
|
|
|
const bool is_selected = (selected() == i);
|
2021-07-10 18:59:36 +08:00
|
|
|
|
2022-12-20 01:51:25 +08:00
|
|
|
const EntryState state = {
|
2023-06-25 23:22:05 +08:00
|
|
|
entries[i],
|
2022-03-14 01:51:46 +08:00
|
|
|
false,
|
|
|
|
is_selected,
|
|
|
|
is_focused,
|
|
|
|
};
|
|
|
|
|
2024-04-28 22:03:00 +08:00
|
|
|
auto focus_management = (selected_focus_ != i) ? nothing
|
|
|
|
: is_menu_focused ? focus
|
|
|
|
: select;
|
2022-08-21 23:23:13 +08:00
|
|
|
|
2022-12-20 01:51:25 +08:00
|
|
|
const Element element =
|
2023-06-25 23:22:05 +08:00
|
|
|
(entries_option.transform ? entries_option.transform
|
|
|
|
: DefaultOptionTransform) //
|
2022-03-31 08:17:43 +08:00
|
|
|
(state);
|
2022-03-14 01:51:46 +08:00
|
|
|
elements.push_back(element | AnimatedColorStyle(i) | reflect(boxes_[i]) |
|
|
|
|
focus_management);
|
|
|
|
}
|
2023-06-25 23:22:05 +08:00
|
|
|
if (elements_postfix) {
|
|
|
|
elements.push_back(elements_postfix());
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2022-03-14 01:51:46 +08:00
|
|
|
|
2023-06-25 23:22:05 +08:00
|
|
|
if (IsInverted(direction)) {
|
2022-03-14 01:51:46 +08:00
|
|
|
std::reverse(elements.begin(), elements.end());
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2022-03-14 01:51:46 +08:00
|
|
|
|
2022-12-20 01:51:25 +08:00
|
|
|
const Element bar =
|
2022-03-14 01:51:46 +08:00
|
|
|
IsHorizontal() ? hbox(std::move(elements)) : vbox(std::move(elements));
|
|
|
|
|
2023-06-25 23:22:05 +08:00
|
|
|
if (!underline.enabled) {
|
2022-03-14 01:51:46 +08:00
|
|
|
return bar | reflect(box_);
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2022-03-14 01:51:46 +08:00
|
|
|
|
|
|
|
if (IsHorizontal()) {
|
|
|
|
return vbox({
|
|
|
|
bar | xflex,
|
|
|
|
separatorHSelector(first_, second_, //
|
2023-06-25 23:22:05 +08:00
|
|
|
underline.color_active,
|
|
|
|
underline.color_inactive),
|
2022-03-14 01:51:46 +08:00
|
|
|
}) |
|
|
|
|
reflect(box_);
|
|
|
|
} else {
|
|
|
|
return hbox({
|
|
|
|
separatorVSelector(first_, second_, //
|
2023-06-25 23:22:05 +08:00
|
|
|
underline.color_active,
|
|
|
|
underline.color_inactive),
|
2022-03-14 01:51:46 +08:00
|
|
|
bar | yflex,
|
|
|
|
}) |
|
|
|
|
reflect(box_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-21 23:23:13 +08:00
|
|
|
void SelectedTakeFocus() {
|
2023-06-25 23:22:05 +08:00
|
|
|
selected_previous_ = selected();
|
|
|
|
selected_focus_ = selected();
|
2022-08-21 23:23:13 +08:00
|
|
|
}
|
|
|
|
|
2022-03-14 01:51:46 +08:00
|
|
|
void OnUp() {
|
2023-06-25 23:22:05 +08:00
|
|
|
switch (direction) {
|
2023-03-10 03:21:23 +08:00
|
|
|
case Direction::Up:
|
2023-06-25 23:22:05 +08:00
|
|
|
selected()++;
|
2022-03-14 01:51:46 +08:00
|
|
|
break;
|
2023-03-10 03:21:23 +08:00
|
|
|
case Direction::Down:
|
2023-06-25 23:22:05 +08:00
|
|
|
selected()--;
|
2022-03-14 01:51:46 +08:00
|
|
|
break;
|
2023-03-10 03:21:23 +08:00
|
|
|
case Direction::Left:
|
|
|
|
case Direction::Right:
|
2022-03-14 01:51:46 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnDown() {
|
2023-06-25 23:22:05 +08:00
|
|
|
switch (direction) {
|
2023-03-10 03:21:23 +08:00
|
|
|
case Direction::Up:
|
2023-06-25 23:22:05 +08:00
|
|
|
selected()--;
|
2022-03-14 01:51:46 +08:00
|
|
|
break;
|
2023-03-10 03:21:23 +08:00
|
|
|
case Direction::Down:
|
2023-06-25 23:22:05 +08:00
|
|
|
selected()++;
|
2022-03-14 01:51:46 +08:00
|
|
|
break;
|
2023-03-10 03:21:23 +08:00
|
|
|
case Direction::Left:
|
|
|
|
case Direction::Right:
|
2022-03-14 01:51:46 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnLeft() {
|
2023-06-25 23:22:05 +08:00
|
|
|
switch (direction) {
|
2023-03-10 03:21:23 +08:00
|
|
|
case Direction::Left:
|
2023-06-25 23:22:05 +08:00
|
|
|
selected()++;
|
2022-03-14 01:51:46 +08:00
|
|
|
break;
|
2023-03-10 03:21:23 +08:00
|
|
|
case Direction::Right:
|
2023-06-25 23:22:05 +08:00
|
|
|
selected()--;
|
2022-03-14 01:51:46 +08:00
|
|
|
break;
|
2023-03-10 03:21:23 +08:00
|
|
|
case Direction::Down:
|
|
|
|
case Direction::Up:
|
2022-03-14 01:51:46 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnRight() {
|
2023-06-25 23:22:05 +08:00
|
|
|
switch (direction) {
|
2023-03-10 03:21:23 +08:00
|
|
|
case Direction::Left:
|
2023-06-25 23:22:05 +08:00
|
|
|
selected()--;
|
2022-03-14 01:51:46 +08:00
|
|
|
break;
|
2023-03-10 03:21:23 +08:00
|
|
|
case Direction::Right:
|
2023-06-25 23:22:05 +08:00
|
|
|
selected()++;
|
2022-03-14 01:51:46 +08:00
|
|
|
break;
|
2023-03-10 03:21:23 +08:00
|
|
|
case Direction::Down:
|
|
|
|
case Direction::Up:
|
2022-03-14 01:51:46 +08:00
|
|
|
break;
|
2021-07-10 18:59:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
|
2021-09-08 15:36:37 +08:00
|
|
|
bool OnEvent(Event event) override {
|
2022-01-07 05:38:32 +08:00
|
|
|
Clamp();
|
2022-03-31 08:17:43 +08:00
|
|
|
if (!CaptureMouse(event)) {
|
2021-07-10 18:59:36 +08:00
|
|
|
return false;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-09-08 15:36:37 +08:00
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
if (event.is_mouse()) {
|
2021-07-10 18:59:36 +08:00
|
|
|
return OnMouseEvent(event);
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-07-10 18:59:36 +08:00
|
|
|
|
2021-09-08 15:36:37 +08:00
|
|
|
if (Focused()) {
|
2023-06-25 23:22:05 +08:00
|
|
|
const int old_selected = selected();
|
2022-03-31 08:17:43 +08:00
|
|
|
if (event == Event::ArrowUp || event == Event::Character('k')) {
|
2022-03-14 01:51:46 +08:00
|
|
|
OnUp();
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
if (event == Event::ArrowDown || event == Event::Character('j')) {
|
2022-03-14 01:51:46 +08:00
|
|
|
OnDown();
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
if (event == Event::ArrowLeft || event == Event::Character('h')) {
|
2022-03-14 01:51:46 +08:00
|
|
|
OnLeft();
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
if (event == Event::ArrowRight || event == Event::Character('l')) {
|
2022-03-14 01:51:46 +08:00
|
|
|
OnRight();
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
if (event == Event::PageUp) {
|
2023-06-25 23:22:05 +08:00
|
|
|
selected() -= box_.y_max - box_.y_min;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
if (event == Event::PageDown) {
|
2023-06-25 23:22:05 +08:00
|
|
|
selected() += box_.y_max - box_.y_min;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
if (event == Event::Home) {
|
2023-06-25 23:22:05 +08:00
|
|
|
selected() = 0;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
if (event == Event::End) {
|
2023-06-25 23:22:05 +08:00
|
|
|
selected() = size() - 1;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
if (event == Event::Tab && size()) {
|
2023-06-25 23:22:05 +08:00
|
|
|
selected() = (selected() + 1) % size();
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
if (event == Event::TabReverse && size()) {
|
2023-06-25 23:22:05 +08:00
|
|
|
selected() = (selected() + size() - 1) % size();
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-09-08 15:36:37 +08:00
|
|
|
|
2023-06-25 23:22:05 +08:00
|
|
|
selected() = util::clamp(selected(), 0, size() - 1);
|
2021-09-08 15:36:37 +08:00
|
|
|
|
2023-06-25 23:22:05 +08:00
|
|
|
if (selected() != old_selected) {
|
|
|
|
focused_entry() = selected();
|
2022-08-21 23:23:13 +08:00
|
|
|
SelectedTakeFocus();
|
2022-03-14 01:51:46 +08:00
|
|
|
OnChange();
|
2021-09-08 15:36:37 +08:00
|
|
|
return true;
|
|
|
|
}
|
2021-07-10 18:59:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (event == Event::Return) {
|
2022-03-14 01:51:46 +08:00
|
|
|
OnEnter();
|
2021-07-10 18:59:36 +08:00
|
|
|
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;
|
|
|
|
}
|
2022-03-31 08:17:43 +08:00
|
|
|
if (!CaptureMouse(event)) {
|
2021-07-10 18:59:36 +08:00
|
|
|
return false;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2022-01-07 05:38:32 +08:00
|
|
|
for (int i = 0; i < size(); ++i) {
|
2022-03-31 08:17:43 +08:00
|
|
|
if (!boxes_[i].Contain(event.mouse().x, event.mouse().y)) {
|
2021-07-10 18:59:36 +08:00
|
|
|
continue;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-07-10 18:59:36 +08:00
|
|
|
|
|
|
|
TakeFocus();
|
|
|
|
focused_entry() = i;
|
2023-12-17 17:35:21 +08:00
|
|
|
|
|
|
|
if (event.mouse().button == Mouse::Left &&
|
|
|
|
event.mouse().motion == Mouse::Pressed) {
|
2023-06-25 23:22:05 +08:00
|
|
|
if (selected() != i) {
|
|
|
|
selected() = i;
|
|
|
|
selected_previous_ = selected();
|
2022-03-14 01:51:46 +08:00
|
|
|
OnChange();
|
2021-07-10 18:59:36 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-08 15:36:37 +08:00
|
|
|
bool OnMouseWheel(Event event) {
|
2022-03-31 08:17:43 +08:00
|
|
|
if (!box_.Contain(event.mouse().x, event.mouse().y)) {
|
2021-09-08 15:36:37 +08:00
|
|
|
return false;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2023-06-25 23:22:05 +08:00
|
|
|
const int old_selected = selected();
|
2021-09-08 15:36:37 +08:00
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
if (event.mouse().button == Mouse::WheelUp) {
|
2023-06-25 23:22:05 +08:00
|
|
|
selected()--;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
if (event.mouse().button == Mouse::WheelDown) {
|
2023-06-25 23:22:05 +08:00
|
|
|
selected()++;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-09-08 15:36:37 +08:00
|
|
|
|
2023-06-25 23:22:05 +08:00
|
|
|
selected() = util::clamp(selected(), 0, size() - 1);
|
2021-09-08 15:36:37 +08:00
|
|
|
|
2023-06-25 23:22:05 +08:00
|
|
|
if (selected() != old_selected) {
|
2022-08-21 23:23:13 +08:00
|
|
|
SelectedTakeFocus();
|
2022-03-14 01:51:46 +08:00
|
|
|
OnChange();
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-09-08 15:36:37 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-14 01:51:46 +08:00
|
|
|
void UpdateAnimationTarget() {
|
|
|
|
UpdateColorTarget();
|
|
|
|
UpdateUnderlineTarget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateColorTarget() {
|
2023-03-31 23:13:48 +08:00
|
|
|
if (size() != int(animation_background_.size())) {
|
2022-03-14 01:51:46 +08:00
|
|
|
animation_background_.resize(size());
|
|
|
|
animation_foreground_.resize(size());
|
|
|
|
animator_background_.clear();
|
|
|
|
animator_foreground_.clear();
|
|
|
|
|
2023-06-01 01:24:08 +08:00
|
|
|
const int len = size();
|
|
|
|
animator_background_.reserve(len);
|
|
|
|
animator_foreground_.reserve(len);
|
|
|
|
for (int i = 0; i < len; ++i) {
|
2022-03-31 08:17:43 +08:00
|
|
|
animation_background_[i] = 0.F;
|
|
|
|
animation_foreground_[i] = 0.F;
|
|
|
|
animator_background_.emplace_back(&animation_background_[i], 0.F,
|
2022-03-14 01:51:46 +08:00
|
|
|
std::chrono::milliseconds(0),
|
|
|
|
animation::easing::Linear);
|
2022-03-31 08:17:43 +08:00
|
|
|
animator_foreground_.emplace_back(&animation_foreground_[i], 0.F,
|
2022-03-14 01:51:46 +08:00
|
|
|
std::chrono::milliseconds(0),
|
|
|
|
animation::easing::Linear);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 01:51:25 +08:00
|
|
|
const bool is_menu_focused = Focused();
|
2022-03-14 01:51:46 +08:00
|
|
|
for (int i = 0; i < size(); ++i) {
|
2022-12-20 01:51:25 +08:00
|
|
|
const bool is_focused = (focused_entry() == i) && is_menu_focused;
|
2023-06-25 23:22:05 +08:00
|
|
|
const bool is_selected = (selected() == i);
|
2022-03-31 08:17:43 +08:00
|
|
|
float target = is_selected ? 1.F : is_focused ? 0.5F : 0.F; // NOLINT
|
2022-03-14 01:51:46 +08:00
|
|
|
if (animator_background_[i].to() != target) {
|
|
|
|
animator_background_[i] = animation::Animator(
|
|
|
|
&animation_background_[i], target,
|
2023-06-25 23:22:05 +08:00
|
|
|
entries_option.animated_colors.background.duration,
|
|
|
|
entries_option.animated_colors.background.function);
|
2022-03-14 01:51:46 +08:00
|
|
|
animator_foreground_[i] = animation::Animator(
|
|
|
|
&animation_foreground_[i], target,
|
2023-06-25 23:22:05 +08:00
|
|
|
entries_option.animated_colors.foreground.duration,
|
|
|
|
entries_option.animated_colors.foreground.function);
|
2022-03-14 01:51:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Decorator AnimatedColorStyle(int i) {
|
|
|
|
Decorator style = nothing;
|
2023-06-25 23:22:05 +08:00
|
|
|
if (entries_option.animated_colors.foreground.enabled) {
|
2022-03-14 01:51:46 +08:00
|
|
|
style = style | color(Color::Interpolate(
|
|
|
|
animation_foreground_[i],
|
2023-06-25 23:22:05 +08:00
|
|
|
entries_option.animated_colors.foreground.inactive,
|
|
|
|
entries_option.animated_colors.foreground.active));
|
2022-03-14 01:51:46 +08:00
|
|
|
}
|
|
|
|
|
2023-06-25 23:22:05 +08:00
|
|
|
if (entries_option.animated_colors.background.enabled) {
|
2022-03-14 01:51:46 +08:00
|
|
|
style = style | bgcolor(Color::Interpolate(
|
|
|
|
animation_background_[i],
|
2023-06-25 23:22:05 +08:00
|
|
|
entries_option.animated_colors.background.inactive,
|
|
|
|
entries_option.animated_colors.background.active));
|
2022-03-14 01:51:46 +08:00
|
|
|
}
|
|
|
|
return style;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateUnderlineTarget() {
|
2023-06-25 23:22:05 +08:00
|
|
|
if (!underline.enabled) {
|
2022-03-14 01:51:46 +08:00
|
|
|
return;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2022-03-14 01:51:46 +08:00
|
|
|
|
|
|
|
if (FirstTarget() == animator_first_.to() &&
|
|
|
|
SecondTarget() == animator_second_.to()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FirstTarget() >= animator_first_.to()) {
|
|
|
|
animator_first_ = animation::Animator(
|
2023-06-25 23:22:05 +08:00
|
|
|
&first_, FirstTarget(), underline.follower_duration,
|
|
|
|
underline.follower_function, underline.follower_delay);
|
2022-03-14 01:51:46 +08:00
|
|
|
|
|
|
|
animator_second_ = animation::Animator(
|
2023-06-25 23:22:05 +08:00
|
|
|
&second_, SecondTarget(), underline.leader_duration,
|
|
|
|
underline.leader_function, underline.leader_delay);
|
2022-03-14 01:51:46 +08:00
|
|
|
} else {
|
|
|
|
animator_first_ = animation::Animator(
|
2023-06-25 23:22:05 +08:00
|
|
|
&first_, FirstTarget(), underline.leader_duration,
|
|
|
|
underline.leader_function, underline.leader_delay);
|
2022-03-14 01:51:46 +08:00
|
|
|
|
|
|
|
animator_second_ = animation::Animator(
|
2023-06-25 23:22:05 +08:00
|
|
|
&second_, SecondTarget(), underline.follower_duration,
|
|
|
|
underline.follower_function, underline.follower_delay);
|
2022-03-14 01:51:46 +08:00
|
|
|
}
|
2022-01-07 05:38:32 +08:00
|
|
|
}
|
|
|
|
|
2023-06-25 23:22:05 +08:00
|
|
|
bool Focusable() const final { return entries.size(); }
|
|
|
|
int size() const { return int(entries.size()); }
|
2022-03-31 08:17:43 +08:00
|
|
|
float FirstTarget() {
|
|
|
|
if (boxes_.empty()) {
|
|
|
|
return 0.F;
|
|
|
|
}
|
2023-06-25 23:22:05 +08:00
|
|
|
const int value = IsHorizontal() ? boxes_[selected()].x_min - box_.x_min
|
|
|
|
: boxes_[selected()].y_min - box_.y_min;
|
2022-03-31 08:17:43 +08:00
|
|
|
return float(value);
|
2022-03-14 01:51:46 +08:00
|
|
|
}
|
2022-03-31 08:17:43 +08:00
|
|
|
float SecondTarget() {
|
|
|
|
if (boxes_.empty()) {
|
|
|
|
return 0.F;
|
|
|
|
}
|
2023-06-25 23:22:05 +08:00
|
|
|
const int value = IsHorizontal() ? boxes_[selected()].x_max - box_.x_min
|
|
|
|
: boxes_[selected()].y_max - box_.y_min;
|
2022-03-31 08:17:43 +08:00
|
|
|
return float(value);
|
2022-03-14 01:51:46 +08:00
|
|
|
}
|
2021-07-10 18:59:36 +08:00
|
|
|
|
|
|
|
protected:
|
2023-06-25 23:22:05 +08:00
|
|
|
int selected_previous_ = selected();
|
|
|
|
int selected_focus_ = selected();
|
2021-07-10 18:59:36 +08:00
|
|
|
|
2023-06-25 23:22:05 +08:00
|
|
|
// Mouse click support:
|
2021-07-10 18:59:36 +08:00
|
|
|
std::vector<Box> boxes_;
|
2021-09-08 15:36:37 +08:00
|
|
|
Box box_;
|
2022-03-14 01:51:46 +08:00
|
|
|
|
2023-06-25 23:22:05 +08:00
|
|
|
// Animation support:
|
2022-03-31 08:17:43 +08:00
|
|
|
float first_ = 0.F;
|
|
|
|
float second_ = 0.F;
|
|
|
|
animation::Animator animator_first_ = animation::Animator(&first_, 0.F);
|
|
|
|
animation::Animator animator_second_ = animation::Animator(&second_, 0.F);
|
2022-03-14 01:51:46 +08:00
|
|
|
std::vector<animation::Animator> animator_background_;
|
|
|
|
std::vector<animation::Animator> animator_foreground_;
|
|
|
|
std::vector<float> animation_background_;
|
|
|
|
std::vector<float> animation_foreground_;
|
2021-07-10 18:59:36 +08:00
|
|
|
};
|
|
|
|
|
2023-06-25 23:22:05 +08:00
|
|
|
/// @brief A list of text. The focused element is selected.
|
|
|
|
/// @param option a structure containing all the paramters.
|
|
|
|
/// @ingroup component
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// auto screen = ScreenInteractive::TerminalOutput();
|
|
|
|
/// std::vector<std::string> entries = {
|
|
|
|
/// "entry 1",
|
|
|
|
/// "entry 2",
|
|
|
|
/// "entry 3",
|
|
|
|
/// };
|
|
|
|
/// int selected = 0;
|
|
|
|
/// auto menu = Menu({
|
|
|
|
/// .entries = &entries,
|
|
|
|
/// .selected = &selected,
|
|
|
|
/// });
|
|
|
|
/// screen.Loop(menu);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// > entry 1
|
|
|
|
/// entry 2
|
|
|
|
/// entry 3
|
|
|
|
/// ```
|
|
|
|
Component Menu(MenuOption option) {
|
|
|
|
return Make<MenuBase>(std::move(option));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
///
|
|
|
|
/// ### 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
|
|
|
|
/// ```
|
2023-06-25 23:22:05 +08:00
|
|
|
Component Menu(ConstStringListRef entries, int* selected, MenuOption option) {
|
|
|
|
option.entries = entries;
|
|
|
|
option.selected = selected;
|
2024-05-01 17:40:49 +08:00
|
|
|
return Menu(option);
|
2021-05-10 02:32:27 +08:00
|
|
|
}
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2022-03-14 01:51:46 +08:00
|
|
|
/// @brief An horizontal list of elements. The user can navigate through them.
|
|
|
|
/// @param entries The list of selectable entries to display.
|
|
|
|
/// @param selected Reference the selected entry.
|
2023-03-31 23:13:48 +08:00
|
|
|
/// See also |Menu|.
|
2022-03-14 01:51:46 +08:00
|
|
|
/// @ingroup component
|
|
|
|
Component Toggle(ConstStringListRef entries, int* selected) {
|
|
|
|
return Menu(entries, selected, MenuOption::Toggle());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief A specific menu entry. They can be put into a Container::Vertical to
|
|
|
|
/// form a menu.
|
|
|
|
/// @param label The text drawn representing this element.
|
|
|
|
/// @param option Additional optional parameters.
|
|
|
|
/// @ingroup component
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// auto screen = ScreenInteractive::TerminalOutput();
|
|
|
|
/// int selected = 0;
|
|
|
|
/// auto menu = Container::Vertical({
|
|
|
|
/// MenuEntry("entry 1"),
|
|
|
|
/// MenuEntry("entry 2"),
|
|
|
|
/// MenuEntry("entry 3"),
|
|
|
|
/// }, &selected);
|
|
|
|
/// screen.Loop(menu);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// > entry 1
|
|
|
|
/// entry 2
|
|
|
|
/// entry 3
|
|
|
|
/// ```
|
2023-06-25 23:22:05 +08:00
|
|
|
Component MenuEntry(ConstStringRef label, MenuEntryOption option) {
|
2024-05-01 17:40:49 +08:00
|
|
|
option.label = std::move(label);
|
2023-06-25 23:22:05 +08:00
|
|
|
return MenuEntry(std::move(option));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief A specific menu entry. They can be put into a Container::Vertical to
|
|
|
|
/// form a menu.
|
|
|
|
/// @param option The parameters.
|
|
|
|
/// @ingroup component
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// auto screen = ScreenInteractive::TerminalOutput();
|
|
|
|
/// int selected = 0;
|
|
|
|
/// auto menu = Container::Vertical({
|
|
|
|
/// MenuEntry({.label = "entry 1"}),
|
|
|
|
/// MenuEntry({.label = "entry 2"}),
|
|
|
|
/// MenuEntry({.label = "entry 3"}),
|
|
|
|
/// }, &selected);
|
|
|
|
/// screen.Loop(menu);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// > entry 1
|
|
|
|
/// entry 2
|
|
|
|
/// entry 3
|
|
|
|
/// ```
|
|
|
|
Component MenuEntry(MenuEntryOption option) {
|
|
|
|
class Impl : public ComponentBase, public MenuEntryOption {
|
2021-09-05 00:43:56 +08:00
|
|
|
public:
|
2023-06-25 23:22:05 +08:00
|
|
|
explicit Impl(MenuEntryOption option)
|
|
|
|
: MenuEntryOption(std::move(option)) {}
|
2021-09-05 00:43:56 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
Element Render() override {
|
2022-12-20 01:51:25 +08:00
|
|
|
const bool focused = Focused();
|
2022-03-14 01:51:46 +08:00
|
|
|
UpdateAnimationTarget();
|
|
|
|
|
2022-12-20 01:51:25 +08:00
|
|
|
const EntryState state = {
|
2023-06-25 23:22:05 +08:00
|
|
|
label(),
|
2022-03-26 14:55:52 +08:00
|
|
|
false,
|
2022-03-14 01:51:46 +08:00
|
|
|
hovered_,
|
|
|
|
focused,
|
|
|
|
};
|
|
|
|
|
2022-12-20 01:51:25 +08:00
|
|
|
const Element element =
|
2023-06-25 23:22:05 +08:00
|
|
|
(transform ? transform : DefaultOptionTransform) //
|
2022-03-31 08:17:43 +08:00
|
|
|
(state);
|
2022-03-14 01:51:46 +08:00
|
|
|
|
2021-09-05 00:43:56 +08:00
|
|
|
auto focus_management = focused ? select : nothing;
|
2022-03-14 01:51:46 +08:00
|
|
|
return element | AnimatedColorStyle() | focus_management | reflect(box_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateAnimationTarget() {
|
2022-12-20 01:51:25 +08:00
|
|
|
const bool focused = Focused();
|
2022-03-31 08:17:43 +08:00
|
|
|
float target = focused ? 1.F : hovered_ ? 0.5F : 0.F; // NOLINT
|
|
|
|
if (target == animator_background_.to()) {
|
2022-03-14 01:51:46 +08:00
|
|
|
return;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2023-06-25 23:22:05 +08:00
|
|
|
animator_background_ = animation::Animator(
|
|
|
|
&animation_background_, target, animated_colors.background.duration,
|
|
|
|
animated_colors.background.function);
|
|
|
|
animator_foreground_ = animation::Animator(
|
|
|
|
&animation_foreground_, target, animated_colors.foreground.duration,
|
|
|
|
animated_colors.foreground.function);
|
2021-09-05 00:43:56 +08:00
|
|
|
}
|
2022-03-14 01:51:46 +08:00
|
|
|
|
|
|
|
Decorator AnimatedColorStyle() {
|
|
|
|
Decorator style = nothing;
|
2023-06-25 23:22:05 +08:00
|
|
|
if (animated_colors.foreground.enabled) {
|
|
|
|
style = style |
|
|
|
|
color(Color::Interpolate(animation_foreground_,
|
|
|
|
animated_colors.foreground.inactive,
|
|
|
|
animated_colors.foreground.active));
|
2022-03-14 01:51:46 +08:00
|
|
|
}
|
|
|
|
|
2023-06-25 23:22:05 +08:00
|
|
|
if (animated_colors.background.enabled) {
|
|
|
|
style = style |
|
|
|
|
bgcolor(Color::Interpolate(animation_background_,
|
|
|
|
animated_colors.background.inactive,
|
|
|
|
animated_colors.background.active));
|
2022-03-14 01:51:46 +08:00
|
|
|
}
|
|
|
|
return style;
|
|
|
|
}
|
|
|
|
|
2021-09-05 00:43:56 +08:00
|
|
|
bool Focusable() const override { return true; }
|
|
|
|
bool OnEvent(Event event) override {
|
2022-03-31 08:17:43 +08:00
|
|
|
if (!event.is_mouse()) {
|
2021-09-05 00:43:56 +08:00
|
|
|
return false;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-09-05 00:43:56 +08:00
|
|
|
|
|
|
|
hovered_ = box_.Contain(event.mouse().x, event.mouse().y);
|
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
if (!hovered_) {
|
2021-09-05 00:43:56 +08:00
|
|
|
return false;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-09-05 00:43:56 +08:00
|
|
|
|
2023-12-17 17:35:21 +08:00
|
|
|
if (event.mouse().button == Mouse::Left &&
|
|
|
|
event.mouse().motion == Mouse::Pressed) {
|
2021-09-05 00:43:56 +08:00
|
|
|
TakeFocus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2022-03-14 01:51:46 +08:00
|
|
|
|
|
|
|
void OnAnimation(animation::Params& params) override {
|
|
|
|
animator_background_.OnAnimation(params);
|
|
|
|
animator_foreground_.OnAnimation(params);
|
|
|
|
}
|
|
|
|
|
2023-06-25 23:22:05 +08:00
|
|
|
MenuEntryOption option_;
|
2021-09-05 00:43:56 +08:00
|
|
|
Box box_;
|
|
|
|
bool hovered_ = false;
|
2022-03-14 01:51:46 +08:00
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
float animation_background_ = 0.F;
|
|
|
|
float animation_foreground_ = 0.F;
|
2022-03-14 01:51:46 +08:00
|
|
|
animation::Animator animator_background_ =
|
2022-03-31 08:17:43 +08:00
|
|
|
animation::Animator(&animation_background_, 0.F);
|
2022-03-14 01:51:46 +08:00
|
|
|
animation::Animator animator_foreground_ =
|
2022-03-31 08:17:43 +08:00
|
|
|
animation::Animator(&animation_foreground_, 0.F);
|
2021-09-05 00:43:56 +08:00
|
|
|
};
|
|
|
|
|
2023-06-25 23:22:05 +08:00
|
|
|
return Make<Impl>(std::move(option));
|
2021-09-05 00:43:56 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
} // namespace ftxui
|