Skip to content

Commit

Permalink
Messaging refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanKingston committed Feb 23, 2022
1 parent 930135c commit bae88fb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 39 deletions.
2 changes: 2 additions & 0 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,7 @@
7B4CE8DA26F02108009134B1 /* UI Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "UI Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
7B4CE8DE26F02108009134B1 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
7B4CE8E626F02134009134B1 /* TabBarTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabBarTests.swift; sourceTree = "<group>"; };
7BEBB33827C6B87C00C7AD19 /* BrowserServicesKit */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = BrowserServicesKit; path = ../BrowserServicesKit; sourceTree = "<group>"; };
7BF8EA5C27C3FACF00C506AA /* ContentOverlayPopover.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContentOverlayPopover.swift; sourceTree = "<group>"; };
7BF8EA5D27C3FACF00C506AA /* ContentOverlay.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = ContentOverlay.storyboard; sourceTree = "<group>"; };
7BF8EA5E27C3FACF00C506AA /* ContentOverlayViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContentOverlayViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2372,6 +2373,7 @@
AA585D75248FD31100E9A3E2 = {
isa = PBXGroup;
children = (
7BEBB33827C6B87C00C7AD19 /* BrowserServicesKit */,
AA68C3D62490F821001B8783 /* README.md */,
AA585D80248FD31100E9A3E2 /* DuckDuckGo */,
AA585D93248FD31400E9A3E2 /* Unit Tests */,
Expand Down
26 changes: 11 additions & 15 deletions DuckDuckGo/Autofill/ContentOverlayPopover.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,28 @@ public final class ContentOverlayPopover {
}
}

