-
-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
280 additions
and
100 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Source: https://stackoverflow.com/questions/65841298/swiftui-onhover-doesnt-register-mouse-leaving-the-element-if-mouse-moves-too-fa | ||
|
||
import SwiftUI | ||
|
||
extension View { | ||
public func whenHovered(_ mouseIsInside: @escaping (Bool) -> Void) -> some View { | ||
modifier(MouseInsideModifier(mouseIsInside)) | ||
} | ||
} | ||
|
||
struct MouseInsideModifier: ViewModifier { | ||
let mouseIsInside: (Bool) -> Void | ||
|
||
init(_ mouseIsInside: @escaping (Bool) -> Void) { | ||
self.mouseIsInside = mouseIsInside | ||
} | ||
|
||
func body(content: Content) -> some View { | ||
content.background( | ||
GeometryReader { proxy in | ||
Representable(mouseIsInside: mouseIsInside, | ||
frame: proxy.frame(in: .global)) | ||
} | ||
) | ||
} | ||
|
||
private struct Representable: NSViewRepresentable { | ||
let mouseIsInside: (Bool) -> Void | ||
let frame: NSRect | ||
|
||
func makeCoordinator() -> Coordinator { | ||
let coordinator = Coordinator() | ||
coordinator.mouseIsInside = mouseIsInside | ||
return coordinator | ||
} | ||
|
||
class Coordinator: NSResponder { | ||
var mouseIsInside: ((Bool) -> Void)? | ||
|
||
override func mouseEntered(with event: NSEvent) { | ||
mouseIsInside?(true) | ||
} | ||
|
||
override func mouseExited(with event: NSEvent) { | ||
mouseIsInside?(false) | ||
} | ||
} | ||
|
||
func makeNSView(context: Context) -> NSView { | ||
let view = NSView(frame: frame) | ||
|
||
let options: NSTrackingArea.Options = [ | ||
.mouseEnteredAndExited, | ||
.inVisibleRect, | ||
.activeInKeyWindow | ||
] | ||
|
||
let trackingArea = NSTrackingArea(rect: frame, | ||
options: options, | ||
owner: context.coordinator, | ||
userInfo: nil) | ||
|
||
view.addTrackingArea(trackingArea) | ||
|
||
return view | ||
} | ||
|
||
func updateNSView(_ nsView: NSView, context: Context) {} | ||
|
||
static func dismantleNSView(_ nsView: NSView, coordinator: Coordinator) { | ||
nsView.trackingAreas.forEach { nsView.removeTrackingArea($0) } | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.