vChewing-macOS/Source/Modules/ControllerModules/ctlInputMethod_HandleStates...

144 lines
5.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// (c) 2011 and onwards The OpenVanilla Project (MIT License).
// All possible vChewing-specific modifications are of:
// (c) 2021 and onwards The vChewing Project (MIT-NTL License).
// ====================
// This code is released under the MIT license (SPDX-License-Identifier: MIT)
// ... with NTL restriction stating that:
// No trademark license is granted to use the trade names, trademarks, service
// marks, or product names of Contributor, except as required to fulfill notice
// requirements defined in MIT License.
import Foundation
// MARK: - 調 (State Handling)
extension ctlInputMethod {
/// 調
///
///
///
/// - Parameter newState:
func handle(state newState: IMEStateProtocol) {
let previous = state
state = newState
switch state.type {
case .ofDeactivated:
ctlInputMethod.ctlCandidateCurrent.delegate = nil
ctlInputMethod.ctlCandidateCurrent.visible = false
ctlInputMethod.tooltipInstance.hide()
if previous.hasComposition {
commit(text: previous.displayedText)
}
clearInlineDisplay()
//
keyHandler.clear()
case .ofEmpty, .ofAbortion:
var previous = previous
if state.type == .ofAbortion {
state = IMEState.ofEmpty()
previous = state
}
ctlInputMethod.ctlCandidateCurrent.visible = false
ctlInputMethod.tooltipInstance.hide()
// .Abortion
if previous.hasComposition, state.type != .ofAbortion {
commit(text: previous.displayedText)
}
//
ctlInputMethod.ctlCandidateCurrent.visible = false
ctlInputMethod.tooltipInstance.hide()
clearInlineDisplay()
//
keyHandler.clear()
case .ofCommitting:
ctlInputMethod.ctlCandidateCurrent.visible = false
ctlInputMethod.tooltipInstance.hide()
let textToCommit = state.textToCommit
if !textToCommit.isEmpty { commit(text: textToCommit) }
clearInlineDisplay()
//
keyHandler.clear()
case .ofInputting:
ctlInputMethod.ctlCandidateCurrent.visible = false
ctlInputMethod.tooltipInstance.hide()
let textToCommit = state.textToCommit
if !textToCommit.isEmpty { commit(text: textToCommit) }
setInlineDisplayWithCursor()
if !state.tooltip.isEmpty {
show(tooltip: state.tooltip)
}
case .ofMarking:
ctlInputMethod.ctlCandidateCurrent.visible = false
setInlineDisplayWithCursor()
if state.tooltip.isEmpty {
ctlInputMethod.tooltipInstance.hide()
} else {
show(tooltip: state.tooltip)
}
case .ofCandidates, .ofAssociates, .ofSymbolTable:
ctlInputMethod.tooltipInstance.hide()
setInlineDisplayWithCursor()
show(candidateWindowWith: state)
default: break
}
//
if state.hasComposition, mgrPrefs.clientsIMKTextInputIncapable.contains(clientBundleIdentifier) {
ctlInputMethod.popupCompositionBuffer.isTypingDirectionVertical = isVerticalTyping
ctlInputMethod.popupCompositionBuffer.show(
state: state, at: lineHeightRect(zeroCursor: true).origin
)
} else {
ctlInputMethod.popupCompositionBuffer.hide()
}
}
/// .NotEmpty()
func setInlineDisplayWithCursor() {
guard let client = client() else { return }
if state.type == .ofAssociates {
client.setMarkedText(
state.data.attributedStringPlaceholder, selectionRange: NSRange(location: 0, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
return
}
if state.hasComposition || state.isCandidateContainer {
/// selectionRange
/// 0 replacementRangeNSNotFound
///
client.setMarkedText(
attributedStringSecured.0, selectionRange: attributedStringSecured.1,
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
return
}
//
clearInlineDisplay()
}
/// .NotEmpty()
/// setInlineDisplayWithCursor()
private func clearInlineDisplay() {
guard let theClient = client() else { return }
theClient.setMarkedText(
"", selectionRange: NSRange(location: 0, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
}
///
/// IMK commitComposition
private func commit(text: String) {
guard let client = client() else { return }
let buffer = IME.kanjiConversionIfRequired(text)
if buffer.isEmpty {
return
}
client.insertText(
buffer, replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
}
}