Loading...
Searching...
No Matches
collapsible.cpp
Go to the documentation of this file.
// Copyright 2021 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include <functional> // for function
#include <utility> // for move
#include "ftxui/component/component.hpp" // for Checkbox, Maybe, Make, Vertical, Collapsible
#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
#include "ftxui/component/component_options.hpp" // for CheckboxOption, EntryState
#include "ftxui/dom/elements.hpp" // for operator|=, text, hbox, Element, bold, inverted
#include "ftxui/util/ref.hpp" // for Ref, ConstStringRef
namespace ftxui {
/// @brief A collapsible component. It display a checkbox with an arrow. Once
/// activated, the children is displayed.
/// @param label The label of the checkbox.
/// @param child The children to display.
/// @param show Hold the state about whether the children is displayed or not.
///
/// ### Example
/// ```cpp
/// auto component = Collapsible("Show details", details);
/// ```
///
/// ### Output
/// ```
///
/// ▼ Show details
/// <details component>
/// ```
// NOLINTNEXTLINE
Component Collapsible(ConstStringRef label, Component child, Ref<bool> show) {
class Impl : public ComponentBase {
public:
Impl(ConstStringRef label, Component child, Ref<bool> show) : show_(show) {
CheckboxOption opt;
opt.transform = [](EntryState s) { // NOLINT
auto prefix = text(s.state ? "▼ " : "▶ "); // NOLINT
auto t = text(s.label);
if (s.active) {
t |= bold;
}
if (s.focused) {
t |= inverted;
}
return hbox({prefix, t});
};
Add(Container::Vertical({
Checkbox(std::move(label), show_.operator->(), opt),
Maybe(std::move(child), show_.operator->()),
}));
}
Ref<bool> show_;
};
return Make<Impl>(std::move(label), std::move(child), show);
}
} // namespace ftxui
ftxui::ComponentBase
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
Definition component_base.hpp:30
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
Definition animation.hpp:10
ftxui::Maybe
Component Maybe(Component, const bool *show)
Decorate a component |child|. It is shown only when |show| is true.
Definition maybe.cpp:74
ftxui::Make
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
ftxui::bold
Element bold(Element)
Use a bold font, for elements with more emphasis.
Definition bold.cpp:33
ftxui::hbox
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:94
ftxui::inverted
Element inverted(Element)
Add a filter that will invert the foreground and the background colors.
Definition inverted.cpp:34
ftxui::text
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
ftxui::Checkbox
Component Checkbox(CheckboxOption options)
Draw checkable element.
Definition checkbox.cpp:108
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24
ftxui::Collapsible
Component Collapsible(ConstStringRef label, Component child, Ref< bool > show=false)
ref.hpp