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
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,48 @@
* 選中「在線文檔」可打開以上網址
* 編輯用戶設定後,選擇「重新部署」以令修改生效

### 修飾鍵提示

此功能預設關閉。在 `~/Library/Rime/squirrel.custom.yaml` 中加入以下設定並重新部署;
若已有 `patch`,只需在其中加入 `"modifier_hint/show": true`。

```yaml
patch:
"modifier_hint/show": true
```

啓用後,按住 Control `⌃`、Option `⌥`、Shift `⇧` 或 Command `⌘` 時,
文字光標旁會顯示當前按住的組合,全部鬆開後隱藏。
提示整合於既有的中英文狀態提示視窗,使用候選字的字型與字號(`font_face`、`font_point`)。
有候選詞時,修飾鍵顯示於候選窗底部,不影響選詞;沒有候選詞時則顯示為狀態提示。

此功能只在鼠鬚管啓用期間生效,包括英文模式及無候選詞的情況。
應用若無法提供有效的文字光標位置,提示會隱藏;Caps Lock 和 Fn 不在提示範圍內。
設為 `false` 並重新部署即可關閉。

### 聚焦輸入框提示

此功能預設關閉。啓用後,聚焦輸入框且處於非英文模式時,光標旁會顯示當前輸入方案名稱。
切換回中文模式時,原有的「中」提示亦改為輸入方案名稱;英文提示保留原有標籤。
開始打字或超時後隱藏名稱;按住的修飾鍵仍可顯示。兩種提示與中英文切換訊息共用同一個狀態提示視窗、主題及候選字型。

在 `~/Library/Rime/squirrel.custom.yaml` 的 `patch` 中加入以下設定並重新部署:

```yaml
patch:
"focus_hint/show": true
"focus_hint/duration": 2
```

`duration` 是顯示秒數,預設為 2;可設為正數,例如 `1.5` 或 `3`。
設為 `"focus_hint/show": false` 可關閉。提示依賴應用提供輸入框啓用通知及有效光標位置。

提示採用事件驅動:修飾鍵隨按鍵事件更新,名稱超時使用一次性計時器,沒有週期輪詢。
名稱提示在打字、切換英文或失焦時取消;後續按鍵事件也會同步修飾鍵狀態。
兩項功能都關閉時不建立提示執行個體,不新增狀態或光標查詢、計時器及 UI 更新,僅保留快取開關判斷;
候選窗及中英文切換提示沿用原有流程。設定在重新部署時載入,關閉會取消現有名稱提示計時器。


安裝輸入方案
---

