Skip to content

Commit c5705a1

Browse files
committed
treat languages of enabled input methods as current
1 parent a706257 commit c5705a1

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/config/inputmethod.swift

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,21 @@ struct InputMethod: Codable, Hashable {
389389
}
390390
}
391391

392-
private func languageCodeMatch(_ code: String) -> Bool {
392+
private func normalizeLanguageCode(_ code: String) -> String {
393+
// "".split throws
394+
if code.isEmpty {
395+
return ""
396+
}
397+
return String(code.split(separator: "_")[0])
398+
}
399+
400+
// Match both system language and languages of enabled input methods.
401+
private func languageCodeMatch(_ code: String, _ languagesOfEnabledIMs: Set<String>) -> Bool {
393402
guard let languageCode = Locale.current.language.languageCode?.identifier else {
394403
return true
395404
}
396-
// "".split throws
397-
return !code.isEmpty && String(code.split(separator: "_")[0]) == languageCode
405+
let normalized = normalizeLanguageCode(code)
406+
return normalized == languageCode || languagesOfEnabledIMs.contains(normalized)
398407
}
399408

400409
struct AvailableInputMethodView: View {
@@ -470,7 +479,8 @@ struct AvailableInputMethodView: View {
470479
updateList()
471480
}
472481
}
473-
@Published var availableIMsForLanguage: [InputMethod] = []
482+
@Published var availableIMsForLanguage = [InputMethod]()
483+
var languagesOfEnabledIMs = Set<String>()
474484

475485
var errorMsg: String? {
476486
didSet {
@@ -490,6 +500,7 @@ struct AvailableInputMethodView: View {
490500

491501
func refresh(_ alreadyEnabled: Set<String>) {
492502
availableIMs.removeAll()
503+
languagesOfEnabledIMs.removeAll()
493504
let jsonStr = String(Fcitx.imGetAvailableIMs())
494505
if let jsonData = jsonStr.data(using: .utf8) {
495506
do {
@@ -501,6 +512,9 @@ struct AvailableInputMethodView: View {
501512
} else {
502513
availableIMs[im.languageCode] = [im]
503514
}
515+
if alreadyEnabled.contains(im.uniqueName) {
516+
languagesOfEnabledIMs.update(with: normalizeLanguageCode(im.languageCode))
517+
}
504518
}
505519
} catch {
506520
errorMsg =
@@ -541,7 +555,9 @@ struct AvailableInputMethodView: View {
541555

542556
fileprivate func languages() -> [LocalizedLanguageCode] {
543557
return Array(availableIMs.keys)
544-
.filter { !(addIMOnlyShowCurrentLanguage ?? false) || languageCodeMatch($0) }
558+
.filter {
559+
!(addIMOnlyShowCurrentLanguage ?? false) || languageCodeMatch($0, languagesOfEnabledIMs)
560+
}
545561
.map { LocalizedLanguageCode(code: $0) }
546562
.sorted()
547563
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy