FTXUI  4.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
collapsible.cpp
Go to the documentation of this file.
1#include <functional> // for function
2#include <memory> // for shared_ptr, allocator
3#include <utility> // for move
4
5#include "ftxui/component/component.hpp" // for Checkbox, Maybe, Make, Vertical, Collapsible
6#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
7#include "ftxui/component/component_options.hpp" // for CheckboxOption, EntryState
8#include "ftxui/dom/elements.hpp" // for operator|=, text, hbox, Element, bold, inverted
9#include "ftxui/util/ref.hpp" // for Ref, ConstStringRef
10
11namespace ftxui {
12
13/// @brief A collapsible component. It display a checkbox with an arrow. Once
14/// activated, the children is displayed.
15/// @params label The label of the checkbox.
16/// @params child The children to display.
17/// @params show Hold the state about whether the children is displayed or not.
18///
19/// ### Example
20/// ```cpp
21/// auto component = Collapsible("Show details", details);
22/// ```
23///
24/// ### Output
25/// ```
26///
27/// ▼ Show details
28/// <details component>
29///  ```
30Component Collapsible(ConstStringRef label, Component child, Ref<bool> show) {
31 class Impl : public ComponentBase {
32 public:
33 Impl(ConstStringRef label, Component child, Ref<bool> show) : show_(show) {
34 CheckboxOption opt;
35 opt.transform = [](EntryState s) { // NOLINT
36 auto prefix = text(s.state ? "▼ " : "▶ "); // NOLINT
37 auto t = text(s.label);
38 if (s.active) {
39 t |= bold;
40 }
41 if (s.focused) {
42 t |= inverted;
43 }
44 return hbox({prefix, t});
45 };
46 Add(Container::Vertical({
47 Checkbox(std::move(label), show_.operator->(), opt),
48 Maybe(std::move(child), show_.operator->()),
49 }));
50 }
51 Ref<bool> show_;
52 };
53
54 return Make<Impl>(std::move(label), std::move(child), show);
55}
56
57} // namespace ftxui
58
59// Copyright 2021 Arthur Sonzogni. All rights reserved.
60// Use of this source code is governed by the MIT license that can be found in
61// the LICENSE file.
Component Checkbox(ConstStringRef label, bool *checked, Ref< CheckboxOption > option=CheckboxOption::Simple())
Draw checkable element.
Definition checkbox.cpp:112
Component Maybe(Component, const bool *show)
Decorate a component |child|. It is shown only when |show| is true. @params child the compoennt to de...
Definition maybe.cpp:67
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:25
Element bold(Element)
Use a bold font, for elements with more emphasis.
Definition bold.cpp:28
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:77
Element inverted(Element)
Add a filter that will invert the foreground and the background colors.
Definition inverted.cpp:29
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:111
std::shared_ptr< ComponentBase > Component
Component Collapsible(ConstStringRef label, Component child, Ref< bool > show=false)