FTXUI  5.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
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 <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 A collapsible component. It display a checkbox with an arrow. Once
16/// activated, the children is displayed.
17/// @param label The label of the checkbox.
18/// @param child The children to display.
19/// @param show Hold the state about whether the children is displayed or not.
20///
21/// ### Example
22/// ```cpp
23/// auto component = Collapsible("Show details", details);
24/// ```
25///
26/// ### Output
27/// ```
28///
29/// ▼ Show details
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 };
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
Component Vertical(Components children)
A list of components, drawn one by one vertically and navigated vertically using up/down arrow key or...
Component Maybe(Component, const bool *show)
Decorate a component |child|. It is shown only when |show| is true.
Definition maybe.cpp:74
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
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:96
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:159
Component Checkbox(CheckboxOption options)
Draw checkable element.
Definition checkbox.cpp:108
Component Collapsible(ConstStringRef label, Component child, Ref< bool > show=false)