254 lines
12 KiB
Swift
254 lines
12 KiB
Swift
import AppKit
|
|
import ConjureKit
|
|
import Security
|
|
|
|
@MainActor
|
|
final class TranslationController: NSViewController, NSTextFieldDelegate, NSTextViewDelegate {
|
|
private let keyField = NSSecureTextField()
|
|
private let provider = NSPopUpButton()
|
|
private let credentials = NSStackView()
|
|
private var usesMyMemory: Bool { provider.indexOfSelectedItem == 0 }
|
|
private let output = NSTextView()
|
|
private let disclosure = NSTextField(wrappingLabelWithString: "")
|
|
private let status = NSTextField(wrappingLabelWithString: "")
|
|
private let translateButton = NSButton(title: "Translate", target: nil, action: nil)
|
|
private var task: Task<Void, Never>?
|
|
var input: (() -> String)?
|
|
var closed: (() -> Void)?
|
|
private static let credential: [String: Any] = [
|
|
kSecClass as String: kSecClassGenericPassword,
|
|
kSecAttrService as String: "Conjure.OpenAI",
|
|
kSecAttrAccount as String: "translation",
|
|
]
|
|
|
|
override func loadView() {
|
|
view = NSView()
|
|
let title = NSTextField(labelWithString: "Conjure Translate · English → Japanese")
|
|
title.font = .systemFont(ofSize: 13, weight: .semibold)
|
|
let back = NSButton(title: "", target: self, action: #selector(goBack))
|
|
back.image = IconStyle.symbol("chevron.left", for: title.font!)
|
|
back.imageScaling = .scaleProportionallyDown
|
|
back.isBordered = false
|
|
back.contentTintColor = SunsetPalette.secondaryText
|
|
back.setAccessibilityLabel("Back to applications")
|
|
back.toolTip = "Back to applications (Escape)"
|
|
let heading = NSStackView(views: [back, title, NSView()])
|
|
heading.alignment = .centerY
|
|
heading.spacing = 12
|
|
provider.addItems(withTitles: ["MyMemory (no key)", "OpenAI · gpt-6-luna"])
|
|
provider.selectItem(at: UserDefaults.standard.string(forKey: "translationProvider") == "openai" ? 1 : 0)
|
|
provider.setAccessibilityLabel("Translation provider")
|
|
provider.target = self
|
|
provider.action = #selector(providerChanged)
|
|
keyField.placeholderString = "OpenAI API key (saved in Keychain)"
|
|
keyField.setAccessibilityLabel("OpenAI API key")
|
|
keyField.delegate = self
|
|
keyField.widthAnchor.constraint(greaterThanOrEqualToConstant: 220).isActive = true
|
|
let save = NSButton(title: "Save Key", target: self, action: #selector(saveKey))
|
|
let forget = NSButton(title: "Forget Key", target: self, action: #selector(forgetKey))
|
|
for control in [keyField, save, forget] { credentials.addArrangedSubview(control) }
|
|
credentials.distribution = .fill
|
|
translateButton.target = self
|
|
translateButton.action = #selector(translate)
|
|
translateButton.keyEquivalent = "\r"
|
|
translateButton.isBordered = false
|
|
translateButton.font = .systemFont(ofSize: 12, weight: .medium)
|
|
translateButton.contentTintColor = SunsetPalette.text
|
|
translateButton.toolTip = "Translate the English input (Return). Escape returns to the launcher."
|
|
let returnKey = ShortcutIndicator("↵")
|
|
let actionPill = NSView()
|
|
actionPill.wantsLayer = true
|
|
actionPill.layer?.cornerRadius = 18
|
|
actionPill.layer?.borderWidth = 0.5
|
|
actionPill.layer?.borderColor = SunsetPalette.accent.withAlphaComponent(0.35).cgColor
|
|
actionPill.layer?.backgroundColor = SunsetPalette.background.withAlphaComponent(0.25).cgColor
|
|
for control in [translateButton, returnKey] {
|
|
control.translatesAutoresizingMaskIntoConstraints = false
|
|
actionPill.addSubview(control)
|
|
}
|
|
NSLayoutConstraint.activate([
|
|
actionPill.heightAnchor.constraint(equalToConstant: 36),
|
|
translateButton.leadingAnchor.constraint(equalTo: actionPill.leadingAnchor, constant: 14),
|
|
translateButton.centerYAnchor.constraint(equalTo: actionPill.centerYAnchor),
|
|
returnKey.leadingAnchor.constraint(equalTo: translateButton.trailingAnchor, constant: 8),
|
|
returnKey.centerYAnchor.constraint(equalTo: actionPill.centerYAnchor),
|
|
returnKey.trailingAnchor.constraint(equalTo: actionPill.trailingAnchor, constant: -12),
|
|
])
|
|
let footer = NSStackView(views: [NSView(), actionPill])
|
|
footer.orientation = .horizontal
|
|
footer.translatesAutoresizingMaskIntoConstraints = false
|
|
view.addSubview(footer)
|
|
output.isEditable = false
|
|
output.isSelectable = true
|
|
output.drawsBackground = false
|
|
output.textColor = SunsetPalette.text
|
|
output.font = .systemFont(ofSize: 20)
|
|
output.setAccessibilityLabel("Japanese translation")
|
|
output.delegate = self
|
|
let scroll = NSScrollView()
|
|
scroll.documentView = output
|
|
scroll.hasVerticalScroller = true
|
|
scroll.drawsBackground = false
|
|
output.isVerticallyResizable = true
|
|
output.autoresizingMask = [.width]
|
|
output.textContainer?.widthTracksTextView = true
|
|
disclosure.textColor = SunsetPalette.secondaryText
|
|
disclosure.font = .systemFont(ofSize: 12)
|
|
disclosure.setAccessibilityLabel("Translation provider, privacy, and limits")
|
|
status.font = .systemFont(ofSize: 12)
|
|
status.setAccessibilityLabel("Translation status and errors")
|
|
let stack = NSStackView(views: [heading, provider, disclosure, credentials, scroll, status])
|
|
stack.orientation = .vertical
|
|
stack.alignment = .leading
|
|
stack.spacing = 12
|
|
stack.setCustomSpacing(4, after: provider)
|
|
stack.translatesAutoresizingMaskIntoConstraints = false
|
|
view.addSubview(stack)
|
|
NSLayoutConstraint.activate([
|
|
back.widthAnchor.constraint(equalToConstant: 22),
|
|
back.heightAnchor.constraint(equalToConstant: 22),
|
|
stack.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
|
|
stack.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
|
|
stack.topAnchor.constraint(equalTo: view.topAnchor),
|
|
stack.bottomAnchor.constraint(equalTo: footer.topAnchor, constant: -12),
|
|
footer.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
|
|
footer.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8),
|
|
footer.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -8),
|
|
footer.heightAnchor.constraint(equalToConstant: 36),
|
|
credentials.widthAnchor.constraint(equalTo: stack.widthAnchor),
|
|
disclosure.widthAnchor.constraint(equalTo: stack.widthAnchor),
|
|
scroll.widthAnchor.constraint(equalTo: stack.widthAnchor),
|
|
scroll.heightAnchor.constraint(greaterThanOrEqualToConstant: 100),
|
|
status.widthAnchor.constraint(equalTo: stack.widthAnchor),
|
|
])
|
|
updateProvider()
|
|
}
|
|
|
|
override func cancelOperation(_ sender: Any?) { goBack() }
|
|
|
|
func control(_ control: NSControl, textView: NSTextView, doCommandBy selector: Selector) -> Bool {
|
|
guard selector == #selector(NSResponder.cancelOperation(_:)) else { return false }
|
|
goBack()
|
|
return true
|
|
}
|
|
|
|
func textView(_ textView: NSTextView, doCommandBy selector: Selector) -> Bool {
|
|
guard selector == #selector(NSResponder.cancelOperation(_:)) else { return false }
|
|
goBack()
|
|
return true
|
|
}
|
|
|
|
func controlTextDidChange(_ notification: Notification) { inputChanged() }
|
|
|
|
@objc func goBack() {
|
|
task?.cancel()
|
|
task = nil
|
|
closed?()
|
|
}
|
|
|
|
func inputChanged() {
|
|
task?.cancel()
|
|
task = nil
|
|
translateButton.isEnabled = true
|
|
output.string = ""
|
|
setStatus("Press Return to translate.")
|
|
}
|
|
|
|
@objc private func providerChanged() {
|
|
UserDefaults.standard.set(usesMyMemory ? "mymemory" : "openai", forKey: "translationProvider")
|
|
keyField.stringValue = ""
|
|
updateProvider()
|
|
}
|
|
|
|
private func updateProvider() {
|
|
credentials.isHidden = usesMyMemory
|
|
disclosure.stringValue = usesMyMemory
|
|
? "MyMemory · No key · 500 bytes/request · 5,000 characters/day. Text is sent to MyMemory; avoid sensitive content."
|
|
: "OpenAI · gpt-6-luna · Text is sent to OpenAI only when you translate."
|
|
inputChanged()
|
|
}
|
|
|
|
@objc private func saveKey() {
|
|
let key = keyField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
guard !key.isEmpty, !key.contains(where: { $0.isNewline }) else {
|
|
setStatus("Enter a valid API key before saving.", isError: true)
|
|
return
|
|
}
|
|
let data = Data(key.utf8)
|
|
var result = SecItemUpdate(Self.credential as CFDictionary, [kSecValueData as String: data] as CFDictionary)
|
|
if result == errSecItemNotFound {
|
|
var item = Self.credential
|
|
item[kSecValueData as String] = data
|
|
item[kSecAttrAccessible as String] = kSecAttrAccessibleWhenUnlockedThisDeviceOnly
|
|
result = SecItemAdd(item as CFDictionary, nil)
|
|
}
|
|
if result == errSecSuccess {
|
|
keyField.stringValue = ""
|
|
setStatus("API key saved in Keychain. Press Return to translate.")
|
|
} else { showKeychainError(result) }
|
|
}
|
|
|
|
@objc private func forgetKey() {
|
|
task?.cancel()
|
|
task = nil
|
|
translateButton.isEnabled = true
|
|
let result = SecItemDelete(Self.credential as CFDictionary)
|
|
if result == errSecSuccess || result == errSecItemNotFound {
|
|
keyField.stringValue = ""
|
|
setStatus("API key removed.")
|
|
} else { showKeychainError(result) }
|
|
}
|
|
|
|
private func showKeychainError(_ code: OSStatus) {
|
|
setStatus("Keychain error: \(SecCopyErrorMessageString(code, nil) as String? ?? String(code))", isError: true)
|
|
}
|
|
|
|
private func setStatus(_ message: String, isError: Bool = false) {
|
|
status.stringValue = message
|
|
status.textColor = isError ? .systemRed : SunsetPalette.secondaryText
|
|
}
|
|
|
|
@objc func translate() {
|
|
guard task == nil else { return }
|
|
let text = input?() ?? ""
|
|
guard !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
|
|
setStatus("Enter English text in the search bar.", isError: true)
|
|
return
|
|
}
|
|
let myMemory = usesMyMemory
|
|
var key = ""
|
|
if !myMemory {
|
|
if !keyField.stringValue.isEmpty { saveKey(); guard keyField.stringValue.isEmpty else { return } }
|
|
var query = Self.credential
|
|
query[kSecReturnData as String] = true
|
|
var item: CFTypeRef?
|
|
let result = SecItemCopyMatching(query as CFDictionary, &item)
|
|
guard result == errSecSuccess, let data = item as? Data, let savedKey = String(data: data, encoding: .utf8) else {
|
|
if result == errSecItemNotFound { setStatus("Enter and save your OpenAI API key first.", isError: true) }
|
|
else { showKeychainError(result) }
|
|
return
|
|
}
|
|
key = savedKey
|
|
}
|
|
output.string = ""
|
|
setStatus("Translating…")
|
|
translateButton.isEnabled = false
|
|
task = Task { [weak self] in
|
|
do {
|
|
let translation = try await myMemory
|
|
? MyMemoryAPI.translate(text)
|
|
: TranslationAPI.translate(text, apiKey: key)
|
|
guard !Task.isCancelled, let self else { return }
|
|
self.output.string = translation
|
|
self.setStatus("Japanese · Select text to copy (⌘C).")
|
|
} catch {
|
|
guard !Task.isCancelled, let self else { return }
|
|
self.setStatus("Translation failed: \(error.localizedDescription)", isError: true)
|
|
}
|
|
guard let self else { return }
|
|
self.task = nil
|
|
self.translateButton.isEnabled = true
|
|
}
|
|
}
|
|
}
|