SessionCtl // Make PCB, tooltip, and candidate window instances dynamic.

This commit is contained in:
ShikiSuen 2022-10-10 11:27:29 +08:00
parent 940e9b6d85
commit a144e20304
5 changed files with 43 additions and 42 deletions

View File

@ -28,7 +28,7 @@ class SessionCtl: IMKInputController {
static var areWeNerfing = false
///
static var ctlCandidateCurrent: CtlCandidateProtocol = {
var ctlCandidateCurrent: CtlCandidateProtocol = {
let direction: NSUserInterfaceLayoutOrientation =
PrefMgr.shared.useHorizontalCandidateList ? .horizontal : .vertical
if #available(macOS 10.15, *) {
@ -39,11 +39,11 @@ class SessionCtl: IMKInputController {
}
}()
///
static var tooltipInstance = TooltipUI()
///
var tooltipInstance = TooltipUI()
///
static var popupCompositionBuffer = PopupCompositionBuffer()
///
var popupCompositionBuffer = PopupCompositionBuffer()
// MARK: -

View File

@ -16,7 +16,7 @@ extension SessionCtl: KeyHandlerDelegate {
return client.bundleIdentifier() ?? ""
}
func candidateController() -> CtlCandidateProtocol { Self.ctlCandidateCurrent }
func candidateController() -> CtlCandidateProtocol { ctlCandidateCurrent }
func candidateSelectionCalledByKeyHandler(at index: Int) {
candidatePairSelected(at: index)

View File

@ -56,14 +56,14 @@ extension SessionCtl {
}()
// NSAttributedTextView
do {
Self.tooltipInstance.hide()
Self.tooltipInstance = .init()
tooltipInstance.hide()
tooltipInstance = .init()
if state.type == .ofMarking {
Self.tooltipInstance.setColor(state: state.data.tooltipColorState)
tooltipInstance.setColor(state: state.data.tooltipColorState)
}
}
//
Self.tooltipInstance.show(
tooltipInstance.show(
tooltip: tooltip, at: finalOrigin,
bottomOutOfScreenAdjustmentHeight: delta, direction: tooltipContentDirection
)
@ -74,34 +74,34 @@ extension SessionCtl {
state.isVerticalCandidateWindow = (isVerticalTyping || !PrefMgr.shared.useHorizontalCandidateList)
/// IMK
Self.ctlCandidateCurrent.delegate = nil
ctlCandidateCurrent.delegate = nil
let candidateLayout: NSUserInterfaceLayoutOrientation =
((isVerticalTyping || !PrefMgr.shared.useHorizontalCandidateList)
? .vertical
: .horizontal)
if #available(macOS 10.15, *) {
Self.ctlCandidateCurrent =
ctlCandidateCurrent =
PrefMgr.shared.useIMKCandidateWindow
? CtlCandidateIMK(candidateLayout) : CtlCandidateTDK(candidateLayout)
if let candidateTDK = Self.ctlCandidateCurrent as? CtlCandidateTDK {
if let candidateTDK = ctlCandidateCurrent as? CtlCandidateTDK {
candidateTDK.maxLinesPerPage = isVerticalTyping ? 1 : 3
}
} else {
Self.ctlCandidateCurrent = CtlCandidateIMK(candidateLayout)
ctlCandidateCurrent = CtlCandidateIMK(candidateLayout)
}
Self.ctlCandidateCurrent.candidateFont = Self.candidateFont(
ctlCandidateCurrent.candidateFont = Self.candidateFont(
name: PrefMgr.shared.candidateTextFontName, size: PrefMgr.shared.candidateListTextSize
)
if state.type == .ofAssociates {
Self.ctlCandidateCurrent.tooltip =
ctlCandidateCurrent.tooltip =
isVerticalTyping ? "" : NSLocalizedString("Hold ⇧ to choose associates.", comment: "")
}
Self.ctlCandidateCurrent.useLangIdentifier = PrefMgr.shared.handleDefaultCandidateFontsByLangIdentifier
Self.ctlCandidateCurrent.locale = {
ctlCandidateCurrent.useLangIdentifier = PrefMgr.shared.handleDefaultCandidateFontsByLangIdentifier
ctlCandidateCurrent.locale = {
switch inputMode {
case .imeModeCHS: return "zh-Hans"
case .imeModeCHT:
@ -115,25 +115,25 @@ extension SessionCtl {
if #available(macOS 10.14, *) {
// Spotlight IMK
if let ctlCandidateCurrent = Self.ctlCandidateCurrent as? CtlCandidateIMK {
if let ctlCandidateCurrent = ctlCandidateCurrent as? CtlCandidateIMK {
while ctlCandidateCurrent.windowLevel() <= client.windowLevel() {
ctlCandidateCurrent.setWindowLevel(UInt64(max(0, client.windowLevel() + 1000)))
}
}
}
Self.ctlCandidateCurrent.delegate = self //
Self.ctlCandidateCurrent.visible = true
ctlCandidateCurrent.delegate = self //
ctlCandidateCurrent.visible = true
if isVerticalTyping {
Self.ctlCandidateCurrent.set(
ctlCandidateCurrent.set(
windowTopLeftPoint: NSPoint(
x: lineHeightRect().origin.x + lineHeightRect().size.width + 4.0, y: lineHeightRect().origin.y - 4.0
),
bottomOutOfScreenAdjustmentHeight: lineHeightRect().size.height + 4.0
)
} else {
Self.ctlCandidateCurrent.set(
ctlCandidateCurrent.set(
windowTopLeftPoint: NSPoint(x: lineHeightRect().origin.x, y: lineHeightRect().origin.y - 4.0),
bottomOutOfScreenAdjustmentHeight: lineHeightRect().size.height + 4.0
)

View File

@ -173,7 +173,7 @@ extension SessionCtl {
// interpretKeyEvents()
// - imkCandidates.interpretKeyEvents()
// - delegate SessionCtl KeyHandler
if let imkCandidates = Self.ctlCandidateCurrent as? CtlCandidateIMK, imkCandidates.visible {
if let imkCandidates = ctlCandidateCurrent as? CtlCandidateIMK, imkCandidates.visible {
let event: NSEvent = CtlCandidateIMK.replaceNumPadKeyCodes(target: eventToDeal) ?? eventToDeal
// Shift+Enter delegate keyHandler
@ -207,7 +207,7 @@ extension SessionCtl {
private func imkCandidatesEventSubHandler(event: NSEvent) -> Bool {
let eventArray = [event]
guard let imkC = Self.ctlCandidateCurrent as? CtlCandidateIMK else { return false }
guard let imkC = ctlCandidateCurrent as? CtlCandidateIMK else { return false }
if event.isEsc || event.isBackSpace || event.isDelete || (event.isShiftHold && !event.isSpace) {
return commonEventHandler(event)
} else if event.isSymbolMenuPhysicalKey {

View File

@ -22,9 +22,10 @@ extension SessionCtl {
state = newState
switch state.type {
case .ofDeactivated:
Self.ctlCandidateCurrent.delegate = nil
Self.ctlCandidateCurrent.visible = false
Self.tooltipInstance.hide()
ctlCandidateCurrent.delegate = nil
ctlCandidateCurrent.visible = false
popupCompositionBuffer.hide()
tooltipInstance.hide()
if previous.hasComposition {
commit(text: previous.displayedText)
}
@ -37,29 +38,29 @@ extension SessionCtl {
state = IMEState.ofEmpty()
previous = state
}
Self.ctlCandidateCurrent.visible = false
Self.tooltipInstance.hide()
ctlCandidateCurrent.visible = false
tooltipInstance.hide()
// .Abortion
if previous.hasComposition, state.type != .ofAbortion {
commit(text: previous.displayedText)
}
//
Self.ctlCandidateCurrent.visible = false
Self.tooltipInstance.hide()
ctlCandidateCurrent.visible = false
tooltipInstance.hide()
clearInlineDisplay()
//
keyHandler.clear()
case .ofCommitting:
Self.ctlCandidateCurrent.visible = false
Self.tooltipInstance.hide()
ctlCandidateCurrent.visible = false
tooltipInstance.hide()
let textToCommit = state.textToCommit
if !textToCommit.isEmpty { commit(text: textToCommit) }
clearInlineDisplay()
//
keyHandler.clear()
case .ofInputting:
Self.ctlCandidateCurrent.visible = false
Self.tooltipInstance.hide()
ctlCandidateCurrent.visible = false
tooltipInstance.hide()
let textToCommit = state.textToCommit
if !textToCommit.isEmpty { commit(text: textToCommit) }
setInlineDisplayWithCursor()
@ -67,27 +68,27 @@ extension SessionCtl {
show(tooltip: state.tooltip)
}
case .ofMarking:
Self.ctlCandidateCurrent.visible = false
ctlCandidateCurrent.visible = false
setInlineDisplayWithCursor()
if state.tooltip.isEmpty {
Self.tooltipInstance.hide()
tooltipInstance.hide()
} else {
show(tooltip: state.tooltip)
}
case .ofCandidates, .ofAssociates, .ofSymbolTable:
Self.tooltipInstance.hide()
tooltipInstance.hide()
setInlineDisplayWithCursor()
showCandidates()
default: break
}
//
if state.hasComposition, PrefMgr.shared.clientsIMKTextInputIncapable.contains(clientBundleIdentifier) {
Self.popupCompositionBuffer.isTypingDirectionVertical = isVerticalTyping
Self.popupCompositionBuffer.show(
popupCompositionBuffer.isTypingDirectionVertical = isVerticalTyping
popupCompositionBuffer.show(
state: state, at: lineHeightRect(zeroCursor: true).origin
)
} else {
Self.popupCompositionBuffer.hide()
popupCompositionBuffer.hide()
}
}