FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
src/ftxui/component/checkbox.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// このソースコードの使用は、LICENSEファイルにあるMITライセンスによって管理されています。
3#include <functional> // for function
4#include <utility> // for move
5
6#include "ftxui/component/component.hpp" // for Make, Checkbox
7#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
8#include "ftxui/component/component_options.hpp" // for CheckboxOption, EntryState
9#include "ftxui/component/event.hpp" // for Event, Event::Return
10#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
11#include "ftxui/dom/elements.hpp" // for operator|, Element, reflect, focus, nothing, select
12#include "ftxui/screen/box.hpp" // for Box
13#include "ftxui/util/ref.hpp" // for Ref, ConstStringRef
14
15namespace ftxui {
16
17namespace {
18class CheckboxBase : public ComponentBase, public CheckboxOption {
19 public:
20 explicit CheckboxBase(CheckboxOption option)
21 : CheckboxOption(std::move(option)) {}
22
23 private:
24 // コンポーネントの実装。
25 Element OnRender() override {
26 const bool is_focused = Focused();
27 const bool is_active = Active();
28 auto entry_state = EntryState{
29 *label, *checked, is_active, is_focused || hovered_, -1,
30 };
31 auto element = (transform ? transform : CheckboxOption::Simple().transform)(
32 entry_state);
33 element |= focus;
34 element |= reflect(box_);
35 return element;
36 }
37
38 bool OnEvent(Event event) override {
39 if (!CaptureMouse(event)) {
40 return false;
41 }
42
43 if (event.is_mouse()) {
44 return OnMouseEvent(event);
45 }
46
47 hovered_ = false;
48 if (event == Event::Character(' ') || event == Event::Return) {
49 *checked = !*checked;
50 on_change();
51 TakeFocus();
52 return true;
53 }
54 return false;
55 }
56
57 bool OnMouseEvent(Event event) {
58 hovered_ = box_.Contain(event.mouse().x, event.mouse().y);
59
60 if (!CaptureMouse(event)) {
61 return false;
62 }
63
64 if (!hovered_) {
65 return false;
66 }
67
68 if (event.mouse().button == Mouse::Left &&
69 event.mouse().motion == Mouse::Pressed) {
70 *checked = !*checked;
71 on_change();
72 return true;
73 }
74
75 return false;
76 }
77
78 bool Focusable() const final { return true; }
79
80 bool hovered_ = false;
81 Box box_;
82};
83} // namespace
84
85/// @brief チェック可能な要素を描画します。
86/// @param option 追加のオプションパラメーター。
87/// @ingroup component
88/// @see CheckboxBase
89///
90/// ### 例
91///
92/// ```cpp
93/// auto screen = ScreenInteractive::FitComponent();
94/// CheckboxOption option;
95/// option.label = "Make a sandwidth";
96/// option.checked = false;
97/// Component checkbox = Checkbox(option);
98/// screen.Loop(checkbox)
99/// ```
100///
101/// ### 出力
102///
103/// ```bash
104/// ☐ Make a sandwitch
105/// ```
106// NOLINTNEXTLINE
108 return Make<CheckboxBase>(std::move(option));
109}
110
111/// @brief チェック可能な要素を描画します。
112/// @param label チェックボックスのラベル。
113/// @param checked チェックボックスがチェックされているかどうか。
114/// @param option 追加のオプションパラメーター。
115/// @ingroup component
116/// @see CheckboxBase
117///
118/// ### 例
119///
120/// ```cpp
121/// auto screen = ScreenInteractive::FitComponent();
122/// std::string label = "Make a sandwidth";
123/// bool checked = false;
124/// Component checkbox = Checkbox(&label, &checked);
125/// screen.Loop(checkbox)
126/// ```
127///
128/// ### 出力
129///
130/// ```bash
131/// ☐ Make a sandwitch
132/// ```
133// NOLINTNEXTLINE
134Component Checkbox(ConstStringRef label, bool* checked, CheckboxOption option) {
135 option.label = std::move(label);
136 option.checked = checked;
137 return Make<CheckboxBase>(std::move(option));
138}
139
140} // namespace ftxui
アダプター。定数文字列を所有または参照します。便宜上、このクラスは複数の不変文字列を共有表現に変換します。
Definition ref.hpp:91
static CheckboxOption Simple()
標準チェックボックスのオプション。
static const Event Return
Definition event.hpp:52
std::function< Element(const EntryState &)> transform
Component Checkbox(CheckboxOption options)
チェック可能な要素を描画します。
Checkboxコンポーネントのオプション。
Element focus(Element)
子要素を兄弟要素の中でフォーカスされたものとして設定します。
Definition frame.cpp:101
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
Decorator reflect(Box &box)
Definition reflect.cpp:42
std::shared_ptr< ComponentBase > Component