diff --git a/CMakeLists.txt b/CMakeLists.txt index cb7cb13..959fed3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,17 +23,18 @@ else() endif() add_library(screen + include/ftxui/screen/box.hpp + include/ftxui/screen/color.hpp + include/ftxui/screen/color_info.hpp + include/ftxui/screen/screen.hpp + include/ftxui/screen/string.hpp src/ftxui/screen/box.cpp src/ftxui/screen/color.cpp src/ftxui/screen/color_info.cpp src/ftxui/screen/screen.cpp src/ftxui/screen/string.cpp src/ftxui/screen/terminal.cpp - include/ftxui/screen/box.hpp - include/ftxui/screen/color.hpp - include/ftxui/screen/color_info.hpp - include/ftxui/screen/screen.hpp - include/ftxui/screen/string.hpp + src/ftxui/screen/util.hpp ) add_library(dom diff --git a/src/ftxui/component/menu.cpp b/src/ftxui/component/menu.cpp index c7095ec..9b3b425 100644 --- a/src/ftxui/component/menu.cpp +++ b/src/ftxui/component/menu.cpp @@ -14,7 +14,8 @@ #include "ftxui/component/screen_interactive.hpp" // for Component #include "ftxui/dom/elements.hpp" // for operator|, Element, reflect, text, nothing, select, vbox, Elements, focus #include "ftxui/screen/box.hpp" // for Box -#include "ftxui/util/ref.hpp" // for Ref, ConstStringListRef, ConstStringRef +#include "ftxui/screen/util.hpp" +#include "ftxui/util/ref.hpp" // for Ref, ConstStringListRef, ConstStringRef namespace ftxui { @@ -74,7 +75,7 @@ class MenuBase : public ComponentBase { if (event == Event::TabReverse && size()) *selected_ = (*selected_ + size() - 1) % size(); - *selected_ = std::clamp(*selected_, 0, size() - 1); + *selected_ = util::clamp(*selected_, 0, size() - 1); if (*selected_ != old_selected) { focused_entry() = *selected_; @@ -131,7 +132,7 @@ class MenuBase : public ComponentBase { if (event.mouse().button == Mouse::WheelDown) (*selected_)++; - *selected_ = std::clamp(*selected_, 0, size() - 1); + *selected_ = util::clamp(*selected_, 0, size() - 1); if (*selected_ != old_selected) option_->on_change(); @@ -140,8 +141,8 @@ class MenuBase : public ComponentBase { void Clamp() { boxes_.resize(size()); - *selected_ = std::clamp(*selected_, 0, size() - 1); - focused_entry() = std::clamp(focused_entry(), 0, size() - 1); + *selected_ = util::clamp(*selected_, 0, size() - 1); + focused_entry() = util::clamp(focused_entry(), 0, size() - 1); } bool Focusable() const final { return entries_.size(); } diff --git a/src/ftxui/component/radiobox.cpp b/src/ftxui/component/radiobox.cpp index 618d301..f53cc41 100644 --- a/src/ftxui/component/radiobox.cpp +++ b/src/ftxui/component/radiobox.cpp @@ -14,7 +14,8 @@ #include "ftxui/component/screen_interactive.hpp" // for Component #include "ftxui/dom/elements.hpp" // for operator|, reflect, text, Element, hbox, vbox, Elements, focus, nothing, select #include "ftxui/screen/box.hpp" // for Box -#include "ftxui/util/ref.hpp" // for Ref, ConstStringListRef +#include "ftxui/screen/util.hpp" +#include "ftxui/util/ref.hpp" // for Ref, ConstStringListRef namespace ftxui { @@ -91,7 +92,7 @@ class RadioboxBase : public ComponentBase { if (event == Event::TabReverse && size()) hovered_ = (hovered_ + size() - 1) % size(); - hovered_ = std::clamp(hovered_, 0, size() - 1); + hovered_ = util::clamp(hovered_, 0, size() - 1); if (hovered_ != old_hovered) { focused_entry() = hovered_; @@ -145,7 +146,7 @@ class RadioboxBase : public ComponentBase { if (event.mouse().button == Mouse::WheelDown) (hovered_)++; - hovered_ = std::clamp(hovered_, 0, size() - 1); + hovered_ = util::clamp(hovered_, 0, size() - 1); if (hovered_ != old_hovered) option_->on_change(); @@ -155,9 +156,9 @@ class RadioboxBase : public ComponentBase { void Clamp() { boxes_.resize(size()); - *selected_ = std::clamp(*selected_, 0, size() - 1); - focused_entry() = std::clamp(focused_entry(), 0, size() - 1); - hovered_ = std::clamp(hovered_, 0, size() - 1); + *selected_ = util::clamp(*selected_, 0, size() - 1); + focused_entry() = util::clamp(focused_entry(), 0, size() - 1); + hovered_ = util::clamp(hovered_, 0, size() - 1); } bool Focusable() const final { return entries_.size(); } diff --git a/src/ftxui/component/toggle.cpp b/src/ftxui/component/toggle.cpp index bc143e9..fb11a7f 100644 --- a/src/ftxui/component/toggle.cpp +++ b/src/ftxui/component/toggle.cpp @@ -12,7 +12,8 @@ #include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed #include "ftxui/dom/elements.hpp" // for operator|, Element, Elements, hbox, reflect, separator, text, focus, nothing, select #include "ftxui/screen/box.hpp" // for Box -#include "ftxui/util/ref.hpp" // for Ref, ConstStringListRef +#include "ftxui/screen/util.hpp" +#include "ftxui/util/ref.hpp" // for Ref, ConstStringListRef namespace ftxui { @@ -68,7 +69,7 @@ class ToggleBase : public ComponentBase { if (event == Event::TabReverse && size()) *selected_ = (*selected_ + size() - 1) % size(); - *selected_ = std::clamp(*selected_, 0, size() - 1); + *selected_ = util::clamp(*selected_, 0, size() - 1); if (old_selected != *selected_) { focused_entry() = *selected_; @@ -108,8 +109,8 @@ class ToggleBase : public ComponentBase { void Clamp() { boxes_.resize(size()); - *selected_ = std::clamp(*selected_, 0, size() - 1); - focused_entry() = std::clamp(focused_entry(), 0, size() - 1); + *selected_ = util::clamp(*selected_, 0, size() - 1); + focused_entry() = util::clamp(focused_entry(), 0, size() - 1); } bool Focusable() const final { return size(); } diff --git a/src/ftxui/screen/util.hpp b/src/ftxui/screen/util.hpp new file mode 100644 index 0000000..d1d1a9f --- /dev/null +++ b/src/ftxui/screen/util.hpp @@ -0,0 +1,15 @@ +namespace ftxui { +namespace util { + +// Similar to std::clamp, but allow hi to be lower than lo. +template +constexpr const T& clamp(const T& v, const T& lo, const T& hi) { + return v < lo ? lo : hi < v ? hi : v; +} + +} // namespace util +} // namespace ftxui + +// Copyright 2022 Arthur Sonzogni. All rights reserved. +// Use of this source code is governed by the MIT license that can be found in +// the LICENSE file. diff --git a/tools/license_headers.cpp b/tools/license_headers.cpp index 6c0a379..2986cb9 100644 --- a/tools/license_headers.cpp +++ b/tools/license_headers.cpp @@ -1,4 +1,4 @@ -// Copyright 2021 Arthur Sonzogni. All rights reserved. +// Copyright 2022 Arthur Sonzogni. All rights reserved. // Use of this source code is governed by the MIT license that can be found in // the LICENSE file.