vChewing-macOS/Packages/vChewing_MainAssembly/Sources/MainAssembly/SessionController/SessionCtl_HandleStates.swift

203 lines
9.3 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 InputMethodKit
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 {
var newState = newState
/// IMK
/// 1.
/// 2. client().setMarkedText() selectionRange 0
///
/// 3. macOS 14 macOS
///
/// .ofInputtingState marker cursor
/// selectionRange 0
if newState.type == .ofInputting, clientMitigationLevel < 2 {
newState.data.marker = newState.data.cursor
}
state = newState
}
switch newState.type {
case .ofDeactivated:
// commitComposition()
// clearInlineDisplay()
// IMK inputMode.didSet()
// hidePalettes() Session Session
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)
if newState.isCandidateContainer { showCandidates() }
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.sync(accent: clientAccentColor, locale: localeForFontFallbacks)
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
}
doSetMarkedText(attrStr)
}
/// 使
func clearInlineDisplay() {
doSetMarkedText(NSAttributedString())
}
///
/// IMK commitComposition
private func commit(text: String) {
guard !text.isEmpty else { return }
let phE = PrefMgr.shared.phraseReplacementEnabled && text.count > 1
var text = text.trimmingCharacters(in: .newlines)
var replaced = false
if phE, let queried = inputHandler?.currentLM.queryReplacementValue(key: text) {
replaced = true
text = queried
}
var buffer = ChineseConverter.kanjiConversionIfRequired(text)
if phE, !replaced, let queried = inputHandler?.currentLM.queryReplacementValue(key: buffer) {
buffer = ChineseConverter.kanjiConversionIfRequired(queried)
}
func doCommit() {
guard let client = client() else { return }
client.insertText(
buffer, replacementRange: replacementRange()
)
}
if isServingIMEItself {
DispatchQueue.main.async {
doCommit()
}
} else {
doCommit()
}
}
/// setMarkedText GCD
/// - Remark:
/// - Parameters:
/// - string: NSAttributedString
/// replacementRange Microsoft Office
/// /
func doSetMarkedText(_ string: NSAttributedString, allowAsync: Bool = true) {
// replacementRange replacementRange
let range = selectionRange()
guard !(string.isEqual(to: recentMarkedText.text) && recentMarkedText.selectionRange == range) else { return }
recentMarkedText.text = string
recentMarkedText.selectionRange = range
if allowAsync, isServingIMEItself || !isActivated {
DispatchQueue.main.async {
guard let client = self.client() else { return }
client.setMarkedText(
string, selectionRange: range, replacementRange: self.replacementRange()
)
}
} else {
guard let client = client() else { return }
client.setMarkedText(
string, selectionRange: range, replacementRange: replacementRange()
)
}
}
}