FTXUI/src/ftxui/component/collapsible.cpp

63 lines
2.0 KiB
C++
Raw Normal View History

2022-03-31 08:17:43 +08:00
#include <functional> // for function
#include <memory> // for shared_ptr, allocator
#include <utility> // for move
2022-01-02 22:48:56 +08:00
2022-01-07 18:03:54 +08:00
#include "ftxui/component/component.hpp" // for Checkbox, Maybe, Make, Vertical, Collapsible
#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
2022-03-31 08:17:43 +08:00
#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
2022-01-02 22:48:56 +08:00
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.
2022-01-07 18:03:54 +08:00
///
2022-01-02 22:48:56 +08:00
/// ### Example
/// ```cpp
/// auto component = Collapsible("Show details", details);
/// ```
///
/// ### Output
/// ```
2022-01-07 18:03:54 +08:00
///
2022-01-02 22:48:56 +08:00
/// ▼ Show details
/// <details component>
2022-12-20 01:51:25 +08:00
///  ```
// NOLINTNEXTLINE
2022-01-07 18:03:54 +08:00
Component Collapsible(ConstStringRef label, Component child, Ref<bool> show) {
2022-01-02 22:48:56 +08:00
class Impl : public ComponentBase {
public:
2022-03-31 08:17:43 +08:00
Impl(ConstStringRef label, Component child, Ref<bool> show) : show_(show) {
2022-01-07 18:03:54 +08:00
CheckboxOption opt;
2022-03-31 08:17:43 +08:00
opt.transform = [](EntryState s) { // NOLINT
auto prefix = text(s.state ? "" : ""); // NOLINT
2022-03-14 01:51:46 +08:00
auto t = text(s.label);
2022-03-31 08:17:43 +08:00
if (s.active) {
2022-03-14 01:51:46 +08:00
t |= bold;
2022-03-31 08:17:43 +08:00
}
if (s.focused) {
2022-03-14 01:51:46 +08:00
t |= inverted;
2022-03-31 08:17:43 +08:00
}
2022-03-14 01:51:46 +08:00
return hbox({prefix, t});
};
2022-01-07 18:03:54 +08:00
Add(Container::Vertical({
Checkbox(label, show_.operator->(), opt),
2022-01-07 18:03:54 +08:00
Maybe(std::move(child), show_.operator->()),
}));
2022-01-02 22:48:56 +08:00
}
Ref<bool> show_;
};
2022-03-31 08:17:43 +08:00
return Make<Impl>(std::move(label), std::move(child), show);
2022-01-02 22:48:56 +08:00
}
} // namespace ftxui
// 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.