vChewing-macOS/Source/Modules/SessionCtl_HandleStates.swift

178 lines
7.8 KiB
Swift
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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) 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 PopupCompositionBuffer
import Shared
// MARK: - 調 (State Handling)
extension SessionCtl {
/// 調
///
///
///
/// - Remark: (Static)
/// deactivateServer() 使
/// 使 SessionCtl
///
/// - Note: Swift Protocol
/// - Parameter newState:
public func switchState(_ newState: IMEStateProtocol) {
handle(state: newState, replace: true)
}
/// 調
///
///
///
/// - Remark: (Static)
/// deactivateServer() 使
/// 使 SessionCtl
///
/// - Parameters:
/// - newState:
/// - replace:
public func handle(state newState: IMEStateProtocol, replace: Bool) {
var previous = state
if replace { state = newState }
switch newState.type {
case .ofDeactivated:
ctlCandidateCurrent.visible = false
popupCompositionBuffer.hide()
tooltipInstance.hide()
if previous.hasComposition {
commit(text: previous.displayedText)
}
clearInlineDisplay()
//
inputHandler.clear()
// deactivateServer() activateServer()
//
// IMKCandidates
//
if PrefMgr.shared.useIMKCandidateWindow {
for instance in Self.allInstances {
guard let imkC = instance.ctlCandidateCurrent as? CtlCandidateIMK else { continue }
if instance.state.isCandidateContainer, !imkC.visible {
instance.handle(state: instance.state, replace: false)
}
}
}
case .ofEmpty, .ofAbortion, .ofCommitting:
innerCircle: switch newState.type {
case .ofAbortion:
previous = IMEState.ofEmpty()
if replace { state = previous }
case .ofCommitting:
commit(text: newState.textToCommit)
if replace { state = IMEState.ofEmpty() }
default: break innerCircle
}
ctlCandidateCurrent.visible = false
// .Abortion
if previous.hasComposition, ![.ofAbortion, .ofCommitting].contains(newState.type) {
commit(text: previous.displayedText)
}
showTooltip(newState.tooltip, duration: 1) //
clearInlineDisplay()
//
inputHandler.clear()
case .ofInputting:
ctlCandidateCurrent.visible = false
commit(text: newState.textToCommit)
setInlineDisplayWithCursor()
showTooltip(newState.tooltip, duration: 1) //
case .ofMarking:
ctlCandidateCurrent.visible = false
setInlineDisplayWithCursor()
showTooltip(newState.tooltip)
case .ofCandidates, .ofAssociates, .ofSymbolTable:
tooltipInstance.hide()
setInlineDisplayWithCursor()
showCandidates()
default: break
}
//
if newState.hasComposition, PrefMgr.shared.clientsIMKTextInputIncapable.contains(clientBundleIdentifier) {
popupCompositionBuffer.isTypingDirectionVertical = isVerticalTyping
popupCompositionBuffer.show(
state: newState, at: lineHeightRect(zeroCursor: true).origin
)
} else {
popupCompositionBuffer.hide()
}
}
/// .NotEmpty()
public func setInlineDisplayWithCursor() {
if state.type == .ofAssociates {
doSetMarkedText(
state.data.attributedStringPlaceholder, selectionRange: NSRange(location: 0, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
return
}
if state.hasComposition || state.isCandidateContainer {
/// selectionRange
/// 0 replacementRangeNSNotFound
///
doSetMarkedText(
attributedStringSecured.0, selectionRange: attributedStringSecured.1,
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
return
}
//
clearInlineDisplay()
}
/// .NotEmpty()
/// setInlineDisplayWithCursor()
private func clearInlineDisplay() {
doSetMarkedText(
"", selectionRange: NSRange(location: 0, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
}
///
/// IMK commitComposition
private func commit(text: String) {
guard let client = client(), !text.isEmpty else { return }
let buffer = ChineseConverter.kanjiConversionIfRequired(text)
if buffer.isEmpty {
return
}
if let myID = Bundle.main.bundleIdentifier, let clientID = client.bundleIdentifier(), myID == clientID {
DispatchQueue.main.async {
client.insertText(
buffer, replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
}
} else {
client.insertText(
buffer, replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
}
}
/// setMarkedText GCD
public func doSetMarkedText(_ string: Any!, selectionRange: NSRange, replacementRange: NSRange) {
guard let client = client() else { return }
if let myID = Bundle.main.bundleIdentifier, let clientID = client.bundleIdentifier(), myID == clientID {
DispatchQueue.main.async {
client.setMarkedText(string, selectionRange: selectionRange, replacementRange: replacementRange)
}
} else {
client.setMarkedText(string, selectionRange: selectionRange, replacementRange: replacementRange)
}
}
}