diff --git a/Sources/App/CmuxMainWindow.swift b/Sources/App/CmuxMainWindow.swift index 04901bbc7eba..806401d56af7 100644 --- a/Sources/App/CmuxMainWindow.swift +++ b/Sources/App/CmuxMainWindow.swift @@ -30,6 +30,27 @@ final class MainWindowHostingView: NSHostingView { @MainActor final class CmuxMainWindow: NSWindow { private var isSoftHiddenForVisibilityController = false + private var isClearingNativeTitlebarProxyIcon = false + + // cmux renders the workspace directory icon inside its custom titlebar, whose layout follows + // the sidebar. AppKit's native proxy icon is positioned in the standard titlebar instead. + override var representedURL: URL? { + get { nil } + set { clearNativeTitlebarProxyIcon() } + } + + override var representedFilename: String { + get { "" } + set { clearNativeTitlebarProxyIcon() } + } + + private func clearNativeTitlebarProxyIcon() { + guard !isClearingNativeTitlebarProxyIcon else { return } + isClearingNativeTitlebarProxyIcon = true + defer { isClearingNativeTitlebarProxyIcon = false } + super.representedURL = nil + super.representedFilename = "" + } func setSoftHiddenForVisibilityController(_ isSoftHidden: Bool) { isSoftHiddenForVisibilityController = isSoftHidden diff --git a/Sources/ContentView.swift b/Sources/ContentView.swift index 20a7825966b6..93af41f2d497 100644 --- a/Sources/ContentView.swift +++ b/Sources/ContentView.swift @@ -2296,7 +2296,7 @@ struct ContentView: View { TitlebarLeadingInsetReader(inset: $titlebarLeadingInset) .allowsHitTesting(false) - HStack(spacing: 8) { + HStack(spacing: TitlebarFolderIconMetrics.iconTitleSpacing) { if isFullScreen && !sidebarState.isVisible { fullscreenControls } @@ -2304,8 +2304,11 @@ struct ContentView: View { // Draggable folder icon + focused command name if let directory = focusedDirectory { DetachedFolderDragIcon(directory: directory) - .frame(width: 16, height: 16) - .padding(.leading, -6) + .frame( + width: TitlebarFolderIconMetrics.iconSize, + height: TitlebarFolderIconMetrics.iconSize + ) + .padding(.leading, TitlebarFolderIconMetrics.iconLeadingPadding) } Text(titlebarText) diff --git a/Sources/DetachedFolderDragIcon.swift b/Sources/DetachedFolderDragIcon.swift index 1292371733ff..19dedf907465 100644 --- a/Sources/DetachedFolderDragIcon.swift +++ b/Sources/DetachedFolderDragIcon.swift @@ -20,11 +20,15 @@ final class DetachedFolderDragIconHostView: NSView { private var childWindow: NSPanel? private var iconView: DraggableFolderNSView? private var observers: [NSObjectProtocol] = [] + private var ancestorViewObservers: [NSObjectProtocol] = [] + private var ancestorViewNotificationStates: [AncestorViewNotificationState] = [] + private var observedAncestorViewIds: [ObjectIdentifier] = [] + private var hasScheduledFrameSync = false private weak var observedParentWindow: NSWindow? init(directory: String) { self.directory = directory - super.init(frame: NSRect(origin: .zero, size: NSSize(width: 16, height: 16))) + super.init(frame: NSRect(origin: .zero, size: TitlebarFolderIconMetrics.iconFrameSize)) } required init?(coder: NSCoder) { @@ -38,18 +42,36 @@ final class DetachedFolderDragIconHostView: NSView { } override var intrinsicContentSize: NSSize { - NSSize(width: 16, height: 16) + TitlebarFolderIconMetrics.iconFrameSize } override var mouseDownCanMoveWindow: Bool { false } + override var frame: NSRect { + didSet { + syncDetachedIconFrame() + } + } + + override func setFrameOrigin(_ newOrigin: NSPoint) { + super.setFrameOrigin(newOrigin) + syncDetachedIconFrame() + } + + override func setFrameSize(_ newSize: NSSize) { + super.setFrameSize(newSize) + syncDetachedIconFrame() + } + override func viewDidMoveToWindow() { super.viewDidMoveToWindow() + installAncestorViewObservers() syncDetachedIcon() } override func viewDidMoveToSuperview() { super.viewDidMoveToSuperview() + installAncestorViewObservers() syncDetachedIcon() } @@ -80,9 +102,15 @@ final class DetachedFolderDragIconHostView: NSView { syncDetachedIconFrame() } + #if DEBUG + var detachedIconFrameForTesting: NSRect? { + childWindow?.frame + } + #endif + private func makeDetachedIconWindow(parentWindow: NSWindow) -> NSPanel { let iconView = DraggableFolderNSView(directory: directory) - iconView.frame = NSRect(origin: .zero, size: NSSize(width: 16, height: 16)) + iconView.frame = NSRect(origin: .zero, size: TitlebarFolderIconMetrics.iconFrameSize) let panel = NSPanel( contentRect: iconView.frame, @@ -133,10 +161,11 @@ final class DetachedFolderDragIconHostView: NSView { } private func syncDetachedIconFrame() { + installAncestorViewObservers() guard let parentWindow = window, let childWindow else { return } let localRect = bounds.isEmpty - ? NSRect(origin: .zero, size: NSSize(width: 16, height: 16)) + ? NSRect(origin: .zero, size: TitlebarFolderIconMetrics.iconFrameSize) : bounds let rectInWindow = convert(localRect, to: nil) let rectOnScreen = parentWindow.convertToScreen(rectInWindow) @@ -145,6 +174,81 @@ final class DetachedFolderDragIconHostView: NSView { } } + private func installAncestorViewObservers() { + var ancestors: [NSView] = [] + var ancestor = superview + while let view = ancestor { + ancestors.append(view) + ancestor = view.superview + } + + let ancestorIds = ancestors.map { ObjectIdentifier($0) } + guard ancestorIds != observedAncestorViewIds else { return } + + removeAncestorViewObservers() + guard !ancestors.isEmpty else { return } + + let center = NotificationCenter.default + for ancestor in ancestors { + // SwiftUI may keep this host's local frame stable while moving an + // enclosing titlebar/content view, such as when the left sidebar + // opens. Observe the ancestor chain so the detached panel follows + // the host's converted window rect, not just its local frame. + ancestorViewNotificationStates.append(AncestorViewNotificationState( + view: ancestor, + postsFrameChangedNotifications: ancestor.postsFrameChangedNotifications, + postsBoundsChangedNotifications: ancestor.postsBoundsChangedNotifications + )) + ancestor.postsFrameChangedNotifications = true + ancestor.postsBoundsChangedNotifications = true + ancestorViewObservers.append(center.addObserver( + forName: NSView.frameDidChangeNotification, + object: ancestor, + queue: .main + ) { [weak self] _ in + MainActor.assumeIsolated { + self?.scheduleDetachedIconFrameSync() + } + }) + ancestorViewObservers.append(center.addObserver( + forName: NSView.boundsDidChangeNotification, + object: ancestor, + queue: .main + ) { [weak self] _ in + MainActor.assumeIsolated { + self?.scheduleDetachedIconFrameSync() + } + }) + } + observedAncestorViewIds = ancestorIds + } + + private func scheduleDetachedIconFrameSync() { + guard !hasScheduledFrameSync else { return } + hasScheduledFrameSync = true + DispatchQueue.main.async { [weak self] in + MainActor.assumeIsolated { + guard let self else { return } + self.hasScheduledFrameSync = false + self.syncDetachedIconFrame() + } + } + } + + private func removeAncestorViewObservers() { + for observer in ancestorViewObservers { + NotificationCenter.default.removeObserver(observer) + } + for state in ancestorViewNotificationStates { + state.view?.postsFrameChangedNotifications = state.postsFrameChangedNotifications + state.view?.postsBoundsChangedNotifications = state.postsBoundsChangedNotifications + } + ancestorViewObservers.removeAll() + ancestorViewNotificationStates.removeAll() + observedAncestorViewIds.removeAll() + hasScheduledFrameSync = false + } + private func removeParentWindowObservers() { for observer in observers { NotificationCenter.default.removeObserver(observer) @@ -158,6 +262,7 @@ final class DetachedFolderDragIconHostView: NSView { // leaves its parent window or deinitializes, remove both so no stale // panel keeps tracking an old parent window. removeParentWindowObservers() + removeAncestorViewObservers() if let childWindow { childWindow.parent?.removeChildWindow(childWindow) childWindow.orderOut(nil) @@ -165,6 +270,12 @@ final class DetachedFolderDragIconHostView: NSView { childWindow = nil iconView = nil } + + private struct AncestorViewNotificationState { + weak var view: NSView? + let postsFrameChangedNotifications: Bool + let postsBoundsChangedNotifications: Bool + } } @MainActor @@ -194,7 +305,7 @@ final class DraggableFolderNSView: NSView, NSDraggingSource { } override var intrinsicContentSize: NSSize { - NSSize(width: 16, height: 16) + TitlebarFolderIconMetrics.iconFrameSize } override var mouseDownCanMoveWindow: Bool { false } @@ -209,8 +320,8 @@ final class DraggableFolderNSView: NSView, NSDraggingSource { imageView.trailingAnchor.constraint(equalTo: trailingAnchor), imageView.topAnchor.constraint(equalTo: topAnchor), imageView.bottomAnchor.constraint(equalTo: bottomAnchor), - imageView.widthAnchor.constraint(equalToConstant: 16), - imageView.heightAnchor.constraint(equalToConstant: 16), + imageView.widthAnchor.constraint(equalToConstant: TitlebarFolderIconMetrics.iconSize), + imageView.heightAnchor.constraint(equalToConstant: TitlebarFolderIconMetrics.iconSize), ]) let dragHint = String(localized: "sidebar.folderIcon.dragHint", defaultValue: "Drag to open in Finder or another app") toolTip = dragHint @@ -224,7 +335,7 @@ final class DraggableFolderNSView: NSView, NSDraggingSource { #endif let icon = NSWorkspace.shared.icon(forFile: directory) - icon.size = NSSize(width: 16, height: 16) + icon.size = TitlebarFolderIconMetrics.iconFrameSize imageView.image = icon } @@ -343,7 +454,7 @@ final class DraggableFolderNSView: NSView, NSDraggingSource { // Add path components (current dir at top, root at bottom - matches native macOS) for pathURL in pathComponents { let icon = NSWorkspace.shared.icon(forFile: pathURL.path) - icon.size = NSSize(width: 16, height: 16) + icon.size = TitlebarFolderIconMetrics.iconFrameSize let displayName: String if pathURL.path == "/" { @@ -367,7 +478,7 @@ final class DraggableFolderNSView: NSView, NSDraggingSource { // Add computer name at the bottom (like native proxy icon) let computerName = Host.current().localizedName ?? ProcessInfo.processInfo.hostName let computerIcon = NSImage(named: NSImage.computerName) ?? NSImage() - computerIcon.size = NSSize(width: 16, height: 16) + computerIcon.size = TitlebarFolderIconMetrics.iconFrameSize let computerItem = NSMenuItem(title: computerName, action: #selector(openComputer(_:)), keyEquivalent: "") computerItem.target = self @@ -386,4 +497,5 @@ final class DraggableFolderNSView: NSView, NSDraggingSource { // Open the root filesystem entry represented by the bottom path item. NSWorkspace.shared.open(URL(fileURLWithPath: "/", isDirectory: true)) } + } diff --git a/Sources/Search/GlobalSearchDocuments.swift b/Sources/Search/GlobalSearchDocuments.swift index 11120bb19475..baa92275e4fc 100644 --- a/Sources/Search/GlobalSearchDocuments.swift +++ b/Sources/Search/GlobalSearchDocuments.swift @@ -34,7 +34,7 @@ enum GlobalSearchDocuments { kind = .browser case .markdown: kind = .markdown - case .terminal, .filePreview: + case .terminal, .filePreview, .rightSidebarTool: kind = .title } diff --git a/Sources/WindowChromeMetrics.swift b/Sources/WindowChromeMetrics.swift index 6abee8ea7d93..b17f57ff7b49 100644 --- a/Sources/WindowChromeMetrics.swift +++ b/Sources/WindowChromeMetrics.swift @@ -14,6 +14,13 @@ enum WindowChromeMetrics { } } +enum TitlebarFolderIconMetrics { + static let iconSize: CGFloat = 16 + static let iconFrameSize = CGSize(width: iconSize, height: iconSize) + static let iconTitleSpacing: CGFloat = 8 + static let iconLeadingPadding: CGFloat = -6 +} + enum MinimalModeChromeMetrics { static let titlebarHeight: CGFloat = WindowChromeMetrics.appTitlebarHeight } diff --git a/cmuxTests/WindowAndDragTests.swift b/cmuxTests/WindowAndDragTests.swift index 4452f9581b2f..115a347e99d1 100644 --- a/cmuxTests/WindowAndDragTests.swift +++ b/cmuxTests/WindowAndDragTests.swift @@ -131,6 +131,94 @@ final class WindowAccessorTests: XCTestCase { } } +@MainActor +final class CmuxMainWindowTitlebarProxyIconTests: XCTestCase { + func testMainWindowSuppressesNativeRepresentedURLProxyIcon() { + _ = NSApplication.shared + let window = CmuxMainWindow( + contentRect: NSRect(x: 0, y: 0, width: 640, height: 420), + styleMask: [.titled, .closable, .resizable, .fullSizeContentView], + backing: .buffered, + defer: false + ) + defer { window.close() } + + let workspaceDirectory = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) + window.representedURL = workspaceDirectory + + XCTAssertNil( + window.representedURL, + "cmux main windows render the workspace folder icon inside the custom titlebar, so AppKit must not keep a native proxy icon at an independent titlebar position." + ) + XCTAssertTrue( + window.representedFilename.isEmpty, + "Clearing the native proxy URL should also leave the legacy represented filename empty." + ) + + window.representedFilename = workspaceDirectory.path + + XCTAssertTrue( + window.representedFilename.isEmpty, + "cmux main windows must also reject the legacy represented filename proxy icon path." + ) + XCTAssertNil(window.representedURL) + } +} + +@MainActor +final class MainWindowFocusRedrawTests: XCTestCase { + func testKeyRegainInvalidatesRootContentView() { + _ = NSApplication.shared + + let appDelegate = AppDelegate() + let tabManager = TabManager(autoWelcomeIfNeeded: false) + let windowId = appDelegate.registerMainWindowContextForTesting(tabManager: tabManager) + defer { + appDelegate.unregisterMainWindowContextForTesting(windowId: windowId) + } + + let contentView = NSView(frame: NSRect(x: 0, y: 0, width: 640, height: 420)) + let splitView = NSSplitView(frame: contentView.bounds) + splitView.isVertical = true + splitView.autoresizingMask = [.width, .height] + splitView.dividerStyle = .thin + + let sidebar = NSView(frame: NSRect(x: 0, y: 0, width: 220, height: 420)) + let main = NSView(frame: NSRect(x: 221, y: 0, width: 419, height: 420)) + splitView.addArrangedSubview(sidebar) + splitView.addArrangedSubview(main) + contentView.addSubview(splitView) + splitView.setPosition(220, ofDividerAt: 0) + + let window = CmuxMainWindow( + contentRect: contentView.bounds, + styleMask: [.titled, .closable, .resizable], + backing: .buffered, + defer: false + ) + window.identifier = NSUserInterfaceItemIdentifier("cmux.main.\(windowId.uuidString)") + window.contentView = contentView + defer { window.close() } + + contentView.layoutSubtreeIfNeeded() + splitView.adjustSubviews() + + contentView.needsDisplay = false + + appDelegate.handleCmuxWindowResignedKey( + Notification(name: NSWindow.didResignKeyNotification, object: window) + ) + appDelegate.handleCmuxWindowBecameKey( + Notification(name: NSWindow.didBecomeKeyNotification, object: window) + ) + + XCTAssertTrue( + contentView.needsDisplay, + "Regaining key focus must invalidate the root content view." + ) + } +} + @MainActor final class AppDelegateWindowContextRoutingTests: XCTestCase { private func makeMainWindow(id: UUID) -> NSWindow { @@ -1497,6 +1585,147 @@ final class WindowDragHandleHitTests: XCTestCase { @MainActor final class DraggableFolderHitTests: XCTestCase { + func testDetachedFolderPanelFollowsHostFrameChanges() { + _ = NSApplication.shared + let window = NSWindow( + contentRect: NSRect(x: 40, y: 40, width: 320, height: 180), + styleMask: [.titled, .closable], + backing: .buffered, + defer: false + ) + defer { window.close() } + let container = NSView(frame: NSRect(x: 0, y: 0, width: 320, height: 180)) + window.contentView = container + + let hostView = DetachedFolderDragIconHostView(directory: NSTemporaryDirectory()) + hostView.frame = NSRect( + x: 24, + y: 120, + width: TitlebarFolderIconMetrics.iconSize, + height: TitlebarFolderIconMetrics.iconSize + ) + container.addSubview(hostView) + defer { hostView.removeFromSuperview() } + + hostView.syncDetachedIcon() + guard let initialFrame = hostView.detachedIconFrameForTesting else { + XCTFail("Expected detached folder panel to be created") + return + } + + hostView.setFrameOrigin(NSPoint(x: 96, y: 120)) + + guard let movedFrame = hostView.detachedIconFrameForTesting else { + XCTFail("Expected detached folder panel to remain attached") + return + } + XCTAssertEqual(movedFrame.minX, initialFrame.minX + 72, accuracy: 0.5) + XCTAssertEqual(movedFrame.minY, initialFrame.minY, accuracy: 0.5) + } + + func testDetachedFolderPanelFollowsHostFrameReplacement() { + _ = NSApplication.shared + let window = NSWindow( + contentRect: NSRect(x: 40, y: 40, width: 320, height: 180), + styleMask: [.titled, .closable], + backing: .buffered, + defer: false + ) + defer { window.close() } + let container = NSView(frame: NSRect(x: 0, y: 0, width: 320, height: 180)) + window.contentView = container + + let hostView = DetachedFolderDragIconHostView(directory: NSTemporaryDirectory()) + hostView.frame = NSRect( + x: 24, + y: 120, + width: TitlebarFolderIconMetrics.iconSize, + height: TitlebarFolderIconMetrics.iconSize + ) + container.addSubview(hostView) + defer { hostView.removeFromSuperview() } + + hostView.syncDetachedIcon() + guard let initialFrame = hostView.detachedIconFrameForTesting else { + XCTFail("Expected detached folder panel to be created") + return + } + + hostView.frame = NSRect( + x: 96, + y: 128, + width: TitlebarFolderIconMetrics.iconSize, + height: TitlebarFolderIconMetrics.iconSize + ) + + guard let movedFrame = hostView.detachedIconFrameForTesting else { + XCTFail("Expected detached folder panel to remain attached") + return + } + XCTAssertEqual(movedFrame.minX, initialFrame.minX + 72, accuracy: 0.5) + XCTAssertEqual(movedFrame.minY, initialFrame.minY + 8, accuracy: 0.5) + } + + func testDetachedFolderPanelFollowsAncestorFrameChanges() { + _ = NSApplication.shared + let window = NSWindow( + contentRect: NSRect(x: 40, y: 40, width: 320, height: 180), + styleMask: [.titled, .closable], + backing: .buffered, + defer: false + ) + defer { window.close() } + let container = NSView(frame: NSRect(x: 0, y: 0, width: 320, height: 180)) + window.contentView = container + + let titlebarHost = NSView(frame: NSRect(x: 24, y: 0, width: 180, height: 180)) + container.addSubview(titlebarHost) + defer { titlebarHost.removeFromSuperview() } + + let hostView = DetachedFolderDragIconHostView(directory: NSTemporaryDirectory()) + hostView.frame = NSRect( + x: 12, + y: 120, + width: TitlebarFolderIconMetrics.iconSize, + height: TitlebarFolderIconMetrics.iconSize + ) + titlebarHost.addSubview(hostView) + defer { hostView.removeFromSuperview() } + + hostView.syncDetachedIcon() + guard let initialFrame = hostView.detachedIconFrameForTesting else { + XCTFail("Expected detached folder panel to be created") + return + } + + titlebarHost.setFrameOrigin(NSPoint(x: 96, y: 0)) + let expectedMinX = initialFrame.minX + 72 + let expectedMinY = initialFrame.minY + let deadline = Date().addingTimeInterval(0.5) + var movedFrame = hostView.detachedIconFrameForTesting + while Date() < deadline { + if let frame = movedFrame, + abs(frame.minX - expectedMinX) <= 0.5, + abs(frame.minY - expectedMinY) <= 0.5 { + break + } + RunLoop.main.run(until: Date().addingTimeInterval(0.005)) + movedFrame = hostView.detachedIconFrameForTesting + } + + guard let movedFrame else { + XCTFail("Expected detached folder panel to remain attached") + return + } + XCTAssertEqual( + movedFrame.minX, + expectedMinX, + accuracy: 0.5, + "The detached panel must follow converted window coordinates when a sidebar toggle moves an ancestor view without changing the host view's local frame." + ) + XCTAssertEqual(movedFrame.minY, expectedMinY, accuracy: 0.5) + } + func testFolderHitTestReturnsContainerWhenInsideBounds() { let folderView = DraggableFolderNSView(directory: "/tmp") folderView.frame = NSRect(x: 0, y: 0, width: 16, height: 16)