Skip to main content

ferroui_android/
cursor_factory.rs

1use ferroui_base::input::StandardCursorType;
2use ferroui_base::media::imaging::Bitmap;
3use ferroui_base::platform::{ICursorFactory, ICursorImpl};
4use ferroui_base::PixelPoint;
5use std::any::Any;
6use std::rc::Rc;
7
8/// The cursor factory of a platform that shows no cursor of the
9/// application: every cursor is the same empty cursor.
10#[derive(Default)]
11pub struct CursorFactory;
12
13struct CursorImpl;
14
15ferroui_base::ferro_thread_local! {
16    static ZERO_CURSOR: Rc<CursorImpl> = Rc::new(CursorImpl);
17}
18
19impl CursorImpl {
20    fn zero_cursor() -> Rc<dyn ICursorImpl> {
21        ZERO_CURSOR.with(|cursor| cursor.clone())
22    }
23}
24
25impl ICursorImpl for CursorImpl {
26    fn dispose(&self) {}
27
28    fn as_any(&self) -> &dyn Any {
29        self
30    }
31}
32
33impl ICursorFactory for CursorFactory {
34    fn get_cursor(&self, _cursor_type: StandardCursorType) -> Rc<dyn ICursorImpl> {
35        CursorImpl::zero_cursor()
36    }
37
38    fn create_cursor(&self, _cursor: &Bitmap, _hot_spot: PixelPoint) -> Rc<dyn ICursorImpl> {
39        CursorImpl::zero_cursor()
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    // Not from the reference, which has no tests of the factory.
46    use super::*;
47
48    #[test]
49    fn every_standard_cursor_is_the_same_cursor() {
50        let factory = CursorFactory;
51        let arrow = factory.get_cursor(StandardCursorType::Arrow);
52        let hand = factory.get_cursor(StandardCursorType::Hand);
53
54        assert!(std::ptr::addr_eq(Rc::as_ptr(&arrow), Rc::as_ptr(&hand)));
55        arrow.dispose();
56    }
57}