From 3f3b4c2e32ee4d0945b3499fb1a126a2a8bdf830 Mon Sep 17 00:00:00 2001 From: Bing ZHEUNG Date: Wed, 31 Jan 2024 13:14:12 +0800 Subject: [PATCH] Attempt to fix crashing on launch --- TypeDuck/EventHandler.swift | 2 +- TypeDuck/Info.plist | 2 +- TypeDuck/TypeDuck.entitlements | 2 ++ TypeDuck/TypeDuckApp.swift | 8 +++++--- TypeDuck/TypeDuckInputController.swift | 9 +++++---- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/TypeDuck/EventHandler.swift b/TypeDuck/EventHandler.swift index 2d24aba..1eedb31 100644 --- a/TypeDuck/EventHandler.swift +++ b/TypeDuck/EventHandler.swift @@ -14,7 +14,7 @@ extension TypeDuckInputController { let modifiers = event.modifierFlags let shouldIgnoreCurrentEvent: Bool = modifiers.contains(.command) || modifiers.contains(.option) guard !shouldIgnoreCurrentEvent else { return false } - let client: IMKTextInput? = (sender as? IMKTextInput) ?? currentClient + let client: InputClient? = (sender as? InputClient) ?? currentClient currentOrigin = client?.position let currentClientID = currentClient?.uniqueClientIdentifierString() let clientID = client?.uniqueClientIdentifierString() diff --git a/TypeDuck/Info.plist b/TypeDuck/Info.plist index f58a26e..5250527 100644 --- a/TypeDuck/Info.plist +++ b/TypeDuck/Info.plist @@ -43,7 +43,7 @@ ITSAppUsesNonExemptEncryption InputMethodConnectionName - hk_eduhk_inputmethod_TypeDuck_Connection + $(PRODUCT_BUNDLE_IDENTIFIER)_Connection InputMethodServerControllerClass $(PRODUCT_MODULE_NAME).TypeDuckInputController InputMethodServerDelegateClass diff --git a/TypeDuck/TypeDuck.entitlements b/TypeDuck/TypeDuck.entitlements index 852fa1a..111bbb5 100644 --- a/TypeDuck/TypeDuck.entitlements +++ b/TypeDuck/TypeDuck.entitlements @@ -4,5 +4,7 @@ com.apple.security.app-sandbox + com.apple.security.temporary-exception.mach-register.global-name + $(PRODUCT_BUNDLE_IDENTIFIER)_Connection diff --git a/TypeDuck/TypeDuckApp.swift b/TypeDuck/TypeDuckApp.swift index 5c325cd..ab90698 100644 --- a/TypeDuck/TypeDuckApp.swift +++ b/TypeDuck/TypeDuckApp.swift @@ -1,6 +1,7 @@ import AppKit import InputMethodKit +@objc(PrincipalApplication) final class PrincipalApplication: NSApplication { private let appDelegate = AppDelegate() @@ -16,15 +17,16 @@ final class PrincipalApplication: NSApplication { } @main +@objc(AppDelegate) final class AppDelegate: NSObject, NSApplicationDelegate { - static var server: IMKServer? = nil + var server: IMKServer? = nil func applicationDidFinishLaunching(_ notification: Notification) { handleCommandLineArguments() - let name: String = (Bundle.main.infoDictionary?["InputMethodConnectionName"] as? String) ?? "hk_eduhk_inputmethod_TypeDuck_Connection" + let name: String = (Bundle.main.infoDictionary?["InputMethodConnectionName"] as? String) ?? "hk.eduhk.inputmethod.TypeDuck_Connection" let identifier: String = Bundle.main.bundleIdentifier ?? "hk.eduhk.inputmethod.TypeDuck" - Self.server = IMKServer(name: name, bundleIdentifier: identifier) + server = IMKServer(name: name, bundleIdentifier: identifier) } private func handleCommandLineArguments() { diff --git a/TypeDuck/TypeDuckInputController.swift b/TypeDuck/TypeDuckInputController.swift index 85adc73..fd136b6 100644 --- a/TypeDuck/TypeDuckInputController.swift +++ b/TypeDuck/TypeDuckInputController.swift @@ -97,7 +97,9 @@ final class TypeDuckInputController: IMKInputController { private lazy var screenWidth: CGFloat = NSScreen.main?.frame.size.width ?? 1920 lazy var currentOrigin: CGPoint? = nil - lazy var currentClient: IMKTextInput? = nil { + + typealias InputClient = (IMKTextInput & NSObjectProtocol) + lazy var currentClient: InputClient? = nil { didSet { guard let origin = currentClient?.position else { return } let isRegularHorizontal: Bool = origin.x < (screenWidth - 400) @@ -128,8 +130,7 @@ final class TypeDuckInputController: IMKInputController { } override init!(server: IMKServer!, delegate: Any!, client inputClient: Any!) { super.init(server: server, delegate: delegate, client: inputClient) - let parameterInputClient = inputClient as? (IMKTextInput & NSObjectProtocol) - let currentInputClient = parameterInputClient ?? client() + let currentInputClient = (inputClient as? InputClient) ?? client() activateServer(currentInputClient) } override func activateServer(_ sender: Any!) { @@ -140,7 +141,7 @@ final class TypeDuckInputController: IMKInputController { if inputStage.isBuffering { clearBufferText() } - currentClient = sender as? IMKTextInput + currentClient = sender as? InputClient currentOrigin = currentClient?.position prepareMasterWindow() if appContext.inputForm.isOptions {