diff --git a/NotchBuddy/Sources/App/IslandRootView.swift b/NotchBuddy/Sources/App/IslandRootView.swift index e4e2c27..dcc9e29 100644 --- a/NotchBuddy/Sources/App/IslandRootView.swift +++ b/NotchBuddy/Sources/App/IslandRootView.swift @@ -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 diff --git a/NotchBuddy/Sources/App/Translator.swift b/NotchBuddy/Sources/App/Translator.swift index 035f378..e0d464d 100644 --- a/NotchBuddy/Sources/App/Translator.swift +++ b/NotchBuddy/Sources/App/Translator.swift @@ -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")) }