vChewing-macOS/Source/Modules/SessionCtl_Delegates.swift

217 lines
9.4 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) 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 Shared
// MARK: - InputHandler Delegate
extension SessionCtl: InputHandlerDelegate {
public var clientMitigationLevel: Int {
guard
let result = PrefMgr.shared.clientsIMKTextInputIncapable[clientBundleIdentifier]
else {
return 0
}
return result ? 2 : 1
}
public func candidateController() -> CtlCandidateProtocol? { candidateUI }
public func candidateSelectionConfirmedByInputHandler(at index: Int) {
candidatePairSelectionConfirmed(at: index)
}
public func callError(_ logMessage: String) {
vCLog(logMessage)
IMEApp.buzz()
}
public func performUserPhraseOperation(addToFilter: Bool) -> Bool {
guard let inputHandler = inputHandler, state.type == .ofMarking else { return false }
var succeeded = true
let kvPair = state.data.userPhraseKVPair
var userPhrase = LMMgr.UserPhrase(
keyArray: kvPair.keyArray, value: kvPair.value, inputMode: inputMode
)
if Self.areWeNerfing { userPhrase.weight = -114.514 }
LMMgr.writeUserPhrasesAtOnce(userPhrase, areWeFiltering: addToFilter) {
succeeded = false
}
if !succeeded { return false }
//
let valueCurrent = userPhrase.value
let valueReversed = ChineseConverter.crossConvert(valueCurrent)
//
//
//
_ = inputHandler.updateUnigramData()
//
//
LMMgr.currentLM.insertTemporaryData(
keyArray: userPhrase.keyArray,
unigram: .init(value: userPhrase.value, score: userPhrase.weight ?? 0),
isFiltering: addToFilter
)
// 使
LMMgr.bleachSpecifiedSuggestions(targets: [valueCurrent], mode: IMEApp.currentInputMode)
LMMgr.bleachSpecifiedSuggestions(targets: [valueReversed], mode: IMEApp.currentInputMode.reversed)
//
return true
}
}
// MARK: - Candidate Controller Delegate
extension SessionCtl: CtlCandidateDelegate {
public var isCandidateState: Bool { state.isCandidateContainer }
public var isCandidateContextMenuEnabled: Bool {
state.type == .ofCandidates && !clientBundleIdentifier.contains("com.apple.Spotlight")
&& !clientBundleIdentifier.contains("com.raycast.macos")
}
public var showReverseLookupResult: Bool { PrefMgr.shared.showReverseLookupInCandidateUI }
@discardableResult public func reverseLookup(for value: String) -> [String] {
let blankResult: [String] = []
//
if !PrefMgr.shared.showReverseLookupInCandidateUI { return blankResult }
if isVerticalTyping { return blankResult } //
if value.isEmpty { return blankResult } // 西
if value.contains("_") { return blankResult }
// LMInstantiator
return LMMgr.currentLM.cassetteReverseLookup(for: value)
}
public var selectionKeys: String {
PrefMgr.shared.useIMKCandidateWindow ? "123456789" : PrefMgr.shared.candidateKeys
}
public func candidatePairs(conv: Bool = false) -> [(keyArray: [String], value: String)] {
if !state.isCandidateContainer || state.candidates.isEmpty { return [] }
if !conv || PrefMgr.shared.cns11643Enabled || state.candidates[0].keyArray.joined().contains("_punctuation") {
return state.candidates
}
let convertedCandidates = state.candidates.map { theCandidatePair -> (keyArray: [String], value: String) in
var theCandidatePair = theCandidatePair
theCandidatePair.value = ChineseConverter.kanjiConversionIfRequired(theCandidatePair.value)
return theCandidatePair
}
return convertedCandidates
}
public func candidatePairHighlightChanged(at theIndex: Int) {
inputHandler?.previewCompositionBufferForCandidate(at: theIndex)
}
public func candidatePairSelectionConfirmed(at index: Int) {
guard let inputHandler = inputHandler else { return }
switch state.type {
case .ofSymbolTable where (0 ..< state.node.members.count).contains(index):
let node = state.node.members[index]
if !node.members.isEmpty {
switchState(IMEState.ofSymbolTable(node: node))
} else {
switchState(IMEState.ofCommitting(textToCommit: node.name))
}
case .ofCandidates where (0 ..< state.candidates.count).contains(index):
let selectedValue = state.candidates[index]
if state.type == .ofCandidates {
inputHandler.consolidateNode(
candidate: selectedValue, respectCursorPushing: true,
preConsolidate: PrefMgr.shared.consolidateContextOnCandidateSelection
)
}
var result: IMEStateProtocol = inputHandler.generateStateOfInputting()
defer { switchState(result) } //
if PrefMgr.shared.useSCPCTypingMode {
switchState(IMEState.ofCommitting(textToCommit: result.displayedText))
// selectedValue.value
if PrefMgr.shared.associatedPhrasesEnabled {
let associates = inputHandler.generateStateOfAssociates(
withPair: .init(keyArray: selectedValue.keyArray, value: selectedValue.value)
)
result = associates.candidates.isEmpty ? IMEState.ofEmpty() : associates
} else {
result = IMEState.ofEmpty()
}
}
case .ofAssociates where (0 ..< state.candidates.count).contains(index):
let selectedValue = state.candidates[index]
var result: IMEStateProtocol = IMEState.ofEmpty()
defer { switchState(result) } //
switchState(IMEState.ofCommitting(textToCommit: selectedValue.value))
guard PrefMgr.shared.associatedPhrasesEnabled else { return }
// selectedValue.value
//
guard let valueKept = selectedValue.value.last?.description else { return }
let associates = inputHandler.generateStateOfAssociates(
withPair: .init(keyArray: selectedValue.keyArray, value: valueKept)
)
if !associates.candidates.isEmpty { result = associates }
default: return
}
}
public func candidatePairRightClicked(at index: Int, action: CandidateContextMenuAction) {
guard let inputHandler = inputHandler, isCandidateContextMenuEnabled else { return }
var succeeded = true
let rawPair = state.candidates[index]
var userPhrase = LMMgr.UserPhrase(
keyArray: rawPair.keyArray, value: rawPair.value, inputMode: inputMode
)
if action == .toNerf { userPhrase.weight = -114.514 }
LMMgr.writeUserPhrasesAtOnce(userPhrase, areWeFiltering: action == .toFilter) {
succeeded = false
}
//
let valueCurrent = userPhrase.value
let valueReversed = ChineseConverter.crossConvert(valueCurrent)
//
//
LMMgr.currentLM.insertTemporaryData(
keyArray: userPhrase.keyArray,
unigram: .init(value: userPhrase.value, score: userPhrase.weight ?? 0),
isFiltering: action == .toFilter
)
// 使
LMMgr.bleachSpecifiedSuggestions(targets: [valueCurrent], mode: IMEApp.currentInputMode)
LMMgr.bleachSpecifiedSuggestions(targets: [valueReversed], mode: IMEApp.currentInputMode.reversed)
//
let updateResult = inputHandler.updateUnigramData()
//
var newState: IMEStateProtocol = updateResult
? inputHandler.generateStateOfCandidates()
: IMEState.ofCommitting(textToCommit: state.displayedText)
newState.tooltipDuration = 1.85
var tooltipMessage = ""
switch action {
case .toBoost:
newState.data.tooltipColorState = .normal
tooltipMessage = succeeded ? "+ Succeeded in boosting a candidate." : "⚠︎ Failed from boosting a candidate."
case .toNerf:
newState.data.tooltipColorState = .succeeded
tooltipMessage = succeeded ? "- Succeeded in nerfing a candidate." : "⚠︎ Failed from nerfing a candidate."
case .toFilter:
newState.data.tooltipColorState = .warning
tooltipMessage = succeeded ? "! Succeeded in filtering a candidate." : "⚠︎ Failed from filtering a candidate."
}
if !succeeded { newState.data.tooltipColorState = .redAlert }
newState.tooltip = NSLocalizedString(tooltipMessage, comment: "")
switchState(newState)
}
}