strata-ui-platform 0.1.4

Native Hardware Abstraction Layer for Android, iOS, macOS, Windows, Linux, and Web
Documentation
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! System Cursor Theme and Xcursor parser.
//!
//! Loads native cursor themes (defaulting to the host's active GNOME/Yaru cursor pack)
//! and provides rasterization utilities for the emulated MCP cursor.

use crate::window::CursorIcon;
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};

/// Decoded 32-bit ARGB/BGRA cursor bitmap with hotspot coordinates.
#[derive(Debug, Clone)]
pub struct CursorBitmap {
    pub width: u32,
    pub height: u32,
    pub xhot: u32,
    pub yhot: u32,
    pub pixels: Vec<u8>, // 4 bytes per pixel: [B, G, R, A]
}

impl CursorBitmap {
    /// Creates a cursor bitmap from raw pixel buffer with explicit hotspot.
    pub fn new(width: u32, height: u32, xhot: u32, yhot: u32, pixels: Vec<u8>) -> Self {
        Self {
            width,
            height,
            xhot,
            yhot,
            pixels,
        }
    }

    /// Parses an Xcursor binary image from a byte slice.
    pub fn parse_xcursor(data: &[u8], target_size: u32) -> Option<Self> {
        if data.len() < 16 {
            return None;
        }
        let magic = u32::from_le_bytes(data[0..4].try_into().ok()?);
        if magic != 0x72756358 {
            // Magic 'Xcur' in little endian
            return None;
        }

        let ntoc = u32::from_le_bytes(data[12..16].try_into().ok()?);
        let mut best_pos = None;
        let mut best_diff = u32::MAX;

        for i in 0..ntoc as usize {
            let offset = 16 + i * 12;
            if offset + 12 > data.len() {
                break;
            }
            let ctype = u32::from_le_bytes(data[offset..offset + 4].try_into().ok()?);
            let subtype = u32::from_le_bytes(data[offset + 4..offset + 8].try_into().ok()?);
            let pos = u32::from_le_bytes(data[offset + 8..offset + 12].try_into().ok()?);

            if ctype == 0xfffd0002 {
                // Image chunk
                let diff = subtype.abs_diff(target_size);
                if diff < best_diff {
                    best_diff = diff;
                    best_pos = Some(pos as usize);
                    if diff == 0 {
                        break;
                    }
                }
            }
        }

        let pos = best_pos?;
        if pos + 36 > data.len() {
            return None;
        }

        let width = u32::from_le_bytes(data[pos + 16..pos + 20].try_into().ok()?);
        let height = u32::from_le_bytes(data[pos + 20..pos + 24].try_into().ok()?);
        let xhot = u32::from_le_bytes(data[pos + 24..pos + 28].try_into().ok()?);
        let yhot = u32::from_le_bytes(data[pos + 28..pos + 32].try_into().ok()?);

        let pixel_bytes = (width as usize) * (height as usize) * 4;
        let pixel_start = pos + 36;
        if pixel_start + pixel_bytes > data.len() {
            return None;
        }

        let pixels = data[pixel_start..pixel_start + pixel_bytes].to_vec();

        Some(Self {
            width,
            height,
            xhot,
            yhot,
            pixels,
        })
    }

    /// Loads an Xcursor binary file from disk.
    pub fn from_file<P: AsRef<Path>>(path: P, target_size: u32) -> Option<Self> {
        let mut file = File::open(path).ok()?;
        let mut buffer = Vec::new();
        file.read_to_end(&mut buffer).ok()?;
        Self::parse_xcursor(&buffer, target_size)
    }
}

/// Active Cursor Theme holding loaded native and fallback cursor shapes.
#[derive(Debug, Clone)]
pub struct CursorTheme {
    pub name: String,
    pub size: u32,
    cursors: HashMap<CursorIcon, CursorBitmap>,
}

impl Default for CursorTheme {
    fn default() -> Self {
        Self::load_active()
    }
}

impl CursorTheme {
    /// Detects the host window manager cursor theme (e.g. Yaru) and loads standard sizes.
    pub fn load_active() -> Self {
        let theme_name = detect_host_cursor_theme().unwrap_or_else(|| "Yaru".to_string());
        Self::load(&theme_name, 24)
    }

