FTXUI  4.1.1
C++ functional terminal UI.
Loading...
Searching...
No Matches
dropdown.cpp
Go to the documentation of this file.
1#include <algorithm> // for max, min
2#include <functional> // for function
3#include <memory> // for __shared_ptr_access, shared_ptr, allocator
4#include <string> // for string
5
6#include "ftxui/component/component.hpp" // for Maybe, Checkbox, Make, Radiobox, Vertical, Dropdown
7#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
8#include "ftxui/component/component_options.hpp" // for CheckboxOption, EntryState
9#include "ftxui/dom/elements.hpp" // for operator|, Element, border, filler, operator|=, separator, size, text, vbox, frame, vscroll_indicator, hbox, HEIGHT, LESS_THAN, bold, inverted
10#include "ftxui/util/ref.hpp" // for ConstStringListRef
11
12namespace ftxui {
13
14Component Dropdown(ConstStringListRef entries, int* selected) {
15 class Impl : public ComponentBase {
16 public:
17 Impl(ConstStringListRef entries, int* selected)
18 : entries_(entries), selected_(selected) {
19 CheckboxOption option;
20 option.transform = [](const EntryState& s) {
21 auto prefix = text(s.state ? "↓ " : "→ "); // NOLINT
22 auto t = text(s.label);
23 if (s.active) {
24 t |= bold;
25 }
26 if (s.focused) {
27 t |= inverted;
28 }
29 return hbox({prefix, t});
30 };
31 checkbox_ = Checkbox(&title_, &show_, option),
32 radiobox_ = Radiobox(entries_, selected_);
33
34 Add(Container::Vertical({
35 checkbox_,
36 Maybe(radiobox_, &show_),
37 }));
38 }
39
40 Element Render() override {
41 *selected_ = std::min((int)entries_.size() - 1, std::max(0, *selected_));
42 title_ = entries_[*selected_];
43 if (show_) {
44 const int max_height = 12;
45 return vbox({
46 checkbox_->Render(),
47 separator(),
48 radiobox_->Render() | vscroll_indicator | frame |
49 size(HEIGHT, LESS_THAN, max_height),
50 }) |
51 border;
52 }
53
54 return vbox({
55 checkbox_->Render() | border,
56 filler(),
57 });
58 }
59
60 private:
61 ConstStringListRef entries_;
62 bool show_ = false;
63 int* selected_;
64 std::string title_;
65 Component checkbox_;
66 Component radiobox_;
67 };
68
69 return Make<Impl>(entries, selected);
70}
71
72} // namespace ftxui
73
74// Copyright 2021 Arthur Sonzogni. All rights reserved.
75// Use of this source code is governed by the MIT license that can be found in
76// 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:83
Component Checkbox(ConstStringRef label, bool *checked, Ref< CheckboxOption > option=CheckboxOption::Simple())
Draw checkable element.
Definition checkbox.cpp:112
Component Radiobox(ConstStringListRef entries, int *selected_, Ref< RadioboxOption > option={})
A list of element, where only one can be selected.
Definition radiobox.cpp:211
Element vscroll_indicator(Element)
Add a filter that will invert the foreground and the background colors.
Component Maybe(Component, const bool *show)
Decorate a component |child|. It is shown only when |show| is true. @params child the compoennt to de...
Definition maybe.cpp:67
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:25
std::shared_ptr< Node > Element
Definition elements.hpp:19
Element bold(Element)
Use a bold font, for elements with more emphasis.
Definition bold.cpp:28
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:77
Element inverted(Element)
Add a filter that will invert the foreground and the background colors.
Definition inverted.cpp:29
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:111
Component Dropdown(ConstStringListRef entries, int *selected)
Definition dropdown.cpp:14
Element separator()
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:95
Element frame(Element)
Allow an element to be displayed inside a 'virtual' area. It size can be larger than its container....
Definition frame.cpp:142
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:44
Decorator size(Direction, Constraint, int value)
Apply a constraint on the size of an element.
Definition size.cpp:85
@ LESS_THAN
Definition elements.hpp:145
Element border(Element)
Draw a border around the element.
Definition border.cpp:222
std::shared_ptr< ComponentBase > Component
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:78
arguments for |ButtonOption::transform|, |CheckboxOption::transform|, |Radiobox::transform|,...
Option for the Checkbox component.
std::function< Element(const EntryState &)> transform