2023-08-19 19:56:36 +08:00
|
|
|
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|
2022-06-12 23:08:22 +08:00
|
|
|
#include <ftxui/screen/box.hpp> // for Box
|
|
|
|
#include <utility> // for move
|
2021-05-02 02:40:35 +08:00
|
|
|
|
2018-10-10 01:06:03 +08:00
|
|
|
#include "ftxui/dom/node.hpp"
|
2021-12-12 00:58:25 +08:00
|
|
|
#include "ftxui/screen/screen.hpp" // for Screen
|
2025-03-19 22:33:05 +08:00
|
|
|
#include "ftxui/screen/util.hpp" // for clamp
|
2018-09-18 14:48:40 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2019-01-07 00:10:35 +08:00
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
Node::Node() = default;
|
2021-05-16 23:18:11 +08:00
|
|
|
Node::Node(Elements children) : children_(std::move(children)) {}
|
2022-03-31 08:17:43 +08:00
|
|
|
Node::~Node() = default;
|
2018-09-18 14:48:40 +08:00
|
|
|
|
2020-08-16 08:24:50 +08:00
|
|
|
/// @brief Compute how much space an elements needs.
|
|
|
|
/// @ingroup dom
|
2018-10-10 01:06:03 +08:00
|
|
|
void Node::ComputeRequirement() {
|
2025-03-19 22:33:05 +08:00
|
|
|
if (children_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-31 08:17:43 +08:00
|
|
|
for (auto& child : children_) {
|
2018-10-10 01:06:03 +08:00
|
|
|
child->ComputeRequirement();
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2025-03-19 22:33:05 +08:00
|
|
|
|
|
|
|
// By default, the requirement is the one of the first child.
|
|
|
|
requirement_ = children_[0]->requirement();
|
|
|
|
|
|
|
|
// Propagate the focused requirement.
|
|
|
|
for (size_t i = 1; i < children_.size(); ++i) {
|
|
|
|
if (!requirement_.focused.enabled &&
|
|
|
|
children_[i]->requirement().focused.enabled) {
|
|
|
|
requirement_.focused = children_[i]->requirement().focused;
|
|
|
|
}
|
|
|
|
}
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
|
2020-08-16 08:24:50 +08:00
|
|
|
/// @brief Assign a position and a dimension to an element for drawing.
|
|
|
|
/// @ingroup dom
|
2018-09-18 14:48:40 +08:00
|
|
|
void Node::SetBox(Box box) {
|
|
|
|
box_ = box;
|
|
|
|
}
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2024-12-27 16:45:13 +08:00
|
|
|
/// @brief Compute the selection of an element.
|
|
|
|
/// @ingroup dom
|
|
|
|
void Node::Select(Selection& selection) {
|
|
|
|
// If this Node box_ doesn't intersect with the selection, then no selection.
|
|
|
|
if (Box::Intersection(selection.GetBox(), box_).IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// By default we defer the selection to the children.
|
|
|
|
for (auto& child : children_) {
|
|
|
|
child->Select(selection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-16 08:24:50 +08:00
|
|
|
/// @brief Display an element on a ftxui::Screen.
|
|
|
|
/// @ingroup dom
|
2018-09-18 14:48:40 +08:00
|
|
|
void Node::Render(Screen& screen) {
|
2022-03-31 08:17:43 +08:00
|
|
|
for (auto& child : children_) {
|
2018-09-18 14:48:40 +08:00
|
|
|
child->Render(screen);
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2018-09-18 14:48:40 +08:00
|
|
|
}
|
|
|
|
|
2021-12-12 00:58:25 +08:00
|
|
|
void Node::Check(Status* status) {
|
2022-03-31 08:17:43 +08:00
|
|
|
for (auto& child : children_) {
|
2021-12-12 00:58:25 +08:00
|
|
|
child->Check(status);
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-12-12 00:58:25 +08:00
|
|
|
status->need_iteration |= (status->iteration == 0);
|
|
|
|
}
|
|
|
|
|
2024-12-27 16:45:13 +08:00
|
|
|
std::string Node::GetSelectedContent(Selection& selection) {
|
|
|
|
std::string content;
|
|
|
|
|
|
|
|
for (auto& child : children_) {
|
|
|
|
content += child->GetSelectedContent(selection);
|
|
|
|
}
|
|
|
|
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2020-08-16 08:24:50 +08:00
|
|
|
/// @brief Display an element on a ftxui::Screen.
|
|
|
|
/// @ingroup dom
|
2020-05-21 03:23:59 +08:00
|
|
|
void Render(Screen& screen, const Element& element) {
|
2024-12-27 16:45:13 +08:00
|
|
|
Selection selection;
|
|
|
|
Render(screen, element.get(), selection);
|
2020-05-21 03:23:59 +08:00
|
|
|
}
|
|
|
|
|
2020-08-16 08:24:50 +08:00
|
|
|
/// @brief Display an element on a ftxui::Screen.
|
|
|
|
/// @ingroup dom
|
2018-09-18 14:48:40 +08:00
|
|
|
void Render(Screen& screen, Node* node) {
|
2024-12-27 16:45:13 +08:00
|
|
|
Selection selection;
|
|
|
|
Render(screen, node, selection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Render(Screen& screen, Node* node, Selection& selection) {
|
2018-09-18 14:48:40 +08:00
|
|
|
Box box;
|
2019-01-20 05:06:05 +08:00
|
|
|
box.x_min = 0;
|
|
|
|
box.y_min = 0;
|
|
|
|
box.x_max = screen.dimx() - 1;
|
|
|
|
box.y_max = screen.dimy() - 1;
|
2020-03-23 05:32:44 +08:00
|
|
|
|
2021-12-12 00:58:25 +08:00
|
|
|
Node::Status status;
|
|
|
|
node->Check(&status);
|
2022-03-31 08:17:43 +08:00
|
|
|
const int max_iterations = 20;
|
|
|
|
while (status.need_iteration && status.iteration < max_iterations) {
|
2021-12-12 00:58:25 +08:00
|
|
|
// Step 1: Find what dimension this elements wants to be.
|
|
|
|
node->ComputeRequirement();
|
|
|
|
|
|
|
|
// Step 2: Assign a dimension to the element.
|
|
|
|
node->SetBox(box);
|
|
|
|
|
|
|
|
// Check if the element needs another iteration of the layout algorithm.
|
|
|
|
status.need_iteration = false;
|
|
|
|
status.iteration++;
|
|
|
|
node->Check(&status);
|
|
|
|
}
|
2018-09-18 14:48:40 +08:00
|
|
|
|
2024-12-27 16:45:13 +08:00
|
|
|
// Step 3: Selection
|
|
|
|
if (!selection.IsEmpty()) {
|
|
|
|
node->Select(selection);
|
|
|
|
}
|
|
|
|
|
2025-03-19 22:33:05 +08:00
|
|
|
// Setting the cursor to the right position allow folks using CJK (China,
|
|
|
|
// Japanese, Korean, ...) characters to see their [input method editor]
|
|
|
|
// displayed at the right location. See [issue].
|
|
|
|
//
|
|
|
|
// [input method editor]:
|
|
|
|
// https://en.wikipedia.org/wiki/Input_method
|
|
|
|
//
|
|
|
|
// [issue]:
|
|
|
|
// https://github.com/ArthurSonzogni/FTXUI/issues/2#issuecomment-505282355
|
|
|
|
//
|
|
|
|
// Unfortunately, Microsoft terminal do not handle properly hiding the
|
|
|
|
// cursor. Instead the character under the cursor is hidden, which is a big
|
|
|
|
// problem. As a result, we can't enable setting cursor to the right
|
|
|
|
// location. It will be displayed at the bottom right corner.
|
|
|
|
// See:
|
|
|
|
// https://github.com/microsoft/terminal/issues/1203
|
|
|
|
// https://github.com/microsoft/terminal/issues/3093
|
|
|
|
if (node->requirement().focused.enabled
|
|
|
|
#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
|
|
|
|
||
|
|
|
|
node->requirement().focused.cursor_shape == Screen::Cursor::Shape::Hidden
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
screen.SetCursor(Screen::Cursor{
|
|
|
|
node->requirement().focused.node->box_.x_max,
|
|
|
|
node->requirement().focused.node->box_.y_max,
|
|
|
|
node->requirement().focused.cursor_shape,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
screen.SetCursor(Screen::Cursor{
|
|
|
|
screen.dimx() - 1,
|
|
|
|
screen.dimy() - 1,
|
|
|
|
Screen::Cursor::Shape::Hidden,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-12-27 16:45:13 +08:00
|
|
|
// Step 4: Draw the element.
|
2021-12-12 00:58:25 +08:00
|
|
|
screen.stencil = box;
|
2018-09-18 14:48:40 +08:00
|
|
|
node->Render(screen);
|
2019-01-19 07:20:29 +08:00
|
|
|
|
2024-12-27 16:45:13 +08:00
|
|
|
// Step 5: Apply shaders
|
2019-01-19 07:20:29 +08:00
|
|
|
screen.ApplyShader();
|
2018-09-18 14:48:40 +08:00
|
|
|
}
|
|
|
|
|
2024-12-27 16:45:13 +08:00
|
|
|
std::string GetNodeSelectedContent(Screen& screen,
|
|
|
|
Node* node,
|
|
|
|
Selection& selection) {
|
|
|
|
Box box;
|
|
|
|
box.x_min = 0;
|
|
|
|
box.y_min = 0;
|
|
|
|
box.x_max = screen.dimx() - 1;
|
|
|
|
box.y_max = screen.dimy() - 1;
|
|
|
|
|
|
|
|
Node::Status status;
|
|
|
|
node->Check(&status);
|
|
|
|
const int max_iterations = 20;
|
|
|
|
while (status.need_iteration && status.iteration < max_iterations) {
|
|
|
|
// Step 1: Find what dimension this elements wants to be.
|
|
|
|
node->ComputeRequirement();
|
|
|
|
|
|
|
|
// Step 2: Assign a dimension to the element.
|
|
|
|
node->SetBox(box);
|
|
|
|
|
|
|
|
// Check if the element needs another iteration of the layout algorithm.
|
|
|
|
status.need_iteration = false;
|
|
|
|
status.iteration++;
|
|
|
|
node->Check(&status);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 3: Selection
|
|
|
|
node->Select(selection);
|
|
|
|
|
|
|
|
// Step 4: get the selected content.
|
|
|
|
return node->GetSelectedContent(selection);
|
|
|
|
}
|
|
|
|
|
2020-02-12 04:44:55 +08:00
|
|
|
} // namespace ftxui
|