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

View File

@ -93,9 +93,8 @@ extension SessionCtl {
/// .NotEmpty() /// .NotEmpty()
func setInlineDisplayWithCursor() { func setInlineDisplayWithCursor() {
guard let client = client() else { return }
if state.type == .ofAssociates { if state.type == .ofAssociates {
client.setMarkedText( doSetMarkedText(
state.data.attributedStringPlaceholder, selectionRange: NSRange(location: 0, length: 0), state.data.attributedStringPlaceholder, selectionRange: NSRange(location: 0, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound) replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
) )
@ -106,7 +105,7 @@ extension SessionCtl {
/// selectionRange /// selectionRange
/// 0 replacementRangeNSNotFound /// 0 replacementRangeNSNotFound
/// ///
client.setMarkedText( doSetMarkedText(
attributedStringSecured.0, selectionRange: attributedStringSecured.1, attributedStringSecured.0, selectionRange: attributedStringSecured.1,
replacementRange: NSRange(location: NSNotFound, length: NSNotFound) replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
) )
@ -120,8 +119,7 @@ extension SessionCtl {
/// .NotEmpty() /// .NotEmpty()
/// setInlineDisplayWithCursor() /// setInlineDisplayWithCursor()
private func clearInlineDisplay() { private func clearInlineDisplay() {
guard let theClient = client() else { return } doSetMarkedText(
theClient.setMarkedText(
"", selectionRange: NSRange(location: 0, length: 0), "", selectionRange: NSRange(location: 0, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound) replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
) )
@ -135,8 +133,28 @@ extension SessionCtl {
if buffer.isEmpty { if buffer.isEmpty {
return return
} }
client.insertText( if let myID = Bundle.main.bundleIdentifier, let clientID = client.bundleIdentifier(), myID == clientID {
buffer, replacementRange: NSRange(location: NSNotFound, length: NSNotFound) 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 { public enum IMEApp {
// MARK: - // MARK: -
public static var currentInputMode: Shared.InputMode = public static var currentInputMode: Shared.InputMode {
.init(rawValue: PrefMgr.shared.mostRecentInputMode) ?? .imeModeNULL .init(rawValue: PrefMgr.shared.mostRecentInputMode) ?? .imeModeNULL
}
/// Fart or Beep? /// Fart or Beep?
static func buzz() { static func buzz() {