FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
src/ftxui/component/checkbox.cpp
浏览该文件的文档.
1// 版权所有 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 // Component implementation.
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/// @brief 绘制可勾选元素。
87/// @param option 额外的可选参数。
88/// @ingroup component
89/// @see CheckboxBase
90///
91/// ### 示例
92///
93/// ```cpp
94/// auto screen = ScreenInteractive::FitComponent();
95/// CheckboxOption option;
96/// option.label = "Make a sandwidth";
97/// option.checked = false;
98/// Component checkbox = Checkbox(option);
99/// screen.Loop(checkbox)
100/// ```
101///
102/// ### 输出
103///
104/// ```bash
105/// ☐ Make a sandwitch
106/// ```
107// NOLINTNEXTLINE
109 return Make<CheckboxBase>(std::move(option));
110}
111
112/// @brief 绘制可勾选元素。
113/// @param label 复选框的标签。
114/// @param checked 复选框是否被选中。
115/// @param option 额外的可选参数。
116/// @ingroup component
117/// @see CheckboxBase
118///
119/// ### 示例
120///
121/// ```cpp
122/// auto screen = ScreenInteractive::FitComponent();
123/// std::string label = "Make a sandwidth";
124/// bool checked = false;
125/// Component checkbox = Checkbox(&label, &checked);
126/// screen.Loop(checkbox)
127/// ```
128///
129/// ### 输出
130///
131/// ```bash
132/// ☐ Make a sandwitch
133/// ```
134// NOLINTNEXTLINE
135Component Checkbox(ConstStringRef label, bool* checked, CheckboxOption option) {
136 option.label = std::move(label);
137 option.checked = checked;
138 return Make<CheckboxBase>(std::move(option));
139}
140
141} // namespace ftxui
一个适配器。拥有或引用一个常量字符串。为了方便,这个 类将多个不可变字符串转换为共享表示。
定义 ref.hpp:94
static CheckboxOption Simple()
标准Checkbox的选项。
static const Event Return
std::function< Element(const EntryState &)> transform
Component Checkbox(CheckboxOption options)
绘制可勾选元素。
Checkbox 组件的选项。
Element focus(Element)
将 child 设置为其同级元素中获得焦点的元素。
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
std::shared_ptr< T > Make(Args &&... args)
std::shared_ptr< Node > Element
Decorator reflect(Box &box)
std::shared_ptr< ComponentBase > Component