36class SliderBase :
public ComponentBase {
38 explicit SliderBase(Ref<SliderOption<T>> options)
39 : value_(options->value),
42 increment_(options->increment),
46 auto gauge_color =
Focused() ?
color(options_->color_active)
47 :
color(options_->color_inactive);
48 const float percent = float(value_() - min_()) / float(max_() - min_());
50 flexDirection(options_->direction) |
reflect(gauge_box_) |
55 switch (options_->direction) {
57 value_() -= increment_();
60 value_() += increment_();
69 switch (options_->direction) {
71 value_() += increment_();
74 value_() -= increment_();
83 switch (options_->direction) {
85 value_() -= increment_();
88 value_() += increment_();
97 switch (options_->direction) {
99 value_() -= increment_();
102 value_() += increment_();
110 bool OnEvent(Event event)
final {
111 if (event.is_mouse()) {
112 return OnMouseEvent(event);
115 T old_value = value_();
130 if (old_value != value_()) {
137 bool OnMouseEvent(Event event) {
139 captured_mouse_ =
nullptr;
143 if (gauge_box_.
Contain(event.mouse().x, event.mouse().y) &&
150 gauge_box_.
Contain(event.mouse().x, event.mouse().y) &&
155 if (captured_mouse_) {
156 switch (options_->direction) {
158 value_() = min_() + (
event.mouse().x - gauge_box_.
x_min) *
164 value_() = max_() - (
event.mouse().x - gauge_box_.
x_min) *
170 value_() = min_() + (
event.mouse().y - gauge_box_.
y_min) *
176 value_() = max_() - (
event.mouse().y - gauge_box_.
y_min) *
182 value_() = std::max(min_(), std::min(max_(), value_()));
188 bool Focusable() const final {
return true; }
194 ConstRef<T> increment_;
195 Ref<SliderOption<T>> options_;
200class SliderWithLabel :
public ComponentBase {
202 SliderWithLabel(ConstStringRef label,
Component inner)
203 : label_(std::move(label)) {
204 Add(std::move(inner));
209 bool OnEvent(Event event)
final {
214 if (!event.is_mouse()) {
218 if (!box_.
Contain(event.mouse().x, event.mouse().y)) {
244 ConstStringRef label_;
276 option.
value = value;
290 option.
value = value;
303 option.
value = value;
bool Focused() const
Returns if the elements if focused by the user. True when the ComponentBase is focused by the user....
CapturedMouse CaptureMouse(const Event &event)
Take the CapturedMouse if available. There is only one component of them. It represents a component t...
void Add(Component children)
Add a child. @param child The child to be attached.
virtual Element Render()
Draw the component. Build a ftxui::Element to be drawn on the ftxi::Screen representing this ftxui::C...
void TakeFocus()
Configure all the ancestors to give focus to this component.
bool Active() const
Returns if the element if the currently active child of its parent.
virtual void SetActiveChild(ComponentBase *child)
Make the |child| to be the "active" one.
virtual bool OnEvent(Event)
Called in response to an event.
Component & ChildAt(size_t i)
Access the child at index i.
An adapter. Own or reference an immutable object.
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
An adapter. Own or reference an mutable object.
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
Element xflex(Element)
Expand/Minimize if possible/needed on the X axis.
Element gaugeDirection(float progress, Direction direction)
Draw a high definition progress bar progressing in specified direction.
std::function< Element(Element)> Decorator
Element nothing(Element element)
A decoration doing absolutely nothing.
std::unique_ptr< CapturedMouseInterface > CapturedMouse
std::shared_ptr< T > Make(Args &&... args)
std::shared_ptr< Node > Element
Element yflex(Element)
Expand/Minimize if possible/needed on the Y axis.
Element hbox(Elements)
A container displaying elements horizontally one by one.
Element underlined(Element)
Make the underlined element to be underlined.
Element text(std::wstring text)
Display a piece of unicode text.
Component Slider(SliderOption< T > options={})
A slider in any direction.
Decorator reflect(Box &box)
Element dim(Element)
Use a light font, for elements with less emphasis.
Element vcenter(Element)
Center an element vertically.
std::shared_ptr< ComponentBase > Component
Decorator color(Color)
Decorate using a foreground color.
bool Contain(int x, int y) const
static const Event ArrowUp
static const Event ArrowDown
static const Event ArrowLeft
static const Event ArrowRight