Skip to content

Commit

Permalink
More robust overrideDragging approach (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanzhong authored Jun 15, 2023
1 parent 0f2f42a commit 406d454
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions PreviewExtension/PreviewViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,17 @@ private extension PreviewViewController {
// #194 Dragging behavior in preview extension is wacky,
// override the event handling and make a homemade scrolling strategy.
private extension PreviewViewController {
var overrideDragging: Bool {
// Don't handle floating windows,
// which is typically a larger window triggered by pressing spacebar in Finder.
NSApp.windows.allSatisfy { $0.level == .normal }
}

func addEventMonitorsForDragging() {
NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown) { [weak self] event in
guard let self, self.overrideDragging else {
guard let self, self.overrideDragging(event: event) else {
return event
}

return self.startDragging(event: event) ? nil : event
}

NSEvent.addLocalMonitorForEvents(matching: .leftMouseDragged) { [weak self] event in
guard let self, self.overrideDragging else {
guard let self, self.overrideDragging(event: event) else {
return event
}

Expand All @@ -151,6 +145,12 @@ private extension PreviewViewController {
}
}

func overrideDragging(event: NSEvent) -> Bool {
// Don't handle floating windows,
// which is typically a larger window triggered by pressing spacebar in Finder.
view.window?.level != .floating && event.window === view.window
}

func startDragging(event: NSEvent) -> Bool {
let location = webView.convert(event.locationInWindow, from: nil)
let scrollerWidth = NSScroller.scrollerWidth(for: .regular, scrollerStyle: .overlay)
Expand Down

0 comments on commit 406d454

Please sign in to comment.