Skip to content

fix(desktop): keep global shortcuts alive when focus leaves the composer - #55

Merged
leduckhc merged 3 commits into
mainfrom
fix/global-keyboard-shortcuts
Jul 15, 2026
Merged

fix(desktop): keep global shortcuts alive when focus leaves the composer#55
leduckhc merged 3 commits into
mainfrom
fix/global-keyboard-shortcuts

Conversation

@leduckhc

@leduckhc leduckhc commented Jul 15, 2026

Copy link
Copy Markdown
Owner

Global keymap shortcuts are matched by Flutter's Shortcuts widget, which only fires while the primary focus is a descendant of the scope (key events bubble up the focus tree). Once focus drained to a bare focus scope — after clicking an empty region or dismissing a dialog — every global shortcut went dead until the user clicked back into the composer.

DesktopKeymapScope now owns a fallback FocusNode and reclaims focus whenever it goes idle (primary focus is null or a FocusScopeNode), so global shortcuts work regardless of where focus sits. Leaf widgets (text fields, buttons) keep focus untouched; only the dead state is reclaimed. Composer-scope actions (send / Shift+Enter newline) remain local to the text field, unchanged.

Adds a regression test proving Ctrl+B toggles the sidebar with nothing focused.

What & why

How it was tested

  • cd server && pnpm test && pnpm typecheck (if server touched)
  • cd app && flutter test && flutter analyze (if app touched)

Checklist

  • I have read CONTRIBUTING.md.
  • A failing test preceded the production change where applicable (TDD).
  • The change is surgical — no unrelated refactors or formatting churn.
  • I will sign the CLA when the bot prompts (required before merge).

Summary by CodeRabbit

  • Bug Fixes
    • Restored global keyboard shortcut behavior when focus shifts away from the app’s primary focus scope.
    • Fixed Ctrl+B sidebar toggle to work correctly even when nothing currently holds focus.
    • Updated the “Open Settings” shortcut to reliably trigger the configured action.
  • Tests
    • Added a widget test to verify Ctrl+B behavior after focus is cleared.

Global keymap shortcuts are matched by Flutter's Shortcuts widget, which
only fires while the primary focus is a descendant of the scope (key events
bubble up the focus tree). Once focus drained to a bare focus scope — after
clicking an empty region or dismissing a dialog — every global shortcut went
dead until the user clicked back into the composer.

DesktopKeymapScope now owns a fallback FocusNode and reclaims focus whenever
it goes idle (primary focus is null or a FocusScopeNode), so global shortcuts
work regardless of where focus sits. Leaf widgets (text fields, buttons) keep
focus untouched; only the dead state is reclaimed. Composer-scope actions
(send / Shift+Enter newline) remain local to the text field, unchanged.

Adds a regression test proving Ctrl+B toggles the sidebar with nothing focused.
@cursor

cursor Bot commented Jul 15, 2026

Copy link
Copy Markdown

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 6a0d19fd-8f13-46a6-b802-86a23256292a

📥 Commits

Reviewing files that changed from the base of the PR and between 042d43a and 7e8a134.

📒 Files selected for processing (2)
  • app/lib/desktop/chat/keymap_scope.dart
  • app/test/desktop/keymap_scope_test.dart
🚧 Files skipped from review as they are similar to previous changes (2)
  • app/lib/desktop/chat/keymap_scope.dart
  • app/test/desktop/keymap_scope_test.dart

📝 Walkthrough

Walkthrough

DesktopKeymapScope now owns a dedicated focus node, monitors focus changes, and restores focus when focus leaves the application scope. A widget test verifies that Ctrl+B still toggles the sidebar when no widget holds focus.

Changes

Desktop keymap focus retention

Layer / File(s) Summary
Focus scope lifecycle and wiring
app/lib/desktop/chat/keymap_scope.dart
DesktopKeymapScope becomes stateful, manages _scopeFocus across its lifecycle, listens for focus changes, reclaims focus when needed, attaches the node to the wrapped subtree, and invokes the settings callback through widget.onOpenSettings().
Shortcut regression test
app/test/desktop/keymap_scope_test.dart
A widget test removes primary focus and verifies that Ctrl+B toggles the sidebar state.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant FocusManager
  participant DesktopKeymapScope
  participant ScopeFocus
  participant KeyboardShortcut
  FocusManager->>DesktopKeymapScope: notify focus becomes idle or external
  DesktopKeymapScope->>ScopeFocus: request focus
  KeyboardShortcut->>DesktopKeymapScope: dispatch Ctrl+B
  DesktopKeymapScope->>DesktopKeymapScope: toggle sidebar state
Loading

Possibly related PRs

  • leduckhc/makit#54: Modifies the same DesktopKeymapScope shortcut behavior, focus handling, and related widget test.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main fix: preserving desktop global shortcuts when focus leaves the composer.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/global-keyboard-shortcuts

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@app/lib/desktop/chat/keymap_scope.dart`:
- Around line 64-70: Update _reclaimFocusWhenIdle so it only reclaims focus when
primaryFocus is null, the root/empty scope, or a FocusScopeNode belonging to the
current chat subtree; do not treat every FocusScopeNode as idle. Preserve the
existing mounted, canRequestFocus, and hasPrimaryFocus checks, and add a
regression test covering navigation to Settings and opening a dialog/route
without focus returning to the hidden chat shell.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 9217b5a4-57f5-400e-9b19-79a27393a4df

📥 Commits

Reviewing files that changed from the base of the PR and between a929685 and 779ba89.

📒 Files selected for processing (2)
  • app/lib/desktop/chat/keymap_scope.dart
  • app/test/desktop/keymap_scope_test.dart

Comment thread app/lib/desktop/chat/keymap_scope.dart
leduckhc added 2 commits July 15, 2026 12:17
…utes

- Only reclaim focus when primary focus is truly idle: null, root scope,
  empty FocusScopeNode (no focused children), or a descendant of our scope.
- Add _isEmptyFocusScope() to check if a FocusScopeNode has no focused children.
- Add _isDescendantOfScopeFocus() to distinguish internal idle scopes from
  external scopes (dialogs, routes) that should retain focus.
- Fixes issue where global shortcuts would steal focus from open dialogs/routes.
Correct keymap scope resolution in desktop chat and update the
corresponding test.
@leduckhc
leduckhc merged commit d27f5f2 into main Jul 15, 2026
8 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 15, 2026
@leduckhc
leduckhc deleted the fix/global-keyboard-shortcuts branch July 16, 2026 14:04
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant