PrefUI // Performance optimization for the keyboard page.

This commit is contained in:
ShikiSuen 2022-12-06 17:07:39 +08:00
parent 9973e74a19
commit f4787c29da
1 changed files with 136 additions and 144 deletions

View File

@ -14,9 +14,6 @@ import SwiftUI
@available(macOS 10.15, *) @available(macOS 10.15, *)
struct VwrPrefPaneKeyboard: View { struct VwrPrefPaneKeyboard: View {
@State private var selSelectionKeysList = CandidateKey.suggestions
@State private var selSelectionKeys =
UserDefaults.standard.string(forKey: UserDef.kCandidateKeys.rawValue) ?? CandidateKey.defaultKeys
@State private var selKeyboardParser = UserDefaults.standard.integer(forKey: UserDef.kKeyboardParser.rawValue) @State private var selKeyboardParser = UserDefaults.standard.integer(forKey: UserDef.kKeyboardParser.rawValue)
@State private var selBasicKeyboardLayout: String = @State private var selBasicKeyboardLayout: String =
UserDefaults.standard.string(forKey: UserDef.kBasicKeyboardLayout.rawValue) ?? PrefMgr.shared.basicKeyboardLayout UserDefaults.standard.string(forKey: UserDef.kBasicKeyboardLayout.rawValue) ?? PrefMgr.shared.basicKeyboardLayout
@ -24,19 +21,6 @@ struct VwrPrefPaneKeyboard: View {
UserDefaults.standard.string(forKey: UserDef.kAlphanumericalKeyboardLayout.rawValue) UserDefaults.standard.string(forKey: UserDef.kAlphanumericalKeyboardLayout.rawValue)
?? PrefMgr.shared.alphanumericalKeyboardLayout ?? PrefMgr.shared.alphanumericalKeyboardLayout
@State private var selUsingHotKeySCPC = UserDefaults.standard.bool(forKey: UserDef.kUsingHotKeySCPC.rawValue)
@State private var selUsingHotKeyAssociates = UserDefaults.standard.bool(
forKey: UserDef.kUsingHotKeyAssociates.rawValue)
@State private var selUsingHotKeyCNS = UserDefaults.standard.bool(forKey: UserDef.kUsingHotKeyCNS.rawValue)
@State private var selUsingHotKeyKangXi = UserDefaults.standard.bool(forKey: UserDef.kUsingHotKeyKangXi.rawValue)
@State private var selUsingHotKeyJIS = UserDefaults.standard.bool(forKey: UserDef.kUsingHotKeyJIS.rawValue)
@State private var selUsingHotKeyHalfWidthASCII = UserDefaults.standard.bool(
forKey: UserDef.kUsingHotKeyHalfWidthASCII.rawValue)
@State private var selUsingHotKeyCurrencyNumerals = UserDefaults.standard.bool(
forKey: UserDef.kUsingHotKeyCurrencyNumerals.rawValue)
@State private var selUsingHotKeyCassette = UserDefaults.standard.bool(
forKey: UserDef.kUsingHotKeyCassette.rawValue)
private let contentMaxHeight: Double = 440 private let contentMaxHeight: Double = 440
private let contentWidth: Double = { private let contentWidth: Double = {
switch PrefMgr.shared.appleLanguages[0] { switch PrefMgr.shared.appleLanguages[0] {
@ -55,47 +39,7 @@ struct VwrPrefPaneKeyboard: View {
ScrollView { ScrollView {
SSPreferences.Container(contentWidth: contentWidth) { SSPreferences.Container(contentWidth: contentWidth) {
SSPreferences.Section(label: { Text(LocalizedStringKey("Selection Keys:")) }) { SSPreferences.Section(label: { Text(LocalizedStringKey("Selection Keys:")) }) {
ComboBox( VwrPrefPaneKeyboard_SelectionKeys()
items: CandidateKey.suggestions,
text: $selSelectionKeys.onChange {
let value = selSelectionKeys
let keys: String = value.trimmingCharacters(in: .whitespacesAndNewlines).deduplicated
if keys.isEmpty {
selSelectionKeys = PrefMgr.shared.candidateKeys
return
}
// Start Error Handling.
if let errorResult = CandidateKey.validate(keys: keys) {
if let window = CtlPrefUI.shared.controller.window {
let alert = NSAlert(error: NSLocalizedString("Invalid Selection Keys.", comment: ""))
alert.informativeText = errorResult
alert.beginSheetModal(for: window) { _ in
selSelectionKeys = PrefMgr.shared.candidateKeys
}
IMEApp.buzz()
}
} else {
PrefMgr.shared.candidateKeys = keys
selSelectionKeys = PrefMgr.shared.candidateKeys
return
}
}
).frame(width: 180).disabled(PrefMgr.shared.useIMKCandidateWindow)
if PrefMgr.shared.useIMKCandidateWindow {
Text(
LocalizedStringKey(
"⚠︎ This feature in IMK Candidate Window defects. Please consult Apple Developer Relations\nand tell them the related Radar ID: #FB11300759."
)
)
.preferenceDescription()
} else {
Text(
LocalizedStringKey(
"Choose or hit Enter to confim your prefered keys for selecting candidates."
)
)
.preferenceDescription()
}
} }
SSPreferences.Section(label: { Text(LocalizedStringKey("Quick Setup:")) }) { SSPreferences.Section(label: { Text(LocalizedStringKey("Quick Setup:")) }) {
HStack { HStack {
@ -134,41 +78,17 @@ struct VwrPrefPaneKeyboard: View {
PrefMgr.shared.keyboardParser = value PrefMgr.shared.keyboardParser = value
} }
) { ) {
Group { ForEach(KeyboardParser.allCases, id: \.self) { item in
Text(LocalizedStringKey("Dachen (Microsoft Standard / Wang / 01, etc.)")).tag(0) if [7, 10].contains(item.rawValue) { Divider() }
Text(LocalizedStringKey("Eten Traditional")).tag(1) Text(item.localizedMenuName).tag(item.rawValue)
Text(LocalizedStringKey("IBM")).tag(4) }.id(UUID())
Text(LocalizedStringKey("MiTAC")).tag(5)
Text(LocalizedStringKey("Seigyou")).tag(8)
Text(LocalizedStringKey("Fake Seigyou")).tag(6)
}
Divider()
Group {
Text(LocalizedStringKey("Dachen 26 (libChewing)")).tag(7)
Text(LocalizedStringKey("Eten 26")).tag(3)
Text(LocalizedStringKey("Hsu")).tag(2)
Text(LocalizedStringKey("Starlight")).tag(9)
}
Divider()
Group {
Text(LocalizedStringKey("Hanyu Pinyin with Numeral Intonation")).tag(10)
Text(LocalizedStringKey("Secondary Pinyin with Numeral Intonation")).tag(11)
Text(LocalizedStringKey("Yale Pinyin with Numeral Intonation")).tag(12)
Text(LocalizedStringKey("Hualuo Pinyin with Numeral Intonation")).tag(13)
Text(LocalizedStringKey("Universal Pinyin with Numeral Intonation")).tag(14)
}
} }
.fixedSize() .fixedSize()
.labelsHidden() .labelsHidden()
Spacer() Spacer()
} }
.frame(width: 380.0) .frame(width: 380.0)
Text( Text(NSLocalizedString("Choose the phonetic layout for Mandarin parser.", comment: ""))
NSLocalizedString(
"Choose the phonetic layout for Mandarin parser.",
comment: ""
)
)
.preferenceDescription() .preferenceDescription()
} }
SSPreferences.Section(label: { Text(LocalizedStringKey("Basic Keyboard Layout:")) }) { SSPreferences.Section(label: { Text(LocalizedStringKey("Basic Keyboard Layout:")) }) {
@ -229,6 +149,84 @@ struct VwrPrefPaneKeyboard: View {
} }
} }
SSPreferences.Section(label: { Text(LocalizedStringKey("Keyboard Shortcuts:")) }) { SSPreferences.Section(label: { Text(LocalizedStringKey("Keyboard Shortcuts:")) }) {
VwrPrefPaneKeyboard_KeyboardShortcuts()
}
}
}
.frame(maxHeight: contentMaxHeight).fixedSize(horizontal: false, vertical: true)
.background(VisualEffectView(material: .sidebar, blendingMode: .behindWindow))
}
}
@available(macOS 10.15, *)
private struct VwrPrefPaneKeyboard_SelectionKeys: View {
@State private var selSelectionKeysList = CandidateKey.suggestions
@State private var selSelectionKeys =
UserDefaults.standard.string(forKey: UserDef.kCandidateKeys.rawValue) ?? CandidateKey.defaultKeys
var body: some View {
ComboBox(
items: CandidateKey.suggestions,
text: $selSelectionKeys.onChange {
let value = selSelectionKeys
let keys: String = value.trimmingCharacters(in: .whitespacesAndNewlines).deduplicated
if keys.isEmpty {
selSelectionKeys = PrefMgr.shared.candidateKeys
return
}
// Start Error Handling.
if let errorResult = CandidateKey.validate(keys: keys) {
IMEApp.buzz()
if let window = CtlPrefUI.shared.controller.window {
let alert = NSAlert(error: NSLocalizedString("Invalid Selection Keys.", comment: ""))
alert.informativeText = errorResult
alert.beginSheetModal(for: window) { _ in
selSelectionKeys = PrefMgr.shared.candidateKeys
}
} else {
selSelectionKeys = PrefMgr.shared.candidateKeys
}
} else {
PrefMgr.shared.candidateKeys = keys
selSelectionKeys = PrefMgr.shared.candidateKeys
return
}
}
).frame(width: 180).disabled(PrefMgr.shared.useIMKCandidateWindow)
if PrefMgr.shared.useIMKCandidateWindow {
Text(
LocalizedStringKey(
"⚠︎ This feature in IMK Candidate Window defects. Please consult Apple Developer Relations\nand tell them the related Radar ID: #FB11300759."
)
)
.preferenceDescription()
} else {
Text(
LocalizedStringKey(
"Choose or hit Enter to confim your prefered keys for selecting candidates."
)
)
.preferenceDescription()
}
}
}
@available(macOS 10.15, *)
private struct VwrPrefPaneKeyboard_KeyboardShortcuts: View {
@State private var selUsingHotKeySCPC = UserDefaults.standard.bool(forKey: UserDef.kUsingHotKeySCPC.rawValue)
@State private var selUsingHotKeyAssociates = UserDefaults.standard.bool(
forKey: UserDef.kUsingHotKeyAssociates.rawValue)
@State private var selUsingHotKeyCNS = UserDefaults.standard.bool(forKey: UserDef.kUsingHotKeyCNS.rawValue)
@State private var selUsingHotKeyKangXi = UserDefaults.standard.bool(forKey: UserDef.kUsingHotKeyKangXi.rawValue)
@State private var selUsingHotKeyJIS = UserDefaults.standard.bool(forKey: UserDef.kUsingHotKeyJIS.rawValue)
@State private var selUsingHotKeyHalfWidthASCII = UserDefaults.standard.bool(
forKey: UserDef.kUsingHotKeyHalfWidthASCII.rawValue)
@State private var selUsingHotKeyCurrencyNumerals = UserDefaults.standard.bool(
forKey: UserDef.kUsingHotKeyCurrencyNumerals.rawValue)
@State private var selUsingHotKeyCassette = UserDefaults.standard.bool(
forKey: UserDef.kUsingHotKeyCassette.rawValue)
var body: some View {
HStack(alignment: .top, spacing: NSFont.systemFontSize) { HStack(alignment: .top, spacing: NSFont.systemFontSize) {
VStack(alignment: .leading) { VStack(alignment: .leading) {
Toggle( Toggle(
@ -285,11 +283,6 @@ struct VwrPrefPaneKeyboard: View {
} }
} }
} }
}
.frame(maxHeight: contentMaxHeight).fixedSize(horizontal: false, vertical: true)
.background(VisualEffectView(material: .sidebar, blendingMode: .behindWindow))
}
}
@available(macOS 11.0, *) @available(macOS 11.0, *)
struct VwrPrefPaneKeyboard_Previews: PreviewProvider { struct VwrPrefPaneKeyboard_Previews: PreviewProvider {
@ -303,7 +296,6 @@ struct VwrPrefPaneKeyboard_Previews: PreviewProvider {
// Ref: https://stackoverflow.com/a/71058587/4162914 // Ref: https://stackoverflow.com/a/71058587/4162914
// License: https://creativecommons.org/licenses/by-sa/4.0/ // License: https://creativecommons.org/licenses/by-sa/4.0/
// Ref: https://stackoverflow.com/a/71058587/4162914
@available(macOS 10.15, *) @available(macOS 10.15, *)
public struct ComboBox: NSViewRepresentable { public struct ComboBox: NSViewRepresentable {
// The items that will show up in the pop-up menu: // The items that will show up in the pop-up menu: