use std::sync::Arc;
use crate::component::ComponentId;
use crate::image::{Image, ImageSource};
use crate::ui::Ui;
pub const MAX_CURSOR_SIDE: u32 = 1024;
const RECENT: usize = 32;
#[derive(Debug, PartialEq, Eq)]
pub struct CursorImage {
id: u64,
width: u32,
height: u32,
hot_x: u32,
hot_y: u32,
rgba: Vec<u8>,
}
impl CursorImage {
pub fn id(&self) -> u64 {
self.id
}
pub fn width(&self) -> u32 {
self.width
}
pub fn height(&self) -> u32 {
self.height
}
pub fn hotspot(&self) -> (u32, u32) {
(self.hot_x, self.hot_y)
}
pub fn rgba(&self) -> &[u8] {
&self.rgba
}
}
#[derive(Default)]
struct CursorImages {
next: u64,
by_component: Vec<(ComponentId, Arc<CursorImage>)>,
recent: Vec<Arc<CursorImage>>,
}
fn rgba_of(image: &Image) -> Result<(u32, u32, Vec<u8>), String> {
match image.source() {
ImageSource::Rgba { width, height, pixels } => Ok((*width, *height, pixels.clone())),
ImageSource::Bgra { width, height, pixels } => {
let mut p = pixels.clone();
p.chunks_exact_mut(4).for_each(|px| px.swap(0, 2));
Ok((*width, *height, p))
}
ImageSource::Encoded(_) => Err("o cursor precisa da imagem em pixels (RGBA ou BGRA), não de um arquivo codificado".into()),
}
}
impl Ui {
pub fn set_cursor_image(&mut self, id: ComponentId, image: &Image, hot_x: u32, hot_y: u32) -> Result<(), String> {
if !self.contains(id) {
return Err(crate::Error::InvalidId.to_string());
}
let (width, height, rgba) = rgba_of(image)?;
if width == 0 || height == 0 || width > MAX_CURSOR_SIDE || height > MAX_CURSOR_SIDE {
return Err(format!("o cursor precisa ter de 1 a {MAX_CURSOR_SIDE} pixels por lado (veio {width} × {height})"));
}
if rgba.len() != width as usize * height as usize * 4 {
return Err(format!("o cursor de {width} × {height} precisa de {} bytes (veio {})", width as usize * height as usize * 4, rgba.len()));
}
if hot_x >= width || hot_y >= height {
return Err(format!("o ponto de clique ({hot_x}, {hot_y}) fica fora da imagem de {width} × {height}"));
}
let mut images = std::mem::take(self.extension::<CursorImages>());
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();
let cursor = match found {
Some(c) => c,
None => {
images.next += 1;
let c = Arc::new(CursorImage { id: images.next, width, height, hot_x, hot_y, rgba });
if images.recent.len() >= RECENT {
images.recent.remove(0);
}
images.recent.push(c.clone());
c
}
};
images.by_component.retain(|(c, _)| *c != id && self.contains(*c));
images.by_component.push((id, cursor));
*self.extension::<CursorImages>() = images;
Ok(())
}
pub fn clear_cursor_image(&mut self, id: ComponentId) {
if self.extension_ref::<CursorImages>().is_some() {
self.extension::<CursorImages>().by_component.retain(|(c, _)| *c != id);
}
}
pub fn cursor_image_of(&self, id: ComponentId) -> Option<Arc<CursorImage>> {
let images = self.extension_ref::<CursorImages>()?;
images.by_component.iter().find(|(c, _)| *c == id).map(|(_, i)| i.clone())
}
pub fn cursor_image(&self, root: ComponentId) -> Option<Arc<CursorImage>> {
let images = self.extension_ref::<CursorImages>()?;
if images.by_component.is_empty() || self.busy_cursor() {
return None;
}
let mut current = self.hovered(root);
while let Some(c) = current {
if let Some((_, image)) = images.by_component.iter().find(|(id, _)| *id == c) {
return Some(image.clone());
}
if self.cb(c).is_some_and(|cb| cb.cursor() != crate::CursorShape::Default) {
return None;
}
current = self.parent(c);
}
None
}
}