Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions sources/Main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,37 @@ struct SquirrelApp {
case "--sync":
DistributedNotificationCenter.default().postNotificationName(.init("SquirrelSyncNotification"), object: nil)
return true
case "--ascii":
DistributedNotificationCenter.default().postNotificationName(.init("SquirrelToggleASCIIModeNotification"), object: "ascii")
return true
case "--nascii":
DistributedNotificationCenter.default().postNotificationName(.init("SquirrelToggleASCIIModeNotification"), object: "nascii")
return true
case "--getascii":
var responseReceived = false
var asciiStatus = ""
let observer = DistributedNotificationCenter.default().addObserver(
forName: .init("SquirrelASCIIModeResponse"),
object: nil,
queue: .main
) { notification in
if let status = notification.object as? String {
asciiStatus = status
responseReceived = true
}
}
DistributedNotificationCenter.default().postNotificationName(.init("SquirrelGetASCIIModeNotification"), object: nil)
let timeout = Date().addingTimeInterval(2.0)
while !responseReceived && Date() < timeout {
RunLoop.current.run(until: Date().addingTimeInterval(0.01))
}
DistributedNotificationCenter.default().removeObserver(observer)
if responseReceived {
print(asciiStatus)
} else {
print("nascii")
}
return true
case "--help":
print(helpDoc)
return true
Expand Down Expand Up @@ -139,6 +170,9 @@ Perform actions:
--reload deploy
--sync sync user data
--build build all schemas in current directory
--ascii turn on ASCII mode
--nascii turn off ASCII mode
--getascii get current ASCII mode status
Install Squirrel:
--install, --register-input-source register input source
--enable-input-source [source id...] input source list optional
Expand Down
17 changes: 17 additions & 0 deletions sources/SquirrelApplicationDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@
let notifCenter = DistributedNotificationCenter.default()
notifCenter.addObserver(forName: .init("SquirrelReloadNotification"), object: nil, queue: nil, using: rimeNeedsReload)
notifCenter.addObserver(forName: .init("SquirrelSyncNotification"), object: nil, queue: nil, using: rimeNeedsSync)
notifCenter.addObserver(forName: .init("SquirrelToggleASCIIModeNotification"), object: nil, queue: nil, using: rimeToggleASCIIMode)
notifCenter.addObserver(forName: .init("SquirrelGetASCIIModeNotification"), object: nil, queue: nil, using: rimeGetASCIIMode)
}

func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
Expand Down Expand Up @@ -307,6 +309,21 @@
self.syncUserData()
}

func rimeToggleASCIIMode(_ notification: Notification) {
guard let mode = notification.object as? String else { return }
let enableASCII = mode == "ascii"

Check warning on line 315 in sources/SquirrelApplicationDelegate.swift

View workflow job for this annotation

GitHub Actions / build

Lines should not have trailing whitespace (trailing_whitespace)
if enableASCII {
NotificationCenter.default.post(name: .init("SquirrelSetASCIIModeNotification"), object: true)
} else {
NotificationCenter.default.post(name: .init("SquirrelSetASCIIModeNotification"), object: false)
}
}

func rimeGetASCIIMode(_: Notification) {
NotificationCenter.default.post(name: .init("SquirrelReportASCIIModeNotification"), object: nil)
}

func createDirIfNotExist(path: URL) {
let fileManager = FileManager.default
if !fileManager.fileExists(atPath: path.path()) {
Expand Down
47 changes: 47 additions & 0 deletions sources/SquirrelInputController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,26 @@
// print("[DEBUG] initWithServer: \(server ?? .init()) delegate: \(delegate ?? "nil") client:\(client ?? "nil")")
super.init(server: server, delegate: delegate, client: client)
createSession()

Check warning on line 192 in sources/SquirrelInputController.swift

View workflow job for this annotation

GitHub Actions / build

Lines should not have trailing whitespace (trailing_whitespace)
// Listen for ASCII mode toggle notifications
NotificationCenter.default.addObserver(
forName: .init("SquirrelSetASCIIModeNotification"),
object: nil,
queue: nil
) { [weak self] notification in
self?.handleASCIIModeToggle(notification)
}

Check warning on line 201 in sources/SquirrelInputController.swift

View workflow job for this annotation

GitHub Actions / build

Lines should not have trailing whitespace (trailing_whitespace)
// Listen for ASCII mode status requests
NotificationCenter.default.addObserver(
forName: .init("SquirrelReportASCIIModeNotification"),
object: nil,
queue: nil
) { [weak self] notification in
self?.reportASCIIMode(notification)
}

Check warning on line 210 in sources/SquirrelInputController.swift

View workflow job for this annotation

GitHub Actions / build

Lines should not have trailing whitespace (trailing_whitespace)

}

override func deactivateServer(_ sender: Any!) {
Expand Down Expand Up @@ -589,4 +609,31 @@
highlighted: highlighted, page: page, lastPage: lastPage, update: true)
}
}

private func handleASCIIModeToggle(_ notification: Notification) {
guard let enableASCII = notification.object as? Bool else { return }
guard session != 0 && rimeAPI.find_session(session) else { return }

Check warning on line 616 in sources/SquirrelInputController.swift

View workflow job for this annotation

GitHub Actions / build

Lines should not have trailing whitespace (trailing_whitespace)
rimeAPI.set_option(session, "ascii_mode", enableASCII)

Check warning on line 618 in sources/SquirrelInputController.swift

View workflow job for this annotation

GitHub Actions / build

Lines should not have trailing whitespace (trailing_whitespace)
// Force update the UI to reflect the mode change
rimeUpdate()
}

Check warning on line 622 in sources/SquirrelInputController.swift

View workflow job for this annotation

GitHub Actions / build

Lines should not have trailing whitespace (trailing_whitespace)
private func reportASCIIMode(_: Notification) {
// Only active input controller should respond
guard let client = client else { return }
guard session != 0 && rimeAPI.find_session(session) else { return }

Check warning on line 627 in sources/SquirrelInputController.swift

View workflow job for this annotation

GitHub Actions / build

Lines should not have trailing whitespace (trailing_whitespace)
let isASCIIMode = rimeAPI.get_option(session, "ascii_mode")
let status = isASCIIMode ? "ascii" : "nascii"

Check warning on line 630 in sources/SquirrelInputController.swift

View workflow job for this annotation

GitHub Actions / build

Lines should not have trailing whitespace (trailing_whitespace)
// Directly respond with the status
DistributedNotificationCenter.default().postNotificationName(
.init("SquirrelASCIIModeResponse"),

Check warning on line 633 in sources/SquirrelInputController.swift

View workflow job for this annotation

GitHub Actions / build

Lines should not have trailing whitespace (trailing_whitespace)
object: status
)
}


}