// OverlayProtocol
extension ContentOverlayPopover: OverlayProtocol {
// AutofillOverlayDelegate
extension ContentOverlayPopover: AutofillOverlayDelegate {
public var view: NSView {
return viewController!.view
}

public func setMessageInterfaceBack(_ response: AutofillMessagingToChild) {
viewController?.autofillInterfaceToChild = response
}

public func closeOverlay() {
public func autofillCloseOverlay(_ autofillUserScript: AutofillMessagingToChildDelegate?) {
guard let windowController = windowController?.window else {
return
}
if !windowController.isVisible { return }
// Reset window size on close to reduce flicker
viewController?.setSize(height: 0, width: 0)
viewController?.requestResizeToSize(width: 0, height: 0)
windowController.parent?.removeChildWindow(windowController)
windowController.orderOut(nil)
}

public func displayOverlay(of: NSView,
messageInterface: AutofillMessagingToChild,
serializedInputContext: String,
click: NSPoint,
inputPosition: CGRect) {
public func autofillDisplayOverlay(_ autofillUserScript: AutofillMessagingToChildDelegate,
of: NSView,
serializedInputContext: String,
click: NSPoint,
inputPosition: CGRect) {
// Combines native click with offset of JS click.
let y = (click.y - (inputPosition.height - inputPosition.minY))
let x = (click.x - inputPosition.minX)
Expand All @@ -94,8 +90,8 @@ extension ContentOverlayPopover: OverlayProtocol {
let rect = NSRect(x: x, y: y, width: rectWidth, height: inputPosition.height)

// On open initialize to default size to reduce flicker
viewController?.setSize(height: 0, width: 0)
viewController?.autofillInterfaceToChild = messageInterface
viewController?.requestResizeToSize(width: 0, height: 0)
viewController?.autofillInterfaceToChild = autofillUserScript
viewController?.setType(serializedInputContext: serializedInputContext, zoomFactor: zoomFactor)
if let window = windowController?.window {
of.window!.addChildWindow(window, ordered: .above)
Expand Down
4 changes: 2 additions & 2 deletions DuckDuckGo/Autofill/ContentOverlayViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public final class ContentOverlayViewController: NSViewController, EmailManagerR
private var cancellables = Set<AnyCancellable>()
@Published var pendingUpdates = Set<String>()

public var autofillInterfaceToChild: AutofillMessagingToChild?
public var autofillInterfaceToChild: AutofillMessagingToChildDelegate?

lazy var emailManager: EmailManager = {
let emailManager = EmailManager()
Expand Down Expand Up @@ -179,7 +179,7 @@ extension ContentOverlayViewController: AutofillUserScriptDelegate {
static let minHeight: CGFloat = 56
}

public func setSize(height: CGFloat, width: CGFloat) {
public func requestResizeToSize(width: CGFloat, height: CGFloat) {
var widthOut = width
if widthOut < Constants.minWidth {
widthOut = Constants.minWidth
Expand Down
10 changes: 5 additions & 5 deletions DuckDuckGo/BrowserTab/Model/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Combine
import BrowserServicesKit
import TrackerRadarKit

protocol TabDelegate: FileDownloadManagerDelegate, OverlayProtocol {
protocol TabDelegate: FileDownloadManagerDelegate, AutofillOverlayDelegate {
func tabWillStartNavigation(_ tab: Tab, isUserInitiated: Bool)
func tabDidStartNavigation(_ tab: Tab)
func tab(_ tab: Tab, requestedNewTabWith content: Tab.TabContent, selected: Bool)
Expand Down Expand Up @@ -561,7 +561,7 @@ extension Tab: UserContentControllerDelegate {
userScripts.surrogatesScript.delegate = self
userScripts.contentBlockerRulesScript.delegate = self
userScripts.clickToLoadScript.delegate = self
userScripts.autofillScript.topView = self.delegate
userScripts.autofillScript.currentOverlayTab = self.delegate
userScripts.autofillScript.emailDelegate = emailManager
userScripts.autofillScript.vaultDelegate = vaultManager
self.autofillScript = userScripts.autofillScript
Expand All @@ -576,10 +576,10 @@ extension Tab: UserContentControllerDelegate {
}

extension Tab: ChildAutofillUserScriptDelegate {
func clickTriggered(clickPoint: NSPoint) {
func browserTabViewController(_ browserTabViewController: BrowserTabViewController, didClickAtPoint: NSPoint) {
guard let autofillScript = autofillScript else { return }
autofillScript.clickPoint = clickPoint
autofillScript.topView = self.delegate
autofillScript.clickPoint = didClickAtPoint
autofillScript.currentOverlayTab = self.delegate
}
}

Expand Down
35 changes: 18 additions & 17 deletions DuckDuckGo/BrowserTab/View/BrowserTabViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import Combine
import SwiftUI
import BrowserServicesKit

protocol ChildAutofillUserScriptDelegate: AnyObject {
func browserTabViewController(_ browserTabViewController: BrowserTabViewController, didClickAtPoint: CGPoint)
}

// swiftlint:disable file_length
final class BrowserTabViewController: NSViewController {

Expand Down Expand Up @@ -57,7 +61,7 @@ final class BrowserTabViewController: NSViewController {
override func mouseDown(with event: NSEvent) {
guard event.window === self.view.window else { return }
self.clickPoint = event.locationInWindow
tabViewModel?.tab.clickTriggered(clickPoint: event.locationInWindow)
tabViewModel?.tab.browserTabViewController(self, didClickAtPoint: event.locationInWindow)
}

required init?(coder: NSCoder) {
Expand Down Expand Up @@ -348,27 +352,24 @@ final class BrowserTabViewController: NSViewController {
_contentOverlayPopover = ContentOverlayPopover()
WindowControllersManager.shared.stateChanged
.sink { _ in
self._contentOverlayPopover?.closeOverlay()
self._contentOverlayPopover?.autofillCloseOverlay(nil)
}.store(in: &cancellables)
}
return _contentOverlayPopover!
}
}

extension BrowserTabViewController: OverlayProtocol {
public func setMessageInterfaceBack(_ response: AutofillMessagingToChild) {
contentOverlayPopover.viewController?.autofillInterfaceToChild = response
}
public func closeOverlay() {
contentOverlayPopover.closeOverlay()
}
public func displayOverlay(of: NSView,
messageInterface: AutofillMessagingToChild,
serializedInputContext: String,
click: NSPoint,
inputPosition: CGRect) {
contentOverlayPopover.displayOverlay(of: of,
messageInterface: messageInterface,
extension BrowserTabViewController: AutofillOverlayDelegate {
public func autofillCloseOverlay(_ autofillUserScript: AutofillMessagingToChildDelegate?) {
contentOverlayPopover.autofillCloseOverlay(autofillUserScript)
}
public func autofillDisplayOverlay(_ autofillUserScript: AutofillMessagingToChildDelegate,
of: NSView,
serializedInputContext: String,
click: NSPoint,
inputPosition: CGRect) {
contentOverlayPopover.autofillDisplayOverlay(autofillUserScript,
of: of,
serializedInputContext: serializedInputContext,
click: click,
inputPosition: inputPosition)
Expand Down Expand Up @@ -1002,7 +1003,7 @@ extension BrowserTabViewController {
func mouseDown(with event: NSEvent) -> NSEvent? {
self.clickPoint = event.locationInWindow
guard event.window === self.view.window, let clickPoint = self.clickPoint else { return event }
tabViewModel?.tab.clickTriggered(clickPoint: clickPoint)
tabViewModel?.tab.browserTabViewController(self, didClickAtPoint: clickPoint)
return event
}

Expand Down

0 comments on commit bae88fb

Please sign in to comment.