Expand Down
4 changes: 4 additions & 0 deletions Squirrel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
B39771252BED899F0093A49B /* SquirrelConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = B39771242BED899F0093A49B /* SquirrelConfig.swift */; };
B39771272BED9B250093A49B /* SquirrelTheme.swift in Sources */ = {isa = PBXBuildFile; fileRef = B39771262BED9B250093A49B /* SquirrelTheme.swift */; };
B39771292BEDAF4A0093A49B /* SquirrelView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B39771282BEDAF4A0093A49B /* SquirrelView.swift */; };
A13300032F260100009D156B /* SquirrelInputHint.swift in Sources */ = {isa = PBXBuildFile; fileRef = A13300042F260100009D156B /* SquirrelInputHint.swift */; };
B397712B2BEE4C160093A49B /* SquirrelPanel.swift in Sources */ = {isa = PBXBuildFile; fileRef = B397712A2BEE4C160093A49B /* SquirrelPanel.swift */; };
B397712D2BEEB39D0093A49B /* InputSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = B397712C2BEEB39D0093A49B /* InputSource.swift */; };
B397712F2BEECBED0093A49B /* Main.swift in Sources */ = {isa = PBXBuildFile; fileRef = B397712E2BEECBED0093A49B /* Main.swift */; };
Expand Down Expand Up @@ -308,6 +309,7 @@
B39771242BED899F0093A49B /* SquirrelConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = SquirrelConfig.swift; path = sources/SquirrelConfig.swift; sourceTree = "<group>"; };
B39771262BED9B250093A49B /* SquirrelTheme.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = SquirrelTheme.swift; path = sources/SquirrelTheme.swift; sourceTree = "<group>"; };
B39771282BEDAF4A0093A49B /* SquirrelView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = SquirrelView.swift; path = sources/SquirrelView.swift; sourceTree = "<group>"; };
A13300042F260100009D156B /* SquirrelInputHint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = SquirrelInputHint.swift; path = sources/SquirrelInputHint.swift; sourceTree = "<group>"; };
B397712A2BEE4C160093A49B /* SquirrelPanel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = SquirrelPanel.swift; path = sources/SquirrelPanel.swift; sourceTree = "<group>"; };
B397712C2BEEB39D0093A49B /* InputSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = InputSource.swift; path = sources/InputSource.swift; sourceTree = "<group>"; };
B397712E2BEECBED0093A49B /* Main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Main.swift; path = sources/Main.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -344,6 +346,7 @@
B397712E2BEECBED0093A49B /* Main.swift */,
B39771262BED9B250093A49B /* SquirrelTheme.swift */,
B397712A2BEE4C160093A49B /* SquirrelPanel.swift */,
A13300042F260100009D156B /* SquirrelInputHint.swift */,
B39771282BEDAF4A0093A49B /* SquirrelView.swift */,
B39771242BED899F0093A49B /* SquirrelConfig.swift */,
B35D2FE72BF00839009D156B /* BridgingFunctions.swift */,
Expand Down Expand Up @@ -614,6 +617,7 @@
B397712F2BEECBED0093A49B /* Main.swift in Sources */,
B397712D2BEEB39D0093A49B /* InputSource.swift in Sources */,
B397712B2BEE4C160093A49B /* SquirrelPanel.swift in Sources */,
A13300032F260100009D156B /* SquirrelInputHint.swift in Sources */,
B38E9B912BE9AE1E0036ABEF /* SquirrelApplicationDelegate.swift in Sources */,
B39771272BED9B250093A49B /* SquirrelTheme.swift in Sources */,
B35D2FE82BF00839009D156B /* BridgingFunctions.swift in Sources */,
Expand Down
9 changes: 9 additions & 0 deletions data/squirrel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ show_notifications_when: appropriate
status_icon:
show: true

# Show held Control, Option, Shift and Command keys beside the text caret.
modifier_hint:
show: false

# Show the active schema name on focus in non-ASCII mode; duration is in seconds.
focus_hint:
show: false
duration: 2

style:
color_scheme: native
# Optional: define both light and dark color schemes to match system appearance
Expand Down
27 changes: 25 additions & 2 deletions sources/SquirrelApplicationDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ final class SquirrelApplicationDelegate: NSObject, NSApplicationDelegate, SPUSta
let rimeAPI: RimeApi_stdbool = rime_get_api_stdbool().pointee
var config: SquirrelConfig?
var panel: SquirrelPanel?
private(set) var inputHintOptions: SquirrelInputHintOptions?
var enableNotifications = false
var showStatusIcon: Bool = true
var statusItem: NSStatusItem?
Expand Down Expand Up @@ -73,6 +74,7 @@ final class SquirrelApplicationDelegate: NSObject, NSApplicationDelegate, SPUSta
}

