Skip to content

Commit 9a27dae

Browse files
committed
pin and bold popular input methods; pin en
1 parent 5f20850 commit 9a27dae

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

src/config/inputmethod.swift

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import Fcitx
33
import Logging
44
import SwiftUI
55

6+
let en = "en"
7+
let popularIMs = ["keyboard-us", "pinyin", "shuangpin", "wbx", "rime", "mozc", "hallelujah"]
8+
69
class InputMethodConfigController: ConfigWindowController {
710
let view = InputMethodConfigView()
811
convenience init() {
@@ -396,11 +399,14 @@ private func normalizeLanguageCode(_ code: String) -> String {
396399
return String(code.split(separator: "_")[0])
397400
}
398401

399-
// Match both system language and languages of enabled input methods.
402+
// Match English, system language (or language assigned to Fcitx5) and languages of enabled input methods.
400403
private func languageCodeMatch(_ code: String, _ languagesOfEnabledIMs: Set<String>) -> Bool {
401404
guard let languageCode = Locale.current.language.languageCode?.identifier else {
402405
return true
403406
}
407+
if code == en {
408+
return true
409+
}
404410
let normalized = normalizeLanguageCode(code)
405411
return normalized == languageCode || languagesOfEnabledIMs.contains(normalized)
406412
}
@@ -431,7 +437,7 @@ struct AvailableInputMethodView: View {
431437
if viewModel.selectedLanguageCode != nil {
432438
List(selection: $selection) {
433439
ForEach(viewModel.availableIMsForLanguage, id: \.self) { im in
434-
Text(im.displayName)
440+
Text(im.displayName).fontWeight(popularIMs.contains(im.uniqueName) ? .bold : .regular)
435441
}
436442
}.contextMenu(forSelectionType: InputMethod.self) { items in
437443
} primaryAction: { items in
@@ -488,13 +494,28 @@ struct AvailableInputMethodView: View {
488494
}
489495

490496
private func updateList() {
491-
if let selectedLanguageCode = selectedLanguageCode {
492-
if let ims = availableIMs[selectedLanguageCode] {
493-
availableIMsForLanguage = ims.filter { !alreadyEnabled.contains($0.uniqueName) }
494-
return
497+
guard let selectedLanguageCode = selectedLanguageCode,
498+
let ims = availableIMs[selectedLanguageCode]
499+
else {
500+
availableIMsForLanguage = []
501+
return
502+
}
503+
availableIMsForLanguage = ims.filter { !alreadyEnabled.contains($0.uniqueName) }.sorted {
504+
a, b in
505+
// Pin popular input methods.
506+
let ia = popularIMs.firstIndex(of: a.uniqueName)
507+
let ib = popularIMs.firstIndex(of: b.uniqueName)
508+
if ia == nil && ib != nil {
509+
return false
510+
}
511+
if ia != nil && ib == nil {
512+
return true
495513
}
514+
if let ia = ia, let ib = ib {
515+
return ia < ib
516+
}
517+
return a.displayName.localizedCompare(b.displayName) == .orderedAscending
496518
}
497-
availableIMsForLanguage = []
498519
}
499520

500521
func refresh(_ alreadyEnabled: Set<String>) {
@@ -543,14 +564,24 @@ struct AvailableInputMethodView: View {
543564
}
544565

545566
public static func < (lhs: Self, rhs: Self) -> Bool {
567+
// Pin English.
568+
if lhs.code == en {
569+
return true
570+
}
571+
if rhs.code == en {
572+
return false
573+
}
574+
// Pin system language (or language assigned to Fcitx5).
546575
let curIdent = Locale.current.identifier.prefix(2)
547-
if lhs.code.prefix(2) == curIdent {
576+
let le = lhs.code.prefix(2) == curIdent
577+
let re = rhs.code.prefix(2) == curIdent
578+
if le && !re {
548579
return true
549-
} else if rhs.code.prefix(2) == curIdent {
580+
}
581+
if !le && re {
550582
return false
551-
} else {
552-
return lhs.localized.localizedCompare(rhs.localized) == .orderedAscending
553583
}
584+
return lhs.localized.localizedCompare(rhs.localized) == .orderedAscending
554585
}
555586
}
556587

0 commit comments

Comments
 (0)