SessionCtl // Revamp certain client()-related operations.

This commit is contained in:
ShikiSuen 2022-10-06 23:37:36 +08:00
parent f4a786904e
commit 32d35ad911
3 changed files with 66 additions and 58 deletions

View File

@ -95,18 +95,26 @@ class SessionCtl: IMKInputController {
/// IME UserPrefs
var inputMode: Shared.InputMode = IMEApp.currentInputMode {
willSet {
/// Prefs IME
IMEApp.currentInputMode = newValue
PrefMgr.shared.mostRecentInputMode = IMEApp.currentInputMode.rawValue
/// Prefs IMEApp
PrefMgr.shared.mostRecentInputMode = newValue.rawValue
}
didSet {
///
keyHandler.currentLM = LMMgr.currentLM() //
keyHandler.currentUOM = LMMgr.currentUOM()
///
keyHandler.ensureKeyboardParser()
///
syncBaseLMPrefs()
if oldValue != inputMode, inputMode != .imeModeNULL {
UserDefaults.standard.synchronize()
keyHandler.clear() // handle State.Empty()
// ----------------------------
///
keyHandler.currentLM = LMMgr.currentLM() //
keyHandler.currentUOM = LMMgr.currentUOM()
///
keyHandler.ensureKeyboardParser()
///
syncBaseLMPrefs()
// ----------------------------
Self.isVerticalTyping = isVerticalTyping
// 使
handle(state: IMEState.ofEmpty())
}
}
}
@ -122,9 +130,6 @@ class SessionCtl: IMKInputController {
super.init(server: server, delegate: delegate, client: inputClient)
keyHandler.delegate = self
syncBaseLMPrefs()
//
resetKeyHandler()
activateServer(inputClient)
}
}
@ -134,11 +139,18 @@ extension SessionCtl {
///
func setKeyLayout() {
guard let client = client() else { return }
if (isASCIIMode && IMKHelper.isDynamicBasicKeyboardLayoutEnabled) || state.isCandidateContainer {
client.overrideKeyboard(withKeyboardNamed: PrefMgr.shared.alphanumericalKeyboardLayout)
return
func doSetKeyLayout() {
if (isASCIIMode && IMKHelper.isDynamicBasicKeyboardLayoutEnabled) || state.isCandidateContainer {
client.overrideKeyboard(withKeyboardNamed: PrefMgr.shared.alphanumericalKeyboardLayout)
return
}
client.overrideKeyboard(withKeyboardNamed: PrefMgr.shared.basicKeyboardLayout)
}
DispatchQueue.main.async {
doSetKeyLayout()
}
client.overrideKeyboard(withKeyboardNamed: PrefMgr.shared.basicKeyboardLayout)
}
/// 調
@ -189,17 +201,9 @@ extension SessionCtl {
}
}
///
/// macOS
if let client = client() {
Self.isVerticalTyping = isVerticalTyping
if client.bundleIdentifier() != Bundle.main.bundleIdentifier {
// 使
setKeyLayout()
handle(state: IMEState.ofEmpty())
}
} //
(NSApp.delegate as? AppDelegate)?.updateSputnik.checkForUpdate(forced: false, url: kUpdateInfoSourceURL)
DispatchQueue.main.async {
(NSApp.delegate as? AppDelegate)?.updateSputnik.checkForUpdate(forced: false, url: kUpdateInfoSourceURL)
}
}
///
@ -218,28 +222,13 @@ extension SessionCtl {
override func setValue(_ value: Any!, forTag tag: Int, client sender: Any!) {
_ = tag //
_ = sender //
var newInputMode = Shared.InputMode(rawValue: value as? String ?? "") ?? Shared.InputMode.imeModeNULL
switch newInputMode {
case .imeModeCHS:
newInputMode = .imeModeCHS
case .imeModeCHT:
newInputMode = .imeModeCHT
default:
newInputMode = .imeModeNULL
}
let newInputMode = Shared.InputMode(rawValue: value as? String ?? "") ?? Shared.InputMode.imeModeNULL
if PrefMgr.shared.onlyLoadFactoryLangModelsIfNeeded { LMMgr.loadDataModel(newInputMode) }
if inputMode != newInputMode {
UserDefaults.standard.synchronize()
keyHandler.clear() // handle State.Empty()
inputMode = newInputMode
///
/// macOS
if let client = client(), client.bundleIdentifier() != Bundle.main.bundleIdentifier {
// 使
setKeyLayout()
handle(state: IMEState.ofEmpty())
} //
inputMode = newInputMode
if let rawValString = value as? String, let bundleID = Bundle.main.bundleIdentifier,
!bundleID.isEmpty, !rawValString.contains(bundleID)
{
setKeyLayout()
}
}

View File

@ -93,9 +93,8 @@ extension SessionCtl {
/// .NotEmpty()
func setInlineDisplayWithCursor() {
guard let client = client() else { return }
if state.type == .ofAssociates {
client.setMarkedText(
doSetMarkedText(
state.data.attributedStringPlaceholder, selectionRange: NSRange(location: 0, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
@ -106,7 +105,7 @@ extension SessionCtl {
/// selectionRange
/// 0 replacementRangeNSNotFound
///
client.setMarkedText(
doSetMarkedText(
attributedStringSecured.0, selectionRange: attributedStringSecured.1,
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
@ -120,8 +119,7 @@ extension SessionCtl {
/// .NotEmpty()
/// setInlineDisplayWithCursor()
private func clearInlineDisplay() {
guard let theClient = client() else { return }
theClient.setMarkedText(
doSetMarkedText(
"", selectionRange: NSRange(location: 0, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
@ -135,8 +133,28 @@ extension SessionCtl {
if buffer.isEmpty {
return
}
client.insertText(
buffer, replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
if let myID = Bundle.main.bundleIdentifier, let clientID = client.bundleIdentifier(), myID == clientID {
DispatchQueue.main.async {
client.insertText(
buffer, replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
}
} else {
client.insertText(
buffer, replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
}
}
/// setMarkedText GCD
func doSetMarkedText(_ string: Any!, selectionRange: NSRange, replacementRange: NSRange) {
guard let client = client() else { return }
if let myID = Bundle.main.bundleIdentifier, let clientID = client.bundleIdentifier(), myID == clientID {
DispatchQueue.main.async {
client.setMarkedText(string, selectionRange: selectionRange, replacementRange: replacementRange)
}
} else {
client.setMarkedText(string, selectionRange: selectionRange, replacementRange: replacementRange)
}
}
}

View File

@ -74,8 +74,9 @@ NSApp.run()
public enum IMEApp {
// MARK: -
public static var currentInputMode: Shared.InputMode =
public static var currentInputMode: Shared.InputMode {
.init(rawValue: PrefMgr.shared.mostRecentInputMode) ?? .imeModeNULL
}
/// Fart or Beep?
static func buzz() {