vChewing-macOS/Source/Modules/ControllerModules/ctlInputMethod_HandleDispla...

208 lines
10 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) 2011 and onwards The OpenVanilla Project (MIT License).
// All possible vChewing-specific modifications are of:
// (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 Cocoa
// MARK: - Tooltip Display and Candidate Display Methods
extension ctlInputMethod {
// App 使
// App App
// 使 20
var attributedStringSecured: (NSAttributedString, NSRange) {
mgrPrefs.clientsIMKTextInputIncapable.contains(clientBundleIdentifier)
? (state.data.attributedStringPlaceholder, NSRange(location: 0, length: 0))
: (state.attributedString, NSRange(state.data.u16MarkedRange))
}
func lineHeightRect(zeroCursor: Bool = false) -> NSRect {
var lineHeightRect = NSRect.seniorTheBeast
guard let client = client() else {
return lineHeightRect
}
var u16Cursor: Int = {
// iMessage cursor == 0
if clientBundleIdentifier == "com.apple.MobileSMS" { return state.data.u16Cursor }
if state.data.marker >= state.data.cursor { return state.data.u16Cursor }
return state.data.u16Marker //
}()
u16Cursor = max(min(state.data.displayedTextConverted.utf16.count, u16Cursor), 0)
if zeroCursor { u16Cursor = 0 }
while lineHeightRect.origin.x == 0, lineHeightRect.origin.y == 0, u16Cursor >= 0 {
client.attributes(
forCharacterIndex: u16Cursor, lineHeightRectangle: &lineHeightRect
)
u16Cursor -= 1
}
return lineHeightRect
}
func show(tooltip: String) {
guard client() != nil else { return }
let lineHeightRect = lineHeightRect()
var finalOrigin: NSPoint = lineHeightRect.origin
let delta: CGFloat = lineHeightRect.size.height + 4.0 // bottomOutOfScreenAdjustmentHeight
if isVerticalTyping {
finalOrigin = NSPoint(
x: lineHeightRect.origin.x + lineHeightRect.size.width + 5, y: lineHeightRect.origin.y
)
}
let tooltipContentDirection: NSAttributedTextView.writingDirection = {
if mgrPrefs.alwaysShowTooltipTextsHorizontally { return .horizontal }
return isVerticalTyping ? .vertical : .horizontal
}()
// NSAttributedTextView
do {
ctlInputMethod.tooltipInstance.hide()
ctlInputMethod.tooltipInstance = .init()
if state.type == .ofMarking {
ctlInputMethod.tooltipInstance.setColor(state: state.data.tooltipColorState)
}
}
//
ctlInputMethod.tooltipInstance.show(
tooltip: tooltip, at: finalOrigin,
bottomOutOfScreenAdjustmentHeight: delta, direction: tooltipContentDirection
)
}
func show(candidateWindowWith state: IMEStateProtocol) {
guard let client = client() else { return }
var isCandidateWindowVertical: Bool {
var candidates: [(String, String)] = .init()
if state.isCandidateContainer {
candidates = state.candidates
}
if isVerticalTyping { return true }
// IMK
guard ctlInputMethod.ctlCandidateCurrent is ctlCandidateUniversal else { return false }
// 使
//
// 使
// Beer emoji
let maxCandidatesPerPage = mgrPrefs.candidateKeys.count
let firstPageCandidates = candidates[0..<min(maxCandidatesPerPage, candidates.count)].map(\.1)
return firstPageCandidates.joined().count > Int(round(Double(maxCandidatesPerPage) * 1.8))
// true
}
ctlInputMethod.isVerticalCandidateSituation = (isCandidateWindowVertical || !mgrPrefs.useHorizontalCandidateList)
ctlInputMethod.ctlCandidateCurrent.delegate = nil
/// currentLayout
/// ctlCandidate() SymbolTable
///
/// layoutCandidateView
/// macOS 10.x SwiftUI
let candidateLayout: CandidateLayout =
((isCandidateWindowVertical || !mgrPrefs.useHorizontalCandidateList)
? CandidateLayout.vertical
: CandidateLayout.horizontal)
ctlInputMethod.ctlCandidateCurrent =
mgrPrefs.useIMKCandidateWindow
? ctlCandidateIMK.init(candidateLayout) : ctlCandidateUniversal.init(candidateLayout)
// set the attributes for the candidate panel (which uses NSAttributedString)
let textSize = mgrPrefs.candidateListTextSize
let keyLabelSize = max(textSize / 2, mgrPrefs.minKeyLabelSize)
func labelFont(name: String?, size: CGFloat) -> NSFont {
if let name = name {
return NSFont(name: name, size: size) ?? NSFont.systemFont(ofSize: size)
}
return NSFont.systemFont(ofSize: size)
}
ctlInputMethod.ctlCandidateCurrent.keyLabelFont = labelFont(
name: mgrPrefs.candidateKeyLabelFontName, size: keyLabelSize
)
ctlInputMethod.ctlCandidateCurrent.candidateFont = ctlInputMethod.candidateFont(
name: mgrPrefs.candidateTextFontName, size: textSize
)
let candidateKeys = mgrPrefs.candidateKeys
let keyLabels =
candidateKeys.count > 4 ? Array(candidateKeys) : Array(mgrPrefs.defaultCandidateKeys)
let keyLabelSuffix = state.type == .ofAssociates ? "^" : ""
ctlInputMethod.ctlCandidateCurrent.keyLabels = keyLabels.map {
CandidateKeyLabel(key: String($0), displayedText: String($0) + keyLabelSuffix)
}
ctlInputMethod.ctlCandidateCurrent.delegate = self
ctlInputMethod.ctlCandidateCurrent.reloadData()
// Spotlight IMK
if let ctlCandidateCurrent = ctlInputMethod.ctlCandidateCurrent as? ctlCandidateIMK {
while ctlCandidateCurrent.windowLevel() <= client.windowLevel() {
ctlCandidateCurrent.setWindowLevel(UInt64(max(0, client.windowLevel() + 1000)))
}
}
ctlInputMethod.ctlCandidateCurrent.visible = true
if isVerticalTyping {
ctlInputMethod.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 {
ctlInputMethod.ctlCandidateCurrent.set(
windowTopLeftPoint: NSPoint(x: lineHeightRect().origin.x, y: lineHeightRect().origin.y - 4.0),
bottomOutOfScreenAdjustmentHeight: lineHeightRect().size.height + 4.0
)
}
}
/// FB10978412: Since macOS 11 Big Sur, CTFontCreateUIFontForLanguage cannot
/// distinguish zh-Hans and zh-Hant with correct adoptation of proper PingFang SC/TC variants.
///
/// Instructions for Apple Developer relations to reveal this bug:
///
/// 0) Disable IMK Candidate window in the vChewing preferences (disabled by default).
/// **REASON**: IMKCandidates has bug that it does not respect font attributes attached to the
/// results generated from `candidiates() -> [Any]!` function. IMKCandidates is plagued with
/// bugs which are not dealt in the recent decade, regardless Radar complaints from input method developers.
/// 1) Remove the usage of ".languageIdentifier" from ctlCandidateUniversal.swift (already done).
/// 2) Run "make update" in the project folder to download the latest git-submodule of dictionary file.
/// 3) Compile the target "vChewingInstaller", run it. It will install the input method into
/// "~/Library/Input Methods/" folder. Remember to ENABLE BOTH "vChewing-CHS"
/// and "vChewing-CHT" input sources in System Preferences / Settings.
/// 4) Type Zhuyin "ej3" (ˇ) (or "gu3" in Pinyin if you enabled Pinyin typing in vChewing preferences.)
/// using both "vChewing-CHS" and "vChewing-CHT", and check the candidate window by pressing SPACE key.
/// 5) Do NOT enable either KangXi conversion mode nor JIS conversion mode. They are disabled by default.
/// 6) Expecting the glyph differences of the candidate "" between PingFang SC and PingFang TC when rendering
/// the candidate window in different "vChewing-CHS" and "vChewing-CHT" input modes.
static func candidateFont(name: String? = nil, size: CGFloat) -> NSFont {
let finalReturnFont: NSFont =
{
switch IME.currentInputMode {
case InputMode.imeModeCHS:
return CTFontCreateUIFontForLanguage(.system, size, "zh-Hans" as CFString)
case InputMode.imeModeCHT:
return (mgrPrefs.shiftJISShinjitaiOutputEnabled || mgrPrefs.chineseConversionEnabled)
? CTFontCreateUIFontForLanguage(.system, size, "ja" as CFString)
: CTFontCreateUIFontForLanguage(.system, size, "zh-Hant" as CFString)
default:
return CTFontCreateUIFontForLanguage(.system, size, nil)
}
}()
?? NSFont.systemFont(ofSize: size)
if let name = name, !name.isEmpty {
return NSFont(name: name, size: size) ?? finalReturnFont
}
return finalReturnFont
}
}