35class SliderBase :
public ComponentBase {
37 explicit SliderBase(Ref<SliderOption<T>> options)
38 : value_(options->value),
41 increment_(options->increment),
45 auto gauge_color =
Focused() ?
color(options_->color_active)
46 :
color(options_->color_inactive);
47 const float percent = float(value_() - min_()) / float(max_() - min_());
49 flexDirection(options_->direction) |
reflect(gauge_box_) |
54 switch (options_->direction) {
56 value_() -= increment_();
59 value_() += increment_();
68 switch (options_->direction) {
70 value_() += increment_();
73 value_() -= increment_();
82 switch (options_->direction) {
84 value_() -= increment_();
87 value_() += increment_();
96 switch (options_->direction) {
98 value_() -= increment_();
101 value_() += increment_();
109 bool OnEvent(Event event)
final {
110 if (event.is_mouse()) {
111 return OnMouseEvent(event);
114 T old_value = value_();
129 if (old_value != value_()) {
136 bool OnMouseEvent(Event event) {
138 captured_mouse_ =
nullptr;
142 if (gauge_box_.
Contain(event.mouse().x, event.mouse().y) &&
149 gauge_box_.
Contain(event.mouse().x, event.mouse().y) &&
154 if (captured_mouse_) {
155 switch (options_->direction) {
157 value_() = min_() + (
event.mouse().x - gauge_box_.
x_min) *
163 value_() = max_() - (
event.mouse().x - gauge_box_.
x_min) *
169 value_() = min_() + (
event.mouse().y - gauge_box_.
y_min) *
175 value_() = max_() - (
event.mouse().y - gauge_box_.
y_min) *
181 value_() = std::max(min_(), std::min(max_(), value_()));
187 bool Focusable() const final {
return true; }
193 ConstRef<T> increment_;
194 Ref<SliderOption<T>> options_;
199class SliderWithLabel :
public ComponentBase {
201 SliderWithLabel(ConstStringRef label,
Component inner)
202 : label_(std::move(label)) {
203 Add(std::move(inner));
208 bool OnEvent(Event event)
final {
213 if (!event.is_mouse()) {
217 if (!box_.
Contain(event.mouse().x, event.mouse().y)) {
243 ConstStringRef label_;
275 option.
value = value;
289 option.
value = value;
302 option.
value = value;
bool Focused() const
Returns if the elements if focused by the user. True when the ComponentBase is focused by the user....
CapturedMouse CaptureMouse(const Event &event)
Take the CapturedMouse if available. There is only one component of them. It represents a component t...
void Add(Component children)
Add a child. @param child The child to be attached.
virtual Element Render()
Draw the component. Build a ftxui::Element to be drawn on the ftxi::Screen representing this ftxui::C...
void TakeFocus()
Configure all the ancestors to give focus to this component.
bool Active() const
Returns if the element if the currently active child of its parent.
virtual void SetActiveChild(ComponentBase *child)
Make the |child| to be the "active" one.
virtual bool OnEvent(Event)
Called in response to an event.
Component & ChildAt(size_t i)
Access the child at index i.
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.
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.
std::function< Element(Element)> Decorator
Element nothing(Element element)
A decoration doing absolutely nothing.
std::unique_ptr< CapturedMouseInterface > CapturedMouse
std::shared_ptr< T > Make(Args &&... args)
std::shared_ptr< Node > Element
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.
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.
Element vcenter(Element)
Center an element vertically.
Element gaugeDirection(float progress, GaugeDirection)
Draw a high definition progress bar progressing in specified direction.
std::shared_ptr< ComponentBase > Component
Decorator color(Color)
Decorate using a foreground color.
bool Contain(int x, int y) const
static const Event ArrowUp
static const Event ArrowDown
static const Event ArrowLeft
static const Event ArrowRight