1    module mouse
2    
3    type Capture @byval =
4        val x : u32
5        val y : u32
6        val relative_x : u32
7        val relative_y : u32
8        val control : Control
9    
10   type Mouse @[mut local] =
11       val presses = Stream<(MouseAction, Control)>.new
12       var obs capture : Option<Capture> @[mut internal] = None
13       var obs hover : Option<Control> = None
14       local obs position : (u32, u32) = (0, 0)
15       let position_to_control : u32 * u32 * (Control -> bool) -> Option<Control>
16       local tokens = List<Token>.new
17   
18       def update_hovered =
19           let (x, y) = position
20           let maybe_control = position_to_control x y { _.is_receive_hover }
21           if maybe_control <> hover then
22               if hover ? Some control then
23                   control.is_hovered = false
24   
25               hover = maybe_control
26               if maybe_control ? Some control then
27                   control.is_hovered = true
28   
29       fun destruct =
30           for i = tokens.size - 1 downto 0 do
31               tokens[i].discard
32