Repository navigation
feat(translation): panel-only bilingual results, visual line grouping, flag language switcher #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -89,16 +89,20 @@ struct InPlaceResultsPanel: View { | |||||||||||||||||||||||
| .buttonStyle(.plain) | ||||||||||||||||||||||||
| .customTooltip(String(localized: "preview.panel.rerun")) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Button { | ||||||||||||||||||||||||
| copy(currentTabText) | ||||||||||||||||||||||||
| } label: { | ||||||||||||||||||||||||
| Image(systemName: "doc.on.doc") | ||||||||||||||||||||||||
| .font(.system(size: 11)) | ||||||||||||||||||||||||
| .frame(width: 20, height: 20) | ||||||||||||||||||||||||
| if model.activeTab == .translate { | ||||||||||||||||||||||||
| translationCopyButtons | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| Button { | ||||||||||||||||||||||||
| copy(currentTabText) | ||||||||||||||||||||||||
| } label: { | ||||||||||||||||||||||||
| Image(systemName: "doc.on.doc") | ||||||||||||||||||||||||
| .font(.system(size: 11)) | ||||||||||||||||||||||||
| .frame(width: 20, height: 20) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| .buttonStyle(.plain) | ||||||||||||||||||||||||
| .disabled(currentTabText.isEmpty) | ||||||||||||||||||||||||
| .customTooltip(String(localized: "preview.panel.copyAll")) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| .buttonStyle(.plain) | ||||||||||||||||||||||||
| .disabled(currentTabText.isEmpty) | ||||||||||||||||||||||||
| .customTooltip(String(localized: "preview.panel.copyAll")) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Button { | ||||||||||||||||||||||||
| model.onClose?() | ||||||||||||||||||||||||
|
|
@@ -164,34 +168,42 @@ struct InPlaceResultsPanel: View { | |||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /// 国旗横排一键切换目标语言,避免下拉二次点击。 | ||||||||||||||||||||||||
| private var targetLanguagePicker: some View { | ||||||||||||||||||||||||
| HStack(spacing: 6) { | ||||||||||||||||||||||||
| Text(String(localized: "preview.panel.targetLanguage")) | ||||||||||||||||||||||||
| .font(.caption) | ||||||||||||||||||||||||
| .foregroundStyle(.secondary) | ||||||||||||||||||||||||
| Menu { | ||||||||||||||||||||||||
| ForEach(Self.targetLanguages, id: \.self) { language in | ||||||||||||||||||||||||
| Button { | ||||||||||||||||||||||||
| guard AppSettings.shared.translationTargetLanguage != language else { return } | ||||||||||||||||||||||||
| AppSettings.shared.translationTargetLanguage = language | ||||||||||||||||||||||||
| model.onRerun?() | ||||||||||||||||||||||||
| } label: { | ||||||||||||||||||||||||
| if AppSettings.shared.translationTargetLanguage == language { | ||||||||||||||||||||||||
| Label(language.localizedName, systemImage: "checkmark") | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| Text(language.localizedName) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } label: { | ||||||||||||||||||||||||
| Text(AppSettings.shared.translationTargetLanguage?.localizedName ?? "—") | ||||||||||||||||||||||||
| .font(.caption) | ||||||||||||||||||||||||
| HStack(spacing: 4) { | ||||||||||||||||||||||||
| ForEach(Self.targetLanguages, id: \.self) { language in | ||||||||||||||||||||||||
| languageFlagButton(for: language) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| .controlSize(.small) | ||||||||||||||||||||||||
| .fixedSize() | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private func languageFlagButton(for language: TranslationLanguage) -> some View { | ||||||||||||||||||||||||
| let isSelected = AppSettings.shared.translationTargetLanguage == language | ||||||||||||||||||||||||
| return Button { | ||||||||||||||||||||||||
|
Comment on lines
+180
to
+182
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Suggested change
|
||||||||||||||||||||||||
| guard AppSettings.shared.translationTargetLanguage != language else { return } | ||||||||||||||||||||||||
| AppSettings.shared.translationTargetLanguage = language | ||||||||||||||||||||||||
| model.onRerun?() | ||||||||||||||||||||||||
|
Comment on lines
+180
to
+185
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Suggested change
|
||||||||||||||||||||||||
| } label: { | ||||||||||||||||||||||||
| Text(language.flagEmoji) | ||||||||||||||||||||||||
| .font(.system(size: 16)) | ||||||||||||||||||||||||
| .frame(width: 27, height: 22) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| .buttonStyle(.plain) | ||||||||||||||||||||||||
| .background( | ||||||||||||||||||||||||
| isSelected | ||||||||||||||||||||||||
| ? Color.accentColor.opacity(0.28) | ||||||||||||||||||||||||
| : Color.primary.opacity(0.06) | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
| .clipShape(RoundedRectangle(cornerRadius: 5)) | ||||||||||||||||||||||||
| .overlay( | ||||||||||||||||||||||||
| RoundedRectangle(cornerRadius: 5) | ||||||||||||||||||||||||
| .strokeBorder(isSelected ? Color.accentColor : Color.clear, lineWidth: 1) | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
| .customTooltip(language.localizedName) | ||||||||||||||||||||||||
| .accessibilityLabel(Text(language.localizedName)) | ||||||||||||||||||||||||
| .accessibilityAddTraits(isSelected ? [.isSelected] : []) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // MARK: - Helpers | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private var currentTabText: String { | ||||||||||||||||||||||||
|
|
@@ -234,10 +246,52 @@ struct InPlaceResultsPanel: View { | |||||||||||||||||||||||
| NSPasteboard.general.setString(text, forType: .string) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /// 翻译结果三键复制:原文 / 译文 / 全部(原文译文交替,与面板显示一致)。 | ||||||||||||||||||||||||
| private var translationCopyButtons: some View { | ||||||||||||||||||||||||
| HStack(spacing: 3) { | ||||||||||||||||||||||||
| panelCopyButton( | ||||||||||||||||||||||||
| title: String(localized: "preview.panel.copySource"), | ||||||||||||||||||||||||
| text: model.segments.map(\.sourceText).joined(separator: "\n") | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
| panelCopyButton( | ||||||||||||||||||||||||
| title: String(localized: "preview.panel.copyTranslation"), | ||||||||||||||||||||||||
| text: model.segments.map(\.translated).joined(separator: "\n") | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
|
Comment on lines
+252
to
+259
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Suggested change
|
||||||||||||||||||||||||
| panelCopyButton( | ||||||||||||||||||||||||
| title: String(localized: "preview.panel.copyAll"), | ||||||||||||||||||||||||
| text: ResultsTextBuilder.translationText(segments: model.segments).string | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private func panelCopyButton(title: String, text: String) -> some View { | ||||||||||||||||||||||||
| Button { | ||||||||||||||||||||||||
| copy(text) | ||||||||||||||||||||||||
| flashMessage(title) | ||||||||||||||||||||||||
| } label: { | ||||||||||||||||||||||||
| HStack(spacing: 3) { | ||||||||||||||||||||||||
| Image(systemName: "doc.on.doc") | ||||||||||||||||||||||||
| .font(.system(size: 10)) | ||||||||||||||||||||||||
| Text(title) | ||||||||||||||||||||||||
| .font(.caption2) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| .padding(.horizontal, 6) | ||||||||||||||||||||||||
| .frame(height: 20) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| .buttonStyle(.plain) | ||||||||||||||||||||||||
| .background(Color.primary.opacity(0.06), in: RoundedRectangle(cornerRadius: 4)) | ||||||||||||||||||||||||
| .disabled(text.isEmpty) | ||||||||||||||||||||||||
| .customTooltip(title) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /// Shows the copy feedback; a newer flash invalidates older timers, so | ||||||||||||||||||||||||
| /// rapid line clicks don't clear each other's message early. | ||||||||||||||||||||||||
| private func flashCopy(line: String) { | ||||||||||||||||||||||||
| copiedFlash = String(localized: "preview.panel.copyLine") + ": " + line | ||||||||||||||||||||||||
| flashMessage(String(localized: "preview.panel.copyLine") + ": " + line) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private func flashMessage(_ message: String) { | ||||||||||||||||||||||||
| copiedFlash = message | ||||||||||||||||||||||||
| flashGeneration += 1 | ||||||||||||||||||||||||
| let generation = flashGeneration | ||||||||||||||||||||||||
| DispatchQueue.main.asyncAfter(deadline: .now() + 1.2) { | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -57,7 +57,11 @@ struct PreviewResultsPanel: View { | |||||||||||||||||||||||||
| // 无 tab 切换:内容随工具栏最后一次触发的动作而定。 | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| rerunButton | ||||||||||||||||||||||||||
| copyAllButton | ||||||||||||||||||||||||||
| if viewModel.resultsPanelTab == .translate { | ||||||||||||||||||||||||||
| translationCopyButtons | ||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||
| copyAllButton | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| closeButton | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| .padding(.horizontal, 10) | ||||||||||||||||||||||||||
|
|
@@ -187,43 +191,50 @@ struct PreviewResultsPanel: View { | |||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /// Quick target-language switcher; changing it re-runs the translation | ||||||||||||||||||||||||||
| /// 国旗横排一键切换目标语言,避免下拉二次点击;切换后重跑翻译。 | ||||||||||||||||||||||||||
| private var targetLanguagePicker: some View { | ||||||||||||||||||||||||||
| HStack(spacing: 6) { | ||||||||||||||||||||||||||
| Text(String(localized: "preview.panel.targetLanguage")) | ||||||||||||||||||||||||||
| .font(.caption) | ||||||||||||||||||||||||||
| .foregroundStyle(.secondary) | ||||||||||||||||||||||||||
| Menu { | ||||||||||||||||||||||||||
| ForEach(Self.targetLanguages, id: \.self) { language in | ||||||||||||||||||||||||||
| Button { | ||||||||||||||||||||||||||
| guard selectedTargetLanguage != language else { return } | ||||||||||||||||||||||||||
| selectedTargetLanguage = language | ||||||||||||||||||||||||||
| AppSettings.shared.translationTargetLanguage = language | ||||||||||||||||||||||||||
| if viewModel.hasTranslationResults || viewModel.ocrResult != nil { | ||||||||||||||||||||||||||
| viewModel.performTranslation() | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } label: { | ||||||||||||||||||||||||||
| if selectedTargetLanguage == language { | ||||||||||||||||||||||||||
| Label(language.localizedName, systemImage: "checkmark") | ||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||
| Text(language.localizedName) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } label: { | ||||||||||||||||||||||||||
| Text(selectedTargetLanguage?.localizedName ?? "—") | ||||||||||||||||||||||||||
| .font(.caption) | ||||||||||||||||||||||||||
| HStack(spacing: 4) { | ||||||||||||||||||||||||||
| ForEach(Self.targetLanguages, id: \.self) { language in | ||||||||||||||||||||||||||
| languageFlagButton(for: language) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| .controlSize(.small) | ||||||||||||||||||||||||||
| .fixedSize() | ||||||||||||||||||||||||||
| .onAppear { | ||||||||||||||||||||||||||
| if selectedTargetLanguage == nil { | ||||||||||||||||||||||||||
| selectedTargetLanguage = AppSettings.shared.translationTargetLanguage | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| .onAppear { | ||||||||||||||||||||||||||
| if selectedTargetLanguage == nil { | ||||||||||||||||||||||||||
| selectedTargetLanguage = AppSettings.shared.translationTargetLanguage | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| private func languageFlagButton(for language: TranslationLanguage) -> some View { | ||||||||||||||||||||||||||
| let isSelected = selectedTargetLanguage == language | ||||||||||||||||||||||||||
| return Button { | ||||||||||||||||||||||||||
| guard selectedTargetLanguage != language else { return } | ||||||||||||||||||||||||||
| selectedTargetLanguage = language | ||||||||||||||||||||||||||
| AppSettings.shared.translationTargetLanguage = language | ||||||||||||||||||||||||||
| if viewModel.hasTranslationResults || viewModel.ocrResult != nil { | ||||||||||||||||||||||||||
| viewModel.performTranslation() | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } label: { | ||||||||||||||||||||||||||
| Text(language.flagEmoji) | ||||||||||||||||||||||||||
| .font(.system(size: 16)) | ||||||||||||||||||||||||||
| .frame(width: 27, height: 22) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| .buttonStyle(.plain) | ||||||||||||||||||||||||||
| .background( | ||||||||||||||||||||||||||
| isSelected | ||||||||||||||||||||||||||
| ? Color.accentColor.opacity(0.28) | ||||||||||||||||||||||||||
| : Color.primary.opacity(0.06) | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| .clipShape(RoundedRectangle(cornerRadius: 5)) | ||||||||||||||||||||||||||
| .overlay( | ||||||||||||||||||||||||||
| RoundedRectangle(cornerRadius: 5) | ||||||||||||||||||||||||||
| .strokeBorder(isSelected ? Color.accentColor : Color.clear, lineWidth: 1) | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| .customTooltip(language.localizedName) | ||||||||||||||||||||||||||
| .accessibilityLabel(Text(language.localizedName)) | ||||||||||||||||||||||||||
| .accessibilityAddTraits(isSelected ? [.isSelected] : []) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // MARK: - Helpers | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| private var currentTabText: String { | ||||||||||||||||||||||||||
|
|
@@ -249,10 +260,52 @@ struct PreviewResultsPanel: View { | |||||||||||||||||||||||||
| NSPasteboard.general.setString(text, forType: .string) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /// 翻译结果三键复制:原文 / 译文 / 全部(原文译文交替,与面板显示一致)。 | ||||||||||||||||||||||||||
| private var translationCopyButtons: some View { | ||||||||||||||||||||||||||
| HStack(spacing: 3) { | ||||||||||||||||||||||||||
| panelCopyButton( | ||||||||||||||||||||||||||
| title: String(localized: "preview.panel.copySource"), | ||||||||||||||||||||||||||
| text: viewModel.bilingualSegments.map(\.sourceText).joined(separator: "\n") | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| panelCopyButton( | ||||||||||||||||||||||||||
| title: String(localized: "preview.panel.copyTranslation"), | ||||||||||||||||||||||||||
| text: viewModel.bilingualSegments.map(\.translated).joined(separator: "\n") | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
Comment on lines
+266
to
+273
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Suggested change
|
||||||||||||||||||||||||||
| panelCopyButton( | ||||||||||||||||||||||||||
| title: String(localized: "preview.panel.copyAll"), | ||||||||||||||||||||||||||
| text: ResultsTextBuilder.translationText(segments: viewModel.bilingualSegments).string | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| private func panelCopyButton(title: String, text: String) -> some View { | ||||||||||||||||||||||||||
| Button { | ||||||||||||||||||||||||||
| copyToClipboard(text) | ||||||||||||||||||||||||||
| flashMessage(title) | ||||||||||||||||||||||||||
| } label: { | ||||||||||||||||||||||||||
| HStack(spacing: 3) { | ||||||||||||||||||||||||||
| Image(systemName: "doc.on.doc") | ||||||||||||||||||||||||||
| .font(.system(size: 10)) | ||||||||||||||||||||||||||
| Text(title) | ||||||||||||||||||||||||||
| .font(.caption2) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| .padding(.horizontal, 6) | ||||||||||||||||||||||||||
| .frame(height: 20) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| .buttonStyle(.plain) | ||||||||||||||||||||||||||
| .background(Color.primary.opacity(0.06), in: RoundedRectangle(cornerRadius: 4)) | ||||||||||||||||||||||||||
| .disabled(text.isEmpty) | ||||||||||||||||||||||||||
| .customTooltip(title) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /// Shows the copy feedback; a newer flash invalidates older timers, so | ||||||||||||||||||||||||||
| /// rapid line clicks don't clear each other's message early. | ||||||||||||||||||||||||||
| private func flashCopy(line: String) { | ||||||||||||||||||||||||||
| copiedFlash = String(localized: "preview.panel.copyLine") + ": " + line | ||||||||||||||||||||||||||
| flashMessage(String(localized: "preview.panel.copyLine") + ": " + line) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| private func flashMessage(_ message: String) { | ||||||||||||||||||||||||||
| copiedFlash = message | ||||||||||||||||||||||||||
| flashGeneration += 1 | ||||||||||||||||||||||||||
| let generation = flashGeneration | ||||||||||||||||||||||||||
| DispatchQueue.main.asyncAfter(deadline: .now() + 1.2) { | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
8 个国旗按钮固定 27pt 宽,总宽约 240pt(含间距),加上重跑、复制(翻译 tab 下为 3 个文案按钮)、关闭按钮及 flash 提示文本,同一 HStack 在窄面板下可能溢出或被压缩。原下拉菜单仅占一个固定宽度,此处回归风险建议验证最窄面板场景(如考虑 ScrollView(.horizontal) 或减少语言数量)。
Suggestion: