Skip to content

Fix typing lag in Settings text editors by committing on focus loss#275

Open
tejasnafde wants to merge 1 commit into
zachlatta:mainfrom
tejasnafde:fix/settings-editor-typing-lag
Open

Fix typing lag in Settings text editors by committing on focus loss#275
tejasnafde wants to merge 1 commit into
zachlatta:mainfrom
tejasnafde:fix/settings-editor-typing-lag

Conversation

@tejasnafde

@tejasnafde tejasnafde commented Jul 12, 2026

Copy link
Copy Markdown

What

Typing in the Custom Vocabulary, System Prompt, and Context Prompt fields in Settings is laggy. Each of these editors wrote its value into the shared AppState on every keystroke, and since AppState is a single ObservableObject observed by the whole settings window and the menu bar, every character fired objectWillChange and rebuilt all of those views. On the macOS 13 target there is no per-property observation to scope that, so the whole thing re-rendered on each keystroke.

I measured it with a small timer around the affected views on a MacBook Air M1: each keystroke rebuilt 3 view bodies and took 80 to 170 ms of main-thread work before going idle, usually around 85 ms. One of the rebuilt views was the menu bar extra, which has nothing to do with the text box.

Fix

These three editors now commit into AppState when the field loses focus instead of on every keystroke, which is the same pattern the API base URL and key fields already use. The live text stays in local @State while you type, so there is no per-character churn on the shared object.

To avoid losing anything typed, they also commit on onDisappear (in case the window is closed while a field is still focused), and the prompt test runners commit first so they still test the latest text.

Testing

  • Built and ran locally on macOS 13 target, arm64.
  • Confirmed with the timer that typing no longer republishes AppState per keystroke.
  • make test passes.
  • Manually checked that vocabulary and both prompts still save, that reverting a prompt to the default still clears the custom value, and that the prompt test buttons use the current text.

Fixes #274

Summary by CodeRabbit

  • Bug Fixes
    • Custom vocabulary and prompt edits are now saved when leaving the editor or settings screen.
    • Prompt tests now use the latest text entered.
    • Empty or default prompt content is handled consistently.
    • Unchanged custom prompts no longer update their modification date unnecessarily.

The Custom Vocabulary, System Prompt, and Context Prompt editors wrote
their value into the shared AppState on every keystroke. Because AppState
is a single ObservableObject observed by the whole settings window and the
menu bar, each keystroke fired objectWillChange and rebuilt all of those
views. On a macOS 13 target there is no per-property observation to scope
the invalidation, so typing in these fields was noticeably laggy
(measured around 85ms of main-thread work per keystroke, with the menu bar
rebuilding on every character).

These fields now commit to AppState when the editor loses focus, matching
what the API base URL and key fields already do, and also on disappear so
nothing typed is lost if the window closes while focused. The prompt test
runners commit first so they still test the latest text.

Fixes zachlatta#274

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Settings text editors no longer update appState on every keystroke. Vocabulary and prompt values are committed on focus loss, view disappearance, and before running prompt tests, with trimming and default-reset handling for prompts.

Changes

Settings editor commits

Layer / File(s) Summary
Custom vocabulary commit flow
Sources/SettingsView.swift
The vocabulary editor tracks focus, trims input, commits only changed values on focus loss, and persists edits when the settings view disappears.
Custom prompt commit flow
Sources/SettingsView.swift
System and context prompt editors commit on focus loss or disappearance, clear values matching defaults or empty input, update modification dates for custom changes, and commit before prompt tests.

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

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: Settings editors now commit on focus loss to avoid typing lag.
Linked Issues check ✅ Passed The changes address #274 by stopping per-keystroke AppState updates and committing the vocabulary and prompt editors on blur/disappear.
Out of Scope Changes check ✅ Passed The edits stay focused on the lag fix, with disappear handling and pre-test commits as supporting behavior for the same settings fields.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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.

🧹 Nitpick comments (1)
Sources/SettingsView.swift (1)

1570-1596: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consider extracting a shared prompt-commit helper.

commitCustomSystemPrompt() and commitCustomContextPrompt() are structurally identical — same trim, default-comparison, clear-with-timestamp, and update-with-timestamp logic, differing only in the input variable, appState property, and default constant. A generic helper would eliminate the duplication and make future changes (e.g., timestamp format adjustments) a single-point edit.

