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

View File

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

View File

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

View File

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