Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
groverlynn committed Jul 15, 2024
1 parent 00c33ab commit ef44c4b
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 243 deletions.
6 changes: 2 additions & 4 deletions Squirrel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@
isa = PBXProject;
attributes = {
BuildIndependentTargetsInParallel = YES;
LastUpgradeCheck = 1540;
LastUpgradeCheck = 1600;
TargetAttributes = {
8D1107260486CEB800E47090 = {
LastSwiftMigration = 1530;
Expand Down Expand Up @@ -804,7 +804,6 @@
C01FCF4F08A954540054247B /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ALWAYS_SEARCH_USER_PATHS = NO;
ASSETCATALOG_COMPILER_APPICON_NAME = RimeIcon;
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
Expand Down Expand Up @@ -909,7 +908,6 @@
C01FCF5008A954540054247B /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ALWAYS_SEARCH_USER_PATHS = NO;
ASSETCATALOG_COMPILER_APPICON_NAME = RimeIcon;
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
Expand Down Expand Up @@ -998,7 +996,7 @@
PRODUCT_MODULE_NAME = "$(PRODUCT_NAME)";
SDKROOT = macosx;
STRINGS_FILE_OUTPUT_ENCODING = "UTF-8";
SWIFT_COMPILATION_MODE = singlefile;
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_INCLUDE_PATHS = /Library/Developer/Toolchains;
SWIFT_OBJC_BRIDGING_HEADER = "sources/Squirrel-Bridging-Header.h";
SWIFT_OBJC_INTERFACE_HEADER_NAME = "$(SWIFT_MODULE_NAME)-Swift.h";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1540"
LastUpgradeVersion = "1600"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion sources/RimeKeycode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ enum RimeKeycode: CInt, Sendable, Comparable {

init(keychar: unichar, shift: Bool, caps: Bool) {
// NOTE: IBus/Rime use different keycodes for uppercase/lowercase letters.
if 0x61...0x7A ~= keychar && shift != caps {
if 0x61...0x7A ~= keychar, shift != caps {
// lowercase -> Uppercase
self.init(rawValue: CInt(keychar) - 0x20)!; return
}
Expand Down
16 changes: 8 additions & 8 deletions sources/SquirrelApplicationDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ final class SquirrelApplicationDelegate: NSObject, NSApplicationDelegate, SPUSta
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.notification.request.identifier == Self.updaterIdentifier && response.actionIdentifier == UNNotificationDefaultActionIdentifier {
if response.notification.request.identifier == Self.updaterIdentifier, response.actionIdentifier == UNNotificationDefaultActionIdentifier {
updateController.updater.checkForUpdates()
}
completionHandler()
Expand Down Expand Up @@ -232,7 +232,7 @@ final class SquirrelApplicationDelegate: NSObject, NSApplicationDelegate, SPUSta
}
RimeApi.set_notification_handler(notificationHandler, bridge(obj: self))
var squirrelTraits: RimeTraits = RimeStructInit()
squirrelTraits.shared_data_dir = Bundle.main.sharedSupportURL!.withUnsafeFileSystemRepresentation(\.unsafelyUnwrapped)
squirrelTraits.shared_data_dir = Bundle.main.sharedSupportURL?.withUnsafeFileSystemRepresentation(\.unsafelyUnwrapped)
squirrelTraits.user_data_dir = Self.userDataDir.withUnsafeFileSystemRepresentation(\.unsafelyUnwrapped)
squirrelTraits.distribution_code_name = "Squirrel".utf8CString.withUnsafeBufferPointer(\.baseAddress)
squirrelTraits.distribution_name = "鼠鬚管".utf8CString.withUnsafeBufferPointer(\.baseAddress)
Expand Down Expand Up @@ -286,11 +286,11 @@ final class SquirrelApplicationDelegate: NSObject, NSApplicationDelegate, SPUSta
}

func loadSchemaSpecificSettings(schemaId: String, withRimeSession sessionId: RimeSessionId) {
guard !schemaId.isEmpty && !schemaId.hasPrefix(".") else { return }
guard !schemaId.isEmpty, !schemaId.hasPrefix(".") else { return }
// update the list of switchers that change styles and color-themes
let baseConfig = SquirrelConfig(.base)
let schema = SquirrelConfig()
if schema.open(withSchemaId: schemaId, baseConfig: baseConfig) && schema.hasSection("style") {
if schema.open(withSchemaId: schemaId, baseConfig: baseConfig), schema.hasSection("style") {
panel.optionSwitcher = schema.optionSwitcherForSchema()
panel.optionSwitcher.update(withRimeSession: sessionId)
panel.loadConfig(schema)
Expand All @@ -308,7 +308,7 @@ final class SquirrelApplicationDelegate: NSObject, NSApplicationDelegate, SPUSta
panel.loadLabelConfig(defaultConfig, directUpdate: true)
} else {
let schema = SquirrelConfig()
if schema.open(withSchemaId: schemaId, baseConfig: defaultConfig) && schema.hasSection("menu") {
if schema.open(withSchemaId: schemaId, baseConfig: defaultConfig), schema.hasSection("menu") {
panel.loadLabelConfig(schema, directUpdate: false)
} else {
panel.loadLabelConfig(defaultConfig, directUpdate: false)
Expand Down Expand Up @@ -356,7 +356,7 @@ final class SquirrelApplicationDelegate: NSObject, NSApplicationDelegate, SPUSta
}

func inputSourceChanged(_ notification: Notification) {
let inputSource: TISInputSource = TISCopyCurrentKeyboardInputSource().takeUnretainedValue()
let inputSource: TISInputSource = TISCopyCurrentKeyboardInputSource().takeRetainedValue()
if let inputSourceID: CFString = bridge(ptr: TISGetInputSourceProperty(inputSource, kTISPropertyInputSourceID)), !CFStringHasPrefix(inputSourceID, SquirrelApp.bundleId as CFString) {
isCurrentInputMethod = false
}
Expand All @@ -372,7 +372,7 @@ private func showNotification(message: String) {
}
}
center.getNotificationSettings { settings in
guard (settings.authorizationStatus == .authorized || settings.authorizationStatus == .provisional) && settings.alertSetting == .enabled else { return }
guard (settings.authorizationStatus == .authorized || settings.authorizationStatus == .provisional), settings.alertSetting == .enabled else { return }
let content = UNMutableNotificationContent()
content.title = Bundle.main.localizedString(forKey: "Squirrel", value: nil, table: "Notifications")
content.subtitle = Bundle.main.localizedString(forKey: message, value: nil, table: "Notifications")
Expand Down Expand Up @@ -424,7 +424,7 @@ private func notificationHandler(context_object: UnsafeMutableRawPointer?, sessi
appDelegate.loadSchemaSpecificLabels(schemaId: schemaId)
appDelegate.loadSchemaSpecificSettings(schemaId: schemaId, withRimeSession: session_id)
}
if updateScriptVariant && !updateStyleOptions {
if updateScriptVariant, !updateStyleOptions {
appDelegate.panel.updateScriptVariant()
}
guard appDelegate.showNotifications != .never else { break }
Expand Down
50 changes: 21 additions & 29 deletions sources/SquirrelConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class SquirrelOptionSwitcher: NSObject {

// return whether switcher options has been successfully updated
func updateSwitcher(_ switcher: [String : String]) -> Bool {
guard !self.switcher.isEmpty && switcher.count == self.switcher.count else { return false }
guard !self.switcher.isEmpty, switcher.count == self.switcher.count else { return false }
let optionNames: Set<String> = Set(switcher.keys)
if optionNames == self.optionNames {
self.switcher = switcher
Expand All @@ -57,26 +57,25 @@ final class SquirrelOptionSwitcher: NSObject {
return true
}

func updateCurrentScriptVariant(_ scriptVariant: String) -> Bool {
guard !scriptVariantOptions.isEmpty else { return false }
guard let scriptVariantCode = scriptVariantOptions[scriptVariant] else { return false }
func updateCurrentScriptVariant(_ scriptVariant: String?) -> Bool {
guard let scriptVariant = scriptVariant, !scriptVariantOptions.isEmpty, let scriptVariantCode = scriptVariantOptions[scriptVariant] else { return false }
currentScriptVariant = scriptVariantCode
return true
}

func update(withRimeSession session: RimeSessionId) {
guard !switcher.isEmpty && session != 0 else { return }
guard !switcher.isEmpty, session != 0 else { return }
for state in optionStates {
var updatedState: String?
let optionGroup: [String] = Array(switcher.filter({ $0.value == state }).keys)
_ = optionGroup.first(where: { if RimeApi.get_option(session, $0) { updatedState = $0; return true } else { return false } })
updatedState = optionGroup.first { RimeApi.get_option(session, $0) }
updatedState ?= "!" + optionGroup[0]
if updatedState != state {
_ = updateGroupState(updatedState!, ofOption: state)
}
}
// update script variant
_ = scriptVariantOptions.first(where: { if $0.key.hasPrefix("!") ? !RimeApi.get_option(session, String($0.key.dropFirst())) : RimeApi.get_option(session, $0.key) { _ = updateCurrentScriptVariant($0.key); return true } else { return false } })
_ = updateCurrentScriptVariant(scriptVariantOptions.keys.first { $0.hasPrefix("!") ? !RimeApi.get_option(session, String($0.dropFirst())) : RimeApi.get_option(session, $0) })
}
} // SquirrelOptionSwitcher

Expand Down Expand Up @@ -111,12 +110,9 @@ final class SquirrelConfig: NSObject {
private var colorSpaceName: String
var colorSpace: String {
get { colorSpaceName }
set {
let name: String = newValue.replacingOccurrences(of: "_", with: "")
if name != colorSpaceName, let (key, value) = Self.colorSpaceMap.first(where:{ $0.key.caseInsensitiveCompare(name) == .orderedSame }) {
colorSpaceName = key; colorSpaceObject = value
}
}
set { let name: String = newValue.replacingOccurrences(of: "_", with: "")
guard name != colorSpaceName, let (key, value) = Self.colorSpaceMap.first(where: { $0.key.caseInsensitiveCompare(name) == .orderedSame }) else { return }
(colorSpaceName, colorSpaceObject) = (key, value) }
}

override init() {
Expand Down Expand Up @@ -194,7 +190,7 @@ final class SquirrelConfig: NSObject {
}

func close() {
if isOpen && RimeApi.config_close(&config) {
if isOpen, RimeApi.config_close(&config) {
isOpen = false
}
baseConfig = nil
Expand Down Expand Up @@ -251,11 +247,11 @@ final class SquirrelConfig: NSObject {
return cachedValue
}
var value: Bool = false
if isOpen && RimeApi.config_get_bool(&config, option, &value) {
if isOpen, RimeApi.config_get_bool(&config, option, &value) {
cache[option] = value
return value
}
if let aliasOption = option.replaceLastPathComponent(with: alias), isOpen && RimeApi.config_get_bool(&config, aliasOption, &value) {
if let aliasOption = option.replaceLastPathComponent(with: alias), isOpen, RimeApi.config_get_bool(&config, aliasOption, &value) {
cache[option] = value
return value
}
Expand All @@ -267,11 +263,11 @@ final class SquirrelConfig: NSObject {
return cachedValue
}
var value: CInt = 0
if isOpen && RimeApi.config_get_int(&config, option, &value) {
if isOpen, RimeApi.config_get_int(&config, option, &value) {
cache[option] = Int(value)
return Int(value)
}
if let aliasOption = option.replaceLastPathComponent(with: alias), isOpen && RimeApi.config_get_int(&config, aliasOption, &value) {
if let aliasOption = option.replaceLastPathComponent(with: alias), isOpen, RimeApi.config_get_int(&config, aliasOption, &value) {
cache[option] = Int(value)
return Int(value)
}
Expand All @@ -283,11 +279,11 @@ final class SquirrelConfig: NSObject {
return cachedValue
}
var value: Double = 0
if isOpen && RimeApi.config_get_double(&config, option, &value) {
if isOpen, RimeApi.config_get_double(&config, option, &value) {
cache[option] = value
return value
}
if let aliasOption = option.replaceLastPathComponent(with: alias), isOpen && RimeApi.config_get_double(&config, aliasOption, &value) {
if let aliasOption = option.replaceLastPathComponent(with: alias), isOpen, RimeApi.config_get_double(&config, aliasOption, &value) {
cache[option] = value
return value
}
Expand Down Expand Up @@ -394,13 +390,9 @@ final class SquirrelConfig: NSObject {
}

func optionSwitcherForSchema() -> SquirrelOptionSwitcher {
guard let schemaId = schemaId, !schemaId.isEmpty && schemaId != "." else {
return SquirrelOptionSwitcher()
}
guard let schemaId = schemaId, !schemaId.isEmpty, schemaId != "." else { return SquirrelOptionSwitcher() }
var switchIter = RimeConfigIterator()
guard RimeApi.config_begin_list(&switchIter, &config, "switches") else {
return SquirrelOptionSwitcher(schemaId: schemaId)
}
guard RimeApi.config_begin_list(&switchIter, &config, "switches") else { return SquirrelOptionSwitcher(schemaId: schemaId) }
var switcher: [String : String] = [:]
var optionGroups: [String : Set<String>] = [:]
var defaultScriptVariant: String?
Expand All @@ -412,7 +404,7 @@ final class SquirrelConfig: NSObject {
switcher[name] = reset != 0 ? name : "!" + name
optionGroups[name] = [name]
}
if defaultScriptVariant == nil && (name.caseInsensitiveCompare("simplification") == .orderedSame || name.caseInsensitiveCompare("simplified") == .orderedSame || name.caseInsensitiveCompare("traditional") == .orderedSame) {
if defaultScriptVariant == nil, name.caseInsensitiveCompare("simplification") == .orderedSame || name.caseInsensitiveCompare("simplified") == .orderedSame || name.caseInsensitiveCompare("traditional") == .orderedSame {
defaultScriptVariant = reset != 0 ? name : "!" + name
scriptVariantOptions[name] = Self.code(scriptVariant: name)
scriptVariantOptions["!" + name] = Self.code(scriptVariant: "!" + name)
Expand All @@ -433,7 +425,7 @@ final class SquirrelConfig: NSObject {
if hasStyleSection {
optGroup.forEach { switcher[$0] = optGroup[reset]; optionGroups[$0] = Set(optGroup) }
}
if defaultScriptVariant == nil && hasScriptVariant {
if defaultScriptVariant == nil, hasScriptVariant {
optGroup.forEach { scriptVariantOptions[$0] = Self.code(scriptVariant: $0) }
defaultScriptVariant = scriptVariantOptions[optGroup[reset]]
}
Expand Down Expand Up @@ -480,7 +472,7 @@ final class SquirrelConfig: NSObject {
guard let hexCode = hexCode, hexCode.count == 8 || hexCode.count == 10, hexCode.hasPrefix("0x") || hexCode.hasPrefix("0X") else { return nil }
let hexScanner = Scanner(string: hexCode)
var hex: UInt64 = 0x0
guard hexScanner.scanHexInt64(&hex) && hexScanner.isAtEnd else { return nil }
guard hexScanner.scanHexInt64(&hex), hexScanner.isAtEnd else { return nil }
let r = CGFloat(hex % 0x100)
let g = CGFloat(hex / 0x100 % 0x100)
let b = CGFloat(hex / 0x10000 % 0x100)
Expand Down
Loading

0 comments on commit ef44c4b

Please sign in to comment.