50class SliderBase :
public SliderOption<T>,
public ComponentBase {
52 explicit SliderBase(SliderOption<T> options)
53 : SliderOption<T>(std::move(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();
82 if (event == Event::ArrowLeft || event == Event::Character(
'h')) {
85 if (event == Event::ArrowRight || event == Event::Character(
'l')) {
88 if (event == Event::ArrowUp || event == Event::Character(
'k')) {
91 if (event == Event::ArrowDown || event == Event::Character(
'j')) {
95 this->
value() = std::max(this->min(), std::min(this->max(), this->
value()));
96 if (old_value != this->
value()) {
97 App::PostEventOrExecute(this->on_change);
101 return ComponentBase::OnEvent(event);
104 bool OnCapturedMouseEvent(Event event) {
105 if (event.mouse().motion == Mouse::Released) {
106 captured_mouse_ =
nullptr;
110 T old_value = this->
value();
111 switch (this->direction) {
113 this->
value() = this->min() + (
event.mouse().x - gauge_box_.x_min) *
114 (this->max() - this->min()) /
115 (gauge_box_.x_max - gauge_box_.x_min);
120 this->
value() = this->max() - (
event.mouse().x - gauge_box_.x_min) *
121 (this->max() - this->min()) /
122 (gauge_box_.x_max - gauge_box_.x_min);
126 this->
value() = this->min() + (
event.mouse().
y - gauge_box_.y_min) *
127 (this->max() - this->min()) /
128 (gauge_box_.y_max - gauge_box_.y_min);
132 this->
value() = this->max() - (
event.mouse().
y - gauge_box_.y_min) *
133 (this->max() - this->min()) /
134 (gauge_box_.y_max - gauge_box_.y_min);
139 this->
value() = std::max(this->min(), std::min(this->max(), this->
value()));
141 if (old_value != this->
value()) {
142 App::PostEventOrExecute(this->on_change);
147 bool OnMouseEvent(Event event) {
148 if (captured_mouse_) {
149 return OnCapturedMouseEvent(event);
152 if (event.mouse().button != Mouse::Left) {
155 if (event.mouse().motion != Mouse::Pressed) {
159 if (!gauge_box_.Contain(event.mouse().x, event.mouse().y)) {
163 captured_mouse_ = CaptureMouse(event);
165 if (captured_mouse_) {
167 return OnCapturedMouseEvent(event);
173 bool Focusable() const final {
return true; }
180class SliderWithLabel :
public ComponentBase {
182 SliderWithLabel(ConstStringRef label,
Component inner)
183 : label_(std::move(label)) {
184 Add(std::move(inner));
185 SetActiveChild(ChildAt(0));
189 bool OnEvent(Event event)
final {
190 if (ComponentBase::OnEvent(event)) {
194 if (!event.is_mouse()) {
198 mouse_hover_ = box_.Contain(event.mouse().x, event.mouse().y);
204 if (!CaptureMouse(event)) {
212 auto gauge_color = (Focused() || mouse_hover_) ?
color(Color::White)
213 :
color(Color::GrayDark);
222 gauge_color | xflex |
reflect(box_);
228 ConstStringRef label_;
230 bool mouse_hover_ =
false;
267 auto slider = Make<SliderBase<int>>(option);
281 auto slider = Make<SliderBase<float>>(option);
294 auto slider = Make<SliderBase<long>>(option);
315 return Make<SliderBase<T>>(std::move(options));
An adapter. Own or reference an immutable object.
An adapter. Own or reference an mutable object.
#define FTXUI_EXPORT(component)
Element text(std::string_view text)
Display a piece of UTF8 encoded unicode text.
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 child)
Underline the given element.
Element focus(Element child)
Set the child to be the one focused among its siblings.
Element vcenter(Element child)
Center an element vertically.
Element color(Color color, Element child)
Set the foreground color of an element.
The FTXUI ftxui:: namespace.
std::unique_ptr< CapturedMouseInterface > CapturedMouse
std::shared_ptr< T > Make(Args &&... args)
std::shared_ptr< Node > Element
Element hbox(Elements children)
A container displaying elements horizontally one by one.
std::function< Element(Element)> Decorator
Component Slider(SliderOption< T > options)
A slider in any direction.
Decorator reflect(Box &box)
std::shared_ptr< ComponentBase > Component