♻️ Optional refactor: shared commit helper
+ private func commitPrompt(
+     input: String,
+     currentValue: String,
+     defaultPrompt: String,
+     setCustom: (String) -> Void,
+     setLastModified: (String) -> Void
+ ) {
+     let trimmed = input.trimmingCharacters(in: .whitespacesAndNewlines)
+     let defaultTrimmed = defaultPrompt.trimmingCharacters(in: .whitespacesAndNewlines)
+     if trimmed == defaultTrimmed || trimmed.isEmpty {
+         if !currentValue.isEmpty {
+             setCustom("")
+             setLastModified("")
+         }
+     } else if currentValue != trimmed {
+         setCustom(trimmed)
+         setLastModified(iso8601DayFormatter.string(from: Date()))
+     }
+ }
+
 private func commitCustomSystemPrompt() {
-    let trimmed = customSystemPromptInput.trimmingCharacters(in: .whitespacesAndNewlines)
-    let defaultTrimmed = PostProcessingService.defaultSystemPrompt.trimmingCharacters(in: .whitespacesAndNewlines)
-    if trimmed == defaultTrimmed || trimmed.isEmpty {
-        if !appState.customSystemPrompt.isEmpty {
-            appState.customSystemPrompt = ""
-            appState.customSystemPromptLastModified = ""
-        }
-    } else if appState.customSystemPrompt != trimmed {
-        appState.customSystemPrompt = trimmed
-        appState.customSystemPromptLastModified = iso8601DayFormatter.string(from: Date())
-    }
+    commitPrompt(
+        input: customSystemPromptInput,
+        currentValue: appState.customSystemPrompt,
+        defaultPrompt: PostProcessingService.defaultSystemPrompt,
+        setCustom: { appState.customSystemPrompt = $0 },
+        setLastModified: { appState.customSystemPromptLastModified = $0 }
+    )
 }

 private func commitCustomContextPrompt() {
-    let trimmed = customContextPromptInput.trimmingCharacters(in: .whitespacesAndNewlines)
-    let defaultTrimmed = AppContextService.defaultContextPrompt.trimmingCharacters(in: .whitespacesAndNewlines)
-    if trimmed == defaultTrimmed || trimmed.isEmpty {
-        if !appState.customContextPrompt.isEmpty {
-            appState.customContextPrompt = ""
-            appState.customContextPromptLastModified = ""
-        }
-    } else if appState.customContextPrompt != trimmed {
-        appState.customContextPrompt = trimmed
-        appState.customContextPromptLastModified = iso8601DayFormatter.string(from: Date())
-    }
+    commitPrompt(
+        input: customContextPromptInput,
+        currentValue: appState.customContextPrompt,
+        defaultPrompt: AppContextService.defaultContextPrompt,
+        setCustom: { appState.customContextPrompt = $0 },
+        setLastModified: { appState.customContextPromptLastModified = $0 }
+    )
 }
🤖 Prompt for 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.

In `@Sources/SettingsView.swift` around lines 1570 - 1596, Extract the duplicated
logic from commitCustomSystemPrompt() and commitCustomContextPrompt() into a
shared helper that accepts the input text, current stored prompt, default
prompt, and update callback or equivalent state mutation hooks. Update both
commit methods to delegate to this helper while preserving trimming,
default/empty clearing, and ISO8601 timestamp behavior.
🤖 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.

Nitpick comments:
In `@Sources/SettingsView.swift`:
- Around line 1570-1596: Extract the duplicated logic from
commitCustomSystemPrompt() and commitCustomContextPrompt() into a shared helper
that accepts the input text, current stored prompt, default prompt, and update
callback or equivalent state mutation hooks. Update both commit methods to
delegate to this helper while preserving trimming, default/empty clearing, and
ISO8601 timestamp behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 20d87d2f-b969-487f-ad82-e583063e7976

📥 Commits

Reviewing files that changed from the base of the PR and between 7427ca9 and 8fd57df.

📒 Files selected for processing (1)
  • Sources/SettingsView.swift

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Typing in the Custom Vocabulary field lags badly

1 participant