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:
@@ -449,6 +449,9 @@ struct ChatResizeHandle: View {
|
||||
DragGesture(minimumDistance: 2, coordinateSpace: .global)
|
||||
.onChanged { g in
|
||||
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 {
|
||||
state.chatDetent = 0
|
||||
state.view = .prompt
|
||||
|
||||
@@ -17,6 +17,8 @@ struct TranslationResult: Equatable {
|
||||
var engine: String = ""
|
||||
var error: String? = nil
|
||||
var loading = true
|
||||
/// The user stretched the notch while it was still translating: finish into the chat.
|
||||
var toChat = false
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@@ -42,6 +44,7 @@ enum TranslatorRun {
|
||||
state.translation?.engine = engine
|
||||
state.translation?.loading = false
|
||||
SoundEngine.shared.play("pop")
|
||||
if state.translation?.toChat == true { moveToChat(state: state) }
|
||||
} catch {
|
||||
guard state.translation?.source == source else { return }
|
||||
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) {
|
||||
if state.mode != .expanded || !(state.view == .overview || state.view == .empty) {
|
||||
NotificationCenter.default.post(name: .hookExpand, object: IslandView.overview)
|
||||
@@ -118,7 +135,13 @@ enum SelectionGrabber {
|
||||
_ = AXIsProcessTrustedWithOptions(opts)
|
||||
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
|
||||
// the keystroke would land in BroV itself.
|
||||
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"))
|
||||
}
|
||||
} else {
|
||||
ScrollView(.vertical, showsIndicators: false) {
|
||||
ScrollView(.vertical, showsIndicators: true) {
|
||||
Text(result.text)
|
||||
.font(.system(size: 12.5))
|
||||
.textSelection(.enabled)
|
||||
@@ -219,12 +242,7 @@ struct TranslationCard: View {
|
||||
SoundEngine.shared.play("blip")
|
||||
}
|
||||
FuncButton(symbol: "bubble.left", title: "В чат") {
|
||||
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)"))
|
||||
state.translation = nil
|
||||
TranslatorRun.moveToChat(state: state)
|
||||
}
|
||||
Text(result.source).lineLimit(1).font(.system(size: 10.5)).foregroundColor(Color(hex: "#5F646D"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user