Skip to content

armgit / cra

Public Imported from GitHub
0

The open-source Arc replacement for macOS. Spaces, pinned tabs, split view, Little Windows and Arc's shortcuts on Chromium, with your Arc sidebar imported in one go.

Keep Chromium extension popups framed across app switches (#20)

Quinn Battersby36db9f42b6a5
diff --git a/Sources/Cra/Services/ExtensionActions.swift b/Sources/Cra/Services/ExtensionActions.swift
index bab8e90..91d9f7d 100644
--- a/Sources/Cra/Services/ExtensionActions.swift
+++ b/Sources/Cra/Services/ExtensionActions.swift
@@ -47,13 +47,16 @@ final class ExtensionActions {
func run(_ extensionID: String, on browser: CraBrowser, anchor: NSPoint) async -> Bool {
guard isAvailable else { return false }
var triggered = false
+ HitchMonitor.mark("extension request \(extensionID)")
do {
let tab = try await tabTarget(for: browser)
let before = visibleWindows()
placingOwnPopup = true
triggered = true
+ HitchMonitor.mark("extension trigger \(extensionID)")
_ = try await call("Extensions.triggerAction", ["id": extensionID, "targetId": tab])
- placePopup(appearingAfter: before, anchor: anchor)
+ HitchMonitor.mark("extension trigger-replied \(extensionID)")
+ placePopup(appearingAfter: before, anchor: anchor, sourceHost: browser.hostWindow)
return true
} catch {
placingOwnPopup = false
@@ -75,19 +78,29 @@ final class ExtensionActions {
/// Chrome shows the popup once its page has loaded; watch for the new window and frame it
/// under the anchor (ExtensionPopupWindow, Arc's BrowserExtensionWindow).
- private func placePopup(appearingAfter before: Set<ObjectIdentifier>, anchor: NSPoint, attempt: Int = 0) {
- // Chrome's popup bubble is borderless; an extension's own (titled) window is not one.
- if let popup = NSApp.windows.first(where: { !before.contains(ObjectIdentifier($0)) && $0.isVisible && $0.frame.width > 1
- && !$0.styleMask.contains(.titled) && !($0 is NSPanel)
- && !CraCEFRuntime.isPageWindow($0) }) {
+ private func placePopup(appearingAfter before: Set<ObjectIdentifier>, anchor: NSPoint, sourceHost: NSWindow? = nil, attempt: Int = 0) {
+ // Sharing badges and tooltips can also become visible during startup. A popup
+ // must belong to a Chromium page in the browser containing the resolved anchor.
+ if let popup = NSApp.windows.first(where: { candidate in
+ guard !before.contains(ObjectIdentifier(candidate)),
+ let owner = sourceHost?.parent ?? candidate.parent?.parent,
+ owner.frame.contains(anchor) else { return false }
+ return ExtensionPopupCandidate.matches(candidate, owner: owner, sourceHost: sourceHost,
+ isPageHost: CraCEFRuntime.isPageWindow)
+ }) {
+ HitchMonitor.mark("extension present window=\(popup.windowNumber) frame=\(NSStringFromRect(popup.frame)) parent=\(popup.parent?.windowNumber ?? -1)")
placingOwnPopup = false
ExtensionPopupWindow.present(popup, anchor: anchor)
return
}
// No popup yet (still loading), or an action without one (onClicked): keep looking for 3 s.
- guard attempt < 60 else { placingOwnPopup = false; return }
+ guard attempt < 60 else {
+ HitchMonitor.mark("extension placement-expired source=\(sourceHost?.windowNumber ?? -1)")
+ placingOwnPopup = false
+ return
+ }
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { [weak self] in
- MainActor.assumeIsolated { self?.placePopup(appearingAfter: before, anchor: anchor, attempt: attempt + 1) }
+ MainActor.assumeIsolated { self?.placePopup(appearingAfter: before, anchor: anchor, sourceHost: sourceHost, attempt: attempt + 1) }
}
}
diff --git a/Sources/Cra/UI/Windows/ExtensionPopupCandidate.swift b/Sources/Cra/UI/Windows/ExtensionPopupCandidate.swift
new file mode 100644
index 0000000..d73f5d3
--- /dev/null
+++ b/Sources/Cra/UI/Windows/ExtensionPopupCandidate.swift
@@ -0,0 +1,15 @@
+import AppKit
+
+/// Restrict the native-window fallback to Chromium bubbles in the requesting page.
+/// This is still a native ownership check, not a protocol target-to-window mapping.
+enum ExtensionPopupCandidate {
+ @MainActor
+ static func matches(_ window: NSWindow, owner: NSWindow?, sourceHost: NSWindow?,
+ isPageHost: (NSWindow) -> Bool) -> Bool {
+ guard let owner, owner.isVisible, window.isVisible, window.frame.width > 1,
+ !window.styleMask.contains(.titled), !(window is NSPanel),
+ NSStringFromClass(type(of: window)) == "NativeWidgetMacNSWindow",
+ let host = window.parent, isPageHost(host), host.parent === owner else { return false }
+ return sourceHost == nil || host === sourceHost
+ }
+}
diff --git a/Sources/Cra/UI/Windows/ExtensionPopupWindow.swift b/Sources/Cra/UI/Windows/ExtensionPopupWindow.swift
index a884a46..9477831 100644
--- a/Sources/Cra/UI/Windows/ExtensionPopupWindow.swift
+++ b/Sources/Cra/UI/Windows/ExtensionPopupWindow.swift
@@ -78,6 +78,7 @@ final class ExtensionPopupWindow: NSWindow {
host?.addChildWindow(w, ordered: .above)
w.orderFront(nil)
w.addChildWindow(bubble, ordered: .above)
+ HitchMonitor.mark("extension framed window=\(w.windowNumber) bubble=\(bubble.windowNumber) anchor=\(NSStringFromPoint(anchor)) frame=\(NSStringFromRect(w.frame))")
for name in [NSWindow.didResizeNotification, NSWindow.didMoveNotification] {
w.observers.append(NotificationCenter.default.addObserver(forName: name, object: bubble, queue: .main) { [weak w] _ in
@@ -90,7 +91,8 @@ final class ExtensionPopupWindow: NSWindow {
// Chrome closes its popup when the popup loses focus, but only once it had focus: shown
// while the page's own window wasn't key (the button is in Cra's sidebar, or Cra was in
// the background) it never closed. Give it focus, and close it when another Cra window
- // takes focus or Cra goes to the background, as a focused popup does.
+ // takes focus. Chromium deliberately keeps extension popups open when another
+ // app becomes active, so users can read their contents while working elsewhere.
bubble.makeKey()
let shownAt = Date()
w.observers.append(NotificationCenter.default.addObserver(forName: NSWindow.didBecomeKeyNotification, object: nil, queue: .main) { [weak w] note in
@@ -101,9 +103,6 @@ final class ExtensionPopupWindow: NSWindow {
w.dismiss(closingBubble: true)
}
})
- w.observers.append(NotificationCenter.default.addObserver(forName: NSApplication.didResignActiveNotification, object: nil, queue: .main) { [weak w] _ in
- MainActor.assumeIsolated { w?.dismiss(closingBubble: true) }
- })
w.observers.append(NotificationCenter.default.addObserver(forName: NSWindow.willMoveNotification, object: w, queue: .main) { [weak w] _ in
MainActor.assumeIsolated {
guard let w else { return }
diff --git a/Tests/ExtensionPopupCandidateTests.swift b/Tests/ExtensionPopupCandidateTests.swift
new file mode 100644
index 0000000..8fb7567
--- /dev/null
+++ b/Tests/ExtensionPopupCandidateTests.swift
@@ -0,0 +1,58 @@
+import AppKit
+
+@objc(NativeWidgetMacNSWindow)
+private final class ChromiumBubble: NSWindow {}
+
+@main
+enum ExtensionPopupCandidateTests {
+ @MainActor
+ static func main() {
+ _ = NSApplication.shared
+ NSApp.setActivationPolicy(.prohibited)
+ let rect = NSRect(x: -10000, y: -10000, width: 39, height: 39)
+ func make(_ type: NSWindow.Type = NSWindow.self) -> NSWindow {
+ let window = type.init(contentRect: rect, styleMask: .borderless, backing: .buffered, defer: false)
+ window.isReleasedWhenClosed = false
+ window.orderFront(nil)
+ return window
+ }
+ let owner = make(), host = make(), otherHost = make(), otherOwner = make()
+ let bubble = make(ChromiumBubble.self), badge = make(), tooltip = make(ChromiumBubble.self)
+ owner.addChildWindow(host, ordered: .above)
+ owner.addChildWindow(otherHost, ordered: .above)
+ host.addChildWindow(bubble, ordered: .above)
+ host.addChildWindow(badge, ordered: .above)
+ var checks = 0
+ func check(_ value: Bool, _ message: String) {
+ precondition(value, message)
+ checks += 1
+ }
+ func matches(_ candidate: NSWindow, _ expectedOwner: NSWindow? = nil, _ source: NSWindow? = nil) -> Bool {
+ ExtensionPopupCandidate.matches(candidate, owner: expectedOwner ?? owner, sourceHost: source,
+ isPageHost: { $0 === host || $0 === otherHost })
+ }
+ check(matches(bubble, owner, host), "the source page's minimum-size popup is accepted")
+ check(matches(bubble), "shortcut placement accepts the resolved owner's page host")
+ check(!matches(badge), "a non-Chromium sharing badge cannot be reparented")
+ check(!matches(tooltip), "an unowned Chromium tooltip is rejected")
+ check(!matches(bubble, otherOwner), "another browser window cannot own this popup")
+ check(!matches(bubble, owner, otherHost), "a different source page cannot claim this popup")
+ check(!ExtensionPopupCandidate.matches(bubble, owner: nil, sourceHost: host, isPageHost: { _ in true }),
+ "missing browser ownership fails closed")
+ check(!ExtensionPopupCandidate.matches(bubble, owner: owner, sourceHost: host, isPageHost: { _ in false }),
+ "a non-page parent is rejected")
+ bubble.orderOut(nil)
+ check(!matches(bubble), "a hidden popup is not ready for placement")
+ bubble.orderFront(nil)
+ bubble.styleMask.insert(.titled)
+ check(!matches(bubble), "a separate titled extension window is not an action bubble")
+ bubble.styleMask = .borderless
+ owner.orderOut(nil)
+ check(!matches(bubble), "a hidden browser cannot acquire a popup")
+ for window in [bubble, badge, tooltip, host, otherHost, owner, otherOwner] {
+ window.parent?.removeChildWindow(window)
+ window.close()
+ }
+ print("Extension popup native ownership: \(checks) checks passed")
+ }
+}
diff --git a/scripts/test-extension-popup-candidate.sh b/scripts/test-extension-popup-candidate.sh
new file mode 100755
index 0000000..5f1232d
--- /dev/null
+++ b/scripts/test-extension-popup-candidate.sh
@@ -0,0 +1,8 @@
+#!/bin/bash
+set -euo pipefail
+cd "$(dirname "$0")/.."
+TEST_DIR="$(mktemp -d /tmp/cra-popup-candidate.XXXXXX)"
+trap 'rm -rf "$TEST_DIR"' EXIT
+xcrun swiftc -parse-as-library Sources/Cra/UI/Windows/ExtensionPopupCandidate.swift \
+ Tests/ExtensionPopupCandidateTests.swift -o "$TEST_DIR/candidate-tests"
+"$TEST_DIR/candidate-tests"