Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
LEOYoon-Tsaw committed May 26, 2024
1 parent b259fb5 commit 73a5bd6
Show file tree
Hide file tree
Showing 10 changed files with 395 additions and 418 deletions.
2 changes: 1 addition & 1 deletion sources/BridgingFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension DataSizeable {
value.data_size = Int32(MemoryLayout<Self>.size - offset)
return value
}

mutating func setCString(_ swiftString: String, to keypath: WritableKeyPath<Self, UnsafePointer<CChar>?>) {
swiftString.withCString { cStr in
// Duplicate the string to create a persisting C string
Expand Down
16 changes: 8 additions & 8 deletions sources/InputSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class SquirrelInstaller {
}
return inputSources
}()

func enabledModes() -> [InputMode] {
var enabledModes = Set<InputMode>()
for (mode, inputSource) in getInputSource(modes: InputMode.allCases) {
Expand All @@ -39,7 +39,7 @@ final class SquirrelInstaller {
}
return Array(enabledModes)
}

func register() {
let enabledInputModes = enabledModes()
if !enabledInputModes.isEmpty {
Expand All @@ -50,7 +50,7 @@ final class SquirrelInstaller {
TISRegisterInputSource(SquirrelApp.appDir as CFURL)
print("Registered input source from \(SquirrelApp.appDir)")
}

func enable(modes: [InputMode] = []) {
let enabledInputModes = enabledModes()
if !enabledInputModes.isEmpty && modes.isEmpty {
Expand All @@ -62,11 +62,11 @@ final class SquirrelInstaller {
for (mode, inputSource) in getInputSource(modes: modesToEnable) {
if let enabled = getBool(for: inputSource, key: kTISPropertyInputSourceIsEnabled), !enabled {
let error = TISEnableInputSource(inputSource)
print("Enable \(error == noErr ? "succeeds" : "fails") for input source: \(mode.rawValue)");
print("Enable \(error == noErr ? "succeeds" : "fails") for input source: \(mode.rawValue)")
}
}
}

func select(mode: InputMode? = nil) {
let enabledInputModes = enabledModes()
let modeToSelect = mode ?? .primary
Expand All @@ -90,7 +90,7 @@ final class SquirrelInstaller {
}
}
}

func disable(modes: [InputMode] = []) {
let modesToDisable = modes.isEmpty ? InputMode.allCases : modes
for (mode, inputSource) in getInputSource(modes: modesToDisable) {
Expand All @@ -100,7 +100,7 @@ final class SquirrelInstaller {
}
}
}

private func getInputSource(modes: [InputMode]) -> [InputMode: TISInputSource] {
var matchingSources = [InputMode: TISInputSource]()
for mode in modes {
Expand All @@ -110,7 +110,7 @@ final class SquirrelInstaller {
}
return matchingSources
}

private func getBool(for inputSource: TISInputSource, key: CFString!) -> Bool? {
let enabledRef = TISGetInputSourceProperty(inputSource, key)
guard let enabled = unsafeBitCast(enabledRef, to: CFBoolean?.self) else { return nil }
Expand Down
26 changes: 13 additions & 13 deletions sources/MacOSKeyCodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Carbon
import AppKit

struct SquirrelKeycode {

static func osxModifiersToRime(modifiers: NSEvent.ModifierFlags) -> UInt32 {
var ret: UInt32 = 0
if modifiers.contains(.capsLock) {
Expand All @@ -29,19 +29,19 @@ struct SquirrelKeycode {
}
return ret
}

static func osxKeycodeToRime(keycode: UInt16, keychar: Character?, shift: Bool, caps: Bool) -> UInt32 {
if let code = keycodeMappings[Int(keycode)] {
return UInt32(code)
}

if let keychar = keychar, keychar.isASCII, let codeValue = keychar.unicodeScalars.first?.value {
// NOTE: IBus/Rime use different keycodes for uppercase/lowercase letters.
if keychar.isLowercase && (shift || caps) {
// lowercase -> Uppercase
return keychar.uppercased().unicodeScalars.first!.value
}

switch codeValue {
case 0x20...0x7e:
return codeValue
Expand All @@ -57,11 +57,11 @@ struct SquirrelKeycode {
break
}
}

return UInt32(XK_VoidSymbol)
}
private static let keycodeMappings: Dictionary<Int, Int32> = [

private static let keycodeMappings: [Int: Int32] = [
// modifiers
kVK_CapsLock: XK_Caps_Lock,
kVK_Command: XK_Super_L, // XK_Meta_L?
Expand All @@ -73,7 +73,7 @@ struct SquirrelKeycode {
kVK_RightOption: XK_Alt_R,
kVK_Shift: XK_Shift_L,
kVK_RightShift: XK_Shift_R,

// special
kVK_Delete: XK_BackSpace,
kVK_Escape: XK_Escape,
Expand All @@ -82,7 +82,7 @@ struct SquirrelKeycode {
kVK_Return: XK_Return,
kVK_Space: XK_space,
kVK_Tab: XK_Tab,

// function
kVK_F1: XK_F1,
kVK_F2: XK_F2,
Expand All @@ -104,7 +104,7 @@ struct SquirrelKeycode {
kVK_F18: XK_F18,
kVK_F19: XK_F19,
kVK_F20: XK_F20,

// cursor
kVK_UpArrow: XK_Up,
kVK_DownArrow: XK_Down,
Expand All @@ -114,7 +114,7 @@ struct SquirrelKeycode {
kVK_PageDown: XK_Page_Down,
kVK_Home: XK_Home,
kVK_End: XK_End,

// keypad
kVK_ANSI_Keypad0: XK_KP_0,
kVK_ANSI_Keypad1: XK_KP_1,
Expand All @@ -134,13 +134,13 @@ struct SquirrelKeycode {
kVK_ANSI_KeypadPlus: XK_KP_Add,
kVK_ANSI_KeypadDivide: XK_KP_Divide,
kVK_ANSI_KeypadEnter: XK_KP_Enter,

// other
kVK_ISO_Section: XK_section,
kVK_JIS_Yen: XK_yen,
kVK_JIS_Underscore: XK_underscore,
kVK_JIS_KeypadComma: XK_comma,
kVK_JIS_Eisu: XK_Eisu_Shift,
kVK_JIS_Kana: XK_Kana_Shift,
kVK_JIS_Kana: XK_Kana_Shift
]
}
16 changes: 8 additions & 8 deletions sources/Main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ struct SquirrelApp {
static let appDir = "/Library/Input Library/Squirrel.app".withCString { dir in
URL(fileURLWithFileSystemRepresentation: dir, isDirectory: false, relativeTo: nil)
}

static func main() {
let rimeAPI: RimeApi_stdbool = rime_get_api_stdbool().pointee

let handled = autoreleasepool {
let installer = SquirrelInstaller()
let args = CommandLine.arguments
Expand Down Expand Up @@ -67,7 +67,7 @@ struct SquirrelApp {
return true
case "--build":
// Notification
SquirrelApplicationDelegate.showMessage(msgText: NSLocalizedString("deploy_update", comment: ""), msgId: "deploy")
SquirrelApplicationDelegate.showMessage(msgText: NSLocalizedString("deploy_update", comment: ""))
// Build all schemas in current directory
var builderTraits = RimeTraits.rimeStructInit()
builderTraits.setCString("rime.squirrel-builder", to: \.app_name)
Expand All @@ -90,7 +90,7 @@ struct SquirrelApp {
if handled {
return
}

autoreleasepool {
// find the bundle identifier and then initialize the input method server
let main = Bundle.main
Expand All @@ -102,10 +102,10 @@ struct SquirrelApp {
let delegate = SquirrelApplicationDelegate()
app.delegate = delegate
app.setActivationPolicy(.accessory)

// opencc will be configured with relative dictionary paths
FileManager.default.changeCurrentDirectoryPath(main.sharedSupportPath!)

if NSApp.squirrelAppDelegate.problematicLaunchDetected() {
print("Problematic launch detected!")
let args = ["Problematic launch detected! Squirrel may be suffering a crash due to improper configuration. Revert previous modifications to see if the problem recurs."]
Expand All @@ -121,15 +121,15 @@ struct SquirrelApp {
NSApp.squirrelAppDelegate.loadSettings()
print("Squirrel reporting!")
}

// finally run everything
app.run()
print("Squirrel is quitting...")
rimeAPI.finalize()
}
return
}

static let helpDoc = """
Supported arguments:
Perform actions:
Expand Down
Loading

0 comments on commit 73a5bd6

Please sign in to comment.