ctlIME // +isVerticalTyping() & guard-let the client().

This commit is contained in:
ShikiSuen 2022-08-13 20:52:40 +08:00
parent 318f27ad19
commit eab6402e31
4 changed files with 33 additions and 34 deletions

View File

@ -39,6 +39,15 @@ class ctlInputMethod: IMKInputController {
var state: InputStateProtocol = InputState.Empty()
/// ctlInputMethod
var isASCIIMode: Bool = false
///
public var isVerticalTyping: Bool {
guard let client = client() else { return false }
var textFrame = NSRect.zero
let attributes: [AnyHashable: Any]? = client.attributes(
forCharacterIndex: 0, lineHeightRectangle: &textFrame
)
return (attributes?["IMKTextOrientation"] as? NSNumber)?.intValue == 0 || false
}
/// ctlInputMethod
func toggleASCIIMode() -> Bool {
@ -54,7 +63,9 @@ class ctlInputMethod: IMKInputController {
///
func setKeyLayout() {
client().overrideKeyboard(withKeyboardNamed: mgrPrefs.basicKeyboardLayout)
if let client = client() {
client.overrideKeyboard(withKeyboardNamed: mgrPrefs.basicKeyboardLayout)
}
}
/// 調
@ -95,7 +106,8 @@ class ctlInputMethod: IMKInputController {
// activateServer nil
//
if keyHandler.delegate == nil { keyHandler.delegate = self }
setValue(IME.currentInputMode.rawValue, forTag: 114_514, client: client())
guard let client = client() else { return }
setValue(IME.currentInputMode.rawValue, forTag: 114_514, client: client)
keyHandler.clear() // handle State.Empty()
keyHandler.ensureParser()
@ -111,7 +123,7 @@ class ctlInputMethod: IMKInputController {
///
/// macOS
if client().bundleIdentifier() != Bundle.main.bundleIdentifier {
if client.bundleIdentifier() != Bundle.main.bundleIdentifier {
// 使
setKeyLayout()
handle(state: InputState.Empty())
@ -152,7 +164,7 @@ class ctlInputMethod: IMKInputController {
keyHandler.inputMode = newInputMode
///
/// macOS
if client().bundleIdentifier() != Bundle.main.bundleIdentifier {
if let client = client(), client.bundleIdentifier() != Bundle.main.bundleIdentifier {
// 使
setKeyLayout()
handle(state: InputState.Empty())
@ -221,24 +233,16 @@ class ctlInputMethod: IMKInputController {
//
ctlInputMethod.areWeNerfing = event.modifierFlags.contains([.shift, .command])
var textFrame = NSRect.zero
let attributes: [AnyHashable: Any]? = client().attributes(
forCharacterIndex: 0, lineHeightRectangle: &textFrame
)
let isTypingVertical =
(attributes?["IMKTextOrientation"] as? NSNumber)?.intValue == 0 || false
if client().bundleIdentifier()
== "org.atelierInmu.vChewing.vChewingPhraseEditor"
if let client = client(),
client.bundleIdentifier()
== "org.atelierInmu.vChewing.vChewingPhraseEditor"
{
IME.areWeUsingOurOwnPhraseEditor = true
} else {
IME.areWeUsingOurOwnPhraseEditor = false
}
var input = InputSignal(event: event, isVerticalTyping: isTypingVertical)
var input = InputSignal(event: event, isVerticalTyping: isVerticalTyping)
input.isASCIIModeInput = isASCIIMode
//

View File

@ -78,24 +78,16 @@ extension ctlInputMethod: ctlCandidateDelegate {
//
ctlInputMethod.areWeNerfing = event.modifierFlags.contains([.shift, .command])
var textFrame = NSRect.zero
let attributes: [AnyHashable: Any]? = client().attributes(
forCharacterIndex: 0, lineHeightRectangle: &textFrame
)
let isTypingVertical =
(attributes?["IMKTextOrientation"] as? NSNumber)?.intValue == 0 || false
if client().bundleIdentifier()
== "org.atelierInmu.vChewing.vChewingPhraseEditor"
if let client = client(),
client.bundleIdentifier()
== "org.atelierInmu.vChewing.vChewingPhraseEditor"
{
IME.areWeUsingOurOwnPhraseEditor = true
} else {
IME.areWeUsingOurOwnPhraseEditor = false
}
var input = InputSignal(event: event, isVerticalTyping: isTypingVertical)
var input = InputSignal(event: event, isVerticalTyping: isVerticalTyping)
input.isASCIIModeInput = isASCIIMode
//

View File

@ -15,13 +15,14 @@ import Foundation
extension ctlInputMethod {
func show(tooltip: String, composingBuffer: String, cursorIndex: Int) {
guard let client = client() else { return }
var lineHeightRect = NSRect(x: 0.0, y: 0.0, width: 16.0, height: 16.0)
var cursor = cursorIndex
if cursor == composingBuffer.count, cursor != 0 {
cursor -= 1
}
while lineHeightRect.origin.x == 0, lineHeightRect.origin.y == 0, cursor >= 0 {
client().attributes(
client.attributes(
forCharacterIndex: cursor, lineHeightRectangle: &lineHeightRect
)
cursor -= 1
@ -30,6 +31,7 @@ extension ctlInputMethod {
}
func show(candidateWindowWith state: InputStateProtocol) {
guard let client = client() else { return }
var isTypingVertical: Bool {
if let state = state as? InputState.ChoosingCandidate {
return state.isTypingVertical
@ -153,7 +155,7 @@ extension ctlInputMethod {
}
while lineHeightRect.origin.x == 0, lineHeightRect.origin.y == 0, cursor >= 0 {
client().attributes(
client.attributes(
forCharacterIndex: cursor, lineHeightRectangle: &lineHeightRect
)
cursor -= 1

View File

@ -47,8 +47,9 @@ extension ctlInputMethod {
/// .NotEmpty()
private func setInlineDisplayWithCursor() {
guard let client = client() else { return }
if let state = state as? InputState.AssociatedPhrases {
client().setMarkedText(
client.setMarkedText(
state.attributedString, selectionRange: NSRange(location: 0, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
@ -94,7 +95,7 @@ extension ctlInputMethod {
/// selectionRange
/// 0 replacementRangeNSNotFound
///
client().setMarkedText(
client.setMarkedText(
state.attributedString, selectionRange: NSRange(location: state.cursorIndex, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
@ -113,12 +114,12 @@ extension ctlInputMethod {
///
/// IMK commitComposition
private func commit(text: String) {
guard let client = client() else { return }
let buffer = IME.kanjiConversionIfRequired(text)
if buffer.isEmpty {
return
}
client().insertText(
client.insertText(
buffer, replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
}