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.
This commit is contained in:
Jonathan Johnson 2024-10-07 12:52:18 -07:00
parent 2ed140a0fe
commit af208519eb
No known key found for this signature in database
GPG key ID: A66D6A34D6620579
2 changed files with 25 additions and 6 deletions

View file

@ -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

View file

@ -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<Dimension>, "corner_radius", CornerRadii::ZERO)
}
}