Skip to content

Commit a375df9

Browse files
committed
fix(editor): arm new-tab focus claim before the editor view loads, add focus logging (#1765)
1 parent 0ace837 commit a375df9

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

TablePro/Views/Editor/SQLEditorCoordinator.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ final class SQLEditorCoordinator: TextViewCoordinator, TextViewDelegate {
5454

5555
func scheduleEditorFocusClaim() {
5656
focusClaimPending = true
57+
Self.logger.debug("Editor focus claim armed")
5758
}
5859

5960
/// Vim mode for UI observation
@@ -118,12 +119,18 @@ final class SQLEditorCoordinator: TextViewCoordinator, TextViewDelegate {
118119
EditorEventRouter.shared.register(self, textView: textView)
119120

120121
if !self.isDestroyed, let window = textView.window {
121-
if self.focusClaimPending {
122+
let claimPending = self.focusClaimPending
123+
let responderName = window.firstResponder.map { String(describing: type(of: $0)) } ?? "nil"
124+
var made = false
125+
if claimPending {
122126
self.focusClaimPending = false
123-
window.makeFirstResponder(textView)
127+
made = window.makeFirstResponder(textView)
124128
} else if window.firstResponder == nil || window.firstResponder === window {
125-
window.makeFirstResponder(textView)
129+
made = window.makeFirstResponder(textView)
126130
}
131+
Self.logger.debug("Editor focus claim: pending=\(claimPending) isKey=\(window.isKeyWindow) responderBefore=\(responderName, privacy: .public) made=\(made)")
132+
} else {
133+
Self.logger.debug("Editor focus claim skipped: hasWindow=\(textView.window != nil) destroyed=\(self.isDestroyed) pending=\(self.focusClaimPending)")
127134
}
128135
if controller.cursorPositions.isEmpty {
129136
controller.setCursorPositions([CursorPosition(range: NSRange(location: 0, length: 0))])

TablePro/Views/Editor/SQLEditorView.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ struct SQLEditorView: View {
5050
coordinator.databaseType = databaseType
5151
coordinator.tabID = tabID
5252
coordinator.connectionId = connectionId
53+
if claimFocusOnAppear {
54+
coordinator.scheduleEditorFocusClaim()
55+
}
5356

5457
return SourceEditor(
5558
$text,
@@ -60,11 +63,6 @@ struct SQLEditorView: View {
6063
completionDelegate: completionAdapter
6164
)
6265
.accessibilityLabel(String(localized: "SQL query editor"))
63-
.onAppear {
64-
if claimFocusOnAppear {
65-
coordinator.scheduleEditorFocusClaim()
66-
}
67-
}
6866
.onChange(of: editorState.cursorPositions) { _, newValue in
6967
guard let positions = newValue else { return }
7068
// Skip cursor propagation when the editor doesn't have focus

0 commit comments

Comments
 (0)