Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions GhosttyTabs.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
CA39C0304FE351A21C372429 /* SidebarWidthPolicyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE0171AF1F49F7547191CEE5 /* SidebarWidthPolicyTests.swift */; };
CB23911D7E131E8FBC9B82B6 /* SidebarOrderingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC39DE4B96D1931C52AF7D68 /* SidebarOrderingTests.swift */; };
D0E0F0B0A1B2C3D4E5F60718 /* BrowserPaneNavigationKeybindUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0E0F0B1A1B2C3D4E5F60718 /* BrowserPaneNavigationKeybindUITests.swift */; };
D0E0F0B4A1B2C3D4E5F60718 /* FindSelectionShortcutUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0E0F0B5A1B2C3D4E5F60718 /* FindSelectionShortcutUITests.swift */; };
D0E0F0B2A1B2C3D4E5F60718 /* BrowserOmnibarSuggestionsUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0E0F0B3A1B2C3D4E5F60718 /* BrowserOmnibarSuggestionsUITests.swift */; };
D1320AA0D1320AA0D1320AA1 /* AppIconDockTilePlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1320AA0D1320AA0D1320AA4 /* AppIconDockTilePlugin.swift */; };
D1320AA0D1320AA0D1320AA2 /* CmuxDockTilePlugin.plugin in Copy Dock Tile Plugin */ = {isa = PBXBuildFile; fileRef = D1320AA0D1320AA0D1320AA5 /* CmuxDockTilePlugin.plugin */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
Expand Down Expand Up @@ -426,6 +427,7 @@
C1ADE00001A1B2C3D4E5F719 /* claude */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = Resources/bin/claude; sourceTree = SOURCE_ROOT; };
C2577001A1B2C3D4E5F60718 /* TerminalCmdClickUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TerminalCmdClickUITests.swift; sourceTree = "<group>"; };
D0E0F0B1A1B2C3D4E5F60718 /* BrowserPaneNavigationKeybindUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrowserPaneNavigationKeybindUITests.swift; sourceTree = "<group>"; };
D0E0F0B5A1B2C3D4E5F60718 /* FindSelectionShortcutUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FindSelectionShortcutUITests.swift; sourceTree = "<group>"; };
D0E0F0B3A1B2C3D4E5F60718 /* BrowserOmnibarSuggestionsUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrowserOmnibarSuggestionsUITests.swift; sourceTree = "<group>"; };
D1320AA0D1320AA0D1320AA4 /* AppIconDockTilePlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppIconDockTilePlugin.swift; sourceTree = "<group>"; };
D1320AA0D1320AA0D1320AA5 /* CmuxDockTilePlugin.plugin */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CmuxDockTilePlugin.plugin; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -568,6 +570,7 @@
C2577001A1B2C3D4E5F60718 /* TerminalCmdClickUITests.swift */,
E6FA9085A1B2C3D4E5F60718 /* WorkspaceDescriptionUITests.swift */,
D0E0F0B1A1B2C3D4E5F60718 /* BrowserPaneNavigationKeybindUITests.swift */,
D0E0F0B5A1B2C3D4E5F60718 /* FindSelectionShortcutUITests.swift */,
D0E0F0B3A1B2C3D4E5F60718 /* BrowserOmnibarSuggestionsUITests.swift */,
FB100001A1B2C3D4E5F60718 /* BrowserImportProfilesUITests.swift */,
C0B4D9B1A1B2C3D4E5F60718 /* UpdatePillUITests.swift */,
Expand Down Expand Up @@ -1161,6 +1164,7 @@
C2577000A1B2C3D4E5F60718 /* TerminalCmdClickUITests.swift in Sources */,
E6FA9084A1B2C3D4E5F60718 /* WorkspaceDescriptionUITests.swift in Sources */,
D0E0F0B0A1B2C3D4E5F60718 /* BrowserPaneNavigationKeybindUITests.swift in Sources */,
D0E0F0B4A1B2C3D4E5F60718 /* FindSelectionShortcutUITests.swift in Sources */,
D0E0F0B2A1B2C3D4E5F60718 /* BrowserOmnibarSuggestionsUITests.swift in Sources */,
FB100000A1B2C3D4E5F60718 /* BrowserImportProfilesUITests.swift in Sources */,
C0B4D9B0A1B2C3D4E5F60718 /* UpdatePillUITests.swift in Sources */,
Expand Down
37 changes: 17 additions & 20 deletions Sources/Find/BrowserSearchOverlay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,19 @@ private struct BrowserSearchTextFieldRepresentable: NSViewRepresentable {
}
}

