FTXUI 6.1.9 C++ functional terminal UI.

Loading...
Searching...
No Matches
src/ftxui/component/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
13 namespace ftxui {
14
15/// @brief A collapsible component. It displays a checkbox with an arrow. Once
16/// activated, the child is displayed.
17/// @param label The label of the checkbox.
18/// @param child The child to display.
19/// @param show Hold the state about whether the child 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
33 Component 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
ftxui::ConstStringRef
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition ref.hpp:94
ftxui::Ref
An adapter. Own or reference an mutable object.
Definition ref.hpp:46
component.hpp
component_base.hpp
component_options.hpp
elements.hpp
ftxui::CheckboxOption::transform
std::function< Element(const EntryState &)> transform
Definition component_options.hpp:145
ftxui::ComponentBase
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
Definition component_base.hpp:30
ftxui::CheckboxOption
Option for the Checkbox component.
Definition component_options.hpp:136
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::Make
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
ftxui::Collapsible
Component Collapsible(ConstStringRef label, Component child, Ref< bool > show=false)
A collapsible component. It displays a checkbox with an arrow. Once activated, the child is displayed...
Definition src/ftxui/component/collapsible.cpp:33
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24
ref.hpp
ftxui::EntryState
arguments for transform from |ButtonOption|, |CheckboxOption|, |RadioboxOption|, |MenuEntryOption|,...
Definition component_options.hpp:22