Skip to content

Commit

Permalink
Zoom PDF controls (#3204)
Browse files Browse the repository at this point in the history
  • Loading branch information
mallexxx authored Sep 4, 2024
1 parent 7108c78 commit ad8f05f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
21 changes: 19 additions & 2 deletions DuckDuckGo/Common/Extensions/WKPDFHUDViewWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ struct WKPDFHUDViewWrapper {
static let performActionForControlSelector = NSSelectorFromString("_performActionForControl:")
static let visibleKey = "_visible"
static let setVisibleSelector = NSSelectorFromString("_setVisible:")
static let savePDFControlId = "arrow.down.circle"

private enum ControlId: String {
case savePDF = "arrow.down.circle"
case zoomIn = "plus.magnifyingglass"
case zoomOut = "minus.magnifyingglass"
}

private let hudView: NSView

Expand Down Expand Up @@ -72,14 +77,26 @@ struct WKPDFHUDViewWrapper {
}

func savePDF() {
performAction(for: .savePDF)
}

func zoomIn() {
performAction(for: .zoomIn)
}

func zoomOut() {
performAction(for: .zoomOut)
}

private func performAction(for controlId: ControlId) {
let wasVisible = isVisible
self.setIsVisibleIVar(true)
defer {
if !wasVisible {
self.setIsVisibleIVar(false)
}
}
hudView.perform(Self.performActionForControlSelector, with: Self.savePDFControlId)
hudView.perform(Self.performActionForControlSelector, with: controlId.rawValue)
}

// try to set _visible ivar value directly to avoid actually showing the HUD
Expand Down
10 changes: 10 additions & 0 deletions DuckDuckGo/Tab/View/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,22 @@ final class WebView: WKWebView {
}

func zoomIn() {
// if displaying PDF
if let pdfHudView = self.hudView() {
pdfHudView.zoomIn()
return
}
guard canZoomIn else { return }
zoomLevel = DefaultZoomValue.allCases[self.zoomLevel.index + 1]
zoomLevelDelegate?.zoomWasSet(to: zoomLevel)
}

func zoomOut() {
// if displaying PDF
if let pdfHudView = self.hudView() {
pdfHudView.zoomOut()
return
}
guard canZoomOut else { return }
zoomLevel = DefaultZoomValue.allCases[self.zoomLevel.index - 1]
zoomLevelDelegate?.zoomWasSet(to: zoomLevel)
Expand Down

0 comments on commit ad8f05f

Please sign in to comment.