38 class SliderBase :
public SliderOption<T>,
public ComponentBase {
40 explicit SliderBase(SliderOption<T> options) : SliderOption<T>(options) {}
44 Focused() ?
color(this->color_active) :
color(this->color_inactive);
46 float(this->value() - this->min()) / float(this->max() - this->min());
48 flexDirection(this->direction) |
reflect(gauge_box_) | gauge_color;
52 switch (this->direction) {
54 this->value() -= this->increment();
57 this->value() += this->increment();
66 switch (this->direction) {
68 this->value() += this->increment();
71 this->value() -= this->increment();
80 switch (this->direction) {
82 this->value() -= this->increment();
85 this->value() += this->increment();
94 switch (this->direction) {
96 this->value() += this->increment();
99 this->value() -= this->increment();
107 bool OnEvent(Event event)
final {
108 if (event.is_mouse()) {
109 return OnMouseEvent(event);
112 T old_value = this->value();
126 this->value() = std::max(this->min(), std::min(this->max(), this->value()));
127 if (old_value != this->value()) {
128 if (this->on_change) {
137 bool OnMouseEvent(Event event) {
138 if (captured_mouse_) {
140 captured_mouse_ =
nullptr;
144 T old_value = this->value();
145 switch (this->direction) {
148 this->min() + (
event.mouse().x - gauge_box_.x_min) *
149 (this->max() - this->min()) /
150 (gauge_box_.x_max - gauge_box_.x_min);
156 this->max() - (
event.mouse().x - gauge_box_.x_min) *
157 (this->max() - this->min()) /
158 (gauge_box_.x_max - gauge_box_.x_min);
163 this->min() + (
event.mouse().y - gauge_box_.y_min) *
164 (this->max() - this->min()) /
165 (gauge_box_.y_max - gauge_box_.y_min);
170 this->max() - (
event.mouse().y - gauge_box_.y_min) *
171 (this->max() - this->min()) /
172 (gauge_box_.y_max - gauge_box_.y_min);
178 std::max(this->min(), std::min(this->max(), this->value()));
180 if (old_value != this->value() && this->on_change) {
193 if (!gauge_box_.Contain(event.mouse().x, event.mouse().y)) {
197 captured_mouse_ = CaptureMouse(event);
199 if (captured_mouse_) {
207 bool Focusable() const final {
return true; }
214 class SliderWithLabel :
public ComponentBase {
216 SliderWithLabel(ConstStringRef label,
Component inner)
217 : label_(std::move(label)) {
218 Add(std::move(inner));
219 SetActiveChild(ChildAt(0));
223 bool OnEvent(Event event)
final {
228 if (!event.is_mouse()) {
232 mouse_hover_ = box_.Contain(event.mouse().x, event.mouse().y);
238 if (!CaptureMouse(event)) {
260 ConstStringRef label_;
262 bool mouse_hover_ =
false;
295 option.
value = value;
299 auto slider = Make<SliderBase<int>>(option);
300 return Make<SliderWithLabel>(std::move(label), slider);
309 option.
value = value;
313 auto slider = Make<SliderBase<float>>(option);
314 return Make<SliderWithLabel>(std::move(label), slider);
322 option.
value = value;
326 auto slider = Make<SliderBase<long>>(option);
327 return Make<SliderWithLabel>(std::move(label), slider);
345 template <
typename T>
347 return Make<SliderBase<T>>(options);
virtual Element Render()
Draw the component. Build a ftxui::Element to be drawn on the ftxi::Screen representing this ftxui::C...
virtual bool OnEvent(Event)
Called in response to an event.
An adapter. Own or reference an immutable object.
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
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< Node > Element
std::shared_ptr< ComponentBase > Component
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.
Element select(Element)
Set the child to be the one selected among its siblings.
Element focus(Element)
Set the child to be the one in focus globally.
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.
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Element vcenter(Element)
Center an element vertically.
Decorator color(Color)
Decorate using a foreground color.
static Event Character(std::string)
An event corresponding to a given typed character.
static const Event ArrowUp
static const Event ArrowDown
static const Event ArrowLeft
static const Event ArrowRight