FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
src/ftxui/component/dropdown.cpp
Go to the documentation of this file.
1// Copyright 2021 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
5#include <functional> // for function
6#include <string> // for string
7
8#include <utility>
9#include "ftxui/component/component.hpp" // for Maybe, Checkbox, Make, Radiobox, Vertical, Dropdown
10#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
11#include "ftxui/component/component_options.hpp" // for CheckboxOption, EntryState
12#include "ftxui/dom/elements.hpp" // for operator|, Element, border, filler, operator|=, separator, size, text, vbox, frame, vscroll_indicator, hbox, HEIGHT, LESS_THAN, bold, inverted
13#include "ftxui/screen/util.hpp" // for clamp
14#include "ftxui/util/ref.hpp" // for ConstStringListRef
15
16namespace ftxui {
17
18/// @brief A dropdown menu.
19/// @ingroup component
20/// @param entries The list of entries to display.
21/// @param selected The index of the selected entry.
22Component Dropdown(ConstStringListRef entries, int* selected) {
23 DropdownOption option;
24 option.radiobox.entries = std::move(entries);
25 option.radiobox.selected = selected;
26 return Dropdown(option);
27}
28
29/// @brief A dropdown menu.
30/// @ingroup component
31/// @param option The options for the dropdown.
32// NOLINTNEXTLINE
34 class Impl : public ComponentBase, public DropdownOption {
35 public:
36 explicit Impl(DropdownOption option) : DropdownOption(std::move(option)) {
37 FillDefault();
38 checkbox_ = Checkbox(checkbox);
39 radiobox_ = Radiobox(radiobox);
40
41 Add(Container::Vertical({
42 checkbox_,
43 Maybe(radiobox_, checkbox.checked),
44 }));
45 }
46
47 Element OnRender() override {
48 selected_ =
49 util::clamp(radiobox.selected(), 0, int(radiobox.entries.size()) - 1);
50 selected_ = util::clamp(selected_(), 0, int(radiobox.entries.size()) - 1);
51
52 if (selected_() >= 0 && selected_() < int(radiobox.entries.size())) {
53 title_ = radiobox.entries[selected_()];
54 }
55
56 return transform(*open_, checkbox_->Render(), radiobox_->Render());
57 }
58
59 // Switch focus in between the checkbox and the radiobox when selecting it.
60 bool OnEvent(ftxui::Event event) override {
61 const bool open_old = open_();
62 const int selected_old = selected_();
63 bool handled = ComponentBase::OnEvent(event);
64
65 // Transfer focus to the radiobox when the dropdown is opened.
66 if (!open_old && open_()) {
67 radiobox_->TakeFocus();
68 }
69
70 // Auto-close the dropdown when the user selects an item, even if the item
71 // it the same as the previous one.
72 if (open_old && open_()) {
73 const bool should_close =
74 (selected_() != selected_old) || //
75 (event == Event::Return) || //
76 (event == Event::Character(' ')) || //
77 (event == Event::Escape) || //
78 (event.is_mouse() && event.mouse().button == Mouse::Left &&
79 event.mouse().motion == Mouse::Pressed);
80
81 if (should_close) {
82 checkbox_->TakeFocus();
83 open_ = false;
84 handled = true;
85 }
86 }
87
88 return handled;
89 }
90
91 void FillDefault() {
92 open_ = checkbox.checked;
93 selected_ = radiobox.selected;
94 checkbox.checked = &*open_;
95 radiobox.selected = &*selected_;
96 checkbox.label = &title_;
97
98 if (!checkbox.transform) {
99 checkbox.transform = [](const EntryState& s) {
100 auto prefix = text(s.state ? "↓ " : "→ "); // NOLINT
101 auto t = text(s.label);
102 if (s.active) {
103 t |= bold;
104 }
105 if (s.focused) {
106 t |= inverted;
107 }
108 return hbox({prefix, t});
109 };
110 }
111
112 if (!transform) {
113 transform = [](bool is_open, Element checkbox_element,
114 Element radiobox_element) {
115 if (is_open) {
116 const int max_height = 12;
117 return vbox({
118 std::move(checkbox_element),
119 separator(),
120 std::move(radiobox_element) | vscroll_indicator | frame |
121 size(HEIGHT, LESS_THAN, max_height),
122 }) |
123 border;
124 }
125 return vbox({std::move(checkbox_element), filler()}) | border;
126 };
127 }
128 }
129
130 private:
131 Ref<bool> open_;
132 Ref<int> selected_;
133 Component checkbox_;
134 Component radiobox_;
135 std::string title_;
136 };
137
138 return Make<Impl>(option);
139}
140
141} // namespace ftxui
An adapter. Reference a list of strings.
Definition ref.hpp:116
An adapter. Own or reference an mutable object.
Definition ref.hpp:46
struct Mouse mouse
Definition event.hpp:144
ConstStringListRef entries
Button button
Definition mouse.hpp:30
Motion motion
Definition mouse.hpp:33
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
Component Dropdown(ConstStringListRef entries, int *selected)
A dropdown menu.
Option for the Dropdown component.A dropdown menu is a checkbox opening/closing a radiobox.
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:29
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
std::shared_ptr< Node > Element
Definition elements.hpp:22
@ LESS_THAN
Definition elements.hpp:162
std::shared_ptr< ComponentBase > Component
arguments for transform from |ButtonOption|, |CheckboxOption|, |RadioboxOption|, |MenuEntryOption|,...