FTXUI 7.1.0
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
33Component Dropdown(DropdownOption option) {
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 container_ = Container::Vertical({
42 checkbox_,
43 Maybe(radiobox_, checkbox.checked),
44 });
45 Add(container_);
46 }
47
48 Element OnRender() override {
49 selected_ =
50 util::clamp(radiobox.selected(), 0, int(radiobox.entries.size()) - 1);
51 selected_ = util::clamp(selected_(), 0, int(radiobox.entries.size()) - 1);
52
53 if (selected_() >= 0 && selected_() < int(radiobox.entries.size())) {
54 title_ = radiobox.entries[selected_()];
55 }
56
57 // Close the dropdown when another component takes the focus. This can
58 // happen without this dropdown receiving any event, e.g. when the user
59 // clicks on a sibling dropdown. Move the inner focus back to the
60 // checkbox without stealing the focus from the other component.
61 if (open_() && !Focused()) {
62 container_->SetActiveChild(checkbox_);
63 *open_ = false;
64 }
65
66 return transform(*open_, checkbox_->Render(), radiobox_->Render());
67 }
68
69 // Switch focus in between the checkbox and the radiobox when selecting it.
70 bool OnEvent(ftxui::Event event) override {
71 const bool open_old = open_();
72 const int selected_old = selected_();
73 bool handled = ComponentBase::OnEvent(event);
74
75 // Transfer focus to the radiobox when the dropdown is opened.
76 if (!open_old && open_()) {
77 radiobox_->TakeFocus();
78 }
79
80 // Auto-close the dropdown when the user selects an item, even if the item
81 // it the same as the previous one.
82 if (open_old && open_()) {
83 const bool should_close =
84 (selected_() != selected_old) || //
85 (event == Event::Return) || //
86 (event == Event::Character(' ')) || //
87 (event == Event::Escape) || //
88 (event.is_mouse() && event.mouse().button == Mouse::Left &&
89 event.mouse().motion == Mouse::Pressed);
90
91 if (should_close) {
92 checkbox_->TakeFocus();
93 *open_ = false;
94 handled = true;
95 }
96 }
97
98 return handled;
99 }
100
101 void FillDefault() {
102 open_ = checkbox.checked;
103 selected_ = radiobox.selected;
104 checkbox.checked = &*open_;
105 radiobox.selected = &*selected_;
106 checkbox.label = &title_;
107
108 if (!checkbox.transform) {
109 checkbox.transform = [](const EntryState& s) {
110 auto prefix = text(s.state ? "↓ " : "→ "); // NOLINT
111 auto t = text(s.label);
112 if (s.active) {
113 t |= bold;
114 }
115 if (s.focused) {
116 t |= inverted;
117 }
118 return hbox({prefix, t});
119 };
120 }
121
122 if (!transform) {
123 transform = [](bool is_open, Element checkbox_element,
124 Element radiobox_element) {
125 if (is_open) {
126 const int max_height = 12;
127 return vbox({
128 std::move(checkbox_element),
129 separator(),
130 std::move(radiobox_element) | vscroll_indicator | frame |
131 size(HEIGHT, LESS_THAN, max_height),
132 }) |
133 border;
134 }
135 return vbox({std::move(checkbox_element), filler()}) | border;
136 };
137 }
138 }
139
140 private:
141 Ref<bool> open_;
142 Ref<int> selected_;
143 Component container_;
144 Component checkbox_;
145 Component radiobox_;
146 std::string title_;
147 };
148
149 return Make<Impl>(option);
150}
151
152} // namespace ftxui
An adapter. Own or reference an mutable object.
Definition ref.hpp:56
Component Dropdown(ConstStringListRef entries, int *selected)
A dropdown menu.
The FTXUI ftxui:: namespace.
Definition animation.hpp:11
@ LESS_THAN
Definition elements.hpp:194
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:28
std::shared_ptr< Node > Element
Definition elements.hpp:25
std::shared_ptr< ComponentBase > Component
Definition app.hpp:23