Extract common struct Dimensions from Terminal (#171)

- Convert Dimension to namespace to allow defining Fit method from dom.
- Use Dimensions extracted from Terminal as replacement struct.
- Convert Terminal to namespace as it only defines static members.
- Remove dom references from screen library (circular dependency).
This commit is contained in:
Tushar Maheshwari
2021-08-03 02:49:29 +05:30
committed by GitHub
parent 34d955e9ac
commit 49e8cc57d3
6 changed files with 45 additions and 49 deletions

View File

@@ -59,6 +59,16 @@ Element operator|(Element element, Decorator decorator) {
return decorator(std::move(element));
}
/// The minimal dimension that will fit the given element.
/// @see Fixed
/// @see Full
Dimensions Dimension::Fit(Element& e) {
e->ComputeRequirement();
Dimensions size = Dimension::Full();
return {std::min(e->requirement().min_x, size.dimx),
std::min(e->requirement().min_y, size.dimy)};
}
} // namespace ftxui
// Copyright 2020 Arthur Sonzogni. All rights reserved.

View File

@@ -2,8 +2,6 @@
#include <iostream> // for operator<<, basic_ostream, wstringstream, stringstream, flush, cout, ostream
#include <sstream> // IWYU pragma: keep
#include "ftxui/dom/node.hpp" // for Element, Node
#include "ftxui/dom/requirement.hpp" // for Requirement
#include "ftxui/screen/screen.hpp"
#include "ftxui/screen/string.hpp" // for to_string, wchar_width
#include "ftxui/screen/terminal.hpp" // for Terminal::Dimensions, Terminal
@@ -93,42 +91,31 @@ void UpdatePixelStyle(std::wstringstream& ss, Pixel& previous, Pixel& next) {
/// A fixed dimension.
/// @see Fit
/// @see Full
Dimension Dimension::Fixed(int v) {
return Dimension{v, v};
}
/// The minimal dimension that will fit the given element.
/// @see Fixed
/// @see Full
Dimension Dimension::Fit(Element& e) {
e->ComputeRequirement();
Terminal::Dimensions size = Terminal::Size();
return Dimension{std::min(e->requirement().min_x, size.dimx),
std::min(e->requirement().min_y, size.dimy)};
Dimensions Dimension::Fixed(int v) {
return {v, v};
}
/// Use the terminal dimensions.
/// @see Fixed
/// @see Fit
Dimension Dimension::Full() {
Terminal::Dimensions size = Terminal::Size();
return Dimension{size.dimx, size.dimy};
Dimensions Dimension::Full() {
return Terminal::Size();
}
// static
/// Create a screen with the given dimension along the x-axis and y-axis.
Screen Screen::Create(Dimension width, Dimension height) {
Screen Screen::Create(Dimensions width, Dimensions height) {
return Screen(width.dimx, height.dimy);
}
// static
/// Create a screen with the given dimension.
Screen Screen::Create(Dimension dimension) {
Screen Screen::Create(Dimensions dimension) {
return Screen(dimension.dimx, dimension.dimy);
}
Screen::Screen(int dimx, int dimy)
: stencil({0, dimx - 1, 0, dimy - 1}),
: stencil{0, dimx - 1, 0, dimy - 1},
dimx_(dimx),
dimy_(dimy),
pixels_(dimy, std::vector<Pixel>(dimx)) {

View File

@@ -18,7 +18,7 @@
namespace ftxui {
Terminal::Dimensions Terminal::Size() {
Dimensions Terminal::Size() {
#if defined(__EMSCRIPTEN__)
return Dimensions{140, 43};
#elif defined(_WIN32)