ctlIME // Rewrite documentation in Chinese.

- Also removes the useless composedString().
This commit is contained in:
ShikiSuen 2022-06-18 12:10:41 +08:00
parent b9107b04e5
commit ad1141a7fd
2 changed files with 136 additions and 106 deletions

View File

@ -27,29 +27,58 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import Cocoa
import InputMethodKit
///
private let kMinKeyLabelSize: CGFloat = 10
///
private var ctlCandidateCurrent = ctlCandidateUniversal.init(.horizontal)
@objc(ctlInputMethod)
///
///
/// IMKInputController
///
/// / IMKInputController
/// 調
/// - Remark: IMKServer
/// IMKInputController
@objc(ctlInputMethod) // ObjC IMK ObjC
class ctlInputMethod: IMKInputController {
@objc static var areWeDeleting = false
/// 使使
static var areWeDeleting = false
///
static let tooltipController = TooltipController()
// MARK: -
/// 調
private var keyHandler: KeyHandler = .init()
///
private var state: InputState = .Empty()
// MARK: - Keyboard Layout Specifier
// MARK: -
@objc func setKeyLayout() {
///
func setKeyLayout() {
client().overrideKeyboard(withKeyboardNamed: mgrPrefs.basicKeyboardLayout)
}
// MARK: - IMKInputController methods
/// 調
func resetKeyHandler() {
keyHandler.clear()
handle(state: InputState.Empty())
}
// MARK: - IMKInputController
///
///
/// inputClient IMKServer IMKTextInput
/// - Remark: IMKInputController client()
/// - Parameters:
/// - server: IMKServer
/// - delegate:
/// - inputClient:
override init!(server: IMKServer!, delegate: Any!, client inputClient: Any!) {
super.init(server: server, delegate: delegate, client: inputClient)
keyHandler.delegate = self
@ -58,22 +87,19 @@ class ctlInputMethod: IMKInputController {
resetKeyHandler()
}
// MARK: - KeyHandler Reset Command
func resetKeyHandler() {
keyHandler.clear()
handle(state: InputState.Empty())
}
// MARK: - IMKStateSetting protocol methods
// MARK: - IMKStateSetting
///
/// - Parameter sender: 使
override func activateServer(_ sender: Any!) {
_ = sender // Stop clang-format from ruining the parameters of this function.
_ = sender //
UserDefaults.standard.synchronize()
keyHandler.clear()
keyHandler.ensureParser()
///
/// macOS
if client().bundleIdentifier() != Bundle.main.bundleIdentifier {
// Override the keyboard layout to the basic one.
setKeyLayout()
@ -82,16 +108,23 @@ class ctlInputMethod: IMKInputController {
(NSApp.delegate as? AppDelegate)?.checkForUpdate()
}
///
/// - Parameter sender: 使
override func deactivateServer(_ sender: Any!) {
_ = sender // Stop clang-format from ruining the parameters of this function.
_ = sender //
keyHandler.clear()
handle(state: .Empty())
handle(state: .Deactivated())
}
///
/// - Parameters:
/// - value: identifier bundle identifier info.plist
/// - tag: 使
/// - sender: 使
override func setValue(_ value: Any!, forTag tag: Int, client sender: Any!) {
_ = tag // Stop clang-format from ruining the parameters of this function.
_ = sender // Stop clang-format from ruining the parameters of this function.
_ = tag //
_ = sender //
var newInputMode = InputMode(rawValue: value as? String ?? "") ?? InputMode.imeModeNULL
switch newInputMode {
case InputMode.imeModeCHS:
@ -107,6 +140,8 @@ class ctlInputMethod: IMKInputController {
UserDefaults.standard.synchronize()
keyHandler.clear()
keyHandler.inputMode = newInputMode
///
/// macOS
if client().bundleIdentifier() != Bundle.main.bundleIdentifier {
// Remember to override the keyboard layout again -- treat this as an activate event.
setKeyLayout()
@ -118,20 +153,34 @@ class ctlInputMethod: IMKInputController {
IME.currentInputMode = keyHandler.inputMode
}
// MARK: - IMKServerInput protocol methods
// MARK: - IMKServerInput
///
///
///
/// Swift `NSEvent.EventTypeMask = [.keyDown]` ObjC `NSKeyDownMask`
/// IMK
/// 使
/// `commitComposition(_ message)`
/// - Parameter sender: 使
/// - Returns: uint NSEvent NSEvent.h
override func recognizedEvents(_ sender: Any!) -> Int {
_ = sender // Stop clang-format from ruining the parameters of this function.
_ = sender //
let events: NSEvent.EventTypeMask = [.keyDown, .flagsChanged]
return Int(events.rawValue)
}
/// NSEvent
/// - Parameters:
/// - event:
/// - sender: 使
/// - Returns: `true` IMK`false`
@objc(handleEvent:client:) override func handle(_ event: NSEvent!, client sender: Any!) -> Bool {
_ = sender // Stop clang-format from ruining the parameters of this function.
// flags使 KeyHandler
// flags
// event.type == .flagsChanged return false
// NSInternalInconsistencyException
_ = sender //
/// flags使 KeyHandler
/// flags
/// event.type == .flagsChanged return false
/// NSInternalInconsistencyException
if event.type == .flagsChanged {
return false
}
@ -164,6 +213,8 @@ class ctlInputMethod: IMKInputController {
return false
}
/// 調
/// result bool IMK
let result = keyHandler.handle(input: input, state: state) { newState in
self.handle(state: newState)
} errorCallback: {
@ -172,27 +223,28 @@ class ctlInputMethod: IMKInputController {
return result
}
// App Ctrl+Enter / Shift+Enter
// handle(event:) Event
// commitComposition
/// App Ctrl+Enter / Shift+Enter
/// handle(event:) Event
/// commitComposition
/// - Parameter sender: 使
override func commitComposition(_ sender: Any!) {
_ = sender // Stop clang-format from ruining the parameters of this function.
_ = sender //
if let state = state as? InputState.NotEmpty {
/// 調
handle(state: InputState.Committing(textToCommit: state.composingBuffer))
}
resetKeyHandler()
}
//
override func composedString(_ sender: Any!) -> Any! {
_ = sender // Stop clang-format from ruining the parameters of this function.
return (state as? InputState.NotEmpty)?.composingBuffer ?? ""
}
}
// MARK: - State Handling
// MARK: - 調 (State Handling)
extension ctlInputMethod {
/// 調
///
///
///
/// - Parameter newState:
private func handle(state newState: InputState) {
let prevState = state
state = newState
@ -220,6 +272,31 @@ extension ctlInputMethod {
}
}
/// .NotEmpty()
private func setInlineDisplayWithCursor() {
guard let state = state as? InputState.NotEmpty else {
clearInlineDisplay()
return
}
/// selectionRange
/// 0 replacementRangeNSNotFound
///
client().setMarkedText(
state.attributedString, selectionRange: NSRange(location: state.cursorIndex, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
}
/// .NotEmpty()
/// setInlineDisplayWithCursor()
private func clearInlineDisplay() {
client().setMarkedText(
"", selectionRange: NSRange(location: 0, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
}
///
private func commit(text: String) {
func kanjiConversionIfRequired(_ text: String) -> String {
if keyHandler.inputMode == InputMode.imeModeCHT {
@ -256,82 +333,58 @@ extension ctlInputMethod {
}
private func handle(state: InputState.Deactivated, previous: InputState) {
_ = state // Stop clang-format from ruining the parameters of this function.
_ = state //
ctlCandidateCurrent.delegate = nil
ctlCandidateCurrent.visible = false
hideTooltip()
if let previous = previous as? InputState.NotEmpty {
commit(text: previous.composingBuffer)
}
client().setMarkedText(
"", selectionRange: NSRange(location: 0, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
clearInlineDisplay()
}
private func handle(state: InputState.Empty, previous: InputState) {
_ = state // Stop clang-format from ruining the parameters of this function.
_ = state //
ctlCandidateCurrent.visible = false
hideTooltip()
if let previous = previous as? InputState.NotEmpty,
!(state is InputState.EmptyIgnoringPreviousState)
{
commit(text: previous.composingBuffer)
}
client().setMarkedText(
"", selectionRange: NSRange(location: 0, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
clearInlineDisplay()
}
private func handle(
state: InputState.EmptyIgnoringPreviousState, previous: InputState
) {
_ = state // Stop clang-format from ruining the parameters of this function.
_ = previous // Stop clang-format from ruining the parameters of this function.
_ = state //
_ = previous //
ctlCandidateCurrent.visible = false
hideTooltip()
client().setMarkedText(
"", selectionRange: NSRange(location: 0, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
clearInlineDisplay()
}
private func handle(state: InputState.Committing, previous: InputState) {
_ = previous // Stop clang-format from ruining the parameters of this function.
_ = previous //
ctlCandidateCurrent.visible = false
hideTooltip()
let textToCommit = state.textToCommit
if !textToCommit.isEmpty {
commit(text: textToCommit)
}
client().setMarkedText(
"", selectionRange: NSRange(location: 0, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
clearInlineDisplay()
}
private func handle(state: InputState.Inputting, previous: InputState) {
_ = previous // Stop clang-format from ruining the parameters of this function.
_ = previous //
ctlCandidateCurrent.visible = false
hideTooltip()
let textToCommit = state.textToCommit
if !textToCommit.isEmpty {
commit(text: textToCommit)
}
// the selection range is where the cursor is, with the length being 0 and replacement range NSNotFound,
// i.e. the client app needs to take care of where to put this composing buffer
client().setMarkedText(
state.attributedString, selectionRange: NSRange(location: state.cursorIndex, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
setInlineDisplayWithCursor()
if !state.tooltip.isEmpty {
show(
tooltip: state.tooltip, composingBuffer: state.composingBuffer,
@ -341,16 +394,9 @@ extension ctlInputMethod {
}
private func handle(state: InputState.Marking, previous: InputState) {
_ = previous // Stop clang-format from ruining the parameters of this function.
_ = previous //
ctlCandidateCurrent.visible = false
// the selection range is where the cursor is, with the length being 0 and replacement range NSNotFound,
// i.e. the client app needs to take care of where to put this composing buffer
client().setMarkedText(
state.attributedString, selectionRange: NSRange(location: state.cursorIndex, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
setInlineDisplayWithCursor()
if state.tooltip.isEmpty {
hideTooltip()
} else {
@ -362,39 +408,23 @@ extension ctlInputMethod {
}
private func handle(state: InputState.ChoosingCandidate, previous: InputState) {
_ = previous // Stop clang-format from ruining the parameters of this function.
_ = previous //
hideTooltip()
// the selection range is where the cursor is, with the length being 0 and replacement range NSNotFound,
// i.e. the client app needs to take care of where to put this composing buffer
client().setMarkedText(
state.attributedString, selectionRange: NSRange(location: state.cursorIndex, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
setInlineDisplayWithCursor()
show(candidateWindowWith: state)
}
private func handle(state: InputState.SymbolTable, previous: InputState) {
_ = previous // Stop clang-format from ruining the parameters of this function.
_ = previous //
hideTooltip()
// the selection range is where the cursor is, with the length being 0 and replacement range NSNotFound,
// i.e. the client app needs to take care of where to put this composing buffer
client().setMarkedText(
state.attributedString, selectionRange: NSRange(location: state.cursorIndex, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
setInlineDisplayWithCursor()
show(candidateWindowWith: state)
}
private func handle(state: InputState.AssociatedPhrases, previous: InputState) {
_ = previous // Stop clang-format from ruining the parameters of this function.
_ = previous //
hideTooltip()
client().setMarkedText(
"", selectionRange: NSRange(location: 0, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
)
clearInlineDisplay()
show(candidateWindowWith: state)
}
}
@ -464,7 +494,7 @@ extension ctlInputMethod {
? "Sarasa Term Slab SC" : "Sarasa Term Slab TC"
var finalReturnFont =
NSFont(name: currentMUIFont, size: size) ?? NSFont.systemFont(ofSize: size)
// macOS 11 Big Sur macOS 12 Monterey 使
// macOS 11 Big Sur macOS 12 Monterey 使
if #available(macOS 12.0, *) { finalReturnFont = NSFont.systemFont(ofSize: size) }
if let name = name {
return NSFont(name: name, size: size) ?? finalReturnFont
@ -589,7 +619,7 @@ extension ctlInputMethod: KeyHandlerDelegate {
extension ctlInputMethod: ctlCandidateDelegate {
func candidateCountForController(_ controller: ctlCandidate) -> Int {
_ = controller // Stop clang-format from ruining the parameters of this function.
_ = controller //
if let state = state as? InputState.ChoosingCandidate {
return state.candidates.count
} else if let state = state as? InputState.AssociatedPhrases {
@ -601,7 +631,7 @@ extension ctlInputMethod: ctlCandidateDelegate {
func ctlCandidate(_ controller: ctlCandidate, candidateAtIndex index: Int)
-> String
{
_ = controller // Stop clang-format from ruining the parameters of this function.
_ = controller //
if let state = state as? InputState.ChoosingCandidate {
return state.candidates[index]
} else if let state = state as? InputState.AssociatedPhrases {
@ -611,7 +641,7 @@ extension ctlInputMethod: ctlCandidateDelegate {
}
func ctlCandidate(_ controller: ctlCandidate, didSelectCandidateAtIndex index: Int) {
_ = controller // Stop clang-format from ruining the parameters of this function.
_ = controller //
if let state = state as? InputState.SymbolTable,
let node = state.node.children?[index]

View File

@ -171,7 +171,7 @@ extension ctlInputMethod {
)
}
// NSMenu modified key
// NSMenu
setKeyLayout()
return menu