From 7d05bcf1ff3a7f800dd1069455627a162772503a Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Wed, 21 Sep 2022 18:08:06 +0800 Subject: [PATCH] PrefMgr // Split its extension to a standalone file. --- .../{PrefMgr.swift => PrefMgr_Core.swift} | 58 ++----------------- Source/Modules/PrefMgr_Extension.swift | 56 ++++++++++++++++++ vChewing.xcodeproj/project.pbxproj | 12 ++-- 3 files changed, 69 insertions(+), 57 deletions(-) rename Source/Modules/{PrefMgr.swift => PrefMgr_Core.swift} (83%) create mode 100644 Source/Modules/PrefMgr_Extension.swift diff --git a/Source/Modules/PrefMgr.swift b/Source/Modules/PrefMgr_Core.swift similarity index 83% rename from Source/Modules/PrefMgr.swift rename to Source/Modules/PrefMgr_Core.swift index c1ad69dd..9a49fb51 100644 --- a/Source/Modules/PrefMgr.swift +++ b/Source/Modules/PrefMgr_Core.swift @@ -11,14 +11,13 @@ import Shared import SwiftExtension -private let kDefaultKeys = "123456789" -private let kDefaultBasicKeyboardLayout = "com.apple.keylayout.ZhuyinBopomofo" -private let kDefaultAlphanumericalKeyboardLayout = "com.apple.keylayout.ABC" - // MARK: - public class PrefMgr: PrefMgrProtocol { public static let shared = PrefMgr() + public static let kDefaultCandidateKeys = "123456789" + public static let kDefaultBasicKeyboardLayout = "com.apple.keylayout.ZhuyinBopomofo" + public static let kDefaultAlphanumericalKeyboardLayout = "com.apple.keylayout.ABC" // MARK: - Settings (Tier 1) @@ -233,11 +232,11 @@ public class PrefMgr: PrefMgrProtocol { @AppProperty(key: UserDef.kCandidateKeyLabelFontName.rawValue, defaultValue: "") public var candidateKeyLabelFontName: String - @AppProperty(key: UserDef.kCandidateKeys.rawValue, defaultValue: kDefaultKeys) + @AppProperty(key: UserDef.kCandidateKeys.rawValue, defaultValue: kDefaultCandidateKeys) public var candidateKeys: String { didSet { if useIMKCandidateWindow { - candidateKeys = kDefaultKeys + candidateKeys = Self.kDefaultCandidateKeys } } } @@ -293,50 +292,3 @@ public class PrefMgr: PrefMgrProtocol { @AppProperty(key: UserDef.kUsingHotKeyCurrencyNumerals.rawValue, defaultValue: true) public var usingHotKeyCurrencyNumerals: Bool } - -// MARK: Auto parameter fix procedures, executed everytime on ctlInputMethod.activateServer(). - -extension PrefMgr { - public func fixOddPreferences() { - // 防呆。macOS 10.11 用 IMK 選字窗會崩潰,macOS 10.13 的 IMK 選字窗仍有問題。 - // 一般人想用的 IMK 選字窗基於 macOS 10.09 系統內建的注音輸入法的那種矩陣選字窗。 - // 然而,該選字窗的體驗直到 macOS 10.14 開始才在 IMKCandidates 當中正式提供。 - if #unavailable(macOS 10.13) { useIMKCandidateWindow = false } - if #unavailable(macOS 10.15) { - handleDefaultCandidateFontsByLangIdentifier = false - shiftKeyAccommodationBehavior = 0 - disableShiftTogglingAlphanumericalMode = false - togglingAlphanumericalModeWithLShift = false - } - // 客體黑名單自動排序去重複。 - clientsIMKTextInputIncapable = Array(Set(clientsIMKTextInputIncapable)).sorted() - // 注拼槽注音排列選項糾錯。 - var isKeyboardParserOptionValid = false - KeyboardParser.allCases.forEach { - if $0.rawValue == keyboardParser { isKeyboardParserOptionValid = true } - } - if !isKeyboardParserOptionValid { - keyboardParser = 0 - } - // 基礎鍵盤排列選項糾錯。 - if TISInputSource.generate(from: basicKeyboardLayout) == nil { - basicKeyboardLayout = kDefaultBasicKeyboardLayout - } - if TISInputSource.generate(from: alphanumericalKeyboardLayout) == nil { - alphanumericalKeyboardLayout = kDefaultAlphanumericalKeyboardLayout - } - // 其它多元選項參數自動糾錯。 - if ![0, 1, 2].contains(specifyIntonationKeyBehavior) { - specifyIntonationKeyBehavior = 0 - } - if ![0, 1, 2].contains(specifyShiftBackSpaceKeyBehavior) { - specifyShiftBackSpaceKeyBehavior = 0 - } - if ![0, 1, 2].contains(upperCaseLetterKeyBehavior) { - upperCaseLetterKeyBehavior = 0 - } - if ![0, 1, 2].contains(shiftKeyAccommodationBehavior) { - shiftKeyAccommodationBehavior = 0 - } - } -} diff --git a/Source/Modules/PrefMgr_Extension.swift b/Source/Modules/PrefMgr_Extension.swift new file mode 100644 index 00000000..3dfb16f1 --- /dev/null +++ b/Source/Modules/PrefMgr_Extension.swift @@ -0,0 +1,56 @@ +// (c) 2021 and onwards The vChewing Project (MIT-NTL License). +// ==================== +// This code is released under the MIT license (SPDX-License-Identifier: MIT) +// ... with NTL restriction stating that: +// No trademark license is granted to use the trade names, trademarks, service +// marks, or product names of Contributor, except as required to fulfill notice +// requirements defined in MIT License. + +import Shared + +// MARK: Auto parameter fix procedures, executed everytime on ctlInputMethod.activateServer(). + +extension PrefMgr { + public func fixOddPreferences() { + // 防呆。macOS 10.11 用 IMK 選字窗會崩潰,macOS 10.13 的 IMK 選字窗仍有問題。 + // 一般人想用的 IMK 選字窗基於 macOS 10.09 系統內建的注音輸入法的那種矩陣選字窗。 + // 然而,該選字窗的體驗直到 macOS 10.14 開始才在 IMKCandidates 當中正式提供。 + if #unavailable(macOS 10.13) { useIMKCandidateWindow = false } + if #unavailable(macOS 10.15) { + handleDefaultCandidateFontsByLangIdentifier = false + shiftKeyAccommodationBehavior = 0 + disableShiftTogglingAlphanumericalMode = false + togglingAlphanumericalModeWithLShift = false + } + // 客體黑名單自動排序去重複。 + clientsIMKTextInputIncapable = Array(Set(clientsIMKTextInputIncapable)).sorted() + // 注拼槽注音排列選項糾錯。 + var isKeyboardParserOptionValid = false + KeyboardParser.allCases.forEach { + if $0.rawValue == keyboardParser { isKeyboardParserOptionValid = true } + } + if !isKeyboardParserOptionValid { + keyboardParser = 0 + } + // 基礎鍵盤排列選項糾錯。 + if TISInputSource.generate(from: basicKeyboardLayout) == nil { + basicKeyboardLayout = Self.kDefaultBasicKeyboardLayout + } + if TISInputSource.generate(from: alphanumericalKeyboardLayout) == nil { + alphanumericalKeyboardLayout = Self.kDefaultAlphanumericalKeyboardLayout + } + // 其它多元選項參數自動糾錯。 + if ![0, 1, 2].contains(specifyIntonationKeyBehavior) { + specifyIntonationKeyBehavior = 0 + } + if ![0, 1, 2].contains(specifyShiftBackSpaceKeyBehavior) { + specifyShiftBackSpaceKeyBehavior = 0 + } + if ![0, 1, 2].contains(upperCaseLetterKeyBehavior) { + upperCaseLetterKeyBehavior = 0 + } + if ![0, 1, 2].contains(shiftKeyAccommodationBehavior) { + shiftKeyAccommodationBehavior = 0 + } + } +} diff --git a/vChewing.xcodeproj/project.pbxproj b/vChewing.xcodeproj/project.pbxproj index e1f1da88..c21f1f69 100644 --- a/vChewing.xcodeproj/project.pbxproj +++ b/vChewing.xcodeproj/project.pbxproj @@ -34,7 +34,7 @@ 5B8457A12871ADBE00C93B01 /* ChineseConverterBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B8457A02871ADBE00C93B01 /* ChineseConverterBridge.swift */; }; 5B963C9D28D5BFB800DCEE88 /* CocoaExtension in Frameworks */ = {isa = PBXBuildFile; productRef = 5B963C9C28D5BFB800DCEE88 /* CocoaExtension */; }; 5B963CA328D5C23600DCEE88 /* SwiftExtension in Frameworks */ = {isa = PBXBuildFile; productRef = 5B963CA228D5C23600DCEE88 /* SwiftExtension */; }; - 5B963CA828D5DB1400DCEE88 /* PrefMgr.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B963CA728D5DB1400DCEE88 /* PrefMgr.swift */; }; + 5B963CA828D5DB1400DCEE88 /* PrefMgr_Core.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B963CA728D5DB1400DCEE88 /* PrefMgr_Core.swift */; }; 5B98114828D6198700CBC605 /* PinyinPhonaConverter in Frameworks */ = {isa = PBXBuildFile; productRef = 5B98114728D6198700CBC605 /* PinyinPhonaConverter */; }; 5BA9FD0F27FEDB6B002DE248 /* suiPrefPaneGeneral.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BA9FD0A27FEDB6B002DE248 /* suiPrefPaneGeneral.swift */; }; 5BA9FD1027FEDB6B002DE248 /* suiPrefPaneKeyboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BA9FD0B27FEDB6B002DE248 /* suiPrefPaneKeyboard.swift */; }; @@ -58,6 +58,7 @@ 5BC447A02865686500EDC323 /* data-symbols.plist in Resources */ = {isa = PBXBuildFile; fileRef = 5BEDB71E283B4AEA0078EB25 /* data-symbols.plist */; }; 5BC447A12865686500EDC323 /* data-zhuyinwen.plist in Resources */ = {isa = PBXBuildFile; fileRef = 5BEDB71F283B4AEA0078EB25 /* data-zhuyinwen.plist */; }; 5BC447A628656A1900EDC323 /* Placeholder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BC447A228656A1900EDC323 /* Placeholder.swift */; }; + 5BCCAFF828DB19A300AB1B27 /* PrefMgr_Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BCCAFF728DB19A300AB1B27 /* PrefMgr_Extension.swift */; }; 5BD0113D2818543900609769 /* KeyHandler_Core.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BD0113C2818543900609769 /* KeyHandler_Core.swift */; }; 5BD05BCA27B2A43D004C4F1D /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6A2E40F5253A69DA00D1AE1D /* Images.xcassets */; }; 5BD05C5D27B2BBA9004C4F1D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5BD05C5B27B2BBA9004C4F1D /* Main.storyboard */; }; @@ -222,7 +223,7 @@ 5B963C9B28D5BE4100DCEE88 /* vChewing_CocoaExtension */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = vChewing_CocoaExtension; path = Packages/vChewing_CocoaExtension; sourceTree = ""; }; 5B963C9E28D5C14600DCEE88 /* vChewing_Shared */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = vChewing_Shared; path = Packages/vChewing_Shared; sourceTree = ""; }; 5B963CA128D5C22D00DCEE88 /* vChewing_SwiftExtension */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = vChewing_SwiftExtension; path = Packages/vChewing_SwiftExtension; sourceTree = ""; }; - 5B963CA728D5DB1400DCEE88 /* PrefMgr.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrefMgr.swift; sourceTree = ""; }; + 5B963CA728D5DB1400DCEE88 /* PrefMgr_Core.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrefMgr_Core.swift; sourceTree = ""; }; 5B98114628D6198000CBC605 /* vChewing_PinyinPhonaConverter */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = vChewing_PinyinPhonaConverter; path = Packages/vChewing_PinyinPhonaConverter; sourceTree = ""; }; 5BA9FD0A27FEDB6B002DE248 /* suiPrefPaneGeneral.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = suiPrefPaneGeneral.swift; sourceTree = ""; tabWidth = 2; usesTabs = 0; }; 5BA9FD0B27FEDB6B002DE248 /* suiPrefPaneKeyboard.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = suiPrefPaneKeyboard.swift; sourceTree = ""; tabWidth = 2; usesTabs = 0; }; @@ -245,6 +246,7 @@ 5BC2652127E04B7B00700291 /* uninstall.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; lineEnding = 0; path = uninstall.sh; sourceTree = ""; }; 5BC447A228656A1900EDC323 /* Placeholder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Placeholder.swift; sourceTree = ""; }; 5BC447AB2865BEF500EDC323 /* AUTHORS */ = {isa = PBXFileReference; lastKnownFileType = text; path = AUTHORS; sourceTree = ""; }; + 5BCCAFF728DB19A300AB1B27 /* PrefMgr_Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrefMgr_Extension.swift; sourceTree = ""; }; 5BD0113C2818543900609769 /* KeyHandler_Core.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; lineEnding = 0; path = KeyHandler_Core.swift; sourceTree = ""; usesTabs = 0; }; 5BD05BB827B2A429004C4F1D /* vChewingPhraseEditor.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = vChewingPhraseEditor.app; sourceTree = BUILT_PRODUCTS_DIR; }; 5BD05BC627B2A42A004C4F1D /* vChewingPhraseEditor.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = vChewingPhraseEditor.entitlements; sourceTree = ""; }; @@ -692,7 +694,8 @@ 5B3133BE280B229700A4A505 /* KeyHandler_States.swift */, 5BAEFACF28012565001F42C9 /* LMMgr.swift */, D47B92BF27972AC800458394 /* main.swift */, - 5B963CA728D5DB1400DCEE88 /* PrefMgr.swift */, + 5B963CA728D5DB1400DCEE88 /* PrefMgr_Core.swift */, + 5BCCAFF728DB19A300AB1B27 /* PrefMgr_Extension.swift */, ); path = Modules; sourceTree = ""; @@ -1057,7 +1060,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 5B963CA828D5DB1400DCEE88 /* PrefMgr.swift in Sources */, + 5B963CA828D5DB1400DCEE88 /* PrefMgr_Core.swift in Sources */, D427F76C278CA2B0004A2160 /* AppDelegate.swift in Sources */, 5B7F225D2808501000DDD3CB /* KeyHandler_HandleInput.swift in Sources */, 5BA9FD1227FEDB6B002DE248 /* suiPrefPaneExperience.swift in Sources */, @@ -1082,6 +1085,7 @@ 5B782EC4280C243C007276DE /* KeyHandler_HandleCandidate.swift in Sources */, 5BA9FD0F27FEDB6B002DE248 /* suiPrefPaneGeneral.swift in Sources */, 5B242403284B0D6500520FE4 /* ctlCandidateUniversal.swift in Sources */, + 5BCCAFF828DB19A300AB1B27 /* PrefMgr_Extension.swift in Sources */, 5BA9FD1127FEDB6B002DE248 /* ctlPrefUI.swift in Sources */, 5B8457A12871ADBE00C93B01 /* ChineseConverterBridge.swift in Sources */, 5BA9FD1327FEDB6B002DE248 /* suiPrefPaneDictionary.swift in Sources */,