1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use iced::{
    layout, mouse, Clipboard, Element, Event, Hasher, Layout, Length, Point, Rectangle, Size,
    Widget,
};
use std::hash::Hash;

#[derive(Debug, Default)]
pub struct State {
    mouse_over: bool,
}
impl State {
    pub fn mouse_over(&self) -> bool { self.mouse_over }
}

#[derive(Debug)]
pub struct MouseDetector<'a> {
    width: Length,
    height: Length,
    state: &'a mut State,
}

impl<'a> MouseDetector<'a> {
    pub fn new(state: &'a mut State, width: Length, height: Length) -> Self {
        Self {
            width,
            height,
            state,
        }
    }
}

impl<'a, M, R> Widget<M, R> for MouseDetector<'a>
where
    R: Renderer,
{
    fn width(&self) -> Length { self.width }

    fn height(&self) -> Length { self.height }

    fn layout(&self, _renderer: &R, limits: &layout::Limits) -> layout::Node {
        let limits = limits.width(self.width).height(self.height);

        layout::Node::new(limits.resolve(Size::ZERO))
    }

    fn draw(
        &self,
        renderer: &mut R,
        _defaults: &R::Defaults,
        layout: Layout<'_>,
        _cursor_position: Point,
        _viewport: &Rectangle,
    ) -> R::Output {
        renderer.draw(layout.bounds())
    }

    fn hash_layout(&self, state: &mut Hasher) {
        struct Marker;
        std::any::TypeId::of::<Marker>().hash(state);

        self.width.hash(state);
        self.height.hash(state);
    }

    fn on_event(
        &mut self,
        event: Event,
        layout: Layout<'_>,
        _cursor_position: Point,
        _renderer: &R,
        _clipboard: &mut dyn Clipboard,
        _messages: &mut Vec<M>,
    ) -> iced::event::Status {
        if let Event::Mouse(mouse::Event::CursorMoved {
            position: Point { x, y },
        }) = event
        {
            let bounds = layout.bounds();
            let mouse_over = x > bounds.x
                && x < bounds.x + bounds.width
                && y > bounds.y
                && y < bounds.y + bounds.height;
            if mouse_over != self.state.mouse_over {
                self.state.mouse_over = mouse_over;
            }
        }

        iced::event::Status::Ignored
    }
}

pub trait Renderer: iced::Renderer {
    fn draw(&mut self, bounds: Rectangle) -> Self::Output;
}

impl<'a, M, R> From<MouseDetector<'a>> for Element<'a, M, R>
where
    R: Renderer,
    M: 'a,
{
    fn from(mouse_detector: MouseDetector<'a>) -> Element<'a, M, R> { Element::new(mouse_detector) }
}