func updateStatusIcon(asciiMode: Bool, schemaLabel: String?) {
guard showStatusIcon else { return }
DispatchQueue.main.async { [weak self] in
self?.applyStatusIcon(asciiMode: asciiMode, schemaLabel: schemaLabel)
}
Expand Down Expand Up @@ -174,11 +176,20 @@ final class SquirrelApplicationDelegate: NSObject, NSApplicationDelegate, SPUSta

enableNotifications = config!.getString("show_notifications_when") != "never"
showStatusIcon = config!.getBool("status_icon/show") ?? true
let showModifiers = config!.getBool("modifier_hint/show") ?? false
let showFocus = config!.getBool("focus_hint/show") ?? false
if showModifiers || showFocus {
inputHintOptions = .init(showModifiers: showModifiers, showFocus: showFocus,
focusDuration: Double(config!.getDouble("focus_hint/duration") ?? 2))
} else {
inputHintOptions = nil
}
refreshStatusItem()
if let panel = panel, let config = self.config {
panel.load(config: config, forDarkMode: false)
panel.load(config: config, forDarkMode: true)
}
panel?.inputController?.reloadInputHintOptions()
}

func loadSettings(for schemaID: String) {
Expand Down Expand Up @@ -304,11 +315,23 @@ private func notificationHandler(contextObject: UnsafeMutableRawPointer?, sessio
let stateLabelLong = delegate.rimeAPI.get_state_label_abbreviated(sessionId, name, state, false)
return stateLabelLong.asString
}
if optionName == "ascii_mode" {
if optionName == "ascii_mode", delegate.showStatusIcon {
delegate.updateStatusIcon(asciiMode: state, schemaLabel: shortLabel())
}
if delegate.enableNotifications {
delegate.showStatusMessage(msgTextLong: longLabel(), msgTextShort: shortLabel())
var longMessage = longLabel()
var shortMessage = shortLabel()
if optionName == "ascii_mode", !state, delegate.inputHintOptions?.showFocus == true {
var status = RimeStatus_stdbool.rimeStructInit()
if delegate.rimeAPI.get_status(sessionId, &status) {
defer { _ = delegate.rimeAPI.free_status(&status) }
if let schemaName = status.schema_name.map({ String(cString: $0) }), !schemaName.isEmpty {
longMessage = schemaName
shortMessage = schemaName
}
}
}
delegate.showStatusMessage(msgTextLong: longMessage, msgTextShort: shortMessage)
}
}
}
Expand Down
84 changes: 82 additions & 2 deletions sources/SquirrelInputController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ final class SquirrelInputController: IMKInputController {
private var chordTimer: Timer?
private var chordDuration: TimeInterval = 0
private var currentApp: String = ""
private var inputHints: SquirrelInputHints?

// swiftlint:disable:next cyclomatic_complexity
override func handle(_ event: NSEvent!, client sender: Any!) -> Bool {
guard let event = event else { return false }
if let hints = inputHints, event.type == .keyDown { hints.dismissFocus() }
let modifiers = event.modifierFlags
let changes = lastModifiers.symmetricDifference(modifiers)

Expand All @@ -49,6 +51,7 @@ final class SquirrelInputController: IMKInputController {
currentApp = app
updateAppOptions()
}
defer { inputHints?.update(modifiers: modifiers) }

switch event.type {
case .flagsChanged:
Expand Down Expand Up @@ -183,13 +186,23 @@ final class SquirrelInputController: IMKInputController {
// session-wide hardware state; otherwise the next Caps Lock press can
// compare equal to the stale lastModifiers and be dropped by the
// early-return in handle().
if CGEventSource.flagsState(.combinedSessionState).contains(.maskAlphaShift) {
let hardwareFlags = CGEventSource.flagsState(.combinedSessionState)
if hardwareFlags.contains(.maskAlphaShift) {
lastModifiers.insert(.capsLock)
} else {
lastModifiers.remove(.capsLock)
}
preedit = ""
if session != 0 {
if NSApp.squirrelAppDelegate.inputHintOptions != nil {
let modifiers = hardwareModifiers(from: hardwareFlags)
if !reloadInputHintOptions(modifiers: modifiers) {
NSApp.squirrelAppDelegate.panel?.inputController = self
inputHints?.activate(modifiers: modifiers)
}
} else if inputHints != nil {
reloadInputHintOptions()
}
if session != 0, NSApp.squirrelAppDelegate.showStatusIcon {
let state = rimeAPI.get_option(session, "ascii_mode")
let label = rimeAPI.get_state_label_abbreviated(session, "ascii_mode", state, true).asString
NSApp.squirrelAppDelegate.updateStatusIcon(asciiMode: state, schemaLabel: label)
Expand Down Expand Up @@ -219,6 +232,14 @@ final class SquirrelInputController: IMKInputController {
}

override func deactivateServer(_ sender: Any!) {
if let hints = inputHints {
hints.deactivate()
inputHints = nil
if let panel = NSApp.squirrelAppDelegate.panel, panel.inputController === self {
panel.setInputHintsEnabled(false)
panel.inputController = nil
}
}
hidePalettes()
commitComposition(sender)
client = nil
Expand Down Expand Up @@ -305,13 +326,71 @@ final class SquirrelInputController: IMKInputController {
}
}

@discardableResult
func reloadInputHintOptions(modifiers: NSEvent.ModifierFlags? = nil) -> Bool {
let delegate = NSApp.squirrelAppDelegate
let options = delegate.inputHintOptions
guard options != inputHints?.options else { return false }
inputHints?.deactivate()
inputHints = nil
delegate.panel?.setInputHintsEnabled(false)
guard client != nil else { return true }
inputHints = SquirrelInputHints.create(options: options) { [weak self] in
.init(readFocusName: { [weak self] in self?.focusHintName() },
present: { [weak self] text in self?.presentInputHint(text) })
}
if let hints = inputHints {
delegate.panel?.inputController = self
delegate.panel?.setInputHintsEnabled(true)
hints.activate(modifiers: modifiers ?? lastModifiers)
}
return true
}

deinit {
destroySession()
}
}

private extension SquirrelInputController {

func hardwareModifiers(from flags: CGEventFlags) -> NSEvent.ModifierFlags {
var modifiers: NSEvent.ModifierFlags = []
if flags.contains(.maskControl) { modifiers.insert(.control) }
if flags.contains(.maskAlternate) { modifiers.insert(.option) }
if flags.contains(.maskShift) { modifiers.insert(.shift) }
if flags.contains(.maskCommand) { modifiers.insert(.command) }
return modifiers
}

func focusHintName() -> String? {
guard session != 0, rimeAPI.find_session(session) else { return nil }
var status = RimeStatus_stdbool.rimeStructInit()
guard rimeAPI.get_status(session, &status) else { return nil }
defer { _ = rimeAPI.free_status(&status) }
guard !status.is_ascii_mode else { return nil }
if let identifier = status.schema_id {
NSApp.squirrelAppDelegate.loadSettings(for: String(cString: identifier))
}
return status.schema_name.map { String(cString: $0) } ?? "鼠鬚管"
}

func presentInputHint(_ text: String) {
guard let client, let panel = NSApp.squirrelAppDelegate.panel, panel.inputController === self else { return }
if text.isEmpty {
panel.updateInputHint(text: "", position: panel.position)
return
}
var caret = NSRect.zero
client.attributes(forCharacterIndex: 0, lineHeightRectangle: &caret)
guard caret.height > 0, caret.origin.x.isFinite, caret.origin.y.isFinite,
caret.width.isFinite, caret.height.isFinite else {
panel.updateInputHint(text: "", position: panel.position)
return
}
panel.updateInputHint(text: text, position: caret)
}

func onChordTimer(_: Timer) {
var processedKeys = false
if chordKeyCount > 0 && session != 0 {
Expand Down Expand Up @@ -453,6 +532,7 @@ private extension SquirrelInputController {

var status = RimeStatus_stdbool.rimeStructInit()
if rimeAPI.get_status(session, &status) {
inputHints?.setAsciiMode(status.is_ascii_mode)
// swiftlint:disable:next identifier_name
if let schema_id = status.schema_id, schemaId == "" || schemaId != String(cString: schema_id) {
schemaId = String(cString: schema_id)
Expand Down
Loading