51class SliderBase :
public SliderOption<T>,
public ComponentBase {
53 explicit SliderBase(SliderOption<T> options) : SliderOption<T>(options) {}
57 Focused() ?
color(this->color_active) : color(this->color_inactive);
59 float(this->value() - this->min()) / float(this->max() - this->min());
61 flexDirection(this->direction) |
reflect(gauge_box_) | gauge_color;
65 if (pressed == this->direction) {
66 this->value() += this->increment();
70 if (pressed == Opposite(this->direction)) {
71 this->value() -= this->increment();
76 bool OnEvent(Event event)
final {
77 if (event.is_mouse()) {
78 return OnMouseEvent(event);
81 T old_value = this->value();
95 this->value() = std::max(this->min(), std::min(this->max(), this->value()));
96 if (old_value != this->value()) {
97 if (this->on_change) {
106 bool OnCapturedMouseEvent(Event event) {
108 captured_mouse_ =
nullptr;
112 T old_value = this->value();
113 switch (this->direction) {
115 this->value() = this->min() + (
event.mouse().x - gauge_box_.x_min) *
116 (this->max() - this->min()) /
117 (gauge_box_.x_max - gauge_box_.x_min);
122 this->value() = this->max() - (
event.mouse().x - gauge_box_.x_min) *
123 (this->max() - this->min()) /
124 (gauge_box_.x_max - gauge_box_.x_min);
128 this->value() = this->min() + (
event.mouse().y - gauge_box_.y_min) *
129 (this->max() - this->min()) /
130 (gauge_box_.y_max - gauge_box_.y_min);
134 this->value() = this->max() - (
event.mouse().y - gauge_box_.y_min) *
135 (this->max() - this->min()) /
136 (gauge_box_.y_max - gauge_box_.y_min);
141 this->value() = std::max(this->min(), std::min(this->max(), this->value()));
143 if (old_value != this->value() && this->on_change) {
149 bool OnMouseEvent(Event event) {
150 if (captured_mouse_) {
151 return OnCapturedMouseEvent(event);
161 if (!gauge_box_.Contain(event.mouse().x, event.mouse().y)) {
165 captured_mouse_ = CaptureMouse(event);
167 if (captured_mouse_) {
169 return OnCapturedMouseEvent(event);
175 bool Focusable() const final {
return true; }
182class SliderWithLabel :
public ComponentBase {
184 SliderWithLabel(ConstStringRef label,
Component inner)
185 : label_(std::move(label)) {
186 Add(std::move(inner));
187 SetActiveChild(ChildAt(0));
191 bool OnEvent(Event event)
final {
196 if (!event.is_mouse()) {
200 mouse_hover_ = box_.Contain(event.mouse().x, event.mouse().y);
206 if (!CaptureMouse(event)) {
216 auto element =
hbox({
224 gauge_color | xflex |
reflect(box_);
230 ConstStringRef label_;
232 bool mouse_hover_ =
false;
265 option.
value = value;
269 auto slider = Make<SliderBase<int>>(option);
279 option.
value = value;
283 auto slider = Make<SliderBase<float>>(option);
292 option.
value = value;
296 auto slider = Make<SliderBase<long>>(option);
317 return Make<SliderBase<T>>(options);
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.
Element Render()
Draw the component. Build a ftxui::Element to be drawn on the ftxui::Screen representing this ftxui::...
static const Event ArrowUp
static const Event ArrowDown
virtual bool OnEvent(Event)
Called in response to an event.
static const Event ArrowLeft
static const Event ArrowRight
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.
Direction
Direction is an enumeration that represents the four cardinal directions.
Element yflex(Element)
Expand/Minimize if possible/needed on the Y axis.
Element underlined(Element)
Underline the given element.
Element text(std::wstring text)
Display a piece of unicode text.
Element focus(Element)
Set the child to be the one focused among its siblings.
Element vcenter(Element)
Center an element vertically.
Decorator color(Color)
Decorate using a foreground color.
The FTXUI ftxui:: namespace.
std::function< Element(Element)> Decorator
std::unique_ptr< CapturedMouseInterface > CapturedMouse
std::shared_ptr< T > Make(Args &&... args)
std::shared_ptr< Node > Element
Element hbox(Elements)
A container displaying elements horizontally one by one.
Component Slider(SliderOption< T > options)
A slider in any direction.
Decorator reflect(Box &box)
std::shared_ptr< ComponentBase > Component