vChewing-macOS/Source/Modules/UIModules/CandidateUI/ctlCandidateIMK.swift

288 lines
11 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.
public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol {
public var currentLayout: CandidateLayout = .horizontal
public static let defaultIMKSelectionKey: [UInt16: String] = [
18: "1", 19: "2", 20: "3", 21: "4", 23: "5", 22: "6", 26: "7", 28: "8", 25: "9",
]
public weak var delegate: ctlCandidateDelegate? {
didSet {
reloadData()
}
}
public var visible = false { didSet { visible ? show() : hide() } }
public var windowTopLeftPoint: NSPoint {
get {
let frameRect = candidateFrame()
return NSPoint(x: frameRect.minX, y: frameRect.maxY)
}
set {
DispatchQueue.main.async {
self.set(windowTopLeftPoint: newValue, bottomOutOfScreenAdjustmentHeight: 0)
}
}
}
public var keyLabels: [CandidateKeyLabel] = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
.map {
CandidateKeyLabel(key: $0, displayedText: $0)
}
public var keyLabelFont = NSFont.monospacedDigitSystemFont(
ofSize: 14, weight: .medium
)
public var candidateFont = NSFont.systemFont(ofSize: PrefMgr.shared.candidateListTextSize) {
didSet {
if #available(macOS 10.14, *) { setFontSize(candidateFont.pointSize) }
var attributes = attributes()
// FB11300759: Set "NSAttributedString.Key.font" doesn't work.
attributes?[NSAttributedString.Key.font] = candidateFont
if PrefMgr.shared.handleDefaultCandidateFontsByLangIdentifier {
switch IMEApp.currentInputMode {
case .imeModeCHS:
if #available(macOS 12.0, *) {
attributes?[NSAttributedString.Key.languageIdentifier] = "zh-Hans" as AnyObject
}
case .imeModeCHT:
if #available(macOS 12.0, *) {
attributes?[NSAttributedString.Key.languageIdentifier] =
(PrefMgr.shared.shiftJISShinjitaiOutputEnabled || PrefMgr.shared.chineseConversionEnabled)
? "ja" as AnyObject : "zh-Hant" as AnyObject
}
default:
break
}
}
setAttributes(attributes)
update()
}
}
public var tooltip: String = ""
var keyCount = 0
var displayedCandidates = [String]()
public func specifyLayout(_ layout: CandidateLayout = .horizontal) {
currentLayout = layout
switch currentLayout {
case .horizontal:
if #available(macOS 10.14, *) {
setPanelType(kIMKScrollingGridCandidatePanel)
} else {
// macOS 10.13 High Sierra
setPanelType(kIMKSingleRowSteppingCandidatePanel)
}
case .vertical:
setPanelType(kIMKSingleColumnScrollingCandidatePanel)
}
}
public required init(_ layout: CandidateLayout = .horizontal) {
super.init(server: theServer, panelType: kIMKScrollingGridCandidatePanel)
specifyLayout(layout)
// true ctlIME
setAttributes([IMKCandidatesSendServerKeyEventFirst: true])
visible = false
// guard let currentTISInputSource = currentTISInputSource else { return } //
// setSelectionKeys([18, 19, 20, 21, 23, 22, 26, 28, 25]) //
// setSelectionKeysKeylayout(currentTISInputSource) //
}
@available(*, unavailable)
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public func reloadData() {
// guard let delegate = delegate else { return } //
//
// setCandidateData(candidates) //
keyCount = selectionKeys().count
selectedCandidateIndex = 0
update()
}
/// IMKCandidates
/// IMK keyHandler `ctlIME_Core`
private var currentPageIndex: Int = 0
private var pageCount: Int {
guard let delegate = delegate else { return 0 }
let totalCount = delegate.candidateCountForController(self)
let keyLabelCount = keyLabels.count
return totalCount / keyLabelCount + ((totalCount % keyLabelCount) != 0 ? 1 : 0)
}
// IMK
@discardableResult public func showNextPage() -> Bool {
do { currentLayout == .vertical ? moveRight(self) : moveDown(self) }
return true
}
// IMK
@discardableResult public func showPreviousPage() -> Bool {
do { currentLayout == .vertical ? moveLeft(self) : moveUp(self) }
return true
}
// IMK
@discardableResult public func highlightNextCandidate() -> Bool {
do { currentLayout == .vertical ? moveDown(self) : moveRight(self) }
return true
}
// IMK
@discardableResult public func highlightPreviousCandidate() -> Bool {
do { currentLayout == .vertical ? moveUp(self) : moveLeft(self) }
return true
}
public func candidateIndexAtKeyLabelIndex(_ index: Int) -> Int {
guard let delegate = delegate else { return Int.max }
let result = currentPageIndex * keyLabels.count + index
return result < delegate.candidateCountForController(self) ? result : Int.max
}
public var selectedCandidateIndex: Int {
get { selectedCandidate() }
set { selectCandidate(withIdentifier: newValue) }
}
public func set(windowTopLeftPoint: NSPoint, bottomOutOfScreenAdjustmentHeight height: CGFloat) {
DispatchQueue.main.async {
self.doSet(windowTopLeftPoint: windowTopLeftPoint, bottomOutOfScreenAdjustmentHeight: height)
}
}
func doSet(windowTopLeftPoint: NSPoint, bottomOutOfScreenAdjustmentHeight heightDelta: CGFloat) {
var adjustedPoint = windowTopLeftPoint
let windowSize = candidateFrame().size
var delta = heightDelta
var screenFrame = NSScreen.main?.visibleFrame ?? NSRect.seniorTheBeast
for frame in NSScreen.screens.map(\.visibleFrame).filter({ $0.contains(windowTopLeftPoint) }) {
screenFrame = frame
break
}
if delta > screenFrame.size.height / 2.0 { delta = 0.0 }
if adjustedPoint.y < screenFrame.minY + windowSize.height {
adjustedPoint.y = windowTopLeftPoint.y + windowSize.height + delta
}
adjustedPoint.y = min(adjustedPoint.y, screenFrame.maxY - 1.0)
adjustedPoint.x = min(max(adjustedPoint.x, screenFrame.minX), screenFrame.maxX - windowSize.width - 1.0)
setCandidateFrameTopLeft(adjustedPoint)
}
override public func interpretKeyEvents(_ eventArray: [NSEvent]) {
//
// Objective-C nil
guard !eventArray.isEmpty else { return }
let event = eventArray[0]
guard let delegate = delegate else { return }
if event.isEsc || event.isBackSpace || event.isDelete || (event.isShiftHold && !event.isSpace) {
_ = delegate.sharedEventHandler(event)
} else if event.isSymbolMenuPhysicalKey {
//
switch currentLayout {
case .horizontal: event.isShiftHold ? moveUp(self) : moveDown(self)
case .vertical: event.isShiftHold ? moveLeft(self) : moveRight(self)
}
} else if event.isSpace {
switch PrefMgr.shared.specifyShiftSpaceKeyBehavior {
case true: _ = event.isShiftHold ? highlightNextCandidate() : showNextPage()
case false: _ = event.isShiftHold ? showNextPage() : highlightNextCandidate()
}
} else if event.isTab {
switch PrefMgr.shared.specifyShiftTabKeyBehavior {
case true: _ = event.isShiftHold ? showPreviousPage() : showNextPage()
case false: _ = event.isShiftHold ? highlightPreviousCandidate() : highlightNextCandidate()
}
} else {
if let newChar = Self.defaultIMKSelectionKey[event.keyCode] {
/// KeyCode NSEvent Character
/// IMK
let newEvent = event.reinitiate(characters: newChar)
if let newEvent = newEvent {
if PrefMgr.shared.useSCPCTypingMode, delegate.isAssociatedPhrasesState {
// input.isShiftHold ctlInputMethod.handle()
if !event.isShiftHold {
_ = delegate.sharedEventHandler(event)
return
}
} else {
if #available(macOS 10.14, *) {
handleKeyboardEvent(newEvent)
} else {
super.interpretKeyEvents([newEvent])
}
return
}
}
}
if PrefMgr.shared.useSCPCTypingMode, !event.isReservedKey {
_ = delegate.sharedEventHandler(event)
return
}
if delegate.isAssociatedPhrasesState,
!event.isPageUp, !event.isPageDown, !event.isCursorForward, !event.isCursorBackward,
!event.isCursorClockLeft, !event.isCursorClockRight, !event.isSpace,
!event.isEnter || !PrefMgr.shared.alsoConfirmAssociatedCandidatesByEnter
{
_ = delegate.sharedEventHandler(event)
return
}
super.interpretKeyEvents(eventArray)
}
}
public func superInterpretKeyEvents(_ eventArray: [NSEvent]) {
super.interpretKeyEvents(eventArray)
}
}
// MARK: - Generate TISInputSource Object
/// "com.apple.keylayout.ABC" TISInputSource
/// 西
/// IME.swift
var currentTISInputSource: TISInputSource? {
var result: TISInputSource?
let list = TISCreateInputSourceList(nil, true).takeRetainedValue() as! [TISInputSource]
let matchedTISString = "com.apple.keylayout.ABC"
for source in list {
guard let ptrCat = TISGetInputSourceProperty(source, kTISPropertyInputSourceCategory) else { continue }
let category = Unmanaged<CFString>.fromOpaque(ptrCat).takeUnretainedValue()
guard category == kTISCategoryKeyboardInputSource else { continue }
guard let ptrSourceID = TISGetInputSourceProperty(source, kTISPropertyInputSourceID) else { continue }
let sourceID = String(Unmanaged<CFString>.fromOpaque(ptrSourceID).takeUnretainedValue())
if sourceID == matchedTISString { result = source }
}
return result
}
// MARK: - Translating NumPad KeyCodes to Default IMK Candidate Selection KeyCodes.
extension ctlCandidateIMK {
public static func replaceNumPadKeyCodes(target event: NSEvent) -> NSEvent? {
let mapNumPadKeyCodeTranslation: [UInt16: UInt16] = [
83: 18, 84: 19, 85: 20, 86: 21, 87: 23, 88: 22, 89: 26, 91: 28, 92: 25,
]
return event.reinitiate(keyCode: mapNumPadKeyCodeTranslation[event.keyCode] ?? event.keyCode)
}
}