SessionCtl // GCD the init() and IMK-related methods.
This commit is contained in:
parent
bd0fe1bacc
commit
8f7d452a1d
|
@ -136,19 +136,21 @@ public class SessionCtl: IMKInputController {
|
||||||
/// - inputClient: 用以接受輸入的客體應用物件
|
/// - inputClient: 用以接受輸入的客體應用物件
|
||||||
override public init!(server: IMKServer!, delegate: Any!, client inputClient: Any!) {
|
override public init!(server: IMKServer!, delegate: Any!, client inputClient: Any!) {
|
||||||
super.init(server: server, delegate: delegate, client: inputClient)
|
super.init(server: server, delegate: delegate, client: inputClient)
|
||||||
|
DispatchQueue.main.async { [self] in
|
||||||
inputHandler.delegate = self
|
inputHandler.delegate = self
|
||||||
syncBaseLMPrefs()
|
syncBaseLMPrefs()
|
||||||
// 下述兩行很有必要,否則輸入法會在手動重啟之後無法立刻生效。
|
// 下述兩行很有必要,否則輸入法會在手動重啟之後無法立刻生效。
|
||||||
activateServer(inputClient)
|
activateServer(inputClient)
|
||||||
|
// GCD 會觸發 didSet,所以不用擔心。
|
||||||
inputMode = .init(rawValue: PrefMgr.shared.mostRecentInputMode) ?? .imeModeNULL
|
inputMode = .init(rawValue: PrefMgr.shared.mostRecentInputMode) ?? .imeModeNULL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - 工具函式
|
// MARK: - 工具函式
|
||||||
|
|
||||||
extension SessionCtl {
|
extension SessionCtl {
|
||||||
/// 強制重設當前鍵盤佈局、使其與偏好設定同步。
|
/// 強制重設當前鍵盤佈局、使其與偏好設定同步。
|
||||||
/// - Remark: 該函式需要顯著的執行時間。
|
|
||||||
public func setKeyLayout() {
|
public func setKeyLayout() {
|
||||||
guard let client = client(), let myID = Bundle.main.bundleIdentifier, !myID.isEmpty,
|
guard let client = client(), let myID = Bundle.main.bundleIdentifier, !myID.isEmpty,
|
||||||
clientBundleIdentifier != myID
|
clientBundleIdentifier != myID
|
||||||
|
@ -184,6 +186,7 @@ extension SessionCtl {
|
||||||
/// - Parameter sender: 呼叫了該函式的客體(無須使用)。
|
/// - Parameter sender: 呼叫了該函式的客體(無須使用)。
|
||||||
public override func activateServer(_ sender: Any!) {
|
public override func activateServer(_ sender: Any!) {
|
||||||
_ = sender // 防止格式整理工具毀掉與此對應的參數。
|
_ = sender // 防止格式整理工具毀掉與此對應的參數。
|
||||||
|
DispatchQueue.main.async { [self] in
|
||||||
UserDefaults.standard.synchronize()
|
UserDefaults.standard.synchronize()
|
||||||
if Self.allInstances.contains(self) { return }
|
if Self.allInstances.contains(self) { return }
|
||||||
|
|
||||||
|
@ -209,16 +212,19 @@ extension SessionCtl {
|
||||||
Self.allInstances.insert(self)
|
Self.allInstances.insert(self)
|
||||||
setKeyLayout()
|
setKeyLayout()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// 停用輸入法時,會觸發該函式。
|
/// 停用輸入法時,會觸發該函式。
|
||||||
/// - Parameter sender: 呼叫了該函式的客體(無須使用)。
|
/// - Parameter sender: 呼叫了該函式的客體(無須使用)。
|
||||||
public override func deactivateServer(_ sender: Any!) {
|
public override func deactivateServer(_ sender: Any!) {
|
||||||
_ = sender // 防止格式整理工具毀掉與此對應的參數。
|
_ = sender // 防止格式整理工具毀掉與此對應的參數。
|
||||||
|
DispatchQueue.main.async { [self] in
|
||||||
isActivated = false
|
isActivated = false
|
||||||
resetInputHandler() // 這條會自動搞定 Empty 狀態。
|
resetInputHandler() // 這條會自動搞定 Empty 狀態。
|
||||||
switchState(IMEState.ofDeactivated())
|
switchState(IMEState.ofDeactivated())
|
||||||
Self.allInstances.remove(self)
|
Self.allInstances.remove(self)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// 切換至某一個輸入法的某個副本時(比如威注音的簡體輸入法副本與繁體輸入法副本),會觸發該函式。
|
/// 切換至某一個輸入法的某個副本時(比如威注音的簡體輸入法副本與繁體輸入法副本),會觸發該函式。
|
||||||
/// - Remark: 當系統呼叫 activateServer() 的時候,setValue() 會被自動呼叫。
|
/// - Remark: 當系統呼叫 activateServer() 的時候,setValue() 會被自動呼叫。
|
||||||
|
@ -230,10 +236,12 @@ extension SessionCtl {
|
||||||
public override func setValue(_ value: Any!, forTag tag: Int, client sender: Any!) {
|
public override func setValue(_ value: Any!, forTag tag: Int, client sender: Any!) {
|
||||||
_ = tag // 防止格式整理工具毀掉與此對應的參數。
|
_ = tag // 防止格式整理工具毀掉與此對應的參數。
|
||||||
_ = sender // 防止格式整理工具毀掉與此對應的參數。
|
_ = sender // 防止格式整理工具毀掉與此對應的參數。
|
||||||
|
DispatchQueue.main.async { [self] in
|
||||||
let mostRecentInputMode = PrefMgr.shared.mostRecentInputMode
|
let mostRecentInputMode = PrefMgr.shared.mostRecentInputMode
|
||||||
inputMode = .init(rawValue: value as? String ?? mostRecentInputMode) ?? .imeModeNULL
|
inputMode = .init(rawValue: value as? String ?? mostRecentInputMode) ?? .imeModeNULL
|
||||||
Self.isVerticalTyping = isVerticalTyping
|
Self.isVerticalTyping = isVerticalTyping
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// 將輸入法偏好設定同步至語言模組內。
|
/// 將輸入法偏好設定同步至語言模組內。
|
||||||
public func syncBaseLMPrefs() {
|
public func syncBaseLMPrefs() {
|
||||||
|
|
Loading…
Reference in New Issue