fix(editor): focus the SQL editor when opening a new tab (#1765)#1766
Open
datlechin wants to merge 2 commits into
Open
fix(editor): focus the SQL editor when opening a new tab (#1765)#1766datlechin wants to merge 2 commits into
datlechin wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1765.
Problem
Opening a new SQL editor tab with ⌘T often put keyboard focus in the sidebar "Filter" search field instead of the editor, so typed SQL went into the filter. Reliable repro: connect, ⌘T (focus correct), ⌘T again, focus jumps to the filter field. It also happens with no tabs open when the filter field already has focus.
Root cause
A new tab's window never sets
initialFirstResponder, so AppKit auto-assigns focus to the geometrically-leading focusable view (the sidebar filter field) when the window becomes key.SQLEditorCoordinator.prepareCoordinator()tried to claim focus 50ms later, but only whenwindow.firstResponder == nil || === window, so it backed off the moment the filter field already held focus. That guard inferred user intent from responder state, which carries no meaning on a window AppKit just pre-filled.Fix
Carry focus as an explicit one-shot intent instead of inferring it from responder state:
QueryTabManager.addTab(claimFocus:)setspendingFocusTabId(@ObservationIgnored, so it never triggers a render loop).addTaband the new-window.newEmptyTabpath. Restored tabs and table/content tabs do not, so they never steal focus.claimFocusOnAppearthroughQueryEditorViewtoSQLEditorView, and clears the flag in.onAppear(read-only inbody).SQLEditorViewcallscoordinator.scheduleEditorFocusClaim();prepareCoordinatorthen callsmakeFirstResponderunconditionally when the latch is set, with anacceptsFirstResponderprecheck. The oldfirstResponder == nilcheck stays only as the fallback for non-intent cases.The native API (deferred
makeFirstResponder, the only way to focus the CodeEditSourceEditorNSTextView, which is opaque to SwiftUI@FocusState) was already correct; only the gating changed.Tests
QueryTabManagerFocusTests(new):claimFocussets/updatespendingFocusTabId; absent it stays nil; file-reopen dedup andaddTableTabdo not set it.SQLEditorCoordinatorTests(extended): the focus-claim latch starts false,scheduleEditorFocusClaim()flips it, idempotent.The live
makeFirstResponderpath needs anNSWindowand a connection, so it is not headless-deterministic; it is verified by manual repro of both scenarios above. This matches the existing unit-onlyFilterFocusStateTests.Notes
swiftlint lint --strictpasses on the changed files.