From 04ef3cbfec92335888fedcdea23a7f0d5c638b7a Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sun, 16 Jan 2022 22:45:37 +0800 Subject: [PATCH] Shiki: OOBE // Write certain default values to user plist if nil. --- Source/AppDelegate.swift | 9 ++- Source/Engine/vChewing/clsOOBEDefaults.swift | 81 ++++++++++++++++++++ Source/InputMethodController.mm | 7 ++ Source/PreferencesModule.swift | 4 +- vChewing.xcodeproj/project.pbxproj | 4 + 5 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 Source/Engine/vChewing/clsOOBEDefaults.swift diff --git a/Source/AppDelegate.swift b/Source/AppDelegate.swift index d29096a1..04af4771 100644 --- a/Source/AppDelegate.swift +++ b/Source/AppDelegate.swift @@ -68,11 +68,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, LanguageModelManager.loadDataModels() LanguageModelManager.loadUserPhrasesModel() - if UserDefaults.standard.object(forKey: kCheckUpdateAutomatically) == nil { - UserDefaults.standard.set(false, forKey: kCheckUpdateAutomatically) - UserDefaults.standard.synchronize() + OOBE.setMissingDefaults() + + // 只要使用者沒有勾選檢查更新、沒有主動做出要檢查更新的操作,就不要檢查更新。 + if (UserDefaults.standard.object(forKey: kCheckUpdateAutomatically) != nil) == true { + checkForUpdate() } - checkForUpdate() } @objc func showPreferences() { diff --git a/Source/Engine/vChewing/clsOOBEDefaults.swift b/Source/Engine/vChewing/clsOOBEDefaults.swift new file mode 100644 index 00000000..b2c059fe --- /dev/null +++ b/Source/Engine/vChewing/clsOOBEDefaults.swift @@ -0,0 +1,81 @@ +// +// clsOOBEDefaults.swift +// +// Copyright (c) 2021-2022 The vChewing Project. +// +// Contributors: +// Shiki Suen (@ShikiSuen) @ vChewing +// +// Based on the Syrup Project and the Formosana Library +// by Lukhnos Liu (@lukhnos). +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +import Cocoa + +private let kCheckUpdateAutomatically = "CheckUpdateAutomatically" +private let kCandidateListTextSize = "CandidateListTextSize" +private let kChooseCandidateUsingSpaceKey = "ChooseCandidateUsingSpaceKey" +private let kSelectPhraseAfterCursorAsCandidate = "SelectPhraseAfterCursorAsCandidate" +private let kUseHorizontalCandidateList = "UseHorizontalCandidateList" +private let kChineseConversionEnabledKey = "ChineseConversionEnabled" + +@objc public class OOBE : NSObject { + + @objc public static func setMissingDefaults () { + // 既然 Preferences Module 的預設屬性不自動寫入 plist、而且還是 private,那這邊就先寫入了。 + + // 首次啟用輸入法時設定不要自動更新,免得在某些要隔絕外部網路連線的保密機構內觸犯資安規則。 + if UserDefaults.standard.object(forKey: kCheckUpdateAutomatically) == nil { + UserDefaults.standard.set(false, forKey: kCheckUpdateAutomatically) + UserDefaults.standard.synchronize() + } + + // 預設選字窗字詞文字尺寸,設成 18 剛剛好 + if UserDefaults.standard.object(forKey: kCandidateListTextSize) == nil { + UserDefaults.standard.set(Preferences.candidateListTextSize, forKey: kCandidateListTextSize) + } + + // 預設摁空格鍵來選字,所以設成 true + if UserDefaults.standard.object(forKey: kChooseCandidateUsingSpaceKey) == nil { + UserDefaults.standard.set(Preferences.chooseCandidateUsingSpace, forKey: kChooseCandidateUsingSpaceKey) + } + + // 預設漢音風格選字,所以要設成 0 + if UserDefaults.standard.object(forKey: kSelectPhraseAfterCursorAsCandidate) == nil { + UserDefaults.standard.set(Preferences.selectPhraseAfterCursorAsCandidate, forKey: kSelectPhraseAfterCursorAsCandidate) + } + + // 預設橫向選字窗,不爽請自行改成縱向選字窗 + if UserDefaults.standard.object(forKey: kUseHorizontalCandidateList) == nil { + UserDefaults.standard.set(Preferences.useHorizontalCandidateList, forKey: kUseHorizontalCandidateList) + } + + // 預設停用繁體轉康熙模組 + if UserDefaults.standard.object(forKey: kChineseConversionEnabledKey) == nil { + UserDefaults.standard.set(Preferences.chineseConversionEnabled, forKey: kChineseConversionEnabledKey) + } + + UserDefaults.standard.synchronize() + } +} diff --git a/Source/InputMethodController.mm b/Source/InputMethodController.mm index 11912be4..5f6b808d 100644 --- a/Source/InputMethodController.mm +++ b/Source/InputMethodController.mm @@ -195,6 +195,10 @@ static double FindHighestScore(const vector& nodes, double epsilon) - (void)activateServer:(id)client { + // Write missing OOBE user plist entries. + [OOBE setMissingDefaults]; + + // Read user plist. [[NSUserDefaults standardUserDefaults] synchronize]; // Override the keyboard layout. Use US if not set. @@ -1470,6 +1474,9 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; } - (void)showPreferences:(id)sender { + // Write missing OOBE user plist entries. + [OOBE setMissingDefaults]; + // show the preferences panel, and also make the IME app itself the focus if ([IMKInputController instancesRespondToSelector:@selector(showPreferences:)]) { [super showPreferences:sender]; diff --git a/Source/PreferencesModule.swift b/Source/PreferencesModule.swift index af17221e..0ad0fb30 100644 --- a/Source/PreferencesModule.swift +++ b/Source/PreferencesModule.swift @@ -214,7 +214,7 @@ struct ComposingKeys { } // MARK: - -class Preferences: NSObject { +@objc public class Preferences: NSObject { @UserDefault(key: kKeyboardLayoutPreferenceKey, defaultValue: 0) @objc static var keyboardLayout: Int @@ -237,7 +237,7 @@ class Preferences: NSObject { @UserDefault(key: kSelectPhraseAfterCursorAsCandidatePreferenceKey, defaultValue: false) @objc static var selectPhraseAfterCursorAsCandidate: Bool - @UserDefault(key: kUseHorizontalCandidateListPreferenceKey, defaultValue: false) + @UserDefault(key: kUseHorizontalCandidateListPreferenceKey, defaultValue: true) @objc static var useHorizontalCandidateList: Bool @ComposingBufferSize(key: kComposingBufferSizePreferenceKey) diff --git a/vChewing.xcodeproj/project.pbxproj b/vChewing.xcodeproj/project.pbxproj index 861ac85c..47e78093 100644 --- a/vChewing.xcodeproj/project.pbxproj +++ b/vChewing.xcodeproj/project.pbxproj @@ -23,6 +23,7 @@ 5BC3EE1C278FC48C00F5E44C /* VTCandidateController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BC3EE19278FC48C00F5E44C /* VTCandidateController.swift */; }; 5BC3EE1D278FC48C00F5E44C /* HorizontalCandidateController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BC3EE1A278FC48C00F5E44C /* HorizontalCandidateController.swift */; }; 5BC3FB83278492DE0022E99A /* data-chs.txt in Resources */ = {isa = PBXBuildFile; fileRef = 5BC3FB82278492DE0022E99A /* data-chs.txt */; }; + 5BD0D1A4279463510008F48E /* clsOOBEDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BD0D1A3279463510008F48E /* clsOOBEDefaults.swift */; }; 5BDF2CFE2791BE4400838ADB /* InputSourceHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BDF2CFD2791BE4400838ADB /* InputSourceHelper.swift */; }; 5BDF2CFF2791BECC00838ADB /* InputSourceHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BDF2CFD2791BE4400838ADB /* InputSourceHelper.swift */; }; 5BDF2D012791C03B00838ADB /* PreferencesWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BDF2D002791C03B00838ADB /* PreferencesWindowController.swift */; }; @@ -113,6 +114,7 @@ 5BC3EE19278FC48C00F5E44C /* VTCandidateController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VTCandidateController.swift; sourceTree = ""; }; 5BC3EE1A278FC48C00F5E44C /* HorizontalCandidateController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HorizontalCandidateController.swift; sourceTree = ""; }; 5BC3FB82278492DE0022E99A /* data-chs.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "data-chs.txt"; sourceTree = ""; }; + 5BD0D1A3279463510008F48E /* clsOOBEDefaults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = clsOOBEDefaults.swift; sourceTree = ""; }; 5BDF2CFD2791BE4400838ADB /* InputSourceHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputSourceHelper.swift; sourceTree = ""; }; 5BDF2D002791C03B00838ADB /* PreferencesWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesWindowController.swift; sourceTree = ""; }; 5BDF2D022791C71200838ADB /* NonModalAlertWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NonModalAlertWindowController.swift; sourceTree = ""; }; @@ -272,6 +274,7 @@ 5BC2D2842793B434002C0BEC /* KeyValueBlobReader.h */, 5BC2D2852793B434002C0BEC /* CMakeLists.txt */, 5BC2D2862793B434002C0BEC /* KeyValueBlobReader.cpp */, + 5BD0D1A3279463510008F48E /* clsOOBEDefaults.swift */, ); path = vChewing; sourceTree = ""; @@ -715,6 +718,7 @@ 5BDF2D062791DFF200838ADB /* AppDelegate.swift in Sources */, 5BC3EE1B278FC48C00F5E44C /* VerticalCandidateController.swift in Sources */, 5B42B64027876FDC00BB9B9F /* UserOverrideModel.cpp in Sources */, + 5BD0D1A4279463510008F48E /* clsOOBEDefaults.swift in Sources */, D427A9C125ED28CC005D43E0 /* OpenCCBridge.swift in Sources */, 6A0D4F4515FC0EB100ABF4B3 /* Mandarin.cpp in Sources */, 6A0421A815FEF3F50061ED63 /* FastLM.cpp in Sources */,