Skip to content

Commit

Permalink
less code is better code
Browse files Browse the repository at this point in the history
  • Loading branch information
mallexxx committed Apr 12, 2024
1 parent abfe57b commit 0f5e788
Showing 1 changed file with 12 additions and 53 deletions.
65 changes: 12 additions & 53 deletions DuckDuckGo/Tab/View/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,27 @@ final class WebView: WKWebView {
weak var interactionEventsDelegate: WebViewInteractionEventsDelegate?

private var isLoadingObserver: Any?
private var trackingAreaSuppressor: TrackingAreaSuppressor?
private var proxiedTrackingArea: NSTrackingArea?

override func addTrackingArea(_ trackingArea: NSTrackingArea) {
var trackingArea = trackingArea
/// replace NSTrackingArea with owner proxied with TrackingAreaSuppressor.
/// it will disable mouseEntered/mouseMoved/mouseExited events passing to Web View while it‘s loading
/// disable mouseEntered/mouseMoved/mouseExited events passing to Web View while it‘s loading
/// see https://app.asana.com/0/1177771139624306/1206990108527681/f
if let mouseTrackingObserver = trackingArea.owner, mouseTrackingObserver.className == "WKMouseTrackingObserver" {
let suppressor = TrackingAreaSuppressor(owner: mouseTrackingObserver)
suppressor.isSuppressingMouseEvents = self.trackingAreaSuppressor?.isSuppressingMouseEvents ?? false
self.trackingAreaSuppressor = suppressor

if trackingArea.owner?.className == "WKMouseTrackingObserver" {
// suppress Tracking Area events while loading
isLoadingObserver = self.observe(\.isLoading, options: [.new]) { [weak suppressor] _, c in
suppressor?.isSuppressingMouseEvents = c.newValue /* isLoading */ ?? false
isLoadingObserver = self.observe(\.isLoading, options: [.new]) { [weak self, trackingArea] _, c in
if c.newValue /* isLoading */ ?? false {
guard let self, self.trackingAreas.contains(trackingArea) else { return }
removeTrackingArea(trackingArea)
} else {
guard let self, !self.trackingAreas.contains(trackingArea) else { return }
superAddTrackingArea(trackingArea)
}
}

trackingArea = NSTrackingArea(rect: trackingArea.rect, options: trackingArea.options, owner: suppressor, userInfo: trackingArea.userInfo)
self.proxiedTrackingArea = trackingArea
}
super.addTrackingArea(trackingArea)
}

override func removeTrackingArea(_ trackingArea: NSTrackingArea) {
var trackingArea = trackingArea
if trackingArea.owner?.className == "WKMouseTrackingObserver", let proxiedTrackingArea {
trackingArea = proxiedTrackingArea
self.proxiedTrackingArea = nil
}
super.removeTrackingArea(trackingArea)
private func superAddTrackingArea(_ trackingArea: NSTrackingArea) {
super.addTrackingArea(trackingArea)
}

override var isInFullScreenMode: Bool {
Expand Down Expand Up @@ -359,34 +349,3 @@ extension WebView /* _WKFindDelegate */ {
}

}

/// used to suppress mouseEntered/mouseMoved/mouseExited events while Web View is loading
final private class TrackingAreaSuppressor: NSObject {

private weak var owner: AnyObject?

var isSuppressingMouseEvents = false

init(owner: AnyObject? = nil) {
self.owner = owner
}

@objc(mouseEntered:)
func mouseEntered(with event: NSEvent) {
guard !isSuppressingMouseEvents else { return }
_=owner?.perform(#selector(mouseEntered), with: event)
}

@objc(mouseExited:)
func mouseExited(with event: NSEvent) {
guard !isSuppressingMouseEvents else { return }
_=owner?.perform(#selector(mouseExited), with: event)
}

@objc(mouseMoved:)
func mouseMoved(with event: NSEvent) {
guard !isSuppressingMouseEvents else { return }
_=owner?.perform(#selector(mouseMoved), with: event)
}

}

0 comments on commit 0f5e788

Please sign in to comment.