FTXUI  5.0.0
C++ functional terminal UI.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
collapsible.cpp
Go to the documentation of this file.
1// Copyright 2021 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <functional> // for function
5#include <memory> // for shared_ptr, allocator
6#include <utility> // for move
7
8#include "ftxui/component/component.hpp" // for Checkbox, Maybe, Make, Vertical, Collapsible
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|=, text, hbox, Element, bold, inverted
12#include "ftxui/util/ref.hpp" // for Ref, ConstStringRef
13
14namespace ftxui {
15
16/// @brief A collapsible component. It display a checkbox with an arrow. Once
17/// activated, the children is displayed.
18/// @param label The label of the checkbox.
19/// @param child The children to display.
20/// @param show Hold the state about whether the children is displayed or not.
21///
22/// ### Example
23/// ```cpp
24/// auto component = Collapsible("Show details", details);
25/// ```
26///
27/// ### Output
28/// ```
29///
30/// ▼ Show details
31/// <details component>
32///  ```
33// NOLINTNEXTLINE
34Component Collapsible(ConstStringRef label, Component child, Ref<bool> show) {
35 class Impl : public ComponentBase {
36 public:
37 Impl(ConstStringRef label, Component child, Ref<bool> show) : show_(show) {
38 CheckboxOption opt;
39 opt.transform = [](EntryState s) { // NOLINT
40 auto prefix = text(s.state ? "▼ " : "▶ "); // NOLINT
41 auto t = text(s.label);
42 if (s.active) {
43 t |= bold;
44 }
45 if (s.focused) {
46 t |= inverted;
47 }
48 return hbox({prefix, t});
49 };
51 Checkbox(label, show_.operator->(), opt),
52 Maybe(std::move(child), show_.operator->()),
53 }));
54 }
55 Ref<bool> show_;
56 };
57
58 return Make<Impl>(std::move(label), std::move(child), show);
59}
60
61} // namespace ftxui
Component Vertical(Components children)
A list of components, drawn one by one vertically and navigated vertically using up/down arrow key or...
Definition: container.cpp:317
Component Maybe(Component, const bool *show)
Decorate a component |child|. It is shown only when |show| is true.
Definition: maybe.cpp:75
Component Checkbox(CheckboxOption options)
std::shared_ptr< ComponentBase > Component
Element bold(Element)
Use a bold font, for elements with more emphasis.
Definition: bold.cpp:33
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition: hbox.cpp:83
Element inverted(Element)
Add a filter that will invert the foreground and the background colors.
Definition: inverted.cpp:34
Element text(std::wstring text)
Display a piece of unicode text.
Definition: text.cpp:120
Component Collapsible(ConstStringRef label, Component child, Ref< bool > show=false)