vChewing-macOS/Source/Modules/SessionCtl_Delegates.swift

233 lines
9.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 Shared
// MARK: - InputHandler Delegate
extension SessionCtl: InputHandlerDelegate {
public var clientBundleIdentifier: String {
guard let client = client() else { return "" }
return client.bundleIdentifier() ?? ""
}
public func candidateController() -> CtlCandidateProtocol? { candidateUI }
public func candidateSelectionCalledByInputHandler(at index: Int) {
candidatePairSelected(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 }
if !LMMgr.writeUserPhrase(
state.data.userPhraseDumped, inputMode: inputMode,
areWeDuplicating: state.data.doesUserPhraseExist,
areWeDeleting: addToFilter
)
|| !LMMgr.writeUserPhrase(
state.data.userPhraseDumpedConverted, inputMode: inputMode.reversed,
areWeDuplicating: false,
areWeDeleting: addToFilter
)
{
return false
}
//
let rawPair = state.data.userPhraseKVPair
let valueCurrent = rawPair.1
let valueReversed = ChineseConverter.crossConvert(rawPair.1)
//
//
//
_ = inputHandler.updateUnigramData()
//
//
let temporaryScore: Double = SessionCtl.areWeNerfing ? -114.514 : 0
LMMgr.currentLM.insertTemporaryData(
key: rawPair.0, unigram: .init(value: rawPair.1, score: temporaryScore), isFiltering: SessionCtl.areWeNerfing
)
// 使
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 }
public var showReverseLookupResult: Bool {
!isVerticalTyping && 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) -> [(String, String)] {
if !state.isCandidateContainer || state.candidates.isEmpty { return [] }
if !conv || PrefMgr.shared.cns11643Enabled || state.candidates[0].0.contains("_punctuation") {
return state.candidates
}
let convertedCandidates: [(String, String)] = state.candidates.map { theCandidatePair -> (String, String) in
let theCandidate = theCandidatePair.1
let theConverted = ChineseConverter.kanjiConversionIfRequired(theCandidate)
let result = (theCandidate == theConverted) ? theCandidate : "\(theConverted)(\(theCandidate))"
return (theCandidatePair.0, result)
}
return convertedCandidates
}
public func candidatePairSelected(at index: Int) {
guard let inputHandler = inputHandler else { return }
if state.type == .ofSymbolTable, (0..<state.node.members.count).contains(index) {
let node = state.node.members[index]
if !node.members.isEmpty {
switchState(IMEState.ofEmpty()) //
switchState(IMEState.ofSymbolTable(node: node))
} else {
switchState(IMEState.ofCommitting(textToCommit: node.name))
switchState(IMEState.ofEmpty())
}
return
}
if [.ofCandidates, .ofSymbolTable].contains(state.type) {
let selectedValue = state.candidates[index]
if state.type == .ofCandidates {
inputHandler.consolidateNode(
candidate: selectedValue, respectCursorPushing: true,
preConsolidate: PrefMgr.shared.consolidateContextOnCandidateSelection
)
}
let inputting = inputHandler.generateStateOfInputting()
if PrefMgr.shared.useSCPCTypingMode {
switchState(IMEState.ofCommitting(textToCommit: inputting.displayedText))
// selectedValue.1
if PrefMgr.shared.associatedPhrasesEnabled {
let associates = inputHandler.generateStateOfAssociates(
withPair: .init(key: selectedValue.0, value: selectedValue.1)
)
switchState(associates.candidates.isEmpty ? IMEState.ofEmpty() : associates)
} else {
switchState(IMEState.ofEmpty())
}
} else {
switchState(inputting)
}
return
}
if state.type == .ofAssociates {
let selectedValue = state.candidates[index]
switchState(IMEState.ofCommitting(textToCommit: selectedValue.1))
// selectedValue.1
//
guard let valueKept = selectedValue.1.last else {
switchState(IMEState.ofEmpty())
return
}
if PrefMgr.shared.associatedPhrasesEnabled {
let associates = inputHandler.generateStateOfAssociates(
withPair: .init(key: selectedValue.0, value: String(valueKept))
)
if !associates.candidates.isEmpty {
switchState(associates)
return
}
}
switchState(IMEState.ofEmpty())
}
}
public func candidatePairRightClicked(at index: Int, action: CandidateContextMenuAction) {
guard let inputHandler = inputHandler, isCandidateContextMenuEnabled else { return }
var succeeded = true
let rawPair = state.candidates[index]
let valueCurrent = rawPair.1
let valueReversed = ChineseConverter.crossConvert(rawPair.1)
let nerfedScore = (action == .toNerf) ? " -114.514" : ""
let convertedMark = "#𝙃𝙪𝙢𝙖𝙣𝘾𝙝𝙚𝙘𝙠𝙍𝙚𝙦𝙪𝙞𝙧𝙚𝙙"
let userPhraseDumped = "\(valueCurrent) \(rawPair.0)\(nerfedScore)"
let userPhraseDumpedConverted = "\(valueReversed) \(rawPair.0)\(nerfedScore)\t\(convertedMark)"
if !LMMgr.writeUserPhrase(
userPhraseDumped, inputMode: inputMode,
areWeDuplicating: action != .toFilter,
areWeDeleting: action == .toFilter
)
|| !LMMgr.writeUserPhrase(
userPhraseDumpedConverted, inputMode: inputMode.reversed,
areWeDuplicating: action != .toFilter,
areWeDeleting: action == .toFilter
)
{
succeeded = false
}
//
//
let temporaryScore: Double = (action == .toNerf) ? -114.514 : 0
LMMgr.currentLM.insertTemporaryData(
key: rawPair.0, unigram: .init(value: rawPair.1, score: temporaryScore), 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)
}
}