From 0279bed8c4a3718802aa8e56ebcff84a9173332e Mon Sep 17 00:00:00 2001 From: ArthurSonzogni Date: Thu, 11 Apr 2024 23:07:09 +0200 Subject: [PATCH] Add Box::IsEmpty() --- CHANGELOG.md | 3 +++ include/ftxui/screen/box.hpp | 2 +- src/ftxui/dom/canvas.cpp | 3 ++- src/ftxui/screen/box.cpp | 7 +++---- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a976af7..b441a05e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,9 @@ current (development) reflecting the current scroll position. Proposed by @ibrahimnasson in [issue 752](https://github.com/ArthurSonzogni/FTXUI/issues/752) +### Screen +- Feature: Add `Box::IsEmpty()`. + ### Build - Support for cmake's "unity/jumbo" builds. Fixed by @ClausKlein. diff --git a/include/ftxui/screen/box.hpp b/include/ftxui/screen/box.hpp index 32e08fff..9fc27921 100644 --- a/include/ftxui/screen/box.hpp +++ b/include/ftxui/screen/box.hpp @@ -15,7 +15,7 @@ struct Box { static auto Intersection(Box a, Box b) -> Box; static auto Union(Box a, Box b) -> Box; bool Contain(int x, int y) const; - int Area() const noexcept; + bool IsEmpty(); bool operator==(const Box& other) const; bool operator!=(const Box& other) const; }; diff --git a/src/ftxui/dom/canvas.cpp b/src/ftxui/dom/canvas.cpp index c441e6da..854c8c6c 100644 --- a/src/ftxui/dom/canvas.cpp +++ b/src/ftxui/dom/canvas.cpp @@ -850,8 +850,9 @@ void Canvas::DrawImage(int x, y = 0; } - if (crop.Area() == 0) + if (crop.IsEmpty()) { return; + } const auto xend = x + crop.x_max - crop.x_min; const auto yend = y + crop.y_max - crop.y_min; diff --git a/src/ftxui/screen/box.cpp b/src/ftxui/screen/box.cpp index e3eba85b..0210153b 100644 --- a/src/ftxui/screen/box.cpp +++ b/src/ftxui/screen/box.cpp @@ -39,11 +39,10 @@ bool Box::Contain(int x, int y) const { y_max >= y; } -/// @return the are of the box +/// @return whether the box is empty. /// @ingroup screen -int Box::Area() const noexcept { - const auto area = (x_max - x_min) * (y_max - y_min); - return area < 0 ? 0 : area; +bool Box::IsEmpty() { + return x_min > x_max || y_min > y_max; } /// @return whether |other| is the same as |this|