Fix focus vs flexbox interaction. (#405)

- Fix focus in flexbox. This required resetting the focus state at the
  beginning of the ComputeRequirement(), because it can now run several
  times.

  This resolves:https://github.com/ArthurSonzogni/FTXUI/issues/399

- Add Box::Union.

- Add a preliminary implementation of forwarding selected_box from
  within the flexbox.
This commit is contained in:
Arthur Sonzogni
2022-05-22 21:41:29 +02:00
committed by GitHub
parent f9256fa132
commit 11519ef1c6
10 changed files with 77 additions and 8 deletions

View File

@@ -21,6 +21,7 @@ class DBox : public Node {
requirement_.flex_grow_y = 0;
requirement_.flex_shrink_x = 0;
requirement_.flex_shrink_y = 0;
requirement_.selection = Requirement::NORMAL;
for (auto& child : children_) {
child->ComputeRequirement();
requirement_.min_x =

View File

@@ -99,41 +99,63 @@ class Flexbox : public Node {
}
Layout(global, true);
// Reset:
requirement_.selection = Requirement::Selection::NORMAL;
requirement_.selected_box = Box();
requirement_.min_x = 0;
requirement_.min_y = 0;
if (global.blocks.empty()) {
requirement_.min_x = 0;
requirement_.min_y = 0;
return;
}
// Compute the union of all the blocks:
Box box;
box.x_min = global.blocks[0].x;
box.y_min = global.blocks[0].y;
box.x_max = global.blocks[0].x + global.blocks[0].dim_x;
box.y_max = global.blocks[0].y + global.blocks[0].dim_y;
for (auto& b : global.blocks) {
box.x_min = std::min(box.x_min, b.x);
box.y_min = std::min(box.y_min, b.y);
box.x_max = std::max(box.x_max, b.x + b.dim_x);
box.y_max = std::max(box.y_max, b.y + b.dim_y);
}
requirement_.min_x = box.x_max - box.x_min;
requirement_.min_y = box.y_max - box.y_min;
// Find the selection:
for (size_t i = 0; i < children_.size(); ++i) {
if (requirement_.selection >= children_[i]->requirement().selection) {
continue;
}
requirement_.selection = children_[i]->requirement().selection;
Box selected_box = children_[i]->requirement().selected_box;
// Shift |selected_box| according to its position inside this component:
auto& b = global.blocks[i];
selected_box.x_min += b.x;
selected_box.y_min += b.y;
selected_box.x_max += b.x;
selected_box.y_max += b.y;
requirement_.selected_box = Box::Intersection(selected_box, box);
}
}
void SetBox(Box box) override {
Node::SetBox(box);
int asked_previous = asked_;
asked_ = std::min(asked_, IsColumnOriented() ? box.y_max - box.y_min + 1
: box.x_max - box.x_min + 1);
need_iteration_ = (asked_ != asked_previous);
flexbox_helper::Global global;
global.config = config_;
global.size_x = box.x_max - box.x_min + 1;
global.size_y = box.y_max - box.y_min + 1;
Layout(global);
need_iteration_ = false;
for (size_t i = 0; i < children_.size(); ++i) {
auto& child = children_[i];
auto& b = global.blocks[i];

View File

@@ -432,6 +432,29 @@ TEST(FlexboxTest, GapY) {
" ");
}
TEST(FlexboxTest, Focus) {
auto document = vbox({
paragraph("0 -"),
paragraph("1 -"),
paragraph("2 -"),
paragraph("3 -"),
paragraph("4 -"),
paragraph("5 -"),
paragraph("6 -"),
paragraph("7 -") | focus,
paragraph("8 -"),
paragraph("9 -"),
}) | yframe | flex;
Screen screen(1, 3);
Render(screen, document);
EXPECT_EQ(screen.ToString(),
"7\r\n"
"-\r\n"
"8"
);
}
} // namespace ftxui
// Copyright 2021 Arthur Sonzogni. All rights reserved.

View File

@@ -23,6 +23,7 @@ class HBox : public Node {
requirement_.flex_grow_y = 0;
requirement_.flex_shrink_x = 0;
requirement_.flex_shrink_y = 0;
requirement_.selection = Requirement::NORMAL;
for (auto& child : children_) {
child->ComputeRequirement();
if (requirement_.selection < child->requirement().selection) {

View File

@@ -23,6 +23,7 @@ class VBox : public Node {
requirement_.flex_grow_y = 0;
requirement_.flex_shrink_x = 0;
requirement_.flex_shrink_y = 0;
requirement_.selection = Requirement::NORMAL;
for (auto& child : children_) {
child->ComputeRequirement();
if (requirement_.selection < child->requirement().selection) {