i_slint_core/cursor.rs
1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
3
4use crate::items::BuiltInMouseCursor;
5
6/// This enum represents different types of mouse cursors. It's a subset of the mouse cursors available in CSS.
7/// For details and pictograms see the [MDN Documentation for cursor](https://developer.mozilla.org/en-US/docs/Web/CSS/cursor#values).
8/// Depending on the backend and used OS unidirectional resize cursors may be replaced with bidirectional ones.
9#[repr(C, u32)]
10#[non_exhaustive]
11#[derive(Debug, Clone, PartialEq)]
12pub enum MouseCursorInner {
13 /// One of the built-in mouse cursors.
14 BuiltIn(BuiltInMouseCursor),
15 /// Custom cursor from an `Image`.
16 CustomMouseCursor {
17 /// Image backing for this cursor.
18 image: crate::graphics::Image,
19 /// X pixel coordinate of the hotspot from the left edge of the image.
20 ///
21 /// The value is clamped to the image bounds.
22 hotspot_x: i32,
23 /// Y pixel coordinate of the hotspot from the top edge of the image.
24 ///
25 /// The value is clamped to the image bounds.
26 hotspot_y: i32,
27 },
28}
29
30impl Default for MouseCursorInner {
31 fn default() -> Self {
32 Self::BuiltIn(BuiltInMouseCursor::Default)
33 }
34}
35
36/// Maps a custom cursor's hotspot from the source image into a buffer rendered at
37/// `rendered_size` pixels, clamped to stay inside it.
38pub fn scaled_hotspot(hotspot: i32, source_size: u32, rendered_size: u32) -> u32 {
39 let scaled = if source_size == 0 {
40 0
41 } else {
42 hotspot as i64 * rendered_size as i64 / source_size as i64
43 };
44 scaled.clamp(0, rendered_size.saturating_sub(1) as i64) as u32
45}