51class SliderBase :
public SliderOption<T>,
public ComponentBase {
53 explicit SliderBase(SliderOption<T> options) : SliderOption<T>(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();
95 this->value() = std::max(this->min(), std::min(this->max(), this->value()));
96 if (old_value != this->value()) {
97 if (this->on_change) {
106 bool OnCapturedMouseEvent(Event event) {
108 captured_mouse_ =
nullptr;
112 T old_value = this->value();
113 switch (this->direction) {
115 this->value() = this->min() + (
event.mouse().x - gauge_box_.x_min) *
116 (this->max() - this->min()) /
117 (gauge_box_.x_max - gauge_box_.x_min);
122 this->value() = this->max() - (
event.mouse().x - gauge_box_.x_min) *
123 (this->max() - this->min()) /
124 (gauge_box_.x_max - gauge_box_.x_min);
128 this->value() = this->min() + (
event.mouse().y - gauge_box_.y_min) *
129 (this->max() - this->min()) /
130 (gauge_box_.y_max - gauge_box_.y_min);
134 this->value() = this->max() - (
event.mouse().y - gauge_box_.y_min) *
135 (this->max() - this->min()) /
136 (gauge_box_.y_max - gauge_box_.y_min);
141 this->value() = std::max(this->min(), std::min(this->max(), this->value()));
143 if (old_value != this->value() && this->on_change) {
149 bool OnMouseEvent(Event event) {
150 if (captured_mouse_) {
151 return OnCapturedMouseEvent(event);
161 if (!gauge_box_.Contain(event.mouse().x, event.mouse().y)) {
165 captured_mouse_ = CaptureMouse(event);
167 if (captured_mouse_) {
169 return OnCapturedMouseEvent(event);
175 bool Focusable() const final {
return true; }
182class SliderWithLabel :
public ComponentBase {
184 SliderWithLabel(ConstStringRef label,
Component inner)
185 : label_(std::move(label)) {
186 Add(std::move(inner));
187 SetActiveChild(ChildAt(0));
191 bool OnEvent(Event event)
final {
196 if (!event.is_mouse()) {
200 mouse_hover_ = box_.Contain(event.mouse().x, event.mouse().y);
206 if (!CaptureMouse(event)) {
216 auto element =
hbox({
224 gauge_color | xflex |
reflect(box_);
230 ConstStringRef label_;
232 bool mouse_hover_ =
false;
265 option.
value = value;
269 auto slider = Make<SliderBase<int>>(option);
279 option.
value = value;
283 auto slider = Make<SliderBase<float>>(option);
292 option.
value = value;
296 auto slider = Make<SliderBase<long>>(option);
317 return Make<SliderBase<T>>(options);
一个适配器。拥有或引用一个常量字符串。为了方便,这个 类将多个不可变字符串转换为共享表示。
Element Render()
绘制组件。 构建一个 ftxui::Element,用于在表示此 ftxui::ComponentBase 的 ftxui::Screen 上绘制。 请覆盖 OnRender() 以修改渲染。
static const Event ArrowUp
static const Event ArrowDown
virtual bool OnEvent(Event)
响应事件时调用。
static const Event ArrowLeft
static const Event ArrowRight
Element xflex(Element)
在 X 轴上尽可能地扩展/收缩。
Element gaugeDirection(float progress, Direction direction)
绘制一个向指定方向推进的高清进度条。
Direction
Direction 是一个枚举,表示四个基本方向。
Element yflex(Element)
在 Y 轴上尽可能地扩展/收缩。
Element underlined(Element)
给定元素加下划线。
Element text(std::wstring text)
显示一段Unicode文本。
Element focus(Element)
将 child 设置为其同级元素中获得焦点的元素。
Element vcenter(Element)
垂直居中一个元素。
Decorator color(Color)
使用前景色进行装饰。
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
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)
一个按水平顺序逐一显示元素的容器。
Component Slider(SliderOption< T > options)
任意方向的滑块。
Decorator reflect(Box &box)
std::shared_ptr< ComponentBase > Component