    /// Loads a cursor theme by name and size.
    pub fn load(theme_name: &str, size: u32) -> Self {
        let mut cursors = HashMap::new();

        // 1. Try loading from host theme directories
        let search_dirs = [
            PathBuf::from(format!("/usr/share/icons/{}/cursors", theme_name)),
            dirs_icon_theme(theme_name),
            PathBuf::from("/usr/share/icons/Yaru/cursors"),
            PathBuf::from("/usr/share/icons/default/cursors"),
        ];

        let icon_mappings = [
            (CursorIcon::Default, &["default", "arrow", "left_ptr"][..]),
            (
                CursorIcon::Pointer,
                &["pointer", "pointing_hand", "hand2", "hand"][..],
            ),
            (CursorIcon::Text, &["text", "xterm", "ibeam"][..]),
            (CursorIcon::Crosshair, &["crosshair", "cross"][..]),
            (CursorIcon::Move, &["move", "pointer-move", "size_all"][..]),
            (
                CursorIcon::NotAllowed,
                &["not-allowed", "forbidden", "crossed_circle"][..],
            ),
            (CursorIcon::Grab, &["grab", "openhand"][..]),
            (CursorIcon::Grabbing, &["grabbing", "closedhand"][..]),
            (CursorIcon::Wait, &["wait", "watch"][..]),
        ];

        for (icon, filenames) in icon_mappings {
            let mut loaded = None;
            for dir in &search_dirs {
                if !dir.exists() {
                    continue;
                }
                for fname in filenames {
                    let path = dir.join(fname);
                    if path.exists() {
                        if let Some(bmp) = CursorBitmap::from_file(&path, size) {
                            loaded = Some(bmp);
                            break;
                        }
                    }
                }
                if loaded.is_some() {
                    break;
                }
            }

            // If not found on disk, use embedded authentic Yaru fallback
            let final_bmp = loaded.unwrap_or_else(|| Self::embedded_fallback(icon));
            cursors.insert(icon, final_bmp);
        }

        Self {
            name: theme_name.to_string(),
            size,
            cursors,
        }
    }

    /// Returns the cursor bitmap for a specific icon style.
    pub fn get(&self, icon: CursorIcon) -> &CursorBitmap {
        if let Some(bmp) = self.cursors.get(&icon) {
            bmp
        } else {
            self.cursors
                .get(&CursorIcon::Default)
                .expect("Default cursor must exist")
        }
    }

    /// Renders the cursor into a Cairo graphics context with opacity and press indicator.
    #[cfg(all(target_os = "linux", feature = "gtk-compat"))]
    pub fn render_cairo(
        &self,
        cr: &cairo::Context,
        icon: CursorIcon,
        x: f32,
        y: f32,
        opacity: f32,
        is_down: bool,
    ) {
        if opacity <= 0.001 {
            return;
        }

        let bmp = self.get(icon);
        let draw_x = (x - bmp.xhot as f32) as f64;
        let draw_y = (y - bmp.yhot as f32) as f64;

        if let Ok(mut surface) =
            cairo::ImageSurface::create(cairo::Format::ARgb32, bmp.width as i32, bmp.height as i32)
        {
            if let Ok(mut data) = surface.data() {
                if data.len() == bmp.pixels.len() {
                    data.copy_from_slice(&bmp.pixels);
                }
            }

            let _ = cr.save();

            // When mouse is pressed down, draw subtle touch feedback ring
            if is_down {
                cr.set_source_rgba(0.23, 0.51, 0.96, 0.45 * opacity as f64);
                cr.arc(x as f64, y as f64, 7.0, 0.0, 2.0 * std::f64::consts::PI);
                let _ = cr.fill();

                cr.set_source_rgba(1.0, 1.0, 1.0, 0.8 * opacity as f64);
                cr.set_line_width(1.5);
                cr.arc(x as f64, y as f64, 7.0, 0.0, 2.0 * std::f64::consts::PI);
                let _ = cr.stroke();
            }

            let _ = cr.set_source_surface(&surface, draw_x, draw_y);
            if (opacity - 1.0).abs() < 0.001 {
                let _ = cr.paint();
            } else {
                let _ = cr.paint_with_alpha(opacity as f64);
            }

            let _ = cr.restore();
        }
    }

