aylis_core/
cursor_image.rs1use std::sync::Arc;
9
10use crate::component::ComponentId;
11use crate::image::{Image, ImageSource};
12use crate::ui::Ui;
13
14pub const MAX_CURSOR_SIDE: u32 = 1024;
17
18const RECENT: usize = 32;
21
22#[derive(Debug, PartialEq, Eq)]
25pub struct CursorImage {
26 id: u64,
27 width: u32,
28 height: u32,
29 hot_x: u32,
30 hot_y: u32,
31 rgba: Vec<u8>,
32}
33
34impl CursorImage {
35 pub fn id(&self) -> u64 {
38 self.id
39 }
40
41 pub fn width(&self) -> u32 {
42 self.width
43 }
44
45 pub fn height(&self) -> u32 {
46 self.height
47 }
48
49 pub fn hotspot(&self) -> (u32, u32) {
51 (self.hot_x, self.hot_y)
52 }
53
54 pub fn rgba(&self) -> &[u8] {
56 &self.rgba
57 }
58}
59
60#[derive(Default)]
62struct CursorImages {
63 next: u64,
64 by_component: Vec<(ComponentId, Arc<CursorImage>)>,
65 recent: Vec<Arc<CursorImage>>,
66}
67
68fn rgba_of(image: &Image) -> Result<(u32, u32, Vec<u8>), String> {
70 match image.source() {
71 ImageSource::Rgba { width, height, pixels } => Ok((*width, *height, pixels.clone())),
72 ImageSource::Bgra { width, height, pixels } => {
73 let mut p = pixels.clone();
74 p.chunks_exact_mut(4).for_each(|px| px.swap(0, 2));
75 Ok((*width, *height, p))
76 }
77 ImageSource::Encoded(_) => Err("o cursor precisa da imagem em pixels (RGBA ou BGRA), não de um arquivo codificado".into()),
78 }
79}
80
81impl Ui {
82 pub fn set_cursor_image(&mut self, id: ComponentId, image: &Image, hot_x: u32, hot_y: u32) -> Result<(), String> {
88 if !self.contains(id) {
89 return Err(crate::Error::InvalidId.to_string());
90 }
91 let (width, height, rgba) = rgba_of(image)?;
92 if width == 0 || height == 0 || width > MAX_CURSOR_SIDE || height > MAX_CURSOR_SIDE {
93 return Err(format!("o cursor precisa ter de 1 a {MAX_CURSOR_SIDE} pixels por lado (veio {width} × {height})"));
94 }
95 if rgba.len() != width as usize * height as usize * 4 {
96 return Err(format!("o cursor de {width} × {height} precisa de {} bytes (veio {})", width as usize * height as usize * 4, rgba.len()));
97 }
98 if hot_x >= width || hot_y >= height {
99 return Err(format!("o ponto de clique ({hot_x}, {hot_y}) fica fora da imagem de {width} × {height}"));
100 }
101 let mut images = std::mem::take(self.extension::<CursorImages>());
102 let found = images.recent.iter().find(|c| c.width == width && c.height == height && c.hot_x == hot_x && c.hot_y == hot_y && c.rgba == rgba).cloned();
103 let cursor = match found {
104 Some(c) => c,
105 None => {
106 images.next += 1;
107 let c = Arc::new(CursorImage { id: images.next, width, height, hot_x, hot_y, rgba });
108 if images.recent.len() >= RECENT {
109 images.recent.remove(0);
110 }
111 images.recent.push(c.clone());
112 c
113 }
114 };
115 images.by_component.retain(|(c, _)| *c != id && self.contains(*c));
116 images.by_component.push((id, cursor));
117 *self.extension::<CursorImages>() = images;
118 Ok(())
119 }
120
121 pub fn clear_cursor_image(&mut self, id: ComponentId) {
123 if self.extension_ref::<CursorImages>().is_some() {
124 self.extension::<CursorImages>().by_component.retain(|(c, _)| *c != id);
125 }
126 }
127
128 pub fn cursor_image_of(&self, id: ComponentId) -> Option<Arc<CursorImage>> {
130 let images = self.extension_ref::<CursorImages>()?;
131 images.by_component.iter().find(|(c, _)| *c == id).map(|(_, i)| i.clone())
132 }
133
134 pub fn cursor_image(&self, root: ComponentId) -> Option<Arc<CursorImage>> {
138 let images = self.extension_ref::<CursorImages>()?;
139 if images.by_component.is_empty() || self.busy_cursor() {
140 return None;
141 }
142 let mut current = self.hovered(root);
143 while let Some(c) = current {
144 if let Some((_, image)) = images.by_component.iter().find(|(id, _)| *id == c) {
145 return Some(image.clone());
146 }
147 if self.cb(c).is_some_and(|cb| cb.cursor() != crate::CursorShape::Default) {
148 return None;
149 }
150 current = self.parent(c);
151 }
152 None
153 }
154}