func focusField(_ field: BrowserSearchNativeTextField, in window: NSWindow) {
guard window.makeFirstResponder(field) else { return }
func focusField(_ field: BrowserSearchNativeTextField, in window: NSWindow, selectAll: Bool) {
let alreadyFocused = cmuxTextFieldIsFirstResponder(field, in: window)
guard alreadyFocused || window.makeFirstResponder(field) else { return }
Comment thread
coderabbitai[bot] marked this conversation as resolved.
DispatchQueue.main.async { [weak field] in
guard let field,
let editor = field.currentEditor() as? NSTextView else { return }
let end = field.stringValue.utf16.count
editor.setSelectedRange(NSRange(location: end, length: 0))
guard !editor.hasMarkedText() else { return }
if selectAll {
editor.setSelectedRange(NSRange(location: 0, length: editor.string.utf16.count))
} else if !alreadyFocused {
let end = field.stringValue.utf16.count
editor.setSelectedRange(NSRange(location: end, length: 0))
}
}
}

Expand Down Expand Up @@ -299,12 +305,10 @@ private struct BrowserSearchTextFieldRepresentable: NSViewRepresentable {
notifiedPanelId == coordinator.parent.panelId else { return }
guard coordinator.parent.canApplyFocusRequest(coordinator.parent.focusRequestGeneration) else { return }
guard let window = field.window else { return }
let fr = window.firstResponder
let alreadyFocused = fr === field ||
field.currentEditor() != nil ||
((fr as? NSTextView)?.delegate as? NSTextField) === field
guard !alreadyFocused else { return }
coordinator.focusField(field, in: window)
let selectAll = notification.userInfo?[FindFocusNotificationKey.selectAll] as? Bool == true
let alreadyFocused = cmuxTextFieldIsFirstResponder(field, in: window)
guard !alreadyFocused || selectAll else { return }
coordinator.focusField(field, in: window, selectAll: selectAll)
}
return field
}
Expand All @@ -325,11 +329,7 @@ private struct BrowserSearchTextFieldRepresentable: NSViewRepresentable {
}

if let window = nsView.window {
let fr = window.firstResponder
let isFirstResponder =
fr === nsView ||
nsView.currentEditor() != nil ||
((fr as? NSTextView)?.delegate as? NSTextField) === nsView
let isFirstResponder = cmuxTextFieldIsFirstResponder(nsView, in: window)

if isFocused,
canApplyFocusRequest(focusRequestGeneration),
Expand All @@ -342,12 +342,9 @@ private struct BrowserSearchTextFieldRepresentable: NSViewRepresentable {
coordinator.parent.isFocused,
coordinator.parent.canApplyFocusRequest(coordinator.parent.focusRequestGeneration) else { return }
guard let nsView, let window = nsView.window else { return }
let fr = window.firstResponder
let alreadyFocused = fr === nsView ||
nsView.currentEditor() != nil ||
((fr as? NSTextView)?.delegate as? NSTextField) === nsView
let alreadyFocused = cmuxTextFieldIsFirstResponder(nsView, in: window)
guard !alreadyFocused else { return }
coordinator.focusField(nsView, in: window)
coordinator.focusField(nsView, in: window, selectAll: false)
}
}
}
Expand Down
50 changes: 32 additions & 18 deletions Sources/Find/SurfaceSearchOverlay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import AppKit
import Bonsplit
import SwiftUI

enum FindFocusNotificationKey {
static let selectAll = "cmux.find.selectAll"
}

func cmuxTextFieldIsFirstResponder(_ field: NSTextField, in window: NSWindow) -> Bool {
let fr = window.firstResponder
return fr === field ||
field.currentEditor() != nil ||
(fr as? NSTextView).flatMap { cmuxFieldEditorOwnerView($0) } === field
}

