Browse Source

LibWeb: Add ScopedCornerRadiusClip

This a simple RAII helper for the BorderRadiusCornerClipper it
samples under the corners on construction, then blits them back
on exiting the scope. This encapsulates a fairly common pattern.
MacDue 3 years ago
parent
commit
b7fd844c9d
1 changed files with 28 additions and 0 deletions
  1. 28 0
      Userland/Libraries/LibWeb/Painting/BorderRadiusCornerClipper.h

+ 28 - 0
Userland/Libraries/LibWeb/Painting/BorderRadiusCornerClipper.h

@@ -60,4 +60,32 @@ private:
     }
 };
 
+struct ScopedCornerRadiusClip {
+    ScopedCornerRadiusClip(Gfx::Painter& painter, Gfx::IntRect const& border_rect, BorderRadiiData const& border_radii, CornerClip corner_clip = CornerClip::Outside, BorderRadiusCornerClipper::UseCachedBitmap use_cached_bitmap = BorderRadiusCornerClipper::UseCachedBitmap::Yes)
+        : m_painter(painter)
+    {
+        if (border_radii.has_any_radius()) {
+            auto clipper = BorderRadiusCornerClipper::create(border_rect, border_radii, corner_clip, use_cached_bitmap);
+            if (!clipper.is_error()) {
+                m_corner_clipper = clipper.release_value();
+                m_corner_clipper->sample_under_corners(m_painter);
+            }
+        }
+    }
+
+    ~ScopedCornerRadiusClip()
+    {
+        if (m_corner_clipper.has_value()) {
+            m_corner_clipper->blit_corner_clipping(m_painter);
+        }
+    }
+
+    AK_MAKE_NONMOVABLE(ScopedCornerRadiusClip);
+    AK_MAKE_NONCOPYABLE(ScopedCornerRadiusClip);
+
+private:
+    Gfx::Painter& m_painter;
+    Optional<BorderRadiusCornerClipper> m_corner_clipper;
+};
+
 }