Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions Sources/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,6 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent
private var pendingConfiguredShortcutChord: PendingConfiguredShortcutChord?
var activeConfiguredShortcutChordPrefixForCurrentEvent: ShortcutStroke?
var shortcutEventFocusContextCache: ShortcutEventFocusContextCache?
private var configuredShortcutChordActions: [KeyboardShortcutSettings.Action] = []
private var ghosttyConfigObserver: NSObjectProtocol?
private var ghosttyGotoSplitLeftShortcut: StoredShortcut?
private var ghosttyGotoSplitRightShortcut: StoredShortcut?
Expand Down Expand Up @@ -11014,7 +11013,6 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent

private func installShortcutDefaultsObserver() {
guard shortcutDefaultsObserver == nil else { return }
refreshConfiguredShortcutChordActions()
shortcutDefaultsObserver = NotificationCenter.default.addObserver(
forName: KeyboardShortcutSettings.didChangeNotification,
object: nil,
Expand All @@ -11033,13 +11031,12 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent
}

private func handleShortcutDefaultsDidChange() {
refreshConfiguredShortcutChordActions()
clearConfiguredShortcutChordState()
scheduleSplitButtonTooltipRefreshAcrossWorkspaces()
}

private func refreshConfiguredShortcutChordActions() {
configuredShortcutChordActions = KeyboardShortcutSettings.Action.allCases.filter { action in
private func currentConfiguredShortcutChordActions() -> [KeyboardShortcutSettings.Action] {
KeyboardShortcutSettings.Action.allCases.filter { action in
// System-wide hotkeys are dispatched via Carbon RegisterEventHotKey
// and never routed through AppKit's local key handler. If a managed
// cmux.json entry somehow stores one as a chord, arming the prefix
Expand Down Expand Up @@ -11720,6 +11717,16 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent
return false
}

if activeConfiguredShortcutChordPrefixForCurrentEvent == nil {
let focusContext = shortcutEventFocusContext(event)
let availableChordActions = currentConfiguredShortcutChordActions().filter { action in
action.shortcutContext.isAlwaysAvailable || action.shortcutContext.isAvailable(focusContext)
}
if armConfiguredShortcutChordIfNeeded(event: event, actions: availableChordActions) {
return true
}
}

Comment thread
lawrencecchen marked this conversation as resolved.
Resolved
let configuredCmuxShortcutContext = preferredMainWindowContextForShortcutRouting(event: event)
let configuredCmuxShortcutActions = configuredCmuxShortcutActions(for: configuredCmuxShortcutContext)

Expand Down Expand Up @@ -13204,7 +13211,6 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent
}

func debugResetShortcutRoutingStateForTesting() {
refreshConfiguredShortcutChordActions()
clearConfiguredShortcutChordState()
shortcutEventFocusContextCache = nil
}
Expand Down Expand Up @@ -13402,7 +13408,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent
shortcuts: [StoredShortcut] = []
) -> Bool {
var seen = Set<StoredShortcut>()
let configuredShortcuts = (actions ?? configuredShortcutChordActions).map {
let configuredShortcuts = (actions ?? currentConfiguredShortcutChordActions()).map {
KeyboardShortcutSettings.shortcut(for: $0)
} + shortcuts
for shortcut in configuredShortcuts {
Expand Down
30 changes: 29 additions & 1 deletion cmuxTests/AppDelegateEqualizeSplitsShortcutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ final class AppDelegateEqualizeSplitsShortcutTests: XCTestCase {
XCTFail("Expected cached layout snapshot after seeding split geometry")
return
}
let expectedEqualizedPositions = shortcutRoutingExpectedEqualizedDividerPositions(
in: workspace.bonsplitController.treeSnapshot()
)

guard let event = makeKeyDownEvent(key: "=", modifiers: [.command, .control], keyCode: 24, windowNumber: window.windowNumber) else {
XCTFail("Failed to construct Cmd+Ctrl+= event")
Expand All @@ -130,7 +133,11 @@ final class AppDelegateEqualizeSplitsShortcutTests: XCTestCase {
let equalizedSplits = shortcutRoutingSplitNodes(in: workspace.bonsplitController.treeSnapshot())
XCTAssertEqual(equalizedSplits.count, seededSplits.count)
for split in equalizedSplits {
XCTAssertEqual(split.dividerPosition, 0.5, accuracy: 0.000_1)
guard let expectedPosition = expectedEqualizedPositions[split.id] else {
XCTFail("Expected equalized split ID to remain present")
continue
}
XCTAssertEqual(split.dividerPosition, expectedPosition, accuracy: 0.000_1)
}

let liveEqualizedLayout = workspace.bonsplitController.layoutSnapshot()
Expand All @@ -154,6 +161,27 @@ final class AppDelegateEqualizeSplitsShortcutTests: XCTestCase {
}
}

private func shortcutRoutingExpectedEqualizedDividerPositions(in node: ExternalTreeNode) -> [String: Double] {
var positionsBySplitId: [String: Double] = [:]

@discardableResult
func collectLeafCount(_ node: ExternalTreeNode) -> Int {
switch node {
case .pane:
return 1
case .split(let split):
let firstLeafCount = collectLeafCount(split.first)
let secondLeafCount = collectLeafCount(split.second)
let totalLeafCount = firstLeafCount + secondLeafCount
positionsBySplitId[split.id] = Double(firstLeafCount) / Double(totalLeafCount)
return totalLeafCount
}
}

collectLeafCount(node)
return positionsBySplitId
}

private func shortcutRoutingPaneFramesById(in snapshot: LayoutSnapshot) -> [String: PixelRect] {
Dictionary(uniqueKeysWithValues: snapshot.panes.map { ($0.paneId, $0.frame) })
}
Expand Down
37 changes: 36 additions & 1 deletion cmuxTests/AppDelegateShortcutRoutingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ final class AppDelegateShortcutRoutingTests: XCTestCase {
fallbackPath: nil,
startWatching: false
)
#if DEBUG
appDelegate.debugResetShortcutRoutingStateForTesting()
#endif

window.makeKeyAndOrderFront(nil)
RunLoop.main.run(until: Date(timeIntervalSinceNow: 0.05))
Expand Down Expand Up @@ -820,7 +823,9 @@ final class AppDelegateShortcutRoutingTests: XCTestCase {
)

appDelegate.debugCreateMainWindowSourceIsNativeFullScreenOverride = nil
RunLoop.main.run(until: Date(timeIntervalSinceNow: 0.05))
waitUntil(timeout: 1.0) {
!newWindow.collectionBehavior.contains(.fullScreenDisallowsTiling)
}

XCTAssertFalse(
newWindow.collectionBehavior.contains(.fullScreenDisallowsTiling),
Expand Down Expand Up @@ -1691,12 +1696,16 @@ final class AppDelegateShortcutRoutingTests: XCTestCase {
}

let windowId = appDelegate.createMainWindow()
defer { closeWindow(withId: windowId) }
guard let targetWindow = window(withId: windowId) else {
XCTFail("Expected test window")
return
}
targetWindow.makeKeyAndOrderFront(nil)
RunLoop.main.run(until: Date(timeIntervalSinceNow: 0.05))

appDelegate.debugCloseMainWindowConfirmationHandler = { _ in true }
defer { appDelegate.debugCloseMainWindowConfirmationHandler = nil }

guard let event = makeKeyDownEvent(
key: "w",
Expand Down Expand Up @@ -1839,13 +1848,24 @@ final class AppDelegateShortcutRoutingTests: XCTestCase {

AppDelegate.installWindowResponderSwizzlesForTesting()

let windowId = appDelegate.createMainWindow()
defer { closeWindow(withId: windowId) }

guard let mainWindow = window(withId: windowId) else {
XCTFail("Expected test main window")
return
}
mainWindow.makeKeyAndOrderFront(nil)
RunLoop.main.run(until: Date(timeIntervalSinceNow: 0.05))

let previousMainMenu = NSApp.mainMenu
let probeWindow = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 320, height: 240),
styleMask: [.titled, .closable],
backing: .buffered,
defer: false
)
probeWindow.isReleasedWhenClosed = false
probeWindow.identifier = NSUserInterfaceItemIdentifier("cmux.browser-popup")
let contentView = NSView(frame: probeWindow.contentRect(forFrameRect: probeWindow.frame))
let probeView = GhosttyCommandEquivalentProbeView(frame: NSRect(x: 0, y: 0, width: 200, height: 120))
Expand All @@ -1854,6 +1874,8 @@ final class AppDelegateShortcutRoutingTests: XCTestCase {
defer {
NSApp.mainMenu = previousMainMenu
probeWindow.orderOut(nil)
probeWindow.close()
RunLoop.main.run(until: Date(timeIntervalSinceNow: 0.05))
}

let staleMenu = NSMenu(title: "Test")
Expand Down Expand Up @@ -3327,6 +3349,8 @@ final class AppDelegateShortcutRoutingTests: XCTestCase {
XCTFail("Expected focused browser panel")
return
}
window.makeKeyAndOrderFront(nil)
RunLoop.main.run(until: Date(timeIntervalSinceNow: 0.05))

let field = OmnibarNativeTextField(frame: NSRect(x: 8, y: 8, width: 240, height: 24))
field.identifier = browserOmnibarTextFieldIdentifier
Expand Down Expand Up @@ -4224,6 +4248,8 @@ final class AppDelegateShortcutRoutingTests: XCTestCase {
XCTFail("Expected test window")
return
}
window.makeKeyAndOrderFront(nil)
RunLoop.main.run(until: Date(timeIntervalSinceNow: 0.05))

let overlayContainer = NSView(frame: contentView.bounds)
overlayContainer.identifier = commandPaletteOverlayContainerIdentifier
Expand Down Expand Up @@ -6068,6 +6094,8 @@ final class AppDelegateShortcutRoutingTests: XCTestCase {
XCTFail("Expected focused terminal surface")
return
}
window.makeKeyAndOrderFront(nil)
RunLoop.main.run(until: Date(timeIntervalSinceNow: 0.05))

appDelegate.noteTerminalKeyboardFocusIntent(workspaceId: workspace.id, panelId: terminalPanel.id, in: window)

Expand Down Expand Up @@ -6475,6 +6503,13 @@ final class AppDelegateShortcutRoutingTests: XCTestCase {
RunLoop.main.run(until: Date(timeIntervalSinceNow: 0.05))
}

private func waitUntil(timeout: TimeInterval, condition: () -> Bool) {
let deadline = Date(timeIntervalSinceNow: timeout)
while !condition(), Date() < deadline {
RunLoop.main.run(until: Date(timeIntervalSinceNow: 0.01))
}
}

private func restoreDefaultsValue(_ value: Any?, forKey key: String, defaults: UserDefaults) {
if let value {
defaults.set(value, forKey: key)
Expand Down
Loading