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 areWeNerfing = false
/// ///
static var ctlCandidateCurrent: CtlCandidateProtocol = { var ctlCandidateCurrent: CtlCandidateProtocol = {
let direction: NSUserInterfaceLayoutOrientation = let direction: NSUserInterfaceLayoutOrientation =
PrefMgr.shared.useHorizontalCandidateList ? .horizontal : .vertical PrefMgr.shared.useHorizontalCandidateList ? .horizontal : .vertical
if #available(macOS 10.15, *) { 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: - // MARK: -

View File

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

View File

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

View File

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

View File

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