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// このソースコードの使用は、LICENSEファイルにあるMITライセンスによって管理されています。
4#include <functional> // for function
5#include <string> // for string
6
7#include <utility>
8#include "ftxui/component/component.hpp" // for Maybe, Checkbox, Make, Radiobox, Vertical, Dropdown
9#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
10#include "ftxui/component/component_options.hpp" // for CheckboxOption, EntryState
11#include "ftxui/dom/elements.hpp" // for operator|, Element, border, filler, operator|=, separator, size, text, vbox, frame, vscroll_indicator, hbox, HEIGHT, LESS_THAN, bold, inverted
12#include "ftxui/screen/util.hpp" // for clamp
13#include "ftxui/util/ref.hpp" // for ConstStringListRef
14
15namespace ftxui {
16
17/// @brief ドロップダウンメニュー。
18/// @ingroup component
19/// @param entries 表示するエントリのリスト。
20/// @param selected 選択されたエントリのインデックス。
21Component Dropdown(ConstStringListRef entries, int* selected) {
22 DropdownOption option;
23 option.radiobox.entries = std::move(entries);
24 option.radiobox.selected = selected;
25 return Dropdown(option);
26}
27
28/// @brief ドロップダウンメニュー。
29/// @ingroup component
30/// @param option ドロップダウンのオプション。
31// NOLINTNEXTLINE
33 class Impl : public ComponentBase, public DropdownOption {
34 public:
35 explicit Impl(DropdownOption option) : DropdownOption(std::move(option)) {
36 FillDefault();
37 checkbox_ = Checkbox(checkbox);
38 radiobox_ = Radiobox(radiobox);
39
40 Add(Container::Vertical({
41 checkbox_,
42 Maybe(radiobox_, checkbox.checked),
43 }));
44 }
45
46 Element OnRender() override {
47 selected_ =
48 util::clamp(radiobox.selected(), 0, int(radiobox.entries.size()) - 1);
49 selected_ = util::clamp(selected_(), 0, int(radiobox.entries.size()) - 1);
50
51 if (selected_() >= 0 && selected_() < int(radiobox.entries.size())) {
52 title_ = radiobox.entries[selected_()];
53 }
54
55 return transform(*open_, checkbox_->Render(), radiobox_->Render());
56 }
57
58 // 選択時にチェックボックスとラジオボックスの間でフォーカスを切り替えます。
59 bool OnEvent(ftxui::Event event) override {
60 const bool open_old = open_();
61 const int selected_old = selected_();
62 bool handled = ComponentBase::OnEvent(event);
63
64 // ドロップダウンが開かれたときにラジオボックスにフォーカスを移動します。
65 if (!open_old && open_()) {
66 radiobox_->TakeFocus();
67 }
68
69 // ユーザーがアイテムを選択したときに、以前と同じアイテムであってもドロップダウンを自動的に閉じます。
70 if (open_old && open_()) {
71 const bool should_close =
72 (selected_() != selected_old) || //
73 (event == Event::Return) || //
74 (event == Event::Character(' ')) || //
75 (event == Event::Escape) || //
76 (event.is_mouse() && event.mouse().button == Mouse::Left &&
77 event.mouse().motion == Mouse::Pressed);
78
79 if (should_close) {
80 checkbox_->TakeFocus();
81 open_ = false;
82 handled = true;
83 }
84 }
85
86 return handled;
87 }
88
89 void FillDefault() {
90 open_ = checkbox.checked;
91 selected_ = radiobox.selected;
92 checkbox.checked = &*open_;
93 radiobox.selected = &*selected_;
94 checkbox.label = &title_;
95
96 if (!checkbox.transform) {
97 checkbox.transform = [](const EntryState& s) {
98 auto prefix = text(s.state ? "↓ " : "→ "); // NOLINT
99 auto t = text(s.label);
100 if (s.active) {
101 t |= bold;
102 }
103 if (s.focused) {
104 t |= inverted;
105 }
106 return hbox({prefix, t});
107 };
108 }
109
110 if (!transform) {
111 transform = [](bool is_open, Element checkbox_element,
112 Element radiobox_element) {
113 if (is_open) {
114 const int max_height = 12;
115 return vbox({
116 std::move(checkbox_element),
117 separator(),
118 std::move(radiobox_element) | vscroll_indicator | frame |
119 size(HEIGHT, LESS_THAN, max_height),
120 }) |
121 border;
122 }
123 return vbox({std::move(checkbox_element), filler()}) | border;
124 };
125 }
126 }
127
128 private:
129 Ref<bool> open_;
130 Ref<int> selected_;
131 Component checkbox_;
132 Component radiobox_;
133 std::string title_;
134 };
135
136 return Make<Impl>(option);
137}
138
139} // namespace ftxui
アダプター。文字列のリストを参照します。
Definition ref.hpp:113
アダプター。可変オブジェクトを所有または参照します。
Definition ref.hpp:45
struct Mouse mouse
Definition event.hpp:143
ConstStringListRef entries
Button button
Definition mouse.hpp:30
Motion motion
Definition mouse.hpp:33
ftxui::Elementとして自身のレンダリングを実装します。ftxui::Eventに応答してキーボードナビゲーションを実装します。
Component Dropdown(ConstStringListRef entries, int *selected)
ドロップダウンメニュー。
ドロップダウンコンポーネントのオプション。ドロップダウンメニューは、ラジオボックスを開閉するチェックボックスです。
イベントを表します。キープレスイベント、ターミナルのリサイズなど、さまざまなイベントがあります。
Definition event.hpp:28
FTXUI ftxui:: 名前空間
Definition animation.hpp:9
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
std::shared_ptr< Node > Element
Definition elements.hpp:21
@ LESS_THAN
Definition elements.hpp:157
std::shared_ptr< ComponentBase > Component
return size
Definition string.cpp:1516
|ButtonOption|、|CheckboxOption|、|RadioboxOption|、|MenuEntryOption|、|MenuOption|からの変換の引数。