37class SliderBase :
public SliderOption<T>,
public ComponentBase {
39 explicit SliderBase(SliderOption<T> options) : SliderOption<T>(options) {}
43 Focused() ?
color(this->color_active) : color(this->color_inactive);
45 float(this->value() - this->min()) / float(this->max() - this->min());
47 flexDirection(this->direction) |
reflect(gauge_box_) | gauge_color;
51 switch (this->direction) {
53 this->value() -= this->increment();
56 this->value() += this->increment();
65 switch (this->direction) {
67 this->value() += this->increment();
70 this->value() -= this->increment();
79 switch (this->direction) {
81 this->value() -= this->increment();
84 this->value() += this->increment();
93 switch (this->direction) {
95 this->value() += this->increment();
98 this->value() -= this->increment();
106 bool OnEvent(Event event)
final {
107 if (event.is_mouse()) {
108 return OnMouseEvent(event);
111 T old_value = this->value();
125 this->value() = std::max(this->min(), std::min(this->max(), this->value()));
126 if (old_value != this->value()) {
127 if (this->on_change) {
136 bool OnCapturedMouseEvent(Event event) {
138 captured_mouse_ =
nullptr;
142 T old_value = this->value();
143 switch (this->direction) {
145 this->value() = this->min() + (
event.mouse().x - gauge_box_.x_min) *
146 (this->max() - this->min()) /
147 (gauge_box_.x_max - gauge_box_.x_min);
152 this->value() = this->max() - (
event.mouse().x - gauge_box_.x_min) *
153 (this->max() - this->min()) /
154 (gauge_box_.x_max - gauge_box_.x_min);
158 this->value() = this->min() + (
event.mouse().y - gauge_box_.y_min) *
159 (this->max() - this->min()) /
160 (gauge_box_.y_max - gauge_box_.y_min);
164 this->value() = this->max() - (
event.mouse().y - gauge_box_.y_min) *
165 (this->max() - this->min()) /
166 (gauge_box_.y_max - gauge_box_.y_min);
171 this->value() = std::max(this->min(), std::min(this->max(), this->value()));
173 if (old_value != this->value() && this->on_change) {
179 bool OnMouseEvent(Event event) {
180 if (captured_mouse_) {
181 return OnCapturedMouseEvent(event);
191 if (!gauge_box_.Contain(event.mouse().x, event.mouse().y)) {
195 captured_mouse_ = CaptureMouse(event);
197 if (captured_mouse_) {
199 return OnCapturedMouseEvent(event);
205 bool Focusable() const final {
return true; }
212class SliderWithLabel :
public ComponentBase {
214 SliderWithLabel(ConstStringRef label,
Component inner)
215 : label_(std::move(label)) {
216 Add(std::move(inner));
217 SetActiveChild(ChildAt(0));
221 bool OnEvent(Event event)
final {
226 if (!event.is_mouse()) {
230 mouse_hover_ = box_.Contain(event.mouse().x, event.mouse().y);
236 if (!CaptureMouse(event)) {
246 auto element =
hbox({
254 gauge_color | xflex |
reflect(box_);
260 ConstStringRef label_;
262 bool mouse_hover_ =
false;
295 option.
value = value;
299 auto slider = Make<SliderBase<int>>(option);
309 option.
value = value;
313 auto slider = Make<SliderBase<float>>(option);
322 option.
value = value;
326 auto slider = Make<SliderBase<long>>(option);
347 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