FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
src/ftxui/component/collapsible.cpp
浏览该文件的文档.
1// Copyright 2021 Arthur Sonzogni. All rights reserved.
2// 本源代码的使用受 MIT 许可的约束,
3// 该许可可在 LICENSE 文件中找到。
4#include <functional> // for function
5#include <utility> // for move
6
7#include "ftxui/component/component.hpp" // for Checkbox, Maybe, Make, Vertical, Collapsible
8#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
9#include "ftxui/component/component_options.hpp" // for CheckboxOption, EntryState
10#include "ftxui/dom/elements.hpp" // for operator|=, text, hbox, Element, bold, inverted
11#include "ftxui/util/ref.hpp" // for Ref, ConstStringRef
12
13namespace ftxui {
14
15/// @brief 一个可折叠的组件。它显示一个带箭头的复选框。一旦激活,子组件就会显示。
16/// @param label 复选框的标签。
17/// @param child 要显示的子组件。
18/// @param show 保持子组件是否显示的状态。
19///
20/// ### 示例
21/// ```cpp
22/// auto component = Collapsible("显示详情", details);
23
24/// ```
25///
26/// ### 输出
27/// ```
28///
29/// ▼ 显示详情
30/// <details component>
31/// ```
32// NOLINTNEXTLINE
33Component Collapsible(ConstStringRef label, Component child, Ref<bool> show) {
34 class Impl : public ComponentBase {
35 public:
36 Impl(ConstStringRef label, Component child, Ref<bool> show) : show_(show) {
37 CheckboxOption opt;
38 opt.transform = [](EntryState s) { // NOLINT
39 auto prefix = text(s.state ? "▼ " : "▶ "); // NOLINT
40 auto t = text(s.label);
41 if (s.active) {
42 t |= bold;
43 }
44 if (s.focused) {
45 t |= inverted;
46 }
47 return hbox({prefix, t});
48 };
49 Add(Container::Vertical({
50 Checkbox(std::move(label), show_.operator->(), opt),
51 Maybe(std::move(child), show_.operator->()),
52 }));
53 }
54 Ref<bool> show_;
55 };
56
57 return Make<Impl>(std::move(label), std::move(child), show);
58}
59
60} // namespace ftxui
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
std::shared_ptr< T > Make(Args &&... args)
std::shared_ptr< ComponentBase > Component
Component Collapsible(ConstStringRef label, Component child, Ref< bool > show=false)