Skip to content

Commit

Permalink
fix: 优化安装体验 (#57)
Browse files Browse the repository at this point in the history
* feat: 统计输入

* feat: 关闭首选项窗口之后,释放内存

* feat: 添加清除统计数据的能力,可选择启用是否启用数据统计

* fix: 移除sqlite.swift依赖

* fix: 修复统计设置不生效的问题

* feat: 添加数据库迁移能力

* fix: 移除过时的依赖项

* feat: 使用sqlcipher加密数据库

* fix: 优化安装流程

* fix: 修复安装不结束的问题

Co-authored-by: qwertyyb <[email protected]>
  • Loading branch information
qwertyyb and qwertyyb authored May 28, 2022
1 parent b01d305 commit f356352
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 16 deletions.
23 changes: 19 additions & 4 deletions Fire/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,43 @@ class AppDelegate: NSObject, NSApplicationDelegate {

func installInputSource() {
print("install input source")
InputSource.shared.deactivateInputSource()
InputSource.shared.registerInputSource()
InputSource.shared.activateInputSource()
}

func stop() {
InputSource.shared.deactivateInputSource()
NSApp.terminate(nil)
}

private func commandHandler() {
private func commandHandler() -> Bool {
if CommandLine.arguments.count > 1 {
print("[Fire] launch argument: \(CommandLine.arguments[1])")
let command = CommandLine.arguments[1]
if command == "--install" {
return installInputSource()
installInputSource()
return false
}
if command == "--build-dict" {
print("[Fire] build dict")
buildDict()
return NSApp.terminate(nil)
NSApp.terminate(nil)
return false
}
if command == "--stop" {
print("[Fire] stop")
stop()
return false
}
}
return true
}

func applicationDidFinishLaunching(_ aNotification: Notification) {
commandHandler()
if !commandHandler() {
return
}
if !hasDict() {
NSLog("[Fire] first run,build dict")
buildDict()
Expand Down
58 changes: 48 additions & 10 deletions Fire/InputSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
//

import Carbon
import AppKit

enum InputSourceUsage {
case enable
case selected
}

class InputSource {
let installLocation = "/Library/Input Methods/Fire.app"
Expand All @@ -29,38 +35,70 @@ class InputSource {
return nil
}

private func findInputSource() -> (inputSource: TISInputSource, sourceID: NSString)? {
private func findInputSource(forUsage: InputSourceUsage = .enable) -> (inputSource: TISInputSource, sourceID: NSString)? {
let sourceList = TISCreateInputSourceList(nil, true).takeUnretainedValue()

for index in 0...CFArrayGetCount(sourceList)-1 {
let inputSource = Unmanaged<TISInputSource>.fromOpaque(CFArrayGetValueAtIndex(
sourceList, index)).takeUnretainedValue()
if let result = transformTargetSource(inputSource) {
return result
let selectable = CFBooleanGetValue(Unmanaged<CFBoolean>.fromOpaque(
TISGetInputSourceProperty(result.inputSource, kTISPropertyInputSourceIsSelectCapable)
).takeUnretainedValue())
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
}
if forUsage == .selected && selectable {
return result
}
if selectable {
return result
}
}
}
return nil
}

func selectInputSource() {
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in
guard let result = self.findInputSource(forUsage: .selected) else {
return
}
TISSelectInputSource(result.inputSource)
let isSelected = CFBooleanGetValue(Unmanaged<CFBoolean>.fromOpaque(
TISGetInputSourceProperty(result.inputSource, kTISPropertyInputSourceIsSelected)
).takeUnretainedValue())
NSLog("Selected input source: %@, selected: \(isSelected)", result.sourceID)
if isSelected {
timer.invalidate()
NSApp.terminate(nil)
}
}
}

func activateInputSource() {
guard let result = findInputSource() else {
return
}
TISEnableInputSource(result.inputSource)
NSLog("Enabled input source: %@", result.sourceID)
let isSelectable = Unmanaged<CFBoolean>.fromOpaque(
TISGetInputSourceProperty(result.inputSource, kTISPropertyInputSourceIsSelectCapable)
).takeUnretainedValue()
if CFBooleanGetValue(isSelectable) {
TISSelectInputSource(result.inputSource)
NSLog("Selected input source: %@", result.sourceID)
let enabled = CFBooleanGetValue(Unmanaged<CFBoolean>.fromOpaque(
TISGetInputSourceProperty(result.inputSource, kTISPropertyInputSourceIsEnabled)
).takeUnretainedValue())
if !enabled {
TISEnableInputSource(result.inputSource)
NSLog("Enabled input source: %@", result.sourceID)
}
selectInputSource()
}

func deactivateInputSource() {
guard let source = findInputSource() else {
return
}
TISDeselectInputSource(source.inputSource)
TISDisableInputSource(source.inputSource)
NSLog("Disable input source: %@", source.sourceID)
}
Expand Down
2 changes: 0 additions & 2 deletions package/scripts/postinstall
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ TARGET='Fire'
login_user=`/usr/bin/stat -f%Su /dev/console`

/usr/bin/sudo -u "${login_user}" pkill -9 "${TARGET}"
/usr/bin/sudo -u "${login_user}" rm -rf "/Library/Input\ Methods/${TARGET}.app"


/usr/bin/sudo -u "${login_user}" "/Library/Input Methods/${TARGET}.app/Contents/MacOS/${TARGET}" --build-dict

Expand Down

0 comments on commit f356352

Please sign in to comment.