Skip to content

Commit

Permalink
Attempt to fix crashing on launch
Browse files Browse the repository at this point in the history
  • Loading branch information
bingzheung committed Jan 31, 2024
1 parent f59a111 commit 3f3b4c2
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion TypeDuck/EventHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion TypeDuck/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>InputMethodConnectionName</key>
<string>hk_eduhk_inputmethod_TypeDuck_Connection</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)_Connection</string>
<key>InputMethodServerControllerClass</key>
<string>$(PRODUCT_MODULE_NAME).TypeDuckInputController</string>
<key>InputMethodServerDelegateClass</key>
Expand Down
2 changes: 2 additions & 0 deletions TypeDuck/TypeDuck.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.temporary-exception.mach-register.global-name</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)_Connection</string>
</dict>
</plist>
8 changes: 5 additions & 3 deletions TypeDuck/TypeDuckApp.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import AppKit
import InputMethodKit

@objc(PrincipalApplication)
final class PrincipalApplication: NSApplication {

private let appDelegate = AppDelegate()
Expand All @@ -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() {
Expand Down
9 changes: 5 additions & 4 deletions TypeDuck/TypeDuckInputController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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!) {
Expand All @@ -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 {
Expand Down

0 comments on commit 3f3b4c2

Please sign in to comment.