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

View File

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

View File

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