FTXUI 7.0.3
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 下拉式選單。
19/// @ingroup component
20/// @param entries 要顯示的項目清單。
21/// @param selected 選定項目的索引。
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 下拉式選單。
30/// @ingroup component
31/// @param option 下拉式選單的選項。
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 // 當另一個元件取得焦點時,關閉下拉選單。這可能
58 // 在此下拉選單沒有收到任何事件的情況下發生,例如當使用者
59 // 點擊兄弟下拉選單時。將內部焦點移回
60 // 核取方塊,而不從其他元件搶走焦點。
61 if (open_() && !Focused()) {
62 container_->SetActiveChild(checkbox_);
63 *open_ = false;
64 }
65
66 return transform(*open_, checkbox_->Render(), radiobox_->Render());
67 }
68
69 // 選取核取方塊時,在核取方塊與單選方塊之間切換焦點。
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 // 當下拉選單開啟時,將焦點轉移到單選方塊。
76 if (!open_old && open_()) {
77 radiobox_->TakeFocus();
78 }
79
80 // 當使用者選擇一個項目時自動關閉下拉式選單,即使該項目與前一個相同。 if (open_old && open_()) {
81 if (open_old && open_()) {
82 const bool should_close =
83 (selected_() != selected_old) || //
84 (event == Event::Return) || //
85 (event == Event::Character(' ')) || //
86 (event == Event::Escape) || //
87 (event.is_mouse() && event.mouse().button == Mouse::Left &&
88 event.mouse().motion == Mouse::Pressed);
89
90 if (should_close) {
91 checkbox_->TakeFocus();
92 *open_ = false;
93 handled = true;
94 }
95 }
96
97 return handled;
98 }
99
100 void FillDefault() {
101 open_ = checkbox.checked;
102 selected_ = radiobox.selected;
103 checkbox.checked = &*open_;
104 radiobox.selected = &*selected_;
105 checkbox.label = &title_;
106
107 if (!checkbox.transform) {
108 checkbox.transform = [](const EntryState& s) {
109 auto prefix = text(s.state ? "↓ " : "→ "); // NOLINT
110 auto t = text(s.label);
111 if (s.active) {
112 t |= bold;
113 }
114 if (s.focused) {
115 t |= inverted;
116 }
117 return hbox({prefix, t});
118 };
119 }
120
121 if (!transform) {
122 transform = [](bool is_open, Element checkbox_element,
123 Element radiobox_element) {
124 if (is_open) {
125 const int max_height = 12;
126 return vbox({
127 std::move(checkbox_element),
128 separator(),
129 std::move(radiobox_element) | vscroll_indicator | frame |
130 size(HEIGHT, LESS_THAN, max_height),
131 }) |
132 border;
133 }
134 return vbox({std::move(checkbox_element), filler()}) | border;
135 };
136 }
137 }
138
139 private:
140 Ref<bool> open_;
141 Ref<int> selected_;
142 Component container_;
143 Component checkbox_;
144 Component radiobox_;
145 std::string title_;
146 };
147
148 return Make<Impl>(option);
149}
150
151} // namespace ftxui
一個適配器。擁有或引用一個可變的物件。
Definition ref.hpp:56
Component Dropdown(ConstStringListRef entries, int *selected)
下拉式選單。
FTXUI ftxui:: 命名空間
Definition animation.hpp:11
@ LESS_THAN
Definition elements.hpp:191
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