Skip to content
This repository has been archived by the owner on Feb 24, 2025. It is now read-only.

Commit

Permalink
Zoom improvements (#3810)
Browse files Browse the repository at this point in the history
  • Loading branch information
mallexxx authored Feb 12, 2025
1 parent 746bcde commit d24591d
Show file tree
Hide file tree
Showing 33 changed files with 645 additions and 562 deletions.
321 changes: 16 additions & 305 deletions DuckDuckGo-macOS.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
{
"identity" : "trackerradarkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/duckduckgo/TrackerRadarKit.git",
"location" : "https://github.com/duckduckgo/TrackerRadarKit",
"state" : {
"revision" : "5de0a610a7927b638a5fd463a53032c9934a2c3b",
"version" : "3.0.0"
Expand Down
2 changes: 2 additions & 0 deletions DuckDuckGo/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate {

// Freemium DBP
freemiumDBPFeature.subscribeToDependencyUpdates()

_=NSPopover.swizzleShowRelativeToRectOnce
}

// swiftlint:disable:next cyclomatic_complexity
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"images" : [
{
"filename" : "Zoom-Page-Increase-16D.svg",
"filename" : "Union.pdf",
"idiom" : "universal"
}
],
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"images" : [
{
"filename" : "Shape 1.pdf",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"preserves-vector-representation" : true,
"template-rendering-intent" : "template"
}
}
Binary file not shown.
16 changes: 16 additions & 0 deletions DuckDuckGo/Assets.xcassets/Images/ZoomIn.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"images" : [
{
"filename" : "Shape 2.pdf",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"preserves-vector-representation" : true,
"template-rendering-intent" : "template"
}
}
Binary file not shown.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"images" : [
{
"filename" : "OptionsButtonMenuZoom.pdf",
"filename" : "Shape.pdf",
"idiom" : "universal"
}
],
Expand All @@ -10,6 +10,7 @@
"version" : 1
},
"properties" : {
"preserves-vector-representation" : true,
"template-rendering-intent" : "template"
}
}
Binary file not shown.
2 changes: 0 additions & 2 deletions DuckDuckGo/BookmarksBar/View/BookmarksBarMenuPopover.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ final class BookmarksBarMenuPopover: NSPopover {
private(set) var rootFolder: BookmarkFolder?

private(set) var preferredEdge: NSRectEdge?
private(set) weak var positioningView: NSView?

private var bookmarksMenuPopoverDelegate: BookmarksBarMenuPopoverDelegate? {
delegate as? BookmarksBarMenuPopoverDelegate
Expand Down Expand Up @@ -85,7 +84,6 @@ final class BookmarksBarMenuPopover: NSPopover {
tableView.addSubview(v)
}

self.positioningView = positioningView
self.preferredEdge = preferredEdge
viewController.adjustPreferredContentSize(positionedRelativeTo: positioningRect, of: positioningView, at: preferredEdge)
super.show(relativeTo: positioningRect, of: positioningView, preferredEdge: preferredEdge)
Expand Down
50 changes: 50 additions & 0 deletions DuckDuckGo/Common/Extensions/NSPopoverExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
//

import AppKit
import Combine
import os.log

extension NSPopover {

Expand Down Expand Up @@ -45,6 +47,22 @@ extension NSPopover {
self.contentViewController?.view.window?.parent ?? Self.mainWindow
}

private static let positioningViewKey = UnsafeRawPointer(bitPattern: "positioningViewKey".hashValue)!
private final class WeakPositioningViewRef: NSObject {
weak var view: NSView?
init(_ view: NSView? = nil) {
self.view = view
}
}
@nonobjc var positioningView: NSView? {
get {
(objc_getAssociatedObject(self, Self.positioningViewKey) as? WeakPositioningViewRef)?.view
}
set {
objc_setAssociatedObject(self, Self.positioningViewKey, newValue.map { WeakPositioningViewRef($0) }, .OBJC_ASSOCIATION_RETAIN)
}
}

/// prefferred bounding box for the popover positioning
@objc var boundingFrame: NSRect {
guard let mainWindow else { return .infinite }
Expand Down Expand Up @@ -122,4 +140,36 @@ extension NSPopover {
return nil
}

static let swizzleShowRelativeToRectOnce: () = {
guard let originalMethod = class_getInstanceMethod(NSPopover.self, #selector(show(relativeTo:of:preferredEdge:))),
let swizzledMethod = class_getInstanceMethod(NSPopover.self, #selector(swizzled_show(relativeTo:of:preferredEdge:))) else {
assertionFailure("Methods not available")
return
}

method_exchangeImplementations(originalMethod, swizzledMethod)
}()

// ignore popovers shown from a view not in view hierarchy
// https://app.asana.com/0/1201037661562251/1206407295280737/f
@objc(swizzled_showRelativeToRect:ofView:preferredEdge:)
private dynamic func swizzled_show(relativeTo positioningRect: NSRect, of positioningView: NSView, preferredEdge: NSRectEdge) {
if positioningView.window == nil {
var observer: Cancellable?
observer = positioningView.observe(\.window) { positioningView, _ in
if positioningView.window != nil {
self.swizzled_show(relativeTo: positioningRect, of: positioningView, preferredEdge: preferredEdge)
observer?.cancel()
}
}
positioningView.onDeinit {
observer?.cancel()
}

Logger.general.error("trying to present \(self) from \(positioningView) not in view hierarchy")
return
}
self.positioningView = positioningView
self.swizzled_show(relativeTo: positioningRect, of: positioningView, preferredEdge: preferredEdge)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import Combine
import Common
import WebKit
import UserScript
import os.log

extension WKWebViewConfiguration {

Expand Down Expand Up @@ -79,8 +78,6 @@ extension WKWebViewConfiguration {

self.userContentController = userContentController
self.processPool.geolocationProvider = GeolocationProvider(processPool: self.processPool)

_=NSPopover.swizzleShowRelativeToRectOnce
}

}
Expand Down Expand Up @@ -111,39 +108,3 @@ extension WKPreferences {
}

}

extension NSPopover {

fileprivate static let swizzleShowRelativeToRectOnce: () = {
guard let originalMethod = class_getInstanceMethod(NSPopover.self, #selector(show(relativeTo:of:preferredEdge:))),
let swizzledMethod = class_getInstanceMethod(NSPopover.self, #selector(swizzled_show(relativeTo:of:preferredEdge:))) else {
assertionFailure("Methods not available")
return
}

method_exchangeImplementations(originalMethod, swizzledMethod)
}()

// ignore popovers shown from a view not in view hierarchy
// https://app.asana.com/0/1201037661562251/1206407295280737/f
@objc(swizzled_showRelativeToRect:ofView:preferredEdge:)
private dynamic func swizzled_show(relativeTo positioningRect: NSRect, of positioningView: NSView, preferredEdge: NSRectEdge) {
if positioningView.window == nil {
var observer: Cancellable?
observer = positioningView.observe(\.window) { positioningView, _ in
if positioningView.window != nil {
self.swizzled_show(relativeTo: positioningRect, of: positioningView, preferredEdge: preferredEdge)
observer?.cancel()
}
}
positioningView.onDeinit {
observer?.cancel()
}

Logger.general.error("trying to present \(self) from \(positioningView) not in view hierarchy")
return
}
self.swizzled_show(relativeTo: positioningRect, of: positioningView, preferredEdge: preferredEdge)
}

}
10 changes: 10 additions & 0 deletions DuckDuckGo/Common/View/AppKit/MouseOverButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ internal class MouseOverButton: NSButton, Hoverable {
}
}

@IBInspectable var horizontalPadding: CGFloat = 0
@IBInspectable var verticalPadding: CGFloat = 0

override var intrinsicContentSize: NSSize {
var size = super.intrinsicContentSize
size.width += self.horizontalPadding
size.height += self.verticalPadding
return size
}

var normalTintColor: NSColor? {
didSet {
updateTintColor()
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/Menus/MainMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,9 @@ final class MainMenu: NSMenu {
toggleFullscreenMenuItem
NSMenuItem.separator()

actualSizeMenuItem
zoomInMenuItem
zoomOutMenuItem
actualSizeMenuItem
NSMenuItem.separator()

NSMenuItem(title: UserText.mainMenuDeveloper) {
Expand Down
2 changes: 2 additions & 0 deletions DuckDuckGo/Menus/MainMenuActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,12 @@ extension MainViewController {

@objc func zoomIn(_ sender: Any) {
getActiveTabAndIndex()?.tab.webView.zoomIn()
navigationBarViewController.addressBarViewController?.addressBarButtonsViewController?.openZoomPopover(source: .menu)
}

@objc func zoomOut(_ sender: Any) {
getActiveTabAndIndex()?.tab.webView.zoomOut()
navigationBarViewController.addressBarViewController?.addressBarButtonsViewController?.openZoomPopover(source: .menu)
}

@objc func actualSize(_ sender: Any) {
Expand Down
Loading

0 comments on commit d24591d

Please sign in to comment.