Skip to content

Commit

Permalink
Merge pull request iced-rs#2163 from hicaru/svg_hover
Browse files Browse the repository at this point in the history
added svg hover, for styles impl
  • Loading branch information
hecrj authored Jan 18, 2024
2 parents 070abff + b083eda commit 61e3d85
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 3 additions & 0 deletions style/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ pub trait StyleSheet {

/// Produces the [`Appearance`] of the svg.
fn appearance(&self, style: &Self::Style) -> Appearance;

/// Produces the hovered [`Appearance`] of a svg content.
fn hovered(&self, style: &Self::Style) -> Appearance;
}
8 changes: 8 additions & 0 deletions style/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,10 @@ impl svg::StyleSheet for Theme {
Svg::Custom(custom) => custom.appearance(self),
}
}

fn hovered(&self, style: &Self::Style) -> svg::Appearance {
self.appearance(style)
}
}

impl svg::StyleSheet for fn(&Theme) -> svg::Appearance {
Expand All @@ -947,6 +951,10 @@ impl svg::StyleSheet for fn(&Theme) -> svg::Appearance {
fn appearance(&self, style: &Self::Style) -> svg::Appearance {
(self)(style)
}

fn hovered(&self, style: &Self::Style) -> svg::Appearance {
self.appearance(style)
}
}

/// The style of a scrollable.
Expand Down
9 changes: 7 additions & 2 deletions widget/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,15 @@ where
theme: &Renderer::Theme,
_style: &renderer::Style,
layout: Layout<'_>,
_cursor: mouse::Cursor,
cursor: mouse::Cursor,
_viewport: &Rectangle,
) {
let Size { width, height } = renderer.dimensions(&self.handle);
let image_size = Size::new(width as f32, height as f32);

let bounds = layout.bounds();
let adjusted_fit = self.content_fit.fit(image_size, bounds.size());
let is_mouse_over = cursor.is_over(bounds);

let render = |renderer: &mut Renderer| {
let offset = Vector::new(
Expand All @@ -162,7 +163,11 @@ where
..bounds
};

let appearance = theme.appearance(&self.style);
let appearance = if is_mouse_over {
theme.hovered(&self.style)
} else {
theme.appearance(&self.style)
};

renderer.draw(
self.handle.clone(),
Expand Down

0 comments on commit 61e3d85

Please sign in to comment.