private extension NSView {
func cmuxAncestor<T: NSView>(of type: T.Type) -> T? {
var current: NSView? = self
Expand Down Expand Up @@ -251,6 +262,18 @@ private struct SearchTextFieldRepresentable: NSViewRepresentable {
}
}

func focusField(_ field: SearchNativeTextField, in window: NSWindow, selectAll: Bool) {
let alreadyFocused = cmuxTextFieldIsFirstResponder(field, in: window)
guard alreadyFocused || window.makeFirstResponder(field) else { return }
guard selectAll else { return }
DispatchQueue.main.async { [weak field] in
guard let field,
let editor = field.currentEditor() as? NSTextView else { return }
guard !editor.hasMarkedText() else { return }
editor.setSelectedRange(NSRange(location: 0, length: editor.string.utf16.count))
}
}

func controlTextDidChange(_ obj: Notification) {
guard !isProgrammaticMutation else { return }
guard let field = obj.object as? NSTextField else { return }
Expand Down Expand Up @@ -324,25 +347,23 @@ private struct SearchTextFieldRepresentable: NSViewRepresentable {
surface.id == coordinator.parent.surfaceId else { return }
guard coordinator.parent.canApplyFocusRequest() else { return }
guard let window = field.window else { return }
let selectAll = notification.userInfo?[FindFocusNotificationKey.selectAll] as? Bool == true
// Don't re-focus if already first responder. makeFirstResponder on an
// already-editing NSTextField ends the editing session and restarts it
// with all text selected, causing typed characters to replace each other.
let fr = window.firstResponder
let alreadyFocused = fr === field ||
field.currentEditor() != nil ||
((fr as? NSTextView)?.delegate as? NSTextField) === field
let alreadyFocused = cmuxTextFieldIsFirstResponder(field, in: window)
#if DEBUG
cmuxDebugLog(
"find.nativeField.searchFocusNotification surface=\(coordinator.parent.surfaceId.uuidString.prefix(5)) " +
"alreadyFocused=\(alreadyFocused) firstResponder=\(String(describing: fr))"
"alreadyFocused=\(alreadyFocused) firstResponder=\(String(describing: window.firstResponder))"
)
#endif
guard !alreadyFocused else { return }
let result = window.makeFirstResponder(field)
guard !alreadyFocused || selectAll else { return }
coordinator.focusField(field, in: window, selectAll: selectAll)
#if DEBUG
cmuxDebugLog(
"find.nativeField.searchFocusApply surface=\(coordinator.parent.surfaceId.uuidString.prefix(5)) " +
"result=\(result ? 1 : 0) firstResponder=\(String(describing: window.firstResponder))"
"selectAll=\(selectAll ? 1 : 0) firstResponder=\(String(describing: window.firstResponder))"
)
#endif
}
Expand All @@ -368,11 +389,7 @@ private struct SearchTextFieldRepresentable: NSViewRepresentable {

// Sync focus from binding to AppKit
if let window = nsView.window {
let fr = window.firstResponder
let isFirstResponder =
fr === nsView ||
nsView.currentEditor() != nil ||
((fr as? NSTextView)?.delegate as? NSTextField) === nsView
let isFirstResponder = cmuxTextFieldIsFirstResponder(nsView, in: window)

if isFocused,
canApplyFocusRequest(),
Expand All @@ -385,12 +402,9 @@ private struct SearchTextFieldRepresentable: NSViewRepresentable {
coordinator.parent.isFocused,
coordinator.parent.canApplyFocusRequest() else { return }
guard let nsView, let window = nsView.window else { return }
let fr = window.firstResponder
let alreadyFocused = fr === nsView ||
nsView.currentEditor() != nil ||
((fr as? NSTextView)?.delegate as? NSTextField) === nsView
let alreadyFocused = cmuxTextFieldIsFirstResponder(nsView, in: window)
guard !alreadyFocused else { return }
window.makeFirstResponder(nsView)
coordinator.focusField(nsView, in: window, selectAll: false)
}
}
}
Expand Down
17 changes: 7 additions & 10 deletions Sources/Panels/BrowserPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5031,18 +5031,18 @@ extension BrowserPanel {
"firstResponder=\(String(describing: window?.firstResponder))"
)
#endif
postBrowserSearchFocusNotification(reason: "immediate", generation: generation)
postBrowserSearchFocusNotification(reason: "immediate", generation: generation, selectAll: !created)
// Focus notification can race with portal overlay mount. Re-post on the
// next runloop and shortly after so the find field can claim first responder.
DispatchQueue.main.async { [weak self] in
self?.postBrowserSearchFocusNotification(reason: "async0", generation: generation)
self?.postBrowserSearchFocusNotification(reason: "async0", generation: generation, selectAll: !created)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { [weak self] in
self?.postBrowserSearchFocusNotification(reason: "async50ms", generation: generation)
self?.postBrowserSearchFocusNotification(reason: "async50ms", generation: generation, selectAll: false)
Comment on lines 5040 to +5041

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve select-all intent on final browser focus retry

The async50ms retry is explicitly there to handle cases where earlier focus notifications race with find-bar mount, but this call now hard-codes selectAll: false. In that race path, a repeated Cmd+F (where created == false) will focus the field without selecting existing text, so typing appends instead of replacing. This makes the new behavior flaky on slower/layout-churn paths where only the delayed retry is effective.

Useful? React with 👍 / 👎.

}
}

private func postBrowserSearchFocusNotification(reason: String, generation: UInt64) {
private func postBrowserSearchFocusNotification(reason: String, generation: UInt64, selectAll: Bool) {
guard canApplySearchFocusRequest(generation) else {
#if DEBUG
cmuxDebugLog(
Expand All @@ -5057,11 +5057,11 @@ extension BrowserPanel {
cmuxDebugLog(
"browser.find.focusNotification panel=\(id.uuidString.prefix(5)) " +
"generation=\(generation) " +
"reason=\(reason) window=\(window?.windowNumber ?? -1) " +
"reason=\(reason) selectAll=\(selectAll ? 1 : 0) window=\(window?.windowNumber ?? -1) " +
"firstResponder=\(String(describing: window?.firstResponder))"
)
#endif
NotificationCenter.default.post(name: .browserSearchFocus, object: id)
NotificationCenter.default.post(name: .browserSearchFocus, object: id, userInfo: [FindFocusNotificationKey.selectAll: selectAll])
}

func findNext() {
Expand Down Expand Up @@ -5092,10 +5092,7 @@ extension BrowserPanel {
if replaySearch, !state.needle.isEmpty {
executeFindSearch(state.needle)
}
postBrowserSearchFocusNotification(
reason: "restoreAfterNavigation",
generation: searchFocusRequestGeneration
)
postBrowserSearchFocusNotification(reason: "restoreAfterNavigation", generation: searchFocusRequestGeneration, selectAll: false)
}

private func executeFindSearch(_ needle: String) {
Expand Down
17 changes: 7 additions & 10 deletions Sources/TabManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1917,19 +1917,16 @@ class TabManager: ObservableObject {
}

func startSearch() {
if let panel = selectedTerminalPanel {
if panel.searchState == nil {
panel.searchState = TerminalSurface.SearchState()
}
NSLog("Find: startSearch workspace=%@ panel=%@", panel.workspaceId.uuidString, panel.id.uuidString)
NotificationCenter.default.post(name: .ghosttySearchFocus, object: panel.surface)
_ = panel.performBindingAction("start_search")
return
}
if let panel = selectedTerminalPanel {
let hadExistingSearch = panel.searchState != nil
let handled = startOrFocusTerminalSearch(panel.surface)
NSLog("Find: startSearch workspace=%@ panel=%@", panel.workspaceId.uuidString, panel.id.uuidString)
let handled = startOrFocusTerminalSearch(panel.surface) { surface in
NotificationCenter.default.post(
name: .ghosttySearchFocus,
object: surface,
userInfo: [FindFocusNotificationKey.selectAll: hadExistingSearch]
)
}
#if DEBUG
cmuxDebugLog(
"find.startSearch workspace=\(panel.workspaceId.uuidString.prefix(5)) " +
Expand Down
Loading