From 49e8cc57d35ba745d740648513ad5d272a694653 Mon Sep 17 00:00:00 2001 From: Tushar Maheshwari Date: Tue, 3 Aug 2021 02:49:29 +0530 Subject: [PATCH] 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). --- include/ftxui/dom/elements.hpp | 5 +++++ include/ftxui/screen/screen.hpp | 19 +++++++------------ include/ftxui/screen/terminal.hpp | 31 +++++++++++++++---------------- src/ftxui/dom/util.cpp | 10 ++++++++++ src/ftxui/screen/screen.cpp | 27 +++++++-------------------- src/ftxui/screen/terminal.cpp | 2 +- 6 files changed, 45 insertions(+), 49 deletions(-) diff --git a/include/ftxui/dom/elements.hpp b/include/ftxui/dom/elements.hpp index af250ac..b9be11b 100644 --- a/include/ftxui/dom/elements.hpp +++ b/include/ftxui/dom/elements.hpp @@ -8,6 +8,7 @@ #include "ftxui/screen/box.hpp" #include "ftxui/screen/color.hpp" #include "ftxui/screen/screen.hpp" +#include "ftxui/screen/terminal.hpp" namespace ftxui { class Node; @@ -102,6 +103,10 @@ Element nothing(Element element); // combinaison with dbox. Element clear_under(Element element); +namespace Dimension { +Dimensions Fit(Element&); +} // namespace Dimension + } // namespace ftxui // Make container able to take any number of children as input. diff --git a/include/ftxui/screen/screen.hpp b/include/ftxui/screen/screen.hpp index a76eb2f..ba45bc3 100644 --- a/include/ftxui/screen/screen.hpp +++ b/include/ftxui/screen/screen.hpp @@ -7,10 +7,9 @@ #include "ftxui/screen/box.hpp" #include "ftxui/screen/color.hpp" +#include "ftxui/screen/terminal.hpp" namespace ftxui { -class Node; -using Element = std::shared_ptr; /// @brief A unicode character and its associated style. /// @ingroup screen @@ -41,14 +40,10 @@ struct Pixel { /// @brief Define how the Screen's dimensions should look like. /// @ingroup screen -struct Dimension { - static Dimension Fixed(int); - static Dimension Fit(Element&); - static Dimension Full(); - - int dimx; - int dimy; -}; +namespace Dimension { +Dimensions Fixed(int); +Dimensions Full(); +} // namespace Dimension /// @brief A rectangular grid of Pixel. /// @ingroup screen @@ -56,8 +51,8 @@ class Screen { public: // Constructors: Screen(int dimx, int dimy); - static Screen Create(Dimension dimension); - static Screen Create(Dimension width, Dimension height); + static Screen Create(Dimensions dimension); + static Screen Create(Dimensions width, Dimensions height); // Node write into the screen using Screen::at. wchar_t& at(int x, int y); diff --git a/include/ftxui/screen/terminal.hpp b/include/ftxui/screen/terminal.hpp index 3bb7a13..9b4364c 100644 --- a/include/ftxui/screen/terminal.hpp +++ b/include/ftxui/screen/terminal.hpp @@ -2,24 +2,23 @@ #define FTXUI_CORE_TERMINAL_HPP namespace ftxui { - -class Terminal { - public: - struct Dimensions { - int dimx; - int dimy; - }; - static Dimensions Size(); - - enum Color { - Palette1, - Palette16, - Palette256, - TrueColor, - }; - static Color ColorSupport(); +struct Dimensions { + int dimx; + int dimy; }; +namespace Terminal { +Dimensions Size(); + +enum Color { + Palette1, + Palette16, + Palette256, + TrueColor, +}; +Color ColorSupport(); +} // namespace Terminal + } // namespace ftxui #endif /* end of include guard: FTXUI_CORE_TERMINAL_HPP */ diff --git a/src/ftxui/dom/util.cpp b/src/ftxui/dom/util.cpp index 9a49161..ba08a7b 100644 --- a/src/ftxui/dom/util.cpp +++ b/src/ftxui/dom/util.cpp @@ -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. diff --git a/src/ftxui/screen/screen.cpp b/src/ftxui/screen/screen.cpp index 65ae09f..1200d56 100644 --- a/src/ftxui/screen/screen.cpp +++ b/src/ftxui/screen/screen.cpp @@ -2,8 +2,6 @@ #include // for operator<<, basic_ostream, wstringstream, stringstream, flush, cout, ostream #include // 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(dimx)) { diff --git a/src/ftxui/screen/terminal.cpp b/src/ftxui/screen/terminal.cpp index 1368439..5f02fc0 100644 --- a/src/ftxui/screen/terminal.cpp +++ b/src/ftxui/screen/terminal.cpp @@ -18,7 +18,7 @@ namespace ftxui { -Terminal::Dimensions Terminal::Size() { +Dimensions Terminal::Size() { #if defined(__EMSCRIPTEN__) return Dimensions{140, 43}; #elif defined(_WIN32)