39class SliderBase :
public ComponentBase {
41 explicit SliderBase(SliderOption<T> options)
42 : value_(options.value),
45 increment_(options.increment),
49 auto gauge_color = Focused() ?
color(options_.color_active)
50 :
color(options_.color_inactive);
51 const float percent = float(value_() - min_()) / float(max_() - min_());
53 flexDirection(options_.direction) |
reflect(gauge_box_) |
58 switch (options_.direction) {
60 value_() -= increment_();
63 value_() += increment_();
72 switch (options_.direction) {
74 value_() += increment_();
77 value_() -= increment_();
86 switch (options_.direction) {
88 value_() -= increment_();
91 value_() += increment_();
100 switch (options_.direction) {
102 value_() -= increment_();
105 value_() += increment_();
113 bool OnEvent(Event event)
final {
114 if (event.is_mouse()) {
115 return OnMouseEvent(event);
118 T old_value = value_();
133 if (old_value != value_()) {
140 bool OnMouseEvent(Event event) {
141 if (captured_mouse_) {
143 captured_mouse_ =
nullptr;
147 switch (options_.direction) {
149 value_() = min_() + (
event.mouse().x - gauge_box_.x_min) *
151 (gauge_box_.x_max - gauge_box_.x_min);
155 value_() = max_() - (
event.mouse().x - gauge_box_.x_min) *
157 (gauge_box_.x_max - gauge_box_.x_min);
161 value_() = min_() + (
event.mouse().y - gauge_box_.y_min) *
163 (gauge_box_.y_max - gauge_box_.y_min);
167 value_() = max_() - (
event.mouse().y - gauge_box_.y_min) *
169 (gauge_box_.y_max - gauge_box_.y_min);
173 value_() = std::max(min_(), std::min(max_(), value_()));
177 if (!event.mouse().IsPressed()) {
181 if (!gauge_box_.Contain(event.mouse().x, event.mouse().y)) {
185 captured_mouse_ = CaptureMouse(event);
187 if (captured_mouse_) {
195 bool Focusable() const final {
return true; }
201 ConstRef<T> increment_;
202 SliderOption<T> options_;
207class SliderWithLabel :
public ComponentBase {
209 SliderWithLabel(ConstStringRef label,
Component inner)
210 : label_(std::move(label)) {
211 Add(std::move(inner));
212 SetActiveChild(ChildAt(0));
216 bool OnEvent(Event event)
final {
221 if (!event.is_mouse()) {
225 mouse_hover_ = box_.Contain(event.mouse().x, event.mouse().y);
231 if (!CaptureMouse(event)) {
253 ConstStringRef label_;
255 bool mouse_hover_ =
false;
287 option.
value = value;
291 auto slider = Make<SliderBase<int>>(option);
292 return Make<SliderWithLabel>(std::move(label), slider);
301 option.
value = value;
305 auto slider = Make<SliderBase<float>>(option);
306 return Make<SliderWithLabel>(std::move(label), slider);
314 option.
value = value;
318 auto slider = Make<SliderBase<long>>(option);
319 return Make<SliderWithLabel>(std::move(label), slider);
339 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...
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< 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