Skip to content

Commit

Permalink
Remove special handling of modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
LEOYoon-Tsaw committed May 18, 2024
1 parent ee185a1 commit 8c7d5d2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 32 deletions.
4 changes: 2 additions & 2 deletions sources/SquirrelApplicationDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ final class SquirrelApplicationDelegate: NSObject, NSApplicationDelegate, SPUSta

func syncUserData() {
print("Sync user data")
let _ = rimeAPI.sync_user_data()
_ = rimeAPI.sync_user_data()
}

func openLogFolder() {
Expand Down Expand Up @@ -153,7 +153,7 @@ final class SquirrelApplicationDelegate: NSObject, NSApplicationDelegate, SPUSta
if rimeAPI.start_maintenance(fullCheck) {
// update squirrel config
// print("[DEBUG] maintenance suceeds")
let _ = rimeAPI.deploy_config_file("squirrel.yaml", "config_version")
_ = rimeAPI.deploy_config_file("squirrel.yaml", "config_version")
} else {
// print("[DEBUG] maintenance fails")
}
Expand Down
4 changes: 2 additions & 2 deletions sources/SquirrelConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class SquirrelConfig {

func close() {
if isOpen {
let _ = rimeAPI.config_close(&config)
_ = rimeAPI.config_close(&config)
baseConfig = nil
isOpen = false
}
Expand Down Expand Up @@ -117,7 +117,7 @@ final class SquirrelConfig {
let rootKey = "app_options/\(appName)"
var appOptions = [String : Bool]()
var iterator = RimeConfigIterator()
let _ = rimeAPI.config_begin_map(&iterator, &config, rootKey)
_ = rimeAPI.config_begin_map(&iterator, &config, rootKey)
while rimeAPI.config_next(&iterator) {
// print("[DEBUG] option[\(iterator.index)]: \(String(cString: iterator.key)), path: (\(String(cString: iterator.path))")
if let key = iterator.key, let path = iterator.path, let value = getBool(String(cString: path)) {
Expand Down
29 changes: 7 additions & 22 deletions sources/SquirrelInputController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ final class SquirrelInputController: IMKInputController {
private var selRange: NSRange = NSMakeRange(NSNotFound, 0)
private var caretPos: Int = 0
private var lastModifiers: NSEvent.ModifierFlags = .init()
private var lastEventType: NSEvent.EventType = .init(rawValue: 0)!
private var lastModifiersChange: NSEvent.ModifierFlags = .init()
private var session: RimeSessionId = 0
private var schemaId: String = ""
private var inlinePreedit = false
Expand Down Expand Up @@ -61,35 +59,24 @@ final class SquirrelInputController: IMKInputController {
}
// print("[DEBUG] FLAGSCHANGED client: \(sender ?? "nil"), modifiers: \(modifiers)")
var rimeModifiers: UInt32 = SquirrelKeycode.osxModifiersToRime(modifiers: modifiers.rawValue)
let lastRimeModifiers: UInt32 = SquirrelKeycode.osxModifiersToRime(modifiers: lastModifiers.rawValue)
// For flags-changed event, keyCode is available since macOS 10.15
// (#715)
let rimeKeycode: UInt32 = SquirrelKeycode.osxKeycodeToRime(keycode: event.keyCode, keychar: nil, shift: false, caps: false)
let chording = rimeAPI.get_option(session, "_chord_typing")

if changes.contains(.capsLock) {
// NOTE: rime assumes XK_Caps_Lock to be sent before modifier changes,
// while NSFlagsChanged event has the flag changed already.
// so it is necessary to revert kLockMask.
rimeModifiers ^= kLockMask.rawValue
handled = processKey(rimeKeycode, modifiers: rimeModifiers)
_ = processKey(rimeKeycode, modifiers: rimeModifiers)
}
for flag in [NSEvent.ModifierFlags.shift, .control, .option, .command] {
if changes.contains(flag) {
// different behavier in chording
if chording {
let releaseMask = modifiers.contains(flag) ? 0 : kReleaseMask.rawValue
handled = processKey(rimeKeycode, modifiers: rimeModifiers | releaseMask)
} else {
// flag is released and the change equals last change, so no other event between pressing and releasing
if lastEventType == .flagsChanged && !modifiers.contains(flag) && changes == lastModifiersChange {
handled = processKey(rimeKeycode, modifiers: lastRimeModifiers)
}
}
let releaseMask = modifiers.contains(flag) ? 0 : kReleaseMask.rawValue
_ = processKey(rimeKeycode, modifiers: rimeModifiers | releaseMask)
}
}
lastModifiers = modifiers
lastModifiersChange = changes

rimeUpdate()

Expand Down Expand Up @@ -117,13 +104,11 @@ final class SquirrelInputController: IMKInputController {
rimeUpdate()
}
}
lastModifiersChange = .init()

default:
break
}

lastEventType = event.type
return handled
}

Expand Down Expand Up @@ -359,7 +344,7 @@ private extension SquirrelInputController {
func destroySession() {
// print("[DEBUG] destroySession:")
if session != 0 {
let _ = rimeAPI.destroy_session(session)
_ = rimeAPI.destroy_session(session)
session = 0
}
clearChord()
Expand Down Expand Up @@ -414,7 +399,7 @@ private extension SquirrelInputController {
if rimeAPI.get_commit(session, &commitText) {
if let text = commitText.text {
commit(string: String(cString: text))
let _ = rimeAPI.free_commit(&commitText)
_ = rimeAPI.free_commit(&commitText)
}
}
}
Expand All @@ -437,7 +422,7 @@ private extension SquirrelInputController {
rimeAPI.set_option(session, "soft_cursor", !inlinePreedit)
}
}
let _ = rimeAPI.free_status(&status)
_ = rimeAPI.free_status(&status)
}

var ctx = RimeContext.rimeStructInit()
Expand Down Expand Up @@ -499,7 +484,7 @@ private extension SquirrelInputController {
}
}
showPanel(preedit: inlinePreedit ? "" : preedit, selRange: NSRange(location: start.utf16Offset(in: preedit), length: preedit.utf16.distance(from: start, to: end)), caretPos: caretPos.utf16Offset(in: preedit), candidates: candidates, comments: comments, labels: labels, highlighted: Int(ctx.menu.highlighted_candidate_index))
let _ = rimeAPI.free_context(&ctx)
_ = rimeAPI.free_context(&ctx)
} else {
hidePalettes()
}
Expand Down
12 changes: 6 additions & 6 deletions sources/SquirrelPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ final class SquirrelPanel: NSPanel {
let (index, preeditIndex) = view.click(at: mousePosition())
if let preeditIndex = preeditIndex, preeditIndex >= 0 && preeditIndex < preedit.utf16.count {
if preeditIndex < caretPos {
let _ = inputController?.moveCaret(forward: true)
_ = inputController?.moveCaret(forward: true)
} else if preeditIndex > caretPos {
let _ = inputController?.moveCaret(forward: false)
_ = inputController?.moveCaret(forward: false)
}
}
if let index = index, index == self.index && index >= 0 && index < candidates.count {
let _ = inputController?.selectCandidate(index)
_ = inputController?.selectCandidate(index)
}
case .mouseEntered:
acceptsMouseMovedEvents = true
Expand All @@ -101,9 +101,9 @@ final class SquirrelPanel: NSPanel {
// Scrollboard span
} else if event.phase == .ended || (event.phase == .init(rawValue: 0) && event.momentumPhase != .init(rawValue: 0)) {
if abs(scrollDirection.dx) > abs(scrollDirection.dy) && abs(scrollDirection.dx) > 10 {
let _ = inputController?.page(up: (scrollDirection.dx < 0) == vertical)
_ = inputController?.page(up: (scrollDirection.dx < 0) == vertical)
} else if abs(scrollDirection.dx) < abs(scrollDirection.dy) && abs(scrollDirection.dy) > 10 {
let _ = inputController?.page(up: scrollDirection.dx > 0)
_ = inputController?.page(up: scrollDirection.dx > 0)
}
scrollDirection = .zero
// Mouse scroll wheel
Expand All @@ -118,7 +118,7 @@ final class SquirrelPanel: NSPanel {
scrollDirection = .zero
}
if abs(scrollDirection.dy) > 10 {
let _ = inputController?.page(up: scrollDirection.dy > 0)
_ = inputController?.page(up: scrollDirection.dy > 0)
scrollDirection = .zero
}
} else {
Expand Down

0 comments on commit 8c7d5d2

Please sign in to comment.