Skip to content

Commit

Permalink
fix: 修复安装后不可选中的问题,修复安装时可能卡死的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
qwertyyb committed Apr 28, 2023
1 parent a215bba commit c5970f2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
7 changes: 5 additions & 2 deletions Fire/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ class AppDelegate: NSObject, NSApplicationDelegate {

var fire: Fire!
var statistics: Statistics!
let statusBar = StatusBar.shared
var statusBar: StatusBar!

func installInputSource() {
print("install input source")
// InputSource.shared.deactivateInputSource()
InputSource.shared.registerInputSource()
InputSource.shared.activateInputSource()
InputSource.shared.selectInputSource { _ in
NSApp.terminate(self)
}
}

func stop() {
Expand Down Expand Up @@ -62,6 +64,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
NSLog("[Fire] app is running")
fire = Fire.shared
statistics = Statistics.shared
statusBar = StatusBar.shared
}

func applicationWillTerminate(_ aNotification: Notification) {
Expand Down
49 changes: 42 additions & 7 deletions Fire/InputSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ class InputSource {
let kInputModeID = "com.qwertyyb.inputmethod.Fire"

func registerInputSource() {
let installedLocationURL = NSURL(fileURLWithPath: installLocation)
TISRegisterInputSource(installedLocationURL as CFURL)
NSLog("register input source")
if !isEnabled() {
// 全新安装或未启用过,需要Register, 已启用的,不需要再次启用
let installedLocationURL = NSURL(fileURLWithPath: installLocation)
TISRegisterInputSource(installedLocationURL as CFURL)
NSLog("register input source")
}
}

private func transformTargetSource(_ inputSource: TISInputSource)
Expand All @@ -49,7 +52,6 @@ class InputSource {
let enableable = CFBooleanGetValue(Unmanaged<CFBoolean>.fromOpaque(
TISGetInputSourceProperty(result.inputSource, kTISPropertyInputSourceIsEnableCapable)
).takeUnretainedValue())
NSLog("find input source: %@, enableable: \(enableable), selectable: \(selectable)", result.sourceID)
if forUsage == .enable && enableable {
return result
}
Expand All @@ -64,8 +66,16 @@ class InputSource {
return nil
}

func selectInputSource() {
func selectInputSource(callback: @escaping (Bool) -> Void) {
let maxTryTimes = 30
var tryTimes = 0
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in
if tryTimes > maxTryTimes {
timer.invalidate()
callback(false)
return
}
tryTimes += 1
guard let result = self.findInputSource(forUsage: .selected) else {
return
}
Expand All @@ -76,7 +86,7 @@ class InputSource {
NSLog("Selected input source: %@, selected: \(isSelected)", result.sourceID)
if isSelected {
timer.invalidate()
NSApp.terminate(nil)
callback(true)
}
}
}
Expand All @@ -92,7 +102,6 @@ class InputSource {
TISEnableInputSource(result.inputSource)
NSLog("Enabled input source: %@", result.sourceID)
}
selectInputSource()
}

func deactivateInputSource() {
Expand All @@ -104,6 +113,19 @@ class InputSource {
NSLog("Disable input source: %@", source.sourceID)
}

func onSelectChanged(callback: @escaping (Bool) -> Void) -> NSObjectProtocol {
let observer = DistributedNotificationCenter.default()
.addObserver(
forName: .init(String(kTISNotifySelectedKeyboardInputSourceChanged)),
object: nil,
queue: nil,
using: { _ in
callback(self.isSelected())
}
)
return observer
}

func isSelected() -> Bool {
guard let result = findInputSource(forUsage: .selected) else {
return false
Expand All @@ -117,5 +139,18 @@ class InputSource {
return isSelected
}

func isEnabled() -> Bool {
guard let result = findInputSource(forUsage: .enable) else {
return false
}
let unsafeIsEnabled = TISGetInputSourceProperty(
result.inputSource,
kTISPropertyInputSourceIsEnabled
).assumingMemoryBound(to: CFBoolean.self)
let isEnabled = CFBooleanGetValue(Unmanaged<CFBoolean>.fromOpaque(unsafeIsEnabled).takeUnretainedValue())

return isEnabled
}

static let shared = InputSource()
}

0 comments on commit c5970f2

Please sign in to comment.