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.

Extension popup close buttons no longer start a titlebar drag (#216)

Grigoriy657a1f9d2c15
diff --git a/Sources/Cra/UI/Windows/ExtensionPopupWindow.swift b/Sources/Cra/UI/Windows/ExtensionPopupWindow.swift
index a9f883d..c8a2f8f 100644
--- a/Sources/Cra/UI/Windows/ExtensionPopupWindow.swift
+++ b/Sources/Cra/UI/Windows/ExtensionPopupWindow.swift
@@ -254,6 +254,7 @@ private final class TrafficLight: NSView {
override func mouseEntered(with event: NSEvent) { hovered = true }
override func mouseExited(with event: NSEvent) { hovered = false }
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
+ override var mouseDownCanMoveWindow: Bool { false }
override func mouseDown(with event: NSEvent) {}
override func mouseUp(with event: NSEvent) {
guard isClose, bounds.contains(convert(event.locationInWindow, from: nil)) else { return }
diff --git a/Tests/ExtensionPopupWindowTests.swift b/Tests/ExtensionPopupWindowTests.swift
new file mode 100644
index 0000000..83d389e
--- /dev/null
+++ b/Tests/ExtensionPopupWindowTests.swift
@@ -0,0 +1,108 @@
+import AppKit
+import CraCEF
+
+@main
+enum ExtensionPopupWindowTests {
+ private static var checks = 0
+
+ @MainActor
+ static func main() async {
+ _ = NSApplication.shared
+ NSApp.setActivationPolicy(.prohibited)
+ let bubble = NSWindow(contentRect: NSRect(x: -10000, y: -10000, width: 314, height: 214),
+ styleMask: [.borderless], backing: .buffered, defer: false)
+ bubble.isReleasedWhenClosed = false
+ ExtensionPopupWindow.present(bubble, anchor: NSPoint(x: -10000, y: -10000))
+ guard let window = bubble.parent as? ExtensionPopupWindow else { fatalError("Popup frame missing") }
+ window.setFrameOrigin(NSPoint(x: -10000, y: -10000))
+ expect(!window.canBecomeKey && !window.canBecomeMain, "the popup frame must not take Chrome's focus")
+ expect(window.isMovable, "the rest of the titlebar remains draggable")
+
+ for kind: NSWindow.ButtonType in [.closeButton, .miniaturizeButton, .zoomButton] {
+ guard let button = window.standardWindowButton(kind), let titlebar = button.superview,
+ let light = titlebar.subviews.first(where: { $0 !== button && $0.frame == button.frame && !($0 is NSButton) })
+ else { fatalError("Custom traffic light missing for \(kind)") }
+ expect(button.isHidden && !light.isHidden, "the custom light replaces its native button")
+ expect(light.window === window, "the custom light belongs to the popup frame")
+ let center = NSPoint(x: light.bounds.midX, y: light.bounds.midY)
+ expect(titlebar.hitTest(light.convert(center, to: titlebar.superview)) === light, "the custom light owns its hit region")
+ expect(!light.mouseDownCanMoveWindow, "a traffic light must not start a titlebar drag")
+ expect(light.acceptsFirstMouse(for: nil), "the traffic light accepts clicks while its frame is not key")
+ let onScreen = window.convertToScreen(NSRect(origin: light.convert(center, to: nil), size: .zero)).origin
+ expect(!bubble.frame.contains(onScreen), "Chrome's child bubble does not cover the traffic light")
+
+ let outside = NSPoint(x: light.bounds.maxX + 10, y: light.bounds.maxY + 10)
+ light.mouseDown(with: event(.leftMouseDown, at: center, in: light))
+ light.mouseUp(with: event(.leftMouseUp, at: outside, in: light))
+ expect(bubble.parent === window && window.isVisible, "releasing outside a light does not dismiss the popup")
+ if kind != .closeButton {
+ light.mouseDown(with: event(.leftMouseDown, at: center, in: light))
+ light.mouseUp(with: event(.leftMouseUp, at: center, in: light))
+ expect(bubble.parent === window && window.isVisible, "disabled lights do not dismiss the popup")
+ }
+ }
+
+ let close = window.standardWindowButton(.closeButton)!
+ let light = close.superview!.subviews.first { $0 !== close && $0.frame == close.frame && !($0 is NSButton) }!
+ let center = NSPoint(x: light.bounds.midX, y: light.bounds.midY)
+ bubble.orderFront(nil)
+ expect(bubble.isVisible, "the stand-in Chrome bubble is visible before dismissal")
+ light.mouseDown(with: event(.leftMouseDown, at: center, in: light))
+ expect(window.isVisible && bubble.parent === window, "mouse down alone does not close the popup")
+ light.mouseUp(with: event(.leftMouseUp, at: center, in: light))
+ expect(!window.isVisible && bubble.parent == nil, "releasing the close light dismisses and detaches the popup")
+ await withCheckedContinuation { continuation in
+ DispatchQueue.main.async { continuation.resume() }
+ }
+ expect(!bubble.isVisible, "dismissal closes the visible Chrome bubble asynchronously")
+ print("Extension popup window: \(checks) checks passed")
+ }
+
+ @MainActor
+ private static func event(_ type: NSEvent.EventType, at point: NSPoint, in view: NSView) -> NSEvent {
+ NSEvent.mouseEvent(with: type, location: view.convert(point, to: nil), modifierFlags: [], timestamp: 1,
+ windowNumber: view.window!.windowNumber, context: nil, eventNumber: 0,
+ clickCount: 1, pressure: 0)!
+ }
+
+ private static func expect(_ condition: @autoclosure () -> Bool, _ message: String) {
+ guard condition() else { fatalError(message) }
+ checks += 1
+ }
+}
+
+@MainActor
+final class AppDelegate {
+ let store = PopupTestStore()
+ let main = PopupTestMainWindow()
+ func useNormalWindow() {}
+ func handleReservedKey(_ event: NSEvent) -> Bool { false }
+ func handleUnhandledKey(_ event: NSEvent) -> Bool { false }
+}
+
+final class PopupTestStore {
+ func createTab(url: String, select: Bool) {}
+}
+
+final class PopupTestMainWindow {
+ func showWindow(_ sender: Any?) {}
+ func browserManager(download: CraDownloadInfo) {}
+}
+
+enum HitchMonitor {
+ static func mark(_ message: String) {}
+}
+
+enum ChromiumWindowCorners {
+ static func setSquare(_ square: Bool, for window: NSWindow) {}
+}
+
+enum ChromiumChildHover {
+ static func setDocked(_ docked: Bool, for window: NSWindow) {}
+}
+
+extension CraBrowser {
+ convenience init(frame: NSRect, url: String, delegate: CraBrowserDelegate) {
+ self.init(frame: frame, url: url, delegate: delegate, incognito: false, profilePath: nil)
+ }
+}
diff --git a/scripts/test-extension-popup-window.sh b/scripts/test-extension-popup-window.sh
new file mode 100755
index 0000000..ecac9a8
--- /dev/null
+++ b/scripts/test-extension-popup-window.sh
@@ -0,0 +1,14 @@
+#!/bin/bash
+set -euo pipefail
+cd "$(dirname "$0")/.."
+TEST_DIR="$(mktemp -d "${TMPDIR:-/tmp}/cra-popup-window.XXXXXX")"
+trap 'rm -rf "$TEST_DIR"' EXIT
+xcrun swiftc -swift-version 5 -emit-module -emit-library -module-name CraCEF \
+ Tests/BrowserManagerSupport/CraCEF.swift -o "$TEST_DIR/libCraCEF.dylib" \
+ -emit-module-path "$TEST_DIR/CraCEF.swiftmodule"
+xcrun swiftc -swift-version 5 -parse-as-library ${CRA_TEST_SWIFTC_FLAGS:-} -I "$TEST_DIR" -L "$TEST_DIR" -lCraCEF \
+ -Xlinker -rpath -Xlinker "$TEST_DIR" \
+ Sources/Cra/UI/Windows/ExtensionPopupWindow.swift Tests/ExtensionPopupWindowTests.swift \
+ -o "$TEST_DIR/popup-window-tests"
+"$TEST_DIR/popup-window-tests"
+[ -z "${CRA_TEST_KEEP:-}" ] || cp "$TEST_DIR/popup-window-tests" "$CRA_TEST_KEEP/"
diff --git a/scripts/test.sh b/scripts/test.sh
index c18c455..e352f1a 100755
--- a/scripts/test.sh
+++ b/scripts/test.sh
@@ -20,6 +20,7 @@ if [ "${1:-}" != core ]; then
suite "Browser manager" Tests/test-browser-manager.sh
suite "Floating sidebar" scripts/test-floating-sidebar.sh
suite "Extension popup candidate" scripts/test-extension-popup-candidate.sh
+ suite "Extension popup window" scripts/test-extension-popup-window.sh
suite "Extension state" scripts/test-extension-state.sh
suite "Store and importers" scripts/test-store.sh
suite "Policies" scripts/test-policies.sh