# FTXUI: collapsible.cpp Source File

Loading...
Searching...
No Matches
src/ftxui/component/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 displays a checkbox with an arrow. Once
/// activated, the child is displayed.
/// @param label The label of the checkbox.
/// @param child The child to display.
/// @param show Hold the state about whether the child 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::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:146
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:137
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::Make
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:27
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:23