fix(panel): keep the picked language when overriding the target
The header language picker set the effective target and then re-ran prepare, where TranslationDirectionPolicy only knew a Bool override and returned the mother tongue. Any picked language other than the mother tongue was discarded, so the panel appeared not to change. The policy now takes the override target itself, and PanelViewModel stores it, so a picker choice wins over a rule for that capture; a new capture returns to the rule/mother tongue. Move the shared panel test scaffolding into PanelTestSupport and the routing tests into PanelRoutingTests, and add picker and rule-override tests. Verified: make verify green; live checks with a German -> English rule show the picker switching to French, the next capture back to English with the banner, and the banner override to German.
This commit is contained in:
1 parent
bd8e3db82d
commit
9f89000b0d
8 files changed
+382
-316
No files matched your search
@@ -90,7 +90,7 @@ public final class PanelViewModel {
|
||||
public private(set) var isReplacing = false
|
||||
/// The current target, model and prompt settings. `target` is the user's saved preference.
|
||||
public private(set) var configuration: PanelConfiguration
|
||||
/// The target used by the current request, after an optional reverse-direction switch.
|
||||
/// The target used by the current request, after a language rule or an explicit picker choice.
|
||||
public private(set) var effectiveTarget: ModelLanguage
|
||||
/// The matched rule source when `effectiveTarget` differs from the mother tongue, or nil.
|
||||
public private(set) var routedSource: ModelLanguage?
|
||||
@@ -113,7 +113,7 @@ public final class PanelViewModel {
|
||||
private var eventTask: Task<Void, Never>?
|
||||
private var currentEpoch: UInt64 = 0
|
||||
private var lastCaptured: CapturedText?
|
||||
private var directionOverride = false
|
||||
private var targetOverride: ModelLanguage?
|
||||
|
||||
/// Creates a panel model.
|
||||
public init(
|
||||
@@ -206,7 +206,7 @@ public final class PanelViewModel {
|
||||
didCopy = false
|
||||
isReplacing = false
|
||||
if resetDirectionOverride {
|
||||
directionOverride = false
|
||||
targetOverride = nil
|
||||
}
|
||||
let normalized = TextNormalization.normalizeInput(captured.text)
|
||||
let detection = detector.detect(normalized)
|
||||
@@ -215,11 +215,11 @@ public final class PanelViewModel {
|
||||
configuredTarget: configuration.target,
|
||||
routes: configuration.languageRoutes,
|
||||
detection: detection,
|
||||
override: directionOverride
|
||||
override: targetOverride
|
||||
)
|
||||
effectiveTarget = direction.target
|
||||
routedSource = direction.didRoute ? direction.source : nil
|
||||
sameLanguageDismissed = directionOverride
|
||||
sameLanguageDismissed = targetOverride != nil
|
||||
phase = .translating
|
||||
}
|
||||
|
||||
@@ -258,7 +258,7 @@ public final class PanelViewModel {
|
||||
sourceLanguage = nil
|
||||
effectiveTarget = configuration.target
|
||||
routedSource = nil
|
||||
directionOverride = false
|
||||
targetOverride = nil
|
||||
}
|
||||
|
||||
/// Changes the saved target from the panel and retranslates the current selection.
|
||||
@@ -267,7 +267,7 @@ public final class PanelViewModel {
|
||||
configuration.target = language
|
||||
effectiveTarget = language
|
||||
routedSource = nil
|
||||
directionOverride = true
|
||||
targetOverride = language
|
||||
onTargetChanged(language)
|
||||
if let lastCaptured {
|
||||
await begin(lastCaptured, resetDirectionOverride: false)
|
||||
@@ -279,7 +279,7 @@ public final class PanelViewModel {
|
||||
guard language.code != effectiveTarget.code else { return }
|
||||
effectiveTarget = language
|
||||
routedSource = nil
|
||||
directionOverride = true
|
||||
targetOverride = language
|
||||
sameLanguageDismissed = true
|
||||
if let lastCaptured {
|
||||
await begin(lastCaptured, resetDirectionOverride: false)
|
||||
@@ -322,7 +322,7 @@ public final class PanelViewModel {
|
||||
if targetChanged || source == nil {
|
||||
effectiveTarget = configuration.target
|
||||
routedSource = nil
|
||||
directionOverride = false
|
||||
targetOverride = nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -341,7 +341,7 @@ public final class PanelViewModel {
|
||||
sameLanguageDismissed = false
|
||||
effectiveTarget = configuration.target
|
||||
routedSource = nil
|
||||
directionOverride = false
|
||||
targetOverride = nil
|
||||
lastCaptured = nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import SprachmittlerText
|
||||
|
||||
/// The effective target for one translation, after an optional reverse-direction switch.
|
||||
/// The effective target for one translation, after a language rule or an explicit override.
|
||||
public struct TranslationDirection: Sendable, Equatable {
|
||||
/// The language the request will translate into.
|
||||
public let target: ModelLanguage
|
||||
@@ -26,14 +26,17 @@ public enum TranslationDirectionPolicy {
|
||||
/// - configuredTarget: The user's mother tongue, used when no rule matches.
|
||||
/// - routes: The source-to-target rules.
|
||||
/// - detection: The advisory source-language detection.
|
||||
/// - override: True after an explicit target choice for the current capture.
|
||||
/// - override: An explicit target choice for the current capture, or nil to use the routes.
|
||||
public static func resolve(
|
||||
configuredTarget: ModelLanguage,
|
||||
routes: [LanguageRoute],
|
||||
detection: LanguageDetection?,
|
||||
override: Bool
|
||||
override: ModelLanguage?
|
||||
) -> TranslationDirection {
|
||||
guard !override, let detection else {
|
||||
if let override {
|
||||
return TranslationDirection(target: override, didRoute: false, source: nil)
|
||||
}
|
||||
guard let detection else {
|
||||
return TranslationDirection(target: configuredTarget, didRoute: false, source: nil)
|
||||
}
|
||||
for route in routes {
|
||||
|
||||
Reference in new issue
Block a user