FTXUI  0.11.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
dropdown.cpp
Go to the documentation of this file.
1#include <memory> // for __shared_ptr_access
2#include <string> // for string
3#include <utility> // for move
4
5#include "ftxui/component/component.hpp" // for Maybe, Checkbox, Make, Radiobox, Vertical, Dropdown
6#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
7#include "ftxui/component/component_options.hpp" // for CheckboxOption
8#include "ftxui/dom/elements.hpp" // for operator|, Element, border, filler, separator, size, vbox, frame, vscroll_indicator, HEIGHT, LESS_THAN
9#include "ftxui/util/ref.hpp" // for ConstStringListRef
10
11namespace ftxui {
12
13Component Dropdown(ConstStringListRef entries, int* selected) {
14 class Impl : public ComponentBase {
15 public:
16 Impl(ConstStringListRef entries, int* selected)
17 : entries_(std::move(entries)), selected_(selected) {
18 CheckboxOption option;
19 option.style_checked = "↓│";
20 option.style_unchecked = "→│";
21 checkbox_ = Checkbox(&title_, &show_, option),
22 radiobox_ = Radiobox(entries_, selected_);
23
24 Add(Container::Vertical({
25 checkbox_,
26 Maybe(radiobox_, &show_),
27 }));
28 }
29
30 Element Render() override {
31 title_ = entries_[*selected_];
32 if (show_) {
33 return vbox({
34 checkbox_->Render(),
35 separator(),
36 radiobox_->Render() | vscroll_indicator | frame |
37 size(HEIGHT, LESS_THAN, 12),
38 }) |
39 border;
40 }
41
42 return vbox({
43 checkbox_->Render() | border,
44 filler(),
45 });
46 }
47
48 private:
49 ConstStringListRef entries_;
50 bool show_ = false;
51 int* selected_;
52 std::string title_;
53 Component checkbox_;
54 Component radiobox_;
55 };
56
57 return Make<Impl>(std::move(entries), selected);
58}
59
60} // namespace ftxui
61
62// Copyright 2021 Arthur Sonzogni. All rights reserved.
63// Use of this source code is governed by the MIT license that can be found in
64// the LICENSE file.
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
An adapter. Reference a list of strings.
Definition ref.hpp:94
Component Checkbox(ConstStringRef label, bool *checked, Ref< CheckboxOption > option={})
Draw checkable element.
Definition checkbox.cpp:117
Component Radiobox(ConstStringListRef entries, int *selected_, Ref< RadioboxOption > option={})
A list of element, where only one can be selected.
Definition radiobox.cpp:190
std::string style_unchecked
Prefix for a "unchecked" state.
Element vscroll_indicator(Element)
Add a filter that will invert the foreground and the background colors.
@ HEIGHT
Definition elements.hpp:94
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:25
std::shared_ptr< Node > Element
Definition elements.hpp:15
std::string style_checked
Prefix for a "checked" state.
Component Dropdown(ConstStringListRef entries, int *selected)
Definition dropdown.cpp:13
Element separator(void)
Draw a vertical or horizontal separation in between two other elements.
Element filler()
An element that will take expand proportionnally to the space left in a container.
Definition flex.cpp:94
Element frame(Element)
Allow an element to be displayed inside a 'virtual' area. It size can be larger than its container....
Definition frame.cpp:138
void Render(Screen &screen, const Element &node)
Display an element on a ftxui::Screen.
Definition node.cpp:34
Decorator size(Direction, Constraint, int value)
Apply a constraint on the size of an element.
Definition size.cpp:86
@ LESS_THAN
Definition elements.hpp:95
Element border(Element)
Draw a border around the element.
Definition border.cpp:150
std::shared_ptr< ComponentBase > Component
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:77
Option for the Checkbox component.
Component Maybe(Component child, bool *show)
Definition show.cpp:3