Last active
November 17, 2024 15:18
-
-
Save Amzd/cb8ba40625aeb6a015101d357acaad88 to your computer and use it in GitHub Desktop.
Set the cursor that is displayed when hovering a View. (macOS, SwiftUI)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
extension View { | |
/// https://stackoverflow.com/a/61985678/3393964 | |
public func cursor(_ cursor: NSCursor) -> some View { | |
self.onHover { inside in | |
if inside { | |
cursor.push() | |
} else { | |
NSCursor.pop() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Like its name says,
onContinuousHover
calls its callback continuously, resulting in many cursor pushes and only a singlepop
at the end. The result is that when the cursor leaves the view, the original cursor is not restored. So here's my modification: