From 5a5f6c92297352073f2f06790b86bfc59cad42a5 Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Thu, 17 Oct 2024 08:54:12 -0700 Subject: [PATCH] Fixed clipping of widgets with negative origins Fixes #183 --- CHANGELOG.md | 4 ++++ src/graphics.rs | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7e9d21..cca8687 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -111,6 +111,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 display scale is changed. - `Button`'s colors are now fully reactive. The caching code to prevent color duplicate change animations has been simplified to fix this. +- Clipping math of widgets with negative origins has been fixed to no longer + allow overdrawing the widget's bounds. This was noticable in the + nested-scroll.rs example when reducing the height of the window below 6 + inches. ### Added diff --git a/src/graphics.rs b/src/graphics.rs index fd01e7f..50e300c 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -88,10 +88,22 @@ impl<'clip, 'gfx, 'pass> Graphics<'clip, 'gfx, 'pass> { /// operations will be relative to the origin of `clip`. pub fn clipped_to(&mut self, clip: Rect) -> Graphics<'_, 'gfx, 'pass> { let region = clip + self.region.origin; + + // If the current region has a negative component, we need to adjust the + // clipped rect before we perform an intersection in unsigned space. + let mut effective_region = region; + if region.origin.x < 0 { + effective_region.size.width += region.origin.x; + effective_region.origin.x = Px::ZERO; + } + if region.origin.y < 0 { + effective_region.size.height += region.origin.y; + effective_region.origin.y = Px::ZERO; + } let new_clip = self .renderer .clip_rect() - .intersection(®ion.into_unsigned()) + .intersection(&effective_region.into_unsigned()) .map(|intersection| intersection - self.renderer.clip_rect().origin) .unwrap_or_default();