39class ResizeDecorator :
public NodeDecorator {
47 : NodeDecorator(std::move(child)),
54 void Render(Screen& screen)
override {
58 for (
int y = box_.y_min; y <= box_.y_max; ++y) {
59 auto& cell = screen.PixelAt(box_.x_min, y);
60 cell.foreground_color =
color_;
61 cell.automerge =
false;
65 for (
int y = box_.y_min; y <= box_.y_max; ++y) {
66 auto& cell = screen.PixelAt(box_.x_max, y);
67 cell.foreground_color =
color_;
68 cell.automerge =
false;
72 for (
int x = box_.x_min; x <= box_.x_max; ++x) {
73 auto& cell = screen.PixelAt(x, box_.y_min);
74 cell.foreground_color =
color_;
75 cell.automerge =
false;
79 for (
int x = box_.x_min; x <= box_.x_max; ++x) {
80 auto& cell = screen.PixelAt(x, box_.y_max);
81 cell.foreground_color =
color_;
82 cell.automerge =
false;
94Element DefaultRenderState(
const WindowRenderState& state) {
105 element = std::make_shared<ResizeDecorator>(
117class WindowImpl :
public ComponentBase,
public WindowOptions {
119 explicit WindowImpl(WindowOptions option) : WindowOptions(std::move(option)) {
130 const bool captureable =
133 const WindowRenderState state = {
140 (resize_right_hover_ || resize_right_) && captureable,
142 (resize_down_hover_ || resize_down_) && captureable,
145 element = render ? render(state) : DefaultRenderState(state);
148 element |=
reflect(box_window_);
149 element |= PositionAndSize(
left(),
top(), width(), height());
155 bool OnEvent(Event event)
final {
160 if (!event.is_mouse()) {
164 mouse_hover_ = box_window_.Contain(event.mouse().x, event.mouse().y);
166 resize_down_hover_ =
false;
167 resize_top_hover_ =
false;
168 resize_left_hover_ =
false;
169 resize_right_hover_ =
false;
172 resize_left_hover_ =
event.mouse().x ==
left() + box_.x_min;
173 resize_right_hover_ =
174 event.mouse().x ==
left() + width() - 1 + box_.x_min;
175 resize_top_hover_ =
event.mouse().y ==
top() + box_.y_min;
176 resize_down_hover_ =
event.mouse().y ==
top() + height() - 1 + box_.y_min;
179 resize_top_hover_ &= resize_top();
180 resize_left_hover_ &= resize_left();
181 resize_down_hover_ &= resize_down();
182 resize_right_hover_ &= resize_right();
185 if (captured_mouse_) {
187 captured_mouse_ =
nullptr;
192 width() =
left() + width() -
event.mouse().x + box_.x_min;
193 left() =
event.mouse().x - box_.x_min;
197 width() =
event.mouse().x - resize_start_x - box_.x_min;
201 height() =
top() + height() -
event.mouse().y + box_.y_min;
202 top() =
event.mouse().y - box_.y_min;
206 height() =
event.mouse().y - resize_start_y - box_.y_min;
210 left() =
event.mouse().x - drag_start_x - box_.x_min;
211 top() =
event.mouse().y - drag_start_y - box_.y_min;
215 width() = std::max<int>(width(),
static_cast<int>(title().
size() + 2));
216 height() = std::max<int>(height(), 2);
230 if (!CaptureMouse(event)) {
243 captured_mouse_ = CaptureMouse(event);
244 if (!captured_mouse_) {
253 resize_start_x =
event.mouse().x - width() - box_.x_min;
254 resize_start_y =
event.mouse().y - height() - box_.y_min;
255 drag_start_x =
event.mouse().x -
left() - box_.x_min;
256 drag_start_y =
event.mouse().y -
top() - box_.y_min;
267 int drag_start_x = 0;
268 int drag_start_y = 0;
269 int resize_start_x = 0;
270 int resize_start_y = 0;
272 bool mouse_hover_ =
false;
279 bool resize_top_hover_ =
false;
280 bool resize_left_hover_ =
false;
281 bool resize_down_hover_ =
false;
282 bool resize_right_hover_ =
false;
Element Render()
Draw the component. Build a ftxui::Element to be drawn on the ftxui::Screen representing this ftxui::...
static ScreenInteractive * Active()
Return the currently active screen, or null if none.
CapturedMouse CaptureMouse()
Try to get the unique lock about behing able to capture the mouse.
virtual bool OnEvent(Event)
Called in response to an event.
Component Window(WindowOptions option)
A draggeable / resizeable window. To use multiple of them, they must be stacked using Container::Stac...
friend void Render(Screen &screen, Node *node, Selection &selection)
Element window(Element title, Element content, BorderStyle border=ROUNDED)
Draw window with a title and a border around the element.
Element clear_under(Element element)
Before drawing |child|, clear the pixels below. This is useful in combination with dbox.
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Element text(std::wstring text)
Display a piece of unicode text.
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 vbox(Elements)
A container displaying elements vertically one by one.
Color
Color is an enumeration that represents the color support of the terminal.
The FTXUI ftxui:: namespace.
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)
A container displaying elements horizontally one by one.
Decorator reflect(Box &box)
std::shared_ptr< ComponentBase > Component