feat(panel): close with Esc without a click and render Markdown

Esc: make the panel key when shown (closeWithEscape, default on) without activating the app; the source app stays frontmost and Replace still targets it. Handle Esc with SwiftUI's exit command, which also works from the popover and after clicking controls. Add TranslationPanel.canBecomeKey and amend ADR-009.\n\nMarkdown: add MarkdownPresentation in SprachmittlerText (Foundation inline parser plus a conservative block parser for headings, lists, quotes, fenced code and rules), a SwiftUI MarkdownTranslationView, and a renderMarkdown setting (default on). JSON, HTML and code sources are never rendered; only http/https links open; images become alt text; Copy and Replace keep the raw Markdown (ADR-021).\n\nVerified: make verify green; synthetic Esc closes the panel and closes an open language popover first; a live Markdown translation rendered heading, bold/italic, inline code, list, quote, code block and link; Copy returned the raw Markdown; Replace still changed the TextEdit selection with the panel key.
This commit is contained in:
schihei committed 2026-11-26 15:26:30 +01:00
1 parent d126124f97
commit a3e544fbe4
22 files changed
+1072 -250

No files matched your search

+4 -2
View File
@@ -549,12 +549,12 @@ stateDiagram-v2
| Surface | Implementation | Contents |
|---|---|---|
| Status item | `MenuBarExtra` | Translate clipboard text, Target language submenu, Copy last translation, Replace last translation (if safe), Settings, Set up Sprachmittler, Diagnostics, Quit |
| Panel | `NSPanel` with `.nonactivatingPanel`, `becomesKeyOnlyIfNeeded = true` | Header (source → target, language picker, gear), collapsible source preview, streaming translation, warnings, Copy (left) and **Replace** (right, primary) |
| Panel | `NSPanel` with `.nonactivatingPanel`, `becomesKeyOnlyIfNeeded = false` | Header (source → target, language picker, gear), collapsible source preview, streaming translation (Markdown rendered when detected), warnings, Copy (left) and **Replace** (right, primary) |
| Settings | SwiftUI `Settings` scene | General, Model, Translation, Privacy, Diagnostics |
| Onboarding | `NSWindow` (not a SwiftUI `Window` scene) | Ollama reachable + version, Hy-MT3 installed, target language, optional Accessibility for Replace; each with a fix button |
| Diagnostics | Window from the menu | Server version, model digests, last 50 timings and error kinds; copy as text; no text or translations |
Panel keyboard: **Esc** close, **⌘C** copy translation, **⌘↩** Replace, **⌘R** retry, **⌘,** Settings. The panel becomes key only when clicked; the source app keeps focus until then. The whole header bar (full width, flush with the top edge) moves the panel; the drag view moves the window itself from the screen-space mouse delta because `performDrag` does nothing while the app is inactive; moving it keeps its position while it stays open, and a newly shown panel is placed near the cursor (DeepL-like transient position). The translation text size is a Settings value.
Panel keyboard: **Esc** close, **⌘C** copy translation, **⌘↩** Replace, **⌘R** retry, **⌘,** Settings. The panel is made key when shown (unless the setting is off) so Esc and the shortcuts work without a click; the app is still not activated, the source app remains frontmost and owns the menu bar, and clicking the source app takes keyboard focus back. While the panel is key it receives keystrokes, so a user who types immediately after triggering should disable the Esc-on-show setting. The whole header bar (full width, flush with the top edge) moves the panel; the drag view moves the window itself from the screen-space mouse delta because `performDrag` does nothing while the app is inactive; moving it keeps its position while it stays open, and a newly shown panel is placed near the cursor (DeepL-like transient position). The translation text size is a Settings value.
Accessibility: every control has an `accessibilityLabel`; the panel exposes source and translation as read-only text elements; full keyboard navigation; respects Reduce Motion and Increase Contrast.
@@ -587,6 +587,8 @@ Accessibility: every control has an `accessibilityLabel`; the panel exposes sour
| `autoCopy` | Bool | **false** (never copy automatically) |
| `showSourcePreview` | Bool | true |
| `translationFontSize` | Int | 17 pt (12–28) |
| `renderMarkdown` | Bool | true (presentation only; raw text is copied and replaced) |
| `closeWithEscape` | Bool | true (make the panel key on show; no app activation) |
| `launchAtLogin` | Bool | false |
| `keepRecentTranslationInMemory` | Bool | true (cleared on quit; no disk) |
| `hasCompletedOnboarding` | Bool | false |
+11 -4
View File
@@ -61,11 +61,11 @@ Decision: Ship with `hf.co/tencent/Hy-MT3-7B-GGUF:Q4_K_M` as the provisional def
Consequences: Until the gate, the 7B is labelled provisional in Settings. The decision is re-run when a model, Ollama or the prompt policy changes.
## ADR-009 — Menu-bar agent with a non-activating panel
Status: Accepted
Status: Accepted (amended 2026-11-26: Esc without a click)
Context: The app must be available over any app and must not take focus from the selection. A regular Dock app with a window would interrupt the workflow.
Decision: Use `LSUIElement = YES`, a `MenuBarExtra`, and a floating `NSPanel` with `.nonactivatingPanel` and `becomesKeyOnlyIfNeeded = true`. Never call `NSApp.activate` or `makeKeyAndOrderFront` for the translation panel. Onboarding and Settings are the only surfaces that activate the app.
Consequences: The source app keeps focus, so Replace can target it. Keyboard shortcuts inside the panel require a click first; every action also has a button.
Context: The app must be available over any app and must not activate itself or take application focus from the selection. A regular Dock app with a window would interrupt the workflow. Users also expect Esc to dismiss a transient panel immediately, which requires the panel to be the key window.
Decision: Use `LSUIElement = YES`, a `MenuBarExtra`, and a floating `NSPanel` with `.nonactivatingPanel` and `becomesKeyOnlyIfNeeded = false`. Never call `NSApp.activate` or `makeKeyAndOrderFront` for the translation panel. Onboarding and Settings are the only surfaces that activate the app. By default the panel calls `makeKey()` when shown, so Esc and the keyboard shortcuts work immediately; the setting `closeWithEscape` can restore click-first behaviour. The panel's SwiftUI exit command handles Esc and calls the same close path that cancels an in-flight request.
Consequences: The app stays unactivated: `NSWorkspace.frontmostApplication` and the menu bar stay with the source app, and Replace still re-verifies that app. While the panel is visible it holds keyboard focus, so keystrokes and shortcuts go to the panel until the user clicks back into the source app; the setting turns this off. Every action still has a button.
## ADR-010 — Native OpenCode 2.x configuration
Status: Accepted
@@ -143,3 +143,10 @@ Status: Accepted
Context: Panel, Settings, Onboarding and Diagnostics contain branching worth testing, but their windows are AppKit/SwiftUI. The app target must stay thin.
Decision: `PanelViewModel`, `SettingsViewModel`, `OnboardingViewModel` and `DiagnosticsViewModel` are `@MainActor @Observable` types in `SprachmittlerCore`; `SettingsStore` uses a `SettingsDataStoring` seam. `App` owns only `NSPanel`/`NSWindow` controllers, SwiftUI views, the composition root (`AppEnvironment`) and lifecycle glue. `TranslationSession` uses an injected pipeline factory so endpoint/settings changes apply on the next request without rebuilding the UI.
Consequences: Logic is covered by package tests; AppKit remains untested by `make verify` and requires the manual checklist.
## ADR-021 — Markdown is rendered as presentation only
Status: Accepted
Context: Hy-MT3 preserves Markdown structure, but a plain `Text` showed `**bold**`, headings and lists literally. Users translating Markdown want the formatting rendered, while Copy and Replace must keep the Markdown that the model produced and the validator checked.
Decision: Parse presentation Markdown in `SprachmittlerText` with Foundation's inline Markdown parser plus a small, conservative block parser (headings, lists, quotes, fenced code, rules, paragraphs). `MarkdownPresentation.shouldRender` rejects JSON, HTML and code sources and otherwise requires block or inline Markdown markers. The panel renders only when the setting `renderMarkdown` is on (default). Copy and Replace use the raw translation unchanged. Only `http` and `https` links open, on an explicit click; image syntax is reduced to alt text and no remote resource is fetched. Tables, raw HTML and nested lists/quotes remain plain text in this pass.
Consequences: Rendering never rewrites the translation or weakens validation. Literal markers can be interpreted as formatting, so the setting is the escape hatch; Copy always returns the raw Markdown. The block layout makes text selection per block, while Copy remains the whole-translation path. No new dependency is added.
+2
View File
@@ -153,3 +153,5 @@ Legend: `[ ]` open · `[~]` in progress · `[x]` done · `[!]` blocked (explain)
2026-11-26 · M4 polish · Enlarged the drag surface to the whole left header block, with a visible grip backed by an AppKit `performDrag` area; the panel keeps a moved position while visible and only recenters near the cursor when shown again. Added a persisted `translationFontSize` setting (default 17 pt, 12–28) with a Settings stepper and applied it to the translation text.
2026-11-26 · Panel · The source section now stays available as a collapsed “Show source” disclosure row after the user collapses it, instead of disappearing.
2026-11-26 · Panel · Fixed dragging: `performDrag` was a no-op while the non-activating panel's app is inactive, so `WindowDragArea` now moves the window from the screen-space mouse delta. The drag surface is a full-width 56 pt header bar flush with the panel top; buttons stay clickable above it. Verified with synthetic drags from the title, top edge, empty header and grip while Finder was frontmost.
2026-11-26 · Panel · Esc now closes the panel without a click: `TranslationPanel` becomes key on show without activating the app (`frontmostApplication` and the menu bar stay with the source app), and `keyDown` handles keyCode 53. Added the `closeWithEscape` setting (default on) and amended ADR-009.
2026-11-26 · Panel · Markdown translations render with headings, bold/italic, inline code, lists, quotes, fenced code, rules and http/https links. `MarkdownPresentation` rejects JSON/HTML/code and only renders with the `renderMarkdown` setting (default on); Copy/Replace stay raw. Added ADR-021 and tests.
+1 -1
View File
@@ -62,7 +62,7 @@ Do not substitute tokens with sentinels in v1: sentinels can leak into the outpu
| T3 | No spelling, grammar, style, terminology or punctuation edits. No de-duplication. No "cleanup" of model wording. | always |
| T4 | Do not strip a preamble or explanation. Detect it, warn, keep the raw text, disable Replace by default. | always |
The panel shows the T1 output. There is no hidden rewrite. If the model returns a preamble, the user sees it.
The panel shows the T1 output. There is no hidden rewrite. If the model returns a preamble, the user sees it. Markdown rendering in the panel is presentation only: validation, Copy and Replace all use the raw T1 string, including its Markdown markers. See ADR-021.
## 6. Preamble detection
+1 -1
View File
@@ -35,7 +35,7 @@ the Hy-MT3 model and the target language. You can reopen setup from the menu bar
1. Select text in any app.
2. Press **⌘C** twice quickly (within 500 ms).
3. The floating panel appears near the mouse. The source app keeps focus.
3. The floating panel appears near the mouse. The source app stays frontmost; the panel takes keyboard focus while it is visible so Esc and the shortcuts work immediately. Clicking the source app takes focus back.
4. The translation streams in. Use the language picker to change the target.
5. Click **Copy** to put the translation on the clipboard, or **Replace** to replace the selection.
6. **Esc** closes the panel. **⌘C** copies, **⌘↩** replaces, **⌘R** retries and **⌘,** opens Settings.