Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
feat: map cursor shapes across platform styles
Richie McIlroya1eb92a5e226
diff --git a/crates/cursor-info/src/lib.rs b/crates/cursor-info/src/lib.rsindex 0c22d8570..8dce64db8 100644--- a/crates/cursor-info/src/lib.rs+++ b/crates/cursor-info/src/lib.rs@@ -102,3 +102,213 @@ impl Type for CursorShape {String::inline(types, generics)}}++/// A visual family of cursor assets. Any recording can be re-rendered in any+/// family by cross-mapping its recorded shapes.+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]+pub enum CursorFamily {+ MacOS,+ MacOSTahoe,+ Windows,+}++impl CursorFamily {+ pub fn arrow(self) -> CursorShape {+ match self {+ Self::MacOS => CursorShape::MacOS(CursorShapeMacOS::Arrow),+ Self::MacOSTahoe => CursorShape::MacOS(CursorShapeMacOS::TahoeArrow),+ Self::Windows => CursorShape::Windows(CursorShapeWindows::Arrow),+ }+ }+}++impl CursorShape {+ pub fn family(self) -> CursorFamily {+ match self {+ Self::MacOS(cursor) => {+ if cursor.is_tahoe() {+ CursorFamily::MacOSTahoe+ } else {+ CursorFamily::MacOS+ }+ }+ Self::Windows(_) => CursorFamily::Windows,+ }+ }++ /// The equivalent shape in another family; identity within the family.+ /// Shapes with no counterpart, and any whose asset is missing, fall back+ /// to the target family's arrow so the renderer always has something to+ /// draw.+ pub fn in_family(self, family: CursorFamily) -> CursorShape {+ let mapped = match (self, family) {+ (Self::MacOS(cursor), CursorFamily::MacOS) => Self::MacOS(cursor.to_classic()),+ (Self::MacOS(cursor), CursorFamily::MacOSTahoe) => Self::MacOS(cursor.to_tahoe()),+ (Self::MacOS(cursor), CursorFamily::Windows) => {+ Self::Windows(cursor.to_classic().to_windows())+ }+ (Self::Windows(cursor), CursorFamily::Windows) => Self::Windows(cursor),+ (Self::Windows(cursor), CursorFamily::MacOS) => Self::MacOS(cursor.to_macos()),+ (Self::Windows(cursor), CursorFamily::MacOSTahoe) => {+ Self::MacOS(cursor.to_macos().to_tahoe())+ }+ };++ if mapped.resolve().is_none() {+ family.arrow()+ } else {+ mapped+ }+ }+}++#[cfg(test)]+mod family_tests {+ use super::*;+ use strum::IntoEnumIterator;++ fn all_shapes() -> Vec<CursorShape> {+ CursorShapeMacOS::iter()+ .map(CursorShape::MacOS)+ .chain(CursorShapeWindows::iter().map(CursorShape::Windows))+ .collect()+ }++ #[test]+ fn family_classifies_every_shape() {+ for shape in all_shapes() {+ let family = shape.family();+ match shape {+ CursorShape::Windows(_) => assert_eq!(family, CursorFamily::Windows),+ CursorShape::MacOS(cursor) => {+ let name: &'static str = cursor.into();+ if name.starts_with("Tahoe") {+ assert_eq!(family, CursorFamily::MacOSTahoe, "{shape}");+ } else {+ assert_eq!(family, CursorFamily::MacOS, "{shape}");+ }+ }+ }+ }+ }++ #[test]+ fn in_family_always_resolves() {+ for shape in all_shapes() {+ for family in [+ CursorFamily::MacOS,+ CursorFamily::MacOSTahoe,+ CursorFamily::Windows,+ ] {+ let mapped = shape.in_family(family);+ assert_eq!(mapped.family(), family, "{shape} -> {family:?}");+ assert!(+ mapped.resolve().is_some(),+ "{shape} -> {family:?} produced unresolvable {mapped}"+ );+ }+ }+ }++ #[test]+ fn in_family_is_identity_within_family() {+ for shape in all_shapes() {+ if shape.resolve().is_none() {+ continue;+ }+ assert_eq!(shape.in_family(shape.family()), shape, "{shape}");+ }+ }++ #[test]+ fn unresolvable_shapes_become_the_family_arrow() {+ for shape in [+ CursorShape::MacOS(CursorShapeMacOS::DisappearingItem),+ CursorShape::MacOS(CursorShapeMacOS::TahoeDisappearingItem),+ CursorShape::Windows(CursorShapeWindows::ArrowCD),+ ] {+ assert_eq!(+ shape.in_family(shape.family()),+ shape.family().arrow(),+ "{shape}"+ );+ }+ }++ #[test]+ fn windows_macos_round_trips_are_exact() {+ let pairs = [+ (CursorShapeWindows::Arrow, CursorShapeMacOS::Arrow),+ (CursorShapeWindows::IBeam, CursorShapeMacOS::IBeam),+ (CursorShapeWindows::Hand, CursorShapeMacOS::PointingHand),+ (CursorShapeWindows::Cross, CursorShapeMacOS::Crosshair),+ (+ CursorShapeWindows::No,+ CursorShapeMacOS::OperationNotAllowed,+ ),+ (+ CursorShapeWindows::SizeWE,+ CursorShapeMacOS::ResizeLeftRight,+ ),+ (CursorShapeWindows::SizeNS, CursorShapeMacOS::ResizeUpDown),+ (CursorShapeWindows::SizeAll, CursorShapeMacOS::OpenHand),+ ];++ for (win, mac) in pairs {+ let win = CursorShape::Windows(win);+ let mac = CursorShape::MacOS(mac);+ assert_eq!(win.in_family(CursorFamily::MacOS), mac, "{win} -> macos");+ assert_eq!(+ mac.in_family(CursorFamily::Windows),+ win,+ "{mac} -> windows"+ );+ assert_eq!(+ win.in_family(CursorFamily::MacOS)+ .in_family(CursorFamily::Windows),+ win,+ "{win} round trip"+ );+ }+ }++ #[test]+ fn tahoe_round_trips_by_name() {+ let mac = CursorShape::MacOS(CursorShapeMacOS::PointingHand);+ let tahoe = CursorShape::MacOS(CursorShapeMacOS::TahoePointingHand);++ assert_eq!(mac.in_family(CursorFamily::MacOSTahoe), tahoe);+ assert_eq!(tahoe.in_family(CursorFamily::MacOS), mac);+ assert_eq!(+ tahoe.in_family(CursorFamily::Windows),+ CursorShape::Windows(CursorShapeWindows::Hand)+ );+ }++ #[test]+ fn tahoe_only_shapes_fall_back_to_classic_arrow() {+ let zoom = CursorShape::MacOS(CursorShapeMacOS::TahoeZoomIn);++ assert_eq!(+ zoom.in_family(CursorFamily::MacOS),+ CursorShape::MacOS(CursorShapeMacOS::Arrow)+ );+ assert_eq!(+ zoom.in_family(CursorFamily::Windows),+ CursorShape::Windows(CursorShapeWindows::Arrow)+ );+ }++ #[test]+ fn every_family_arrow_resolves() {+ for family in [+ CursorFamily::MacOS,+ CursorFamily::MacOSTahoe,+ CursorFamily::Windows,+ ] {+ let arrow = family.arrow();+ assert_eq!(arrow.family(), family, "{arrow}");+ assert!(arrow.resolve().is_some(), "{arrow} has no asset");+ }+ }+}diff --git a/crates/cursor-info/src/macos.rs b/crates/cursor-info/src/macos.rsindex c0fa53403..d8cc28fff 100644--- a/crates/cursor-info/src/macos.rs+++ b/crates/cursor-info/src/macos.rs@@ -1,10 +1,11 @@use strum::{EnumString, IntoStaticStr};-use crate::{CursorShape, ResolvedCursor};+use crate::{CursorShape, CursorShapeWindows, ResolvedCursor};/// macOS Cursors/// https://developer.apple.com/documentation/appkit/nscursor#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, EnumString, IntoStaticStr)]+#[cfg_attr(test, derive(strum::EnumIter))]pub enum CursorShapeMacOS {/// https://developer.apple.com/documentation/appkit/nscursor/arrowArrow,@@ -337,3 +338,78 @@ impl From<CursorShapeMacOS> for CursorShape {CursorShape::MacOS(value)}}++impl CursorShapeMacOS {+ pub(crate) fn is_tahoe(self) -> bool {+ self.to_classic() != self+ }++ /// The pre-Tahoe variant of a shape; identity for shapes that already are+ /// one, and `Arrow` for the Tahoe-only additions.+ pub(crate) fn to_classic(self) -> Self {+ match self {+ Self::TahoeArrow => Self::Arrow,+ Self::TahoeContextualMenu => Self::ContextualMenu,+ Self::TahoeClosedHand => Self::ClosedHand,+ Self::TahoeCrosshair => Self::Crosshair,+ Self::TahoeDisappearingItem => Self::DisappearingItem,+ Self::TahoeDragCopy => Self::DragCopy,+ Self::TahoeDragLink => Self::DragLink,+ Self::TahoeIBeam => Self::IBeam,+ Self::TahoeOpenHand => Self::OpenHand,+ Self::TahoeOperationNotAllowed => Self::OperationNotAllowed,+ Self::TahoePointingHand => Self::PointingHand,+ Self::TahoeResizeDown => Self::ResizeDown,+ Self::TahoeResizeLeft => Self::ResizeLeft,+ Self::TahoeResizeLeftRight => Self::ResizeLeftRight,+ Self::TahoeResizeRight => Self::ResizeRight,+ Self::TahoeResizeUp => Self::ResizeUp,+ Self::TahoeResizeUpDown => Self::ResizeUpDown,+ Self::TahoeIBeamVerticalForVerticalLayout => Self::IBeamVerticalForVerticalLayout,+ Self::TahoeZoomIn | Self::TahoeZoomOut => Self::Arrow,+ other => other,+ }+ }++ /// The Tahoe variant of a shape; identity for shapes that already are one.+ pub(crate) fn to_tahoe(self) -> Self {+ match self {+ Self::Arrow => Self::TahoeArrow,+ Self::ContextualMenu => Self::TahoeContextualMenu,+ Self::ClosedHand => Self::TahoeClosedHand,+ Self::Crosshair => Self::TahoeCrosshair,+ Self::DisappearingItem => Self::TahoeDisappearingItem,+ Self::DragCopy => Self::TahoeDragCopy,+ Self::DragLink => Self::TahoeDragLink,+ Self::IBeam => Self::TahoeIBeam,+ Self::OpenHand => Self::TahoeOpenHand,+ Self::OperationNotAllowed => Self::TahoeOperationNotAllowed,+ Self::PointingHand => Self::TahoePointingHand,+ Self::ResizeDown => Self::TahoeResizeDown,+ Self::ResizeLeft => Self::TahoeResizeLeft,+ Self::ResizeLeftRight => Self::TahoeResizeLeftRight,+ Self::ResizeRight => Self::TahoeResizeRight,+ Self::ResizeUp => Self::TahoeResizeUp,+ Self::ResizeUpDown => Self::TahoeResizeUpDown,+ Self::IBeamVerticalForVerticalLayout => Self::TahoeIBeamVerticalForVerticalLayout,+ other => other,+ }+ }++ /// The closest Windows counterpart. Only meaningful for classic variants;+ /// call `to_classic` first.+ pub(crate) fn to_windows(self) -> CursorShapeWindows {+ match self {+ Self::IBeam | Self::IBeamVerticalForVerticalLayout => CursorShapeWindows::IBeam,+ Self::PointingHand => CursorShapeWindows::Hand,+ Self::Crosshair => CursorShapeWindows::Cross,+ Self::OperationNotAllowed => CursorShapeWindows::No,+ Self::ResizeLeft | Self::ResizeRight | Self::ResizeLeftRight => {+ CursorShapeWindows::SizeWE+ }+ Self::ResizeUp | Self::ResizeDown | Self::ResizeUpDown => CursorShapeWindows::SizeNS,+ Self::OpenHand | Self::ClosedHand => CursorShapeWindows::SizeAll,+ _ => CursorShapeWindows::Arrow,+ }+ }+}diff --git a/crates/cursor-info/src/windows.rs b/crates/cursor-info/src/windows.rsindex 7461c3f06..96ca60a08 100644--- a/crates/cursor-info/src/windows.rs+++ b/crates/cursor-info/src/windows.rs@@ -1,9 +1,10 @@use strum::{EnumString, IntoStaticStr};-use crate::{CursorShape, ResolvedCursor};+use crate::{CursorShape, CursorShapeMacOS, ResolvedCursor};// https://learn.microsoft.com/en-us/windows/win32/menurc/about-cursors#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, EnumString, IntoStaticStr)]+#[cfg_attr(test, derive(strum::EnumIter))]pub enum CursorShapeWindows {/// IDC_ARROWArrow,@@ -269,3 +270,19 @@ impl From<CursorShapeWindows> for CursorShape {CursorShape::Windows(value)}}++impl CursorShapeWindows {+ /// The closest classic (pre-Tahoe) macOS counterpart.+ pub(crate) fn to_macos(self) -> CursorShapeMacOS {+ match self {+ Self::IBeam => CursorShapeMacOS::IBeam,+ Self::Hand => CursorShapeMacOS::PointingHand,+ Self::Cross => CursorShapeMacOS::Crosshair,+ Self::No => CursorShapeMacOS::OperationNotAllowed,+ Self::SizeWE => CursorShapeMacOS::ResizeLeftRight,+ Self::SizeNS => CursorShapeMacOS::ResizeUpDown,+ Self::SizeAll => CursorShapeMacOS::OpenHand,+ _ => CursorShapeMacOS::Arrow,+ }+ }+}