    /// Renders the cursor into a software BGRA32 pixel buffer with alpha blending.
    pub fn render_software(
        &self,
        buffer: &mut [u32],
        buf_width: usize,
        buf_height: usize,
        icon: CursorIcon,
        x: f32,
        y: f32,
        opacity: f32,
        is_down: bool,
    ) {
        if opacity <= 0.001 {
            return;
        }

        let bmp = self.get(icon);
        let start_x = (x as i32) - (bmp.xhot as i32);
        let start_y = (y as i32) - (bmp.yhot as i32);

        // Render touch indicator ring if mouse down
        if is_down {
            let cx = x as i32;
            let cy = y as i32;
            let r_sq = 49; // 7px radius squared
            for dy in -7..=7 {
                for dx in -7..=7 {
                    let d = dx * dx + dy * dy;
                    if d <= r_sq {
                        let px = cx + dx;
                        let py = cy + dy;
                        if px >= 0 && px < buf_width as i32 && py >= 0 && py < buf_height as i32 {
                            let idx = (py as usize) * buf_width + (px as usize);
                            if let Some(target) = buffer.get_mut(idx) {
                                // Blend translucent blue touch highlight
                                let ring_alpha = if d > 25 {
                                    0.8 * opacity
                                } else {
                                    0.45 * opacity
                                };
                                *target = blend_pixel_bgra(*target, 0x3b82f6, ring_alpha);
                            }
                        }
                    }
                }
            }
        }

        for cy in 0..bmp.height as i32 {
            for cx in 0..bmp.width as i32 {
                let target_x = start_x + cx;
                let target_y = start_y + cy;

                if target_x >= 0
                    && target_x < buf_width as i32
                    && target_y >= 0
                    && target_y < buf_height as i32
                {
                    let src_idx = ((cy as usize) * (bmp.width as usize) + (cx as usize)) * 4;
                    let b = bmp.pixels[src_idx] as u32;
                    let g = bmp.pixels[src_idx + 1] as u32;
                    let r = bmp.pixels[src_idx + 2] as u32;
                    let a = bmp.pixels[src_idx + 3] as f32 / 255.0;

                    let final_alpha = (a * opacity).clamp(0.0, 1.0);
                    if final_alpha > 0.001 {
                        let dst_idx = (target_y as usize) * buf_width + (target_x as usize);
                        if let Some(dst) = buffer.get_mut(dst_idx) {
                            let src_color = (r << 16) | (g << 8) | b;
                            *dst = blend_pixel_bgra(*dst, src_color, final_alpha);
                        }
                    }
                }
            }
        }
    }

    /// Authentic embedded Yaru cursor pixel data fallback.
    fn embedded_fallback(icon: CursorIcon) -> CursorBitmap {
        match icon {
            CursorIcon::Pointer => CursorBitmap::new(
                24,
                24,
                8,
                5,
                include_bytes!("../assets/yaru/pointer.bin").to_vec(),
            ),
            CursorIcon::Text => CursorBitmap::new(
                24,
                24,
                11,
                12,
                include_bytes!("../assets/yaru/text.bin").to_vec(),
            ),
            CursorIcon::Crosshair => CursorBitmap::new(
                24,
                24,
                11,
                11,
                include_bytes!("../assets/yaru/crosshair.bin").to_vec(),
            ),
            CursorIcon::NotAllowed => CursorBitmap::new(
                24,
                24,
                12,
                12,
                include_bytes!("../assets/yaru/not-allowed.bin").to_vec(),
            ),
            CursorIcon::Grab => CursorBitmap::new(
                24,
                24,
                11,
                7,
                include_bytes!("../assets/yaru/grab.bin").to_vec(),
            ),
            CursorIcon::Grabbing => CursorBitmap::new(
                24,
                24,
                12,
                11,
                include_bytes!("../assets/yaru/grabbing.bin").to_vec(),
            ),
            _ => CursorBitmap::new(
                24,
                24,
                4,
                4,
                include_bytes!("../assets/yaru/default.bin").to_vec(),
            ),
        }
    }
}

/// Blends a source RGB color onto a destination BGRA/ARGB 32-bit pixel.
#[inline]
fn blend_pixel_bgra(dst: u32, src_rgb: u32, alpha: f32) -> u32 {
    let dst_r = ((dst >> 16) & 0xFF) as f32;
    let dst_g = ((dst >> 8) & 0xFF) as f32;
    let dst_b = (dst & 0xFF) as f32;

    let src_r = ((src_rgb >> 16) & 0xFF) as f32;
    let src_g = ((src_rgb >> 8) & 0xFF) as f32;
    let src_b = (src_rgb & 0xFF) as f32;

    let out_r = (src_r * alpha + dst_r * (1.0 - alpha)).clamp(0.0, 255.0) as u32;
    let out_g = (src_g * alpha + dst_g * (1.0 - alpha)).clamp(0.0, 255.0) as u32;
    let out_b = (src_b * alpha + dst_b * (1.0 - alpha)).clamp(0.0, 255.0) as u32;

    0xFF000000 | (out_r << 16) | (out_g << 8) | out_b
}

fn dirs_icon_theme(theme_name: &str) -> PathBuf {
    if let Ok(home) = std::env::var("HOME") {
        PathBuf::from(home)
            .join(".icons")
            .join(theme_name)
            .join("cursors")
    } else {
        PathBuf::from("/tmp")
    }
}

fn detect_host_cursor_theme() -> Option<String> {
    if let Ok(theme) = std::env::var("XCURSOR_THEME") {
        if !theme.is_empty() {
            return Some(theme);
        }
    }

    #[cfg(target_os = "linux")]
    {
        // Try reading gsettings if available
        if let Ok(output) = std::process::Command::new("gsettings")
            .args(["get", "org.gnome.desktop.interface", "cursor-theme"])
            .output()
        {
            if output.status.success() {
                let s = String::from_utf8_lossy(&output.stdout);
                let cleaned = s.trim().trim_matches('\'').trim_matches('"');
                if !cleaned.is_empty() {
                    return Some(cleaned.to_string());
                }
            }
        }
    }

    None
}