Skip to content

Commit

Permalink
Coverts various preferences from int to enum.
Browse files Browse the repository at this point in the history
  • Loading branch information
zonble committed Feb 26, 2024
1 parent 8863b32 commit 4868197
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 58 deletions.
14 changes: 7 additions & 7 deletions McBopomofoTests/PreferencesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ class PreferencesTests: XCTestCase {
}

func testKeyboardLayout() {
XCTAssert(Preferences.keyboardLayout == 0)
Preferences.keyboardLayout = 1
XCTAssert(Preferences.keyboardLayout == 1)
XCTAssert(Preferences.keyboardLayout == .standard)
Preferences.keyboardLayout = .eten
XCTAssert(Preferences.keyboardLayout == .eten)
}

func testKeyboardLayoutName() {
XCTAssert(Preferences.keyboardLayoutName == "Standard")
Preferences.keyboardLayout = 1
Preferences.keyboardLayout = .eten
XCTAssert(Preferences.keyboardLayoutName == "ETen")
}

Expand Down Expand Up @@ -176,9 +176,9 @@ class PreferencesTests: XCTestCase {
}

func testChineseConversionStyle() {
XCTAssert(Preferences.chineseConversionStyle == 0)
Preferences.chineseConversionStyle = 1
XCTAssert(Preferences.chineseConversionStyle == 1)
XCTAssert(Preferences.chineseConversionStyle == .output)
Preferences.chineseConversionStyle = .model
XCTAssert(Preferences.chineseConversionStyle == .model)
}

}
Expand Down
4 changes: 2 additions & 2 deletions Source/InputMethodController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ extension McBopomofoInputMethodController {
if !Preferences.chineseConversionEnabled {
return text
}
if Preferences.chineseConversionStyle == 1 {
if Preferences.chineseConversionStyle == .model {
return text
}
return OpenCCBridge.shared.convertToSimplified(text) ?? ""
Expand Down Expand Up @@ -811,7 +811,7 @@ extension McBopomofoInputMethodController: CandidateControllerDelegate {
break
}
case let state as InputState.AssociatedPhrases:
let scToTc = Preferences.chineseConversionEnabled && Preferences.chineseConversionStyle == 1
let scToTc = Preferences.chineseConversionEnabled && Preferences.chineseConversionStyle == .model
let selectedPhrase: String = {
let selectedPhrase = state.selectedPhrase
return if scToTc {
Expand Down
4 changes: 3 additions & 1 deletion Source/InputState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ class InputState: NSObject {
return false

}
if Preferences.chineseConversionStyle == 1 && Preferences.chineseConversionEnabled {
if Preferences.chineseConversionStyle == ChineseConversionStyle.model
&& Preferences.chineseConversionEnabled
{
tooltip = NSLocalizedString(
"Model-based Chinese conversion is on. Not recommended to add user phrases.",
comment: "")
Expand Down
8 changes: 4 additions & 4 deletions Source/KeyHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ - (void)setInputMode:(NSString *)value
newLanguageModel = [LanguageModelManager languageModelMcBopomofo];
newLanguageModel->setPhraseReplacementEnabled(Preferences.phraseReplacementEnabled);
}
newLanguageModel->setExternalConverterEnabled(Preferences.chineseConversionStyle == 1);
newLanguageModel->setExternalConverterEnabled(Preferences.chineseConversionStyle == ChineseConversionStyleModel);

// Only apply the changes if the value is changed
if (![_inputMode isEqualToString:newInputMode]) {
Expand Down Expand Up @@ -203,7 +203,7 @@ - (void)syncWithPreferences
_bpmfReadingBuffer->setKeyboardLayout(Formosa::Mandarin::BopomofoKeyboardLayout::StandardLayout());
Preferences.keyboardLayout = KeyboardLayoutStandard;
}
_languageModel->setExternalConverterEnabled(Preferences.chineseConversionStyle == 1);
_languageModel->setExternalConverterEnabled(Preferences.chineseConversionStyle == ChineseConversionStyleModel);
}

- (void)fixNodeWithReading:(NSString *)reading value:(NSString *)value originalCursorIndex:(size_t)originalCursorIndex useMoveCursorAfterSelectionSetting:(BOOL)flag
Expand Down Expand Up @@ -1801,7 +1801,7 @@ - (InputStateInputting *)buildInputtingState
// Create a tooltip to warn the user that their cursor is between two
// readings (syllables) even if the cursor is not in the middle of a
// composed string due to its being shorter than the number of readings.
if (valueCodePointCount < readingLength) {
if (valueCodePointCount != readingLength) {
// builderCursor is guaranteed to be > 0. If it was 0, we wouldn't even
// reach here due to runningCursor having already "caught up" with
// builderCursor. It is also guaranteed to be less than the size of the
Expand Down Expand Up @@ -1928,7 +1928,7 @@ - (nullable InputState *)buildAssociatePhraseStateWithPreviousState:(id)state se
}

BOOL scToTc = Preferences.chineseConversionEnabled &&
Preferences.chineseConversionStyle == 1;
Preferences.chineseConversionStyle == ChineseConversionStyleModel;
if (scToTc) {
key = [[OpenCCBridge sharedInstance] convertToTraditional:key];
}
Expand Down
97 changes: 53 additions & 44 deletions Source/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ private let kBasisKeyboardLayoutPreferenceKey = "BasisKeyboardLayout"
/// alphanumeric ("ASCII") input basic keyboard layout.
private let kFunctionKeyKeyboardLayoutPreferenceKey = "FunctionKeyKeyboardLayout"
/// whether include shift.
private let kFunctionKeyKeyboardLayoutOverrideIncludeShiftKey = "FunctionKeyKeyboardLayoutOverrideIncludeShift"
private let kFunctionKeyKeyboardLayoutOverrideIncludeShiftKey =
"FunctionKeyKeyboardLayoutOverrideIncludeShift"
private let kCandidateListTextSizeKey = "CandidateListTextSize"
private let kSelectPhraseAfterCursorAsCandidateKey = "SelectPhraseAfterCursorAsCandidate"
private let kMoveCursorAfterSelectingCandidateKey = "MoveCursorAfterSelectingCandidate"
Expand Down Expand Up @@ -160,7 +161,7 @@ struct CandidateListTextSize {
case IBM = 5

var name: String {
return switch (self) {
return switch self {
case .standard:
"Standard"
case .eten:
Expand All @@ -182,7 +183,7 @@ struct CandidateListTextSize {
case model

var name: String {
return switch (self) {
return switch self {
case .output:
"output"
case .model:
Expand All @@ -195,41 +196,43 @@ struct CandidateListTextSize {

class Preferences: NSObject {
static var allKeys: [String] {
[kKeyboardLayoutPreferenceKey,
kBasisKeyboardLayoutPreferenceKey,
kFunctionKeyKeyboardLayoutPreferenceKey,
kFunctionKeyKeyboardLayoutOverrideIncludeShiftKey,
kCandidateListTextSizeKey,
kSelectPhraseAfterCursorAsCandidateKey,
kUseHorizontalCandidateListPreferenceKey,
kChooseCandidateUsingSpaceKey,
kChineseConversionEnabledKey,
kHalfWidthPunctuationEnabledKey,
kEscToCleanInputBufferKey,
kKeepReadingUponCompositionError,
kCandidateTextFontName,
kCandidateKeyLabelFontName,
kCandidateKeys,
kPhraseReplacementEnabledKey,
kChineseConversionStyleKey,
kAssociatedPhrasesEnabledKey,
kControlEnterOutputKey,
kUseCustomUserPhraseLocation,
kCustomUserPhraseLocation]
[
kKeyboardLayoutPreferenceKey,
kBasisKeyboardLayoutPreferenceKey,
kFunctionKeyKeyboardLayoutPreferenceKey,
kFunctionKeyKeyboardLayoutOverrideIncludeShiftKey,
kCandidateListTextSizeKey,
kSelectPhraseAfterCursorAsCandidateKey,
kUseHorizontalCandidateListPreferenceKey,
kChooseCandidateUsingSpaceKey,
kChineseConversionEnabledKey,
kHalfWidthPunctuationEnabledKey,
kEscToCleanInputBufferKey,
kKeepReadingUponCompositionError,
kCandidateTextFontName,
kCandidateKeyLabelFontName,
kCandidateKeys,
kPhraseReplacementEnabledKey,
kChineseConversionStyleKey,
kAssociatedPhrasesEnabledKey,
kControlEnterOutputKey,
kUseCustomUserPhraseLocation,
kCustomUserPhraseLocation,
]
}


@UserDefault(key: kKeyboardLayoutPreferenceKey, defaultValue: 0)
@objc static var keyboardLayout: Int
@EnumUserDefault(key: kKeyboardLayoutPreferenceKey, defaultValue: KeyboardLayout.standard)
@objc static var keyboardLayout: KeyboardLayout

@objc static var keyboardLayoutName: String {
(KeyboardLayout(rawValue: keyboardLayout) ?? KeyboardLayout.standard).name
keyboardLayout.name
}

@UserDefault(key: kBasisKeyboardLayoutPreferenceKey, defaultValue: "com.apple.keylayout.US")
@objc static var basisKeyboardLayout: String

@UserDefault(key: kFunctionKeyKeyboardLayoutPreferenceKey, defaultValue: "com.apple.keylayout.US")
@UserDefault(
key: kFunctionKeyKeyboardLayoutPreferenceKey, defaultValue: "com.apple.keylayout.US")
@objc static var functionKeyboardLayout: String

@UserDefault(key: kFunctionKeyKeyboardLayoutOverrideIncludeShiftKey, defaultValue: false)
Expand Down Expand Up @@ -326,15 +329,18 @@ class Preferences: NSObject {
case .empty:
return NSLocalizedString("Candidates keys cannot be empty.", comment: "")
case .invalidCharacters:
return NSLocalizedString("Candidate keys can only contain Latin characters and numbers.", comment: "")
return NSLocalizedString(
"Candidate keys can only contain Latin characters and numbers.", comment: "")
case .containSpace:
return NSLocalizedString("Candidate keys cannot contain space.", comment: "")
case .duplicatedCharacters:
return NSLocalizedString("There should not be duplicated keys.", comment: "")
case .tooShort:
return NSLocalizedString("Candidate keys cannot be shorter than 4 characters.", comment: "")
return NSLocalizedString(
"Candidate keys cannot be shorter than 4 characters.", comment: "")
case .tooLong:
return NSLocalizedString("Candidate keys cannot be longer than 15 characters.", comment: "")
return NSLocalizedString(
"Candidate keys cannot be longer than 15 characters.", comment: "")
}
}
}
Expand All @@ -350,11 +356,11 @@ extension Preferences {
///
/// - 0: convert the output
/// - 1: convert the phrase models.
@UserDefault(key: kChineseConversionStyleKey, defaultValue: 0)
@objc static var chineseConversionStyle: Int
@EnumUserDefault(key: kChineseConversionStyleKey, defaultValue: ChineseConversionStyle.output)
@objc static var chineseConversionStyle: ChineseConversionStyle

@objc static var chineseConversionStyleName: String? {
ChineseConversionStyle(rawValue: chineseConversionStyle)?.name
@objc static var chineseConversionStyleName: String {
chineseConversionStyle.name
}
}

Expand All @@ -377,7 +383,6 @@ extension Preferences {
}
}


@objc enum ControlEnterOutput: Int {
case off = 0
case bpmfReading = 1
Expand All @@ -402,14 +407,16 @@ extension Preferences {

@objc class UserPhraseLocationHelper: NSObject {
@objc static var defaultUserPhraseLocation: String {
let paths = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true)
let paths = NSSearchPathForDirectoriesInDomains(
.applicationSupportDirectory, .userDomainMask, true)
let appSupportPath = paths.first!
return (appSupportPath as NSString).appendingPathComponent("McBopomofo")
}
}

extension NSNotification.Name {
static var userPhraseLocationDidChange = NSNotification.Name(rawValue: "UserPhraseLocationDidChangeNotification")
static var userPhraseLocationDidChange = NSNotification.Name(
rawValue: "UserPhraseLocationDidChangeNotification")
}

extension Preferences {
Expand All @@ -424,9 +431,11 @@ extension Preferences {
}
return customUserPhraseLocation
}()
let notification = Notification(name: .userPhraseLocationDidChange, object: self, userInfo: [
"location": location
])
let notification = Notification(
name: .userPhraseLocationDidChange, object: self,
userInfo: [
"location": location
])
NotificationQueue.default.dequeueNotifications(matching: notification, coalesceMask: 0)
NotificationQueue.default.enqueue(notification, postingStyle: .now)
}
Expand All @@ -446,7 +455,6 @@ extension Preferences {
}
}


extension Preferences {
static func defaultAddPhraseHookPath() -> String {
let bundle = Bundle.main
Expand All @@ -457,7 +465,8 @@ extension Preferences {
@UserDefault(key: kAddPhraseHookEnabledKey, defaultValue: false)
@objc static var addPhraseHookEnabled: Bool

@UserDefaultWithFunction(key: kAddPhraseHookPath, defaultValueFunction: defaultAddPhraseHookPath)
@UserDefaultWithFunction(
key: kAddPhraseHookPath, defaultValueFunction: defaultAddPhraseHookPath)
@objc static var addPhraseHookPath: String
}

Expand Down

0 comments on commit 4868197

Please sign in to comment.