FTXUI  0.11.1
C++ functional terminal UI.
dropdown.cpp
Go to the documentation of this file.
1 #include <algorithm> // for max, min
2 #include <memory> // for __shared_ptr_access
3 #include <string> // for string
4 #include <utility> // for move
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
9 #include "ftxui/dom/elements.hpp" // for operator|, Element, border, filler, separator, size, vbox, frame, vscroll_indicator, HEIGHT, LESS_THAN
10 #include "ftxui/util/ref.hpp" // for ConstStringListRef
11 
12 namespace ftxui {
13 
14 Component Dropdown(ConstStringListRef entries, int* selected) {
15  class Impl : public ComponentBase {
16  public:
17  Impl(ConstStringListRef entries, int* selected)
18  : entries_(std::move(entries)), selected_(selected) {
19  CheckboxOption option;
20  option.style_checked = "↓│";
21  option.style_unchecked = "→│";
22  checkbox_ = Checkbox(&title_, &show_, option),
23  radiobox_ = Radiobox(entries_, selected_);
24 
26  checkbox_,
27  Maybe(radiobox_, &show_),
28  }));
29  }
30 
31  Element Render() override {
32  *selected_ = std::min((int)entries_.size() - 1, std::max(0, *selected_));
33  title_ = entries_[*selected_];
34  if (show_) {
35  return vbox({
36  checkbox_->Render(),
37  separator(),
38  radiobox_->Render() | vscroll_indicator | frame |
39  size(HEIGHT, LESS_THAN, 12),
40  }) |
41  border;
42  }
43 
44  return vbox({
45  checkbox_->Render() | border,
46  filler(),
47  });
48  }
49 
50  private:
51  ConstStringListRef entries_;
52  bool show_ = false;
53  int* selected_;
54  std::string title_;
55  Component checkbox_;
56  Component radiobox_;
57  };
58 
59  return Make<Impl>(std::move(entries), selected);
60 }
61 
62 } // namespace ftxui
63 
64 // Copyright 2021 Arthur Sonzogni. All rights reserved.
65 // Use of this source code is governed by the MIT license that can be found in
66 // 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 Vertical(Components children)
A list of components, drawn one by one vertically and navigated vertically using up/down arrow key or...
Definition: container.cpp:216
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:198
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:104
std::shared_ptr< Node > Element
Definition: elements.hpp:16
std::shared_ptr< ComponentBase > Component
std::string style_checked
Prefix for a "checked" state.
Component Dropdown(ConstStringListRef entries, int *selected)
Definition: dropdown.cpp:14
Component Maybe(Component, bool *show)
Definition: maybe.cpp:12
Element separator(void)
Draw a vertical or horizontal separation in between two other elements.
Definition: separator.cpp:114
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:40
Decorator size(Direction, Constraint, int value)
Apply a constraint on the size of an element.
Definition: size.cpp:86
@ LESS_THAN
Definition: elements.hpp:105
Element border(Element)
Draw a border around the element.
Definition: border.cpp:150
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition: vbox.cpp:77
Option for the Checkbox component.