Translator: take the whole copied text, keep the translation when the notch is stretched

Browsers report only part of a selection over Accessibility; when the clipboard
holds a longer text containing it, translate the clipboard. Dragging the handle
moved the home view to the chat and dropped the card; now the translation goes
into the chat (after it finishes, if still loading).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
maksarsanjeev
2026-10-19 11:38:47 +03:00
parent 46901f2b62
commit 4f1e90ad8d
2 changed files with 29 additions and 8 deletions
@@ -449,6 +449,9 @@ struct ChatResizeHandle: View {
DragGesture(minimumDistance: 2, coordinateSpace: .global) DragGesture(minimumDistance: 2, coordinateSpace: .global)
.onChanged { g in .onChanged { g in
if startHeight == nil { if startHeight == nil {
// A translation card on the home view goes along into the chat
// instead of disappearing.
if state.translation != nil { TranslatorRun.moveToChat(state: state) }
if state.view != .prompt { if state.view != .prompt {
state.chatDetent = 0 state.chatDetent = 0
state.view = .prompt state.view = .prompt
+26 -8
View File
@@ -17,6 +17,8 @@ struct TranslationResult: Equatable {
var engine: String = "" var engine: String = ""
var error: String? = nil var error: String? = nil
var loading = true var loading = true
/// The user stretched the notch while it was still translating: finish into the chat.
var toChat = false
} }
@MainActor @MainActor
@@ -42,6 +44,7 @@ enum TranslatorRun {
state.translation?.engine = engine state.translation?.engine = engine
state.translation?.loading = false state.translation?.loading = false
SoundEngine.shared.play("pop") SoundEngine.shared.play("pop")
if state.translation?.toChat == true { moveToChat(state: state) }
} catch { } catch {
guard state.translation?.source == source else { return } guard state.translation?.source == source else { return }
state.translation?.error = error.localizedDescription state.translation?.error = error.localizedDescription
@@ -50,6 +53,20 @@ enum TranslatorRun {
} }
} }
/// Moves a finished translation into a chat: the full text, room to grow (the handle
/// stretches the chat), and follow-up questions go to the same dialog.
static func moveToChat(state: AppState) {
guard let result = state.translation else { return }
if result.loading, result.error == nil { state.translation?.toChat = true; return }
state.translation = nil
guard result.error == nil else { return }
if state.tabs.count < AppState.maxTabs { state.openTab(.chat) } else { state.view = .prompt }
if let tab = state.activeTabId {
ChatTabs.shared.seed(tab, "Исходный текст:\n\(result.source)\n\nПеревод:\n\(result.text)\n\nВопросы ниже — про этот перевод.")
}
state.chatHistory.append(ChatMessage(role: .assistant, content: "**Перевод:**\n\n\(result.text)"))
}
private static func show(_ state: AppState) { private static func show(_ state: AppState) {
if state.mode != .expanded || !(state.view == .overview || state.view == .empty) { if state.mode != .expanded || !(state.view == .overview || state.view == .empty) {
NotificationCenter.default.post(name: .hookExpand, object: IslandView.overview) NotificationCenter.default.post(name: .hookExpand, object: IslandView.overview)
@@ -118,7 +135,13 @@ enum SelectionGrabber {
_ = AXIsProcessTrustedWithOptions(opts) _ = AXIsProcessTrustedWithOptions(opts)
return clipboardText() return clipboardText()
} }
if let s = axSelection(), !s.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { return s } if let s = axSelection(), !s.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
// Browsers and Electron apps often report only the visible part of a selection.
// If the user copied it, the clipboard holds the whole thing.
if let clip = clipboardText(), clip.count > s.count,
clip.contains(s.trimmingCharacters(in: .whitespacesAndNewlines)) { return clip }
return s
}
// ⌘C only when BroV isn't the key window (hotkey path); from a click in the notch // ⌘C only when BroV isn't the key window (hotkey path); from a click in the notch
// the keystroke would land in BroV itself. // the keystroke would land in BroV itself.
if NSApp.keyWindow == nil, let s = await copyViaCommandC(), !s.isEmpty { return s } if NSApp.keyWindow == nil, let s = await copyViaCommandC(), !s.isEmpty { return s }
@@ -206,7 +229,7 @@ struct TranslationCard: View {
Text(result.source).lineLimit(1).font(.system(size: 11.5)).foregroundColor(Color(hex: "#6B7079")) Text(result.source).lineLimit(1).font(.system(size: 11.5)).foregroundColor(Color(hex: "#6B7079"))
} }
} else { } else {
ScrollView(.vertical, showsIndicators: false) { ScrollView(.vertical, showsIndicators: true) {
Text(result.text) Text(result.text)
.font(.system(size: 12.5)) .font(.system(size: 12.5))
.textSelection(.enabled) .textSelection(.enabled)
@@ -219,12 +242,7 @@ struct TranslationCard: View {
SoundEngine.shared.play("blip") SoundEngine.shared.play("blip")
} }
FuncButton(symbol: "bubble.left", title: "В чат") { FuncButton(symbol: "bubble.left", title: "В чат") {
if state.tabs.count < AppState.maxTabs { state.openTab(.chat) } else { state.view = .prompt } TranslatorRun.moveToChat(state: state)
if let tab = state.activeTabId {
ChatTabs.shared.seed(tab, "Исходный текст:\n\(result.source)\n\nПеревод:\n\(result.text)\n\nВопросы ниже — про этот перевод.")
}
state.chatHistory.append(ChatMessage(role: .assistant, content: "**Перевод:**\n\n\(result.text)"))
state.translation = nil
} }
Text(result.source).lineLimit(1).font(.system(size: 10.5)).foregroundColor(Color(hex: "#5F646D")) Text(result.source).lineLimit(1).font(.system(size: 10.5)).foregroundColor(Color(hex: "#5F646D"))
} }