FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
src/ftxui/component/window.cpp
Go to the documentation of this file.
1// Copyright 2023 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#define NOMINMAX
5#include <algorithm>
6#include <ftxui/component/app.hpp> // for App
10#include <memory>
11#include <utility>
12#include "ftxui/dom/elements.hpp" // for text, window, hbox, vbox, size, clear_under, reflect, emptyElement
13#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
14#include "ftxui/screen/color.hpp" // for Color
15#include "ftxui/screen/screen.hpp" // for Screen
16
17namespace ftxui {
18
19namespace {
20
21Decorator PositionAndSize(int left, int top, int width, int height) {
22 return [=](Element element) {
23 element |= size(WIDTH, EQUAL, width);
24 element |= size(HEIGHT, EQUAL, height);
25
26 auto padding_left = emptyElement() | size(WIDTH, EQUAL, left);
27 auto padding_top = emptyElement() | size(HEIGHT, EQUAL, top);
28
29 return vbox({
30 padding_top,
31 hbox({
32 padding_left,
33 element,
34 }),
35 });
36 };
37}
38
39class ResizeDecorator : public NodeDecorator {
40 public:
41 ResizeDecorator(Element child,
42 bool resize_left,
43 bool resize_right,
44 bool resize_top,
45 bool resize_down,
46 Color color)
47 : NodeDecorator(std::move(child)),
48 color_(color),
49 resize_left_(resize_left),
50 resize_right_(resize_right),
51 resize_top_(resize_top),
52 resize_down_(resize_down) {}
53
54 void Render(Screen& screen) override {
56
57 if (resize_left_) {
58 for (int y = box_.y_min; y <= box_.y_max; ++y) {
59 auto& cell = screen.CellAt(box_.x_min, y);
60 cell.foreground_color = color_;
61 cell.automerge = false;
62 }
63 }
64 if (resize_right_) {
65 for (int y = box_.y_min; y <= box_.y_max; ++y) {
66 auto& cell = screen.CellAt(box_.x_max, y);
67 cell.foreground_color = color_;
68 cell.automerge = false;
69 }
70 }
71 if (resize_top_) {
72 for (int x = box_.x_min; x <= box_.x_max; ++x) {
73 auto& cell = screen.CellAt(x, box_.y_min);
74 cell.foreground_color = color_;
75 cell.automerge = false;
76 }
77 }
78 if (resize_down_) {
79 for (int x = box_.x_min; x <= box_.x_max; ++x) {
80 auto& cell = screen.CellAt(x, box_.y_max);
81 cell.foreground_color = color_;
82 cell.automerge = false;
83 }
84 }
85 }
86
87 Color color_;
88 const bool resize_left_;
89 const bool resize_right_;
90 const bool resize_top_;
91 const bool resize_down_;
92};
93
94Element DefaultRenderState(const WindowRenderState& state) {
95 Element element = state.inner;
96 if (!state.active) {
97 element |= dim;
98 }
99
100 element = window(text(state.title), element);
101 element |= clear_under;
102
103 const Color color = Color::Red;
104
105 element = std::make_shared<ResizeDecorator>( //
106 element, //
107 state.hover_left, //
108 state.hover_right, //
109 state.hover_top, //
110 state.hover_down, //
111 color //
112 );
113
114 return element;
115}
116
117class WindowImpl : public ComponentBase, public WindowOptions {
118 public:
119 explicit WindowImpl(WindowOptions option) : WindowOptions(std::move(option)) {
120 if (!inner) {
121 inner = Make<ComponentBase>();
122 }
123 Add(inner);
124 }
125
126 private:
127 Element OnRender() final {
128 auto element = ComponentBase::Render();
129
130 const bool captureable = captured_mouse_ || App::Active()->CaptureMouse();
131
132 const WindowRenderState state = {
133 element,
134 title(),
135 Active(),
136 drag_,
138 (resize_left_hover_ || resize_left_) && captureable,
139 (resize_right_hover_ || resize_right_) && captureable,
140 (resize_top_hover_ || resize_top_) && captureable,
141 (resize_down_hover_ || resize_down_) && captureable,
142 };
143
144 element = render ? render(state) : DefaultRenderState(state);
145
146 // Position and record the drawn area of the window.
147 element |= reflect(box_window_);
148 element |= PositionAndSize(left(), top(), width(), height());
149 element |= reflect(box_);
150
151 return element;
152 }
153
154 bool OnEvent(Event event) final {
155 if (ComponentBase::OnEvent(event)) {
156 return true;
157 }
158
159 if (!event.is_mouse()) {
160 return false;
161 }
162
163 mouse_hover_ = box_window_.Contain(event.mouse().x, event.mouse().y);
164
165 resize_down_hover_ = false;
166 resize_top_hover_ = false;
167 resize_left_hover_ = false;
168 resize_right_hover_ = false;
169
170 if (mouse_hover_) {
171 resize_left_hover_ = event.mouse().x == left() + box_.x_min;
172 resize_right_hover_ =
173 event.mouse().x == left() + width() - 1 + box_.x_min;
174 resize_top_hover_ = event.mouse().y == top() + box_.y_min;
175 resize_down_hover_ = event.mouse().y == top() + height() - 1 + box_.y_min;
176
177 // Apply the component options:
178 resize_top_hover_ &= resize_top();
179 resize_left_hover_ &= resize_left();
180 resize_down_hover_ &= resize_down();
181 resize_right_hover_ &= resize_right();
182 }
183
184 if (captured_mouse_) {
185 if (event.mouse().motion == Mouse::Released) {
186 captured_mouse_ = nullptr;
187 return true;
188 }
189
190 if (resize_left_) {
191 width() = left() + width() - event.mouse().x + box_.x_min;
192 left() = event.mouse().x - box_.x_min;
193 }
194
195 if (resize_right_) {
196 width() = event.mouse().x - resize_start_x - box_.x_min;
197 }
198
199 if (resize_top_) {
200 height() = top() + height() - event.mouse().y + box_.y_min;
201 top() = event.mouse().y - box_.y_min;
202 }
203
204 if (resize_down_) {
205 height() = event.mouse().y - resize_start_y - box_.y_min;
206 }
207
208 if (drag_) {
209 left() = event.mouse().x - drag_start_x - box_.x_min;
210 top() = event.mouse().y - drag_start_y - box_.y_min;
211 }
212
213 // Clamp the window size.
214 width() = std::max<int>(width(), static_cast<int>(title().size() + 2));
215 height() = std::max<int>(height(), 2);
216
217 return true;
218 }
219
220 resize_left_ = false;
221 resize_right_ = false;
222 resize_top_ = false;
223 resize_down_ = false;
224
225 if (!mouse_hover_) {
226 return false;
227 }
228
229 if (!CaptureMouse(event)) {
230 return true;
231 }
232
233 if (event.mouse().button != Mouse::Left) {
234 return true;
235 }
236 if (event.mouse().motion != Mouse::Pressed) {
237 return true;
238 }
239
240 TakeFocus();
241
242 captured_mouse_ = CaptureMouse(event);
243 if (!captured_mouse_) {
244 return true;
245 }
246
247 resize_left_ = resize_left_hover_;
248 resize_right_ = resize_right_hover_;
249 resize_top_ = resize_top_hover_;
250 resize_down_ = resize_down_hover_;
251
252 resize_start_x = event.mouse().x - width() - box_.x_min;
253 resize_start_y = event.mouse().y - height() - box_.y_min;
254 drag_start_x = event.mouse().x - left() - box_.x_min;
255 drag_start_y = event.mouse().y - top() - box_.y_min;
256
257 // Drag only if we are not resizeing a border yet:
259 return true;
260 }
261
262 Box box_;
263 Box box_window_;
264
265 CapturedMouse captured_mouse_;
266 int drag_start_x = 0;
267 int drag_start_y = 0;
268 int resize_start_x = 0;
269 int resize_start_y = 0;
270
271 bool mouse_hover_ = false;
272 bool drag_ = false;
273 bool resize_top_ = false;
274 bool resize_left_ = false;
275 bool resize_down_ = false;
276 bool resize_right_ = false;
277
278 bool resize_top_hover_ = false;
279 bool resize_left_hover_ = false;
280 bool resize_down_hover_ = false;
281 bool resize_right_hover_ = false;
282};
283
284} // namespace
285
286/// @brief A draggeable / resizeable window. To use multiple of them, they must
287/// be stacked using `Container::Stacked({...})` component;
288///
289/// @param option A struct holding every parameters.
290/// @ingroup component
291/// @see Window
292///
293/// ### Example
294///
295/// ```cpp
296/// auto window_1= Window({
297/// .inner = DummyWindowContent(),
298/// .title = "First window",
299/// });
300///
301/// auto window_2= Window({
302/// .inner = DummyWindowContent(),
303/// .title = "Second window",
304/// });
305///
306/// auto container = Container::Stacked({
307/// window_1,
308/// window_2,
309/// });
310/// ```
312 return Make<WindowImpl>(std::move(option));
313}
314
315}; // namespace ftxui
Element Render()
Draw the component. Build a ftxui::Element to be drawn on the ftxui::Screen representing this ftxui::...
static App * Active()
Return the currently active screen, or null if none.
Definition app.cpp:522
CapturedMouse CaptureMouse()
Try to get the unique lock about behing able to capture the mouse.
Definition app.cpp:412
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)
Definition node.cpp:96
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 cells below. This is useful in combination with dbox.
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Element emptyElement()
Definition dom/util.cpp:140
Element dim(Element)
Use a light font, for elements with less emphasis.
Definition dim.cpp:33
Element text(std::wstring_view text)
Display a piece of unicode text.
Definition text.cpp:164
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:84
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
Color
Color is an enumeration that represents the color support of the terminal.
Definition terminal.hpp:23
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
std::function< Element(Element)> Decorator
Definition elements.hpp:25
std::unique_ptr< CapturedMouseInterface > CapturedMouse
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:27
std::shared_ptr< Node > Element
Definition elements.hpp:23
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:94
Decorator reflect(Box &box)
Definition reflect.cpp:43
std::shared_ptr< ComponentBase > Component
Definition app.hpp:24
std::uint8_t top
Definition screen.cpp:141
std::uint8_t left
Definition screen.cpp:140
const bool resize_left_
const bool resize_top_
const bool resize_right_
const bool resize_down_