InputHandler // Handle Alt+Shift+Enter, etc.

This commit is contained in:
ShikiSuen 2023-01-28 17:26:36 +08:00
parent 35d3c344ec
commit d1a7f3dcc3
3 changed files with 24 additions and 26 deletions

View File

@ -89,7 +89,7 @@ extension InputHandler {
if input.isControlHold, input.isCommandHold, input.isEnter,
!input.isOptionHold, !input.isShiftHold, compositor.isEmpty
{
return handleCtrlCommandEnter()
return handleEnter(input: input, readingOnly: true)
}
//
if !currentLM.hasUnigramsFor(keyArray: [readingKey]) {
@ -238,7 +238,7 @@ extension InputHandler {
if input.isControlHold, input.isCommandHold, input.isEnter,
!input.isOptionHold, !input.isShiftHold, composer.isEmpty
{
return handleCtrlCommandEnter()
return handleEnter(input: input, readingOnly: true)
}
//
if !currentLM.hasUnigramsFor(keyArray: [calligrapher]) {

View File

@ -140,12 +140,7 @@ extension InputHandler {
case .kEnd: return handleEnd()
case .kBackSpace: return handleBackSpace(input: input)
case .kWindowsDelete: return handleDelete(input: input)
case .kCarriageReturn, .kLineFeed: // Enter
return (input.isCommandHold && input.isControlHold)
? (input.isOptionHold
? handleCtrlOptionCommandEnter(isShiftPressed: input.isShiftHold)
: handleCtrlCommandEnter(isShiftPressed: input.isShiftHold))
: handleEnter()
case .kCarriageReturn, .kLineFeed: return handleEnter(input: input)
case .kSpace: // Space
//
switch state.type {

View File

@ -279,16 +279,30 @@ extension InputHandler {
return true
}
// MARK: - Enter
// MARK: - Enter
/// Enter
/// - Parameter input:
/// - Returns: SessionCtl IMK
@discardableResult func handleEnter() -> Bool {
@discardableResult func handleEnter(input: InputSignalProtocol, readingOnly: Bool = false) -> Bool {
guard let delegate = delegate else { return false }
let state = delegate.state
guard state.type == .ofInputting else { return false }
delegate.switchState(IMEState.ofCommitting(textToCommit: state.displayedText))
var displayedText = state.displayedText
if input.modifierFlags == [.option, .shift] {
displayedText = displayedText.charComponents.joined(separator: " ")
} else if readingOnly {
displayedText = commissionByCtrlCommandEnter()
} else if input.isCommandHold, input.isControlHold {
displayedText =
input.isOptionHold
? commissionByCtrlOptionCommandEnter(isShiftPressed: input.isShiftHold)
: commissionByCtrlCommandEnter(isShiftPressed: input.isShiftHold)
}
delegate.switchState(IMEState.ofCommitting(textToCommit: displayedText))
return true
}
@ -297,11 +311,7 @@ extension InputHandler {
/// Command+Enter
/// - Parameter isShiftPressed: Shift
/// - Returns: SessionCtl IMK
func handleCtrlCommandEnter(isShiftPressed: Bool = false) -> Bool {
guard let delegate = delegate else { return false }
let state = delegate.state
guard state.type == .ofInputting else { return false }
private func commissionByCtrlCommandEnter(isShiftPressed: Bool = false) -> String {
var displayedText = compositor.keys.joined(separator: "\t")
if compositor.isEmpty {
displayedText = readingForDisplay
@ -325,9 +335,7 @@ extension InputHandler {
}
displayedText = displayedText.replacingOccurrences(of: "\t", with: isShiftPressed ? "-" : " ")
delegate.switchState(IMEState.ofCommitting(textToCommit: displayedText))
return true
return displayedText
}
// MARK: - Command+Option+Enter Ruby
@ -335,11 +343,7 @@ extension InputHandler {
/// Command+Option+Enter Ruby
/// - Parameter isShiftPressed: Shift
/// - Returns: SessionCtl IMK
func handleCtrlOptionCommandEnter(isShiftPressed: Bool = false) -> Bool {
guard let delegate = delegate else { return false }
let state = delegate.state
guard state.type == .ofInputting else { return false }
private func commissionByCtrlOptionCommandEnter(isShiftPressed: Bool = false) -> String {
var composed = ""
compositor.walkedNodes.smashedPairs.forEach { key, value in
@ -368,8 +372,7 @@ extension InputHandler {
composed += key.contains("_") ? value : "<ruby>\(value)<rp>(</rp><rt>\(key)</rt><rp>)</rp></ruby>"
}
delegate.switchState(IMEState.ofCommitting(textToCommit: composed))
return true
return composed
}
// MARK: - BackSpace (macOS Delete)