diff options
| -rw-r--r-- | include/cru/platform/graphics/Painter.h | 11 | ||||
| -rw-r--r-- | include/cru/platform/graphics/util/Painter.h | 18 | ||||
| -rw-r--r-- | src/ui/render/ScrollBar.cpp | 26 |
3 files changed, 21 insertions, 34 deletions
diff --git a/include/cru/platform/graphics/Painter.h b/include/cru/platform/graphics/Painter.h index de44c7b4..2e122a08 100644 --- a/include/cru/platform/graphics/Painter.h +++ b/include/cru/platform/graphics/Painter.h @@ -1,4 +1,5 @@ #pragma once +#include <type_traits> #include "Base.h" namespace cru::platform::graphics { @@ -42,5 +43,15 @@ struct CRU_PLATFORM_GRAPHICS_API IPainter : virtual IPlatformResource { virtual void PopState() = 0; virtual void EndDraw() = 0; + + template <typename Fn> + std::enable_if_t<std::is_invocable_v<Fn, IPainter*>> WithTransform( + const Matrix& matrix, const Fn& action) { + const auto old = this->GetTransform(); + this->PushState(); + this->ConcatTransform(matrix); + action(this); + this->PopState(); + } }; } // namespace cru::platform::graphics diff --git a/include/cru/platform/graphics/util/Painter.h b/include/cru/platform/graphics/util/Painter.h deleted file mode 100644 index e36d5c55..00000000 --- a/include/cru/platform/graphics/util/Painter.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "../Painter.h" - -#include <functional> -#include <type_traits> - -namespace cru::platform::graphics::util { -template <typename Fn> -void WithTransform(IPainter* painter, const Matrix& matrix, const Fn& action) { - static_assert(std::is_invocable_v<decltype(action), IPainter*>, - "Action must can be be invoked with painter."); - const auto old = painter->GetTransform(); - painter->PushState(); - painter->ConcatTransform(matrix); - action(painter); - painter->PopState(); -} -} // namespace cru::platform::graphics::util diff --git a/src/ui/render/ScrollBar.cpp b/src/ui/render/ScrollBar.cpp index f4f9496e..a3bee22c 100644 --- a/src/ui/render/ScrollBar.cpp +++ b/src/ui/render/ScrollBar.cpp @@ -1,30 +1,24 @@ #include "cru/ui/render/ScrollBar.h" #include "../Helper.h" -#include "cru/base/Base.h" #include "cru/platform/GraphicsBase.h" #include "cru/platform/graphics/Factory.h" #include "cru/platform/graphics/Geometry.h" #include "cru/platform/graphics/Painter.h" -#include "cru/platform/graphics/util/Painter.h" -#include "cru/platform/gui/Base.h" #include "cru/platform/gui/Cursor.h" -#include "cru/platform/gui/Input.h" #include "cru/ui/Base.h" #include "cru/ui/ThemeManager.h" -#include "cru/ui/events/UiEvents.h" -#include "cru/ui/helper/ClickDetector.h" #include "cru/ui/host/WindowHost.h" #include "cru/ui/render/ScrollRenderObject.h" #include <algorithm> +#include <array> #include <cassert> #include <chrono> #include <memory> #include <optional> #include <stdexcept> #include <string> -#include <array> namespace cru::ui::render { using namespace std::chrono_literals; @@ -40,7 +34,7 @@ constexpr std::array<ScrollBarAreaKind, 5> kScrollBarAreaKindList{ ScrollBarAreaKind::Thumb}; std::string GenerateScrollBarThemeColorKey(ScrollBarBrushUsageKind usage, - ScrollBarBrushStateKind state) { + ScrollBarBrushStateKind state) { std::string result = "scrollbar."; switch (usage) { case ScrollBarBrushUsageKind::Arrow: @@ -421,8 +415,8 @@ void HorizontalScrollBar::DrawUpArrow( platform::graphics::IBrush* background_brush) { painter->FillRectangle(area, background_brush); - platform::graphics::util::WithTransform( - painter, Matrix::Translation(area.GetCenter()), + painter->WithTransform( + Matrix::Translation(area.GetCenter()), [this, arrow_brush](platform::graphics::IPainter* painter) { painter->FillGeometry(arrow_geometry_.get(), arrow_brush); }); @@ -434,8 +428,8 @@ void HorizontalScrollBar::DrawDownArrow( platform::graphics::IBrush* background_brush) { painter->FillRectangle(area, background_brush); - platform::graphics::util::WithTransform( - painter, Matrix::Rotation(180) * Matrix::Translation(area.GetCenter()), + painter->WithTransform( + Matrix::Rotation(180) * Matrix::Translation(area.GetCenter()), [this, arrow_brush](platform::graphics::IPainter* painter) { painter->FillGeometry(arrow_geometry_.get(), arrow_brush); }); @@ -569,8 +563,8 @@ void VerticalScrollBar::DrawUpArrow( platform::graphics::IBrush* background_brush) { painter->FillRectangle(area, background_brush); - platform::graphics::util::WithTransform( - painter, Matrix::Rotation(90) * Matrix::Translation(area.GetCenter()), + painter->WithTransform( + Matrix::Rotation(90) * Matrix::Translation(area.GetCenter()), [this, arrow_brush](platform::graphics::IPainter* painter) { painter->FillGeometry(arrow_geometry_.get(), arrow_brush); }); @@ -582,8 +576,8 @@ void VerticalScrollBar::DrawDownArrow( platform::graphics::IBrush* background_brush) { painter->FillRectangle(area, background_brush); - platform::graphics::util::WithTransform( - painter, Matrix::Rotation(270) * Matrix::Translation(area.GetCenter()), + painter->WithTransform( + Matrix::Rotation(270) * Matrix::Translation(area.GetCenter()), [this, arrow_brush](platform::graphics::IPainter* painter) { painter->FillGeometry(arrow_geometry_.get(), arrow_brush); }); |
