Fix automerge in tables. (#333)

This commit is contained in:
Arthur Sonzogni 2022-02-13 11:41:31 +01:00 committed by GitHub
parent 9c4218c2a8
commit 5da7b8a59a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 69 additions and 18 deletions

View File

@ -17,6 +17,9 @@ Element gaugeUp(float ratio);
Element gaugeDown(float ratio);
Element gaugeDirection(float ratio, GaugeDirection);
```
- Add the `automerge` decorator. This makes separator characters to be merged
with others nearby.
- Fix the `Table` rendering function, to allow automerging characters.
#### Component
- Support SIGTSTP. (ctrl+z).

View File

@ -44,6 +44,7 @@ add_library(dom
include/ftxui/dom/node.hpp
include/ftxui/dom/requirement.hpp
include/ftxui/dom/take_any_args.hpp
src/ftxui/dom/automerge.cpp
src/ftxui/dom/blink.cpp
src/ftxui/dom/bold.cpp
src/ftxui/dom/border.cpp

View File

@ -81,6 +81,7 @@ Element color(Color, Element);
Element bgcolor(Color, Element);
Decorator focusPosition(int x, int y);
Decorator focusPositionRelative(float x, float y);
Element automerge(Element);
// --- Layout is
// Horizontal, Vertical or stacked set of elements.

View File

@ -17,8 +17,8 @@ Component Dropdown(ConstStringListRef entries, int* selected) {
Impl(ConstStringListRef entries, int* selected)
: entries_(std::move(entries)), selected_(selected) {
CheckboxOption option;
option.style_checked = "";
option.style_unchecked = "";
option.style_checked = " ";
option.style_unchecked = " ";
checkbox_ = Checkbox(&title_, &show_, option),
radiobox_ = Radiobox(entries_, selected_);

View File

@ -0,0 +1,36 @@
#include <memory> // for make_shared
#include <utility> // for move
#include "ftxui/dom/elements.hpp" // for Element, automerge
#include "ftxui/dom/node.hpp" // for Node
#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
#include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/screen.hpp" // for Pixel, Screen
namespace ftxui {
/// @brief Enable character to be automatically merged with others nearby.
/// @ingroup dom
Element automerge(Element child) {
class Impl : public NodeDecorator {
public:
using NodeDecorator::NodeDecorator;
void Render(Screen& screen) override {
for (int y = box_.y_min; y <= box_.y_max; ++y) {
for (int x = box_.x_min; x <= box_.x_max; ++x) {
screen.PixelAt(x, y).automerge = true;
}
}
Node::Render(screen);
}
};
return std::make_shared<Impl>(std::move(child));
}
} // namespace ftxui
// 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.

View File

@ -250,10 +250,10 @@ void TableSelection::Border(BorderStyle style) {
BorderTop(style);
BorderBottom(style);
table_->elements_[y_min_][x_min_] = text(charset[style][0]);
table_->elements_[y_min_][x_max_] = text(charset[style][1]);
table_->elements_[y_max_][x_min_] = text(charset[style][2]);
table_->elements_[y_max_][x_max_] = text(charset[style][3]);
table_->elements_[y_min_][x_min_] = text(charset[style][0]) | automerge;
table_->elements_[y_min_][x_max_] = text(charset[style][1]) | automerge;
table_->elements_[y_max_][x_min_] = text(charset[style][2]) | automerge;
table_->elements_[y_max_][x_max_] = text(charset[style][3]) | automerge;
}
void TableSelection::Separator(BorderStyle style) {
@ -261,8 +261,8 @@ void TableSelection::Separator(BorderStyle style) {
for (int x = x_min_ + 1; x <= x_max_ - 1; ++x) {
if (y % 2 == 0 || x % 2 == 0) {
Element& e = table_->elements_[y][x];
e = (y % 2) ? separatorCharacter(charset[style][5])
: separatorCharacter(charset[style][4]);
e = (y % 2) ? separatorCharacter(charset[style][5]) | automerge
: separatorCharacter(charset[style][4]) | automerge;
}
}
}
@ -272,7 +272,8 @@ void TableSelection::SeparatorVertical(BorderStyle style) {
for (int y = y_min_ + 1; y <= y_max_ - 1; ++y) {
for (int x = x_min_ + 1; x <= x_max_ - 1; ++x) {
if (x % 2 == 0) {
table_->elements_[y][x] = separatorCharacter(charset[style][5]);
table_->elements_[y][x] =
separatorCharacter(charset[style][5]) | automerge;
}
}
}
@ -282,30 +283,39 @@ void TableSelection::SeparatorHorizontal(BorderStyle style) {
for (int y = y_min_ + 1; y <= y_max_ - 1; ++y) {
for (int x = x_min_ + 1; x <= x_max_ - 1; ++x) {
if (y % 2 == 0) {
table_->elements_[y][x] = separatorCharacter(charset[style][4]);
table_->elements_[y][x] =
separatorCharacter(charset[style][4]) | automerge;
}
}
}
}
void TableSelection::BorderLeft(BorderStyle style) {
for (int y = y_min_; y <= y_max_; y++)
table_->elements_[y][x_min_] = separatorCharacter(charset[style][5]);
for (int y = y_min_; y <= y_max_; y++) {
table_->elements_[y][x_min_] =
separatorCharacter(charset[style][5]) | automerge;
}
}
void TableSelection::BorderRight(BorderStyle style) {
for (int y = y_min_; y <= y_max_; y++)
table_->elements_[y][x_max_] = separatorCharacter(charset[style][5]);
for (int y = y_min_; y <= y_max_; y++) {
table_->elements_[y][x_max_] =
separatorCharacter(charset[style][5]) | automerge;
}
}
void TableSelection::BorderTop(BorderStyle style) {
for (int x = x_min_; x <= x_max_; x++)
table_->elements_[y_min_][x] = separatorCharacter(charset[style][4]);
for (int x = x_min_; x <= x_max_; x++) {
table_->elements_[y_min_][x] =
separatorCharacter(charset[style][4]) | automerge;
}
}
void TableSelection::BorderBottom(BorderStyle style) {
for (int x = x_min_; x <= x_max_; x++)
table_->elements_[y_max_][x] = separatorCharacter(charset[style][4]);
for (int x = x_min_; x <= x_max_; x++) {
table_->elements_[y_max_][x] =
separatorCharacter(charset[style][4]) | automerge;
}
}
} // namespace ftxui