vChewing-macOS/Source/Modules/SessionCtl_HandleStates.swift

171 lines
7.7 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)
public extension SessionCtl {
/// 調
///
///
///
/// - Remark: (Static)
/// deactivateServer() 使
/// 使 SessionCtl
///
/// - Note: Swift Protocol
/// - Parameter newState:
func switchState(_ newState: IMEStateProtocol) {
handle(state: newState, replace: true)
}
/// 調
///
///
///
/// - Remark: (Static)
/// deactivateServer() 使
/// 使 SessionCtl
///
/// - Parameters:
/// - newState:
/// - replace:
func handle(state newState: IMEStateProtocol, replace: Bool) {
var previous = state
if replace { state = newState }
switch newState.type {
case .ofDeactivated:
// commitComposition()
// clearInlineDisplay()
// IMK inputMode.didSet()
hidePalettes()
inputHandler?.clear()
if ![.ofAbortion, .ofEmpty].contains(previous.type), !previous.displayedText.isEmpty {
clearInlineDisplay()
}
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
}
candidateUI?.visible = false
// .Abortion
if previous.hasComposition, ![.ofAbortion, .ofCommitting].contains(newState.type) {
commit(text: previous.displayedText)
}
//
showTooltip(newState.tooltip, duration: newState.tooltipDuration)
clearInlineDisplay()
inputHandler?.clear()
case .ofInputting:
candidateUI?.visible = false
if !newState.textToCommit.isEmpty { commit(text: newState.textToCommit) }
setInlineDisplayWithCursor()
//
showTooltip(newState.tooltip, duration: newState.tooltipDuration)
case .ofMarking:
candidateUI?.visible = false
setInlineDisplayWithCursor()
showTooltip(newState.tooltip)
case .ofCandidates, .ofAssociates, .ofSymbolTable:
tooltipInstance.hide()
setInlineDisplayWithCursor()
showCandidates()
}
//
updatePopupDisplayWithCursor()
}
///
func updatePopupDisplayWithCursor() {
if state.hasComposition, clientMitigationLevel >= 2 {
updateVerticalTypingStatus()
popupCompositionBuffer.isTypingDirectionVertical = isVerticalTyping
popupCompositionBuffer.show(
state: state, at: lineHeightRect(zeroCursor: true).origin
)
} else {
popupCompositionBuffer.hide()
}
}
/// /
func setInlineDisplayWithCursor() {
var attrStr: NSAttributedString = attributedStringSecured.value
// QQNT client.setMarkedText() .thick
mitigation: if clientMitigationLevel == 1, state.type == .ofMarking {
if !PrefMgr.shared.disableSegmentedThickUnderlineInMarkingModeForManagedClients { break mitigation }
let neo = NSMutableAttributedString(attributedString: attributedStringSecured.value)
let rangeNeo = NSRange(location: 0, length: neo.string.utf16.count)
neo.setAttributes(
mark(forStyle: kTSMHiliteSelectedConvertedText, at: rangeNeo)
as? [NSAttributedString.Key: Any]
?? [.underlineStyle: NSUnderlineStyle.thick.rawValue], range: rangeNeo
)
attrStr = neo
}
/// selectionRange
/// 0 replacementRangeNSNotFound
///
doSetMarkedText(attrStr)
}
/// 使
func clearInlineDisplay() {
doSetMarkedText(NSAttributedString())
}
///
/// IMK commitComposition
private func commit(text: String) {
let text = text.trimmingCharacters(in: .newlines)
let buffer = ChineseConverter.kanjiConversionIfRequired(text)
if isServingIMEItself {
DispatchQueue.main.async {
guard let client = self.client() else { return }
client.insertText(
buffer, replacementRange: self.replacementRange()
)
}
} else {
guard let client = client() else { return }
client.insertText(
buffer, replacementRange: replacementRange()
)
}
}
/// setMarkedText GCD
/// - Parameters:
/// - string: NSAttributedString
/// replacementRange Microsoft Office
/// /
func doSetMarkedText(_ string: NSAttributedString) {
if isServingIMEItself || !isActivated {
DispatchQueue.main.async {
guard let client = self.client() else { return }
client.setMarkedText(
string, selectionRange: self.selectionRange(), replacementRange: self.replacementRange()
)
}
} else {
guard let client = client() else { return }
client.setMarkedText(
string, selectionRange: selectionRange(), replacementRange: replacementRange()
)
}
}
}