fix: Svg status not getting updated
This commit is contained in:
parent
ec4b925616
commit
2e88efddd5
1 changed files with 32 additions and 12 deletions
|
|
@ -20,9 +20,10 @@ use crate::core::mouse;
|
||||||
use crate::core::renderer;
|
use crate::core::renderer;
|
||||||
use crate::core::svg;
|
use crate::core::svg;
|
||||||
use crate::core::widget::Tree;
|
use crate::core::widget::Tree;
|
||||||
|
use crate::core::window;
|
||||||
use crate::core::{
|
use crate::core::{
|
||||||
Color, ContentFit, Element, Layout, Length, Point, Rectangle, Rotation,
|
Clipboard, Color, ContentFit, Element, Event, Layout, Length, Point,
|
||||||
Size, Theme, Vector, Widget,
|
Rectangle, Rotation, Shell, Size, Theme, Vector, Widget,
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
@ -62,6 +63,7 @@ where
|
||||||
class: Theme::Class<'a>,
|
class: Theme::Class<'a>,
|
||||||
rotation: Rotation,
|
rotation: Rotation,
|
||||||
opacity: f32,
|
opacity: f32,
|
||||||
|
status: Option<Status>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Theme> Svg<'a, Theme>
|
impl<'a, Theme> Svg<'a, Theme>
|
||||||
|
|
@ -78,6 +80,7 @@ where
|
||||||
class: Theme::default(),
|
class: Theme::default(),
|
||||||
rotation: Rotation::default(),
|
rotation: Rotation::default(),
|
||||||
opacity: 1.0,
|
opacity: 1.0,
|
||||||
|
status: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -194,6 +197,30 @@ where
|
||||||
layout::Node::new(final_size)
|
layout::Node::new(final_size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn update(
|
||||||
|
&mut self,
|
||||||
|
_state: &mut Tree,
|
||||||
|
event: &Event,
|
||||||
|
layout: Layout<'_>,
|
||||||
|
cursor: mouse::Cursor,
|
||||||
|
_renderer: &Renderer,
|
||||||
|
_clipboard: &mut dyn Clipboard,
|
||||||
|
shell: &mut Shell<'_, Message>,
|
||||||
|
_viewport: &Rectangle,
|
||||||
|
) {
|
||||||
|
let current_status = if cursor.is_over(layout.bounds()) {
|
||||||
|
Status::Hovered
|
||||||
|
} else {
|
||||||
|
Status::Idle
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Event::Window(window::Event::RedrawRequested(_now)) = event {
|
||||||
|
self.status = Some(current_status);
|
||||||
|
} else if self.status.is_some_and(|status| status != current_status) {
|
||||||
|
shell.request_redraw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn draw(
|
fn draw(
|
||||||
&self,
|
&self,
|
||||||
_state: &Tree,
|
_state: &Tree,
|
||||||
|
|
@ -201,7 +228,7 @@ where
|
||||||
theme: &Theme,
|
theme: &Theme,
|
||||||
_style: &renderer::Style,
|
_style: &renderer::Style,
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
cursor: mouse::Cursor,
|
_cursor: mouse::Cursor,
|
||||||
_viewport: &Rectangle,
|
_viewport: &Rectangle,
|
||||||
) {
|
) {
|
||||||
let Size { width, height } = renderer.measure_svg(&self.handle);
|
let Size { width, height } = renderer.measure_svg(&self.handle);
|
||||||
|
|
@ -230,15 +257,8 @@ where
|
||||||
|
|
||||||
let drawing_bounds = Rectangle::new(position, final_size);
|
let drawing_bounds = Rectangle::new(position, final_size);
|
||||||
|
|
||||||
let is_mouse_over = cursor.is_over(bounds);
|
let style =
|
||||||
|
theme.style(&self.class, self.status.unwrap_or(Status::Idle));
|
||||||
let status = if is_mouse_over {
|
|
||||||
Status::Hovered
|
|
||||||
} else {
|
|
||||||
Status::Idle
|
|
||||||
};
|
|
||||||
|
|
||||||
let style = theme.style(&self.class, status);
|
|
||||||
|
|
||||||
renderer.draw_svg(
|
renderer.draw_svg(
|
||||||
svg::Svg {
|
svg::Svg {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue