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. 保留所有權利。
2// 本原始碼的使用受 MIT 授權條款約束,該條款可在
3// LICENSE 文件中找到。
4#include <functional> // for function
5#include <utility> // for move
6
7#include "ftxui/component/component.hpp" // for Make, Checkbox
8#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
9#include "ftxui/component/component_options.hpp" // for CheckboxOption, EntryState
10#include "ftxui/component/event.hpp" // for Event, Event::Return
11#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
12#include "ftxui/dom/elements.hpp" // for operator|, Element, reflect, focus, nothing, select
13#include "ftxui/screen/box.hpp" // for Box
14#include "ftxui/util/ref.hpp" // for Ref, ConstStringRef
15
16namespace ftxui {
17
18namespace {
19class CheckboxBase : public ComponentBase, public CheckboxOption {
20 public:
21 explicit CheckboxBase(CheckboxOption option)
22 : CheckboxOption(std::move(option)) {}
23
24 private:
25 // 元件實作。
26 Element OnRender() override {
27 const bool is_focused = Focused();
28 const bool is_active = Active();
29 auto entry_state = EntryState{
30 *label, *checked, is_active, is_focused || hovered_, -1,
31 };
32 auto element = (transform ? transform : CheckboxOption::Simple().transform)(
33 entry_state);
34 element |= focus;
35 element |= reflect(box_);
36 return element;
37 }
38
39 bool OnEvent(Event event) override {
40 if (!CaptureMouse(event)) {
41 return false;
42 }
43
44 if (event.is_mouse()) {
45 return OnMouseEvent(event);
46 }
47
48 hovered_ = false;
49 if (event == Event::Character(' ') || event == Event::Return) {
50 *checked = !*checked;
51 on_change();
52 TakeFocus();
53 return true;
54 }
55 return false;
56 }
57
58 bool OnMouseEvent(Event event) {
59 hovered_ = box_.Contain(event.mouse().x, event.mouse().y);
60
61 if (!CaptureMouse(event)) {
62 return false;
63 }
64
65 if (!hovered_) {
66 return false;
67 }
68
69 if (event.mouse().button == Mouse::Left &&
70 event.mouse().motion == Mouse::Pressed) {
71 *checked = !*checked;
72 on_change();
73 return true;
74 }
75
76 return false;
77 }
78
79 bool Focusable() const final { return true; }
80
81 bool hovered_ = false;
82 Box box_;
83};
84} // namespace
85
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 = "製作一個三明治";
96/// option.checked = false;
97/// Component checkbox = Checkbox(option);
98/// screen.Loop(checkbox)
99/// ```
100///
101/// ### 輸出
102///
103/// ```bash
104/// ☐ 製作一個三明治
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 = "製作一個三明治";
123/// bool checked = false;
124/// Component checkbox = Checkbox(&label, &checked);
125/// screen.Loop(checkbox)
126/// ```
127///
128/// ### 輸出
129///
130/// ```bash
131/// ☐ 製作一個三明治
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:92
static CheckboxOption Simple()
標準Checkbox的選項。
static const Event Return
Definition event.hpp:51
std::function< Element(const EntryState &)> transform
Component Checkbox(CheckboxOption options)
核取方塊元件的選項。
Element focus(Element)
將 child 設置為其同級元素中被聚焦的元素。
Definition frame.cpp:101
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
std::shared_ptr< Node > Element
Definition elements.hpp:22
Decorator reflect(Box &box)
Definition reflect.cpp:42
std::shared_ptr< ComponentBase > Component