From af208519eba2757cd7c66493fd76b14c23dabeda Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Mon, 7 Oct 2024 12:52:18 -0700 Subject: [PATCH] ImageCornerRadius In the end I think people might be surprised by the default behavior to clip corners, so I've opted to split this behavior into a separate component that defaults to no corner radius. --- CHANGELOG.md | 3 ++- src/widgets/image.rs | 28 +++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2121e38..66b16c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,7 +68,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `WrapperWidget::activate`'s default implementation now activates the wrapped widget. - `Space` now intercepts mouse events if its color has a non-zero alpha channel. -- `Image` now honors `CornerRadius`. Thanks to @danbulant for this change! ### Fixed @@ -230,6 +229,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `Scroll::preserve_max_scroll` controls whether the scroll view automatically scrolls when currently scrolled to the maximum and its child grows. The default is `true`, which was the behavior before this flag was added. +- `Image` now supports `ImageCornerRadius`. Thanks to @danbulant for helping + with this change! [139]: https://github.com/khonsulabs/cushy/issues/139 diff --git a/src/widgets/image.rs b/src/widgets/image.rs index 3981cf3..e24f025 100644 --- a/src/widgets/image.rs +++ b/src/widgets/image.rs @@ -2,12 +2,14 @@ use figures::units::{Px, UPx}; use figures::{FloatConversion, IntoSigned, IntoUnsigned, Point, Rect, ScreenScale, Size, Zero}; -use kludgine::shapes::Shape; -use kludgine::{AnyTexture, CollectedTexture, Color, LazyTexture, SharedTexture, Texture, TextureRegion}; +use kludgine::shapes::{CornerRadii, Shape}; +use kludgine::{ + AnyTexture, CollectedTexture, Color, LazyTexture, SharedTexture, Texture, TextureRegion, +}; use crate::animation::ZeroToOne; use crate::context::{LayoutContext, Trackable}; -use crate::styles::components::CornerRadius; +use crate::styles::Dimension; use crate::value::{IntoValue, Source, Value}; use crate::widget::Widget; use crate::ConstraintLimit; @@ -159,7 +161,7 @@ impl Widget for Image { fn redraw(&mut self, context: &mut crate::context::GraphicsContext<'_, '_, '_, '_>) { self.contents.invalidate_when_changed(context); let opacity = self.opacity.get_tracking_redraw(context); - let radii = context.get(&CornerRadius); + let radii = context.get(&ImageCornerRadius); let radii = radii.map(|r| r.into_px(context.gfx.scale())); self.contents.map(|texture| { @@ -167,7 +169,16 @@ impl Widget for Image { if radii.is_zero() { context.gfx.draw_texture(texture, rect, opacity); } else { - context.gfx.draw_textured_shape(&Shape::textured_round_rect(rect, radii, Rect::from(texture.size()), Color::WHITE), texture, opacity); + context.gfx.draw_textured_shape( + &Shape::textured_round_rect( + rect, + radii, + Rect::from(texture.size()), + Color::WHITE, + ), + texture, + opacity, + ); } }); } @@ -254,3 +265,10 @@ pub enum Aspect { /// size it can be to cover the entire surface. Fill, } + +define_components! { + Image { + /// The corner radius to use to clip when rendering an [`Image`]. + ImageCornerRadius(CornerRadii, "corner_radius", CornerRadii::ZERO) + } +}