diff --git a/Source/Modules/ControllerModules/KeyHandler_Core.swift b/Source/Modules/ControllerModules/KeyHandler_Core.swift index a12349a5..3374829c 100644 --- a/Source/Modules/ControllerModules/KeyHandler_Core.swift +++ b/Source/Modules/ControllerModules/KeyHandler_Core.swift @@ -35,10 +35,10 @@ public enum InputMode: String { // MARK: - Delegate. protocol KeyHandlerDelegate { - func ctlCandidate(for _: KeyHandler) -> Any + func ctlCandidate() -> ctlCandidate func keyHandler( _: KeyHandler, didSelectCandidateAt index: Int, - ctlCandidate controller: Any + ctlCandidate controller: ctlCandidate ) func keyHandler(_ keyHandler: KeyHandler, didRequestWriteUserPhraseWith state: InputState) -> Bool diff --git a/Source/Modules/ControllerModules/KeyHandler_HandleCandidate.swift b/Source/Modules/ControllerModules/KeyHandler_HandleCandidate.swift index 8b9e6eae..a770892c 100644 --- a/Source/Modules/ControllerModules/KeyHandler_HandleCandidate.swift +++ b/Source/Modules/ControllerModules/KeyHandler_HandleCandidate.swift @@ -37,7 +37,7 @@ extension KeyHandler { ) -> Bool { let inputText = input.inputText let charCode: UniChar = input.charCode - if let ctlCandidateCurrent = delegate!.ctlCandidate(for: self) as? ctlCandidate { + if let ctlCandidateCurrent = delegate?.ctlCandidate() { // MARK: Cancel Candidate let cancelCandidateKey = @@ -138,18 +138,21 @@ extension KeyHandler { // MARK: Left Arrow if input.isLeft { - if ctlCandidateCurrent is ctlCandidateHorizontal { - let updated: Bool = ctlCandidateCurrent.highlightPreviousCandidate() - if !updated { - IME.prtDebugIntel("1145148D") - errorCallback() - } - } else { - let updated: Bool = ctlCandidateCurrent.showPreviousPage() - if !updated { - IME.prtDebugIntel("1919810D") - errorCallback() - } + switch ctlCandidateCurrent.currentLayout { + case .horizontal: + do { + if !ctlCandidateCurrent.highlightPreviousCandidate() { + IME.prtDebugIntel("1145148D") + errorCallback() + } + } + case .vertical: + do { + if !ctlCandidateCurrent.showPreviousPage() { + IME.prtDebugIntel("1919810D") + errorCallback() + } + } } return true } @@ -168,18 +171,21 @@ extension KeyHandler { // MARK: Right Arrow if input.isRight { - if ctlCandidateCurrent is ctlCandidateHorizontal { - let updated: Bool = ctlCandidateCurrent.highlightNextCandidate() - if !updated { - IME.prtDebugIntel("9B65138D") - errorCallback() - } - } else { - let updated: Bool = ctlCandidateCurrent.showNextPage() - if !updated { - IME.prtDebugIntel("9244908D") - errorCallback() - } + switch ctlCandidateCurrent.currentLayout { + case .horizontal: + do { + if !ctlCandidateCurrent.highlightNextCandidate() { + IME.prtDebugIntel("9B65138D") + errorCallback() + } + } + case .vertical: + do { + if !ctlCandidateCurrent.showNextPage() { + IME.prtDebugIntel("9244908D") + errorCallback() + } + } } return true } @@ -198,18 +204,21 @@ extension KeyHandler { // MARK: Up Arrow if input.isUp { - if ctlCandidateCurrent is ctlCandidateHorizontal { - let updated: Bool = ctlCandidateCurrent.showPreviousPage() - if !updated { - IME.prtDebugIntel("9B614524") - errorCallback() - } - } else { - let updated: Bool = ctlCandidateCurrent.highlightPreviousCandidate() - if !updated { - IME.prtDebugIntel("ASD9908D") - errorCallback() - } + switch ctlCandidateCurrent.currentLayout { + case .horizontal: + do { + if !ctlCandidateCurrent.showPreviousPage() { + IME.prtDebugIntel("9B614524") + errorCallback() + } + } + case .vertical: + do { + if !ctlCandidateCurrent.highlightPreviousCandidate() { + IME.prtDebugIntel("ASD9908D") + errorCallback() + } + } } return true } @@ -217,18 +226,21 @@ extension KeyHandler { // MARK: Down Arrow if input.isDown { - if ctlCandidateCurrent is ctlCandidateHorizontal { - let updated: Bool = ctlCandidateCurrent.showNextPage() - if !updated { - IME.prtDebugIntel("92B990DD") - errorCallback() - } - } else { - let updated: Bool = ctlCandidateCurrent.highlightNextCandidate() - if !updated { - IME.prtDebugIntel("6B99908D") - errorCallback() - } + switch ctlCandidateCurrent.currentLayout { + case .horizontal: + do { + if !ctlCandidateCurrent.showNextPage() { + IME.prtDebugIntel("92B990DD") + errorCallback() + } + } + case .vertical: + do { + if !ctlCandidateCurrent.highlightNextCandidate() { + IME.prtDebugIntel("6B99908D") + errorCallback() + } + } } return true } diff --git a/Source/Modules/IMEModules/ctlInputMethod.swift b/Source/Modules/IMEModules/ctlInputMethod.swift index e52e7e69..edd0a42e 100644 --- a/Source/Modules/IMEModules/ctlInputMethod.swift +++ b/Source/Modules/IMEModules/ctlInputMethod.swift @@ -29,12 +29,7 @@ import InputMethodKit private let kMinKeyLabelSize: CGFloat = 10 -private var ctlCandidateCurrent: ctlCandidate? - -extension ctlCandidate { - static let horizontal = ctlCandidateUniversal(.horizontal) - static let vertical = ctlCandidateUniversal(.vertical) -} +private var ctlCandidateCurrent = ctlCandidateUniversal.init(.horizontal) @objc(ctlInputMethod) class ctlInputMethod: IMKInputController { @@ -286,8 +281,8 @@ extension ctlInputMethod { _ = state // Stop clang-format from ruining the parameters of this function. currentClient = nil - ctlCandidateCurrent?.delegate = nil - ctlCandidateCurrent?.visible = false + ctlCandidateCurrent.delegate = nil + ctlCandidateCurrent.visible = false hideTooltip() if let previous = previous as? InputState.NotEmpty { @@ -301,7 +296,7 @@ extension ctlInputMethod { private func handle(state: InputState.Empty, previous: InputState, client: Any?) { _ = state // Stop clang-format from ruining the parameters of this function. - ctlCandidateCurrent?.visible = false + ctlCandidateCurrent.visible = false hideTooltip() guard let client = client as? IMKTextInput else { @@ -324,7 +319,7 @@ extension ctlInputMethod { ) { _ = state // Stop clang-format from ruining the parameters of this function. _ = previous // Stop clang-format from ruining the parameters of this function. - ctlCandidateCurrent?.visible = false + ctlCandidateCurrent.visible = false hideTooltip() guard let client = client as? IMKTextInput else { @@ -339,7 +334,7 @@ extension ctlInputMethod { private func handle(state: InputState.Committing, previous: InputState, client: Any?) { _ = previous // Stop clang-format from ruining the parameters of this function. - ctlCandidateCurrent?.visible = false + ctlCandidateCurrent.visible = false hideTooltip() guard let client = client as? IMKTextInput else { @@ -358,7 +353,7 @@ extension ctlInputMethod { private func handle(state: InputState.Inputting, previous: InputState, client: Any?) { _ = previous // Stop clang-format from ruining the parameters of this function. - ctlCandidateCurrent?.visible = false + ctlCandidateCurrent.visible = false hideTooltip() guard let client = client as? IMKTextInput else { @@ -386,7 +381,7 @@ extension ctlInputMethod { private func handle(state: InputState.Marking, previous: InputState, client: Any?) { _ = previous // Stop clang-format from ruining the parameters of this function. - ctlCandidateCurrent?.visible = false + ctlCandidateCurrent.visible = false guard let client = client as? IMKTextInput else { hideTooltip() return @@ -413,7 +408,7 @@ extension ctlInputMethod { _ = previous // Stop clang-format from ruining the parameters of this function. hideTooltip() guard let client = client as? IMKTextInput else { - ctlCandidateCurrent?.visible = false + ctlCandidateCurrent.visible = false return } @@ -430,7 +425,7 @@ extension ctlInputMethod { _ = previous // Stop clang-format from ruining the parameters of this function. hideTooltip() guard let client = client as? IMKTextInput else { - ctlCandidateCurrent?.visible = false + ctlCandidateCurrent.visible = false return } @@ -447,7 +442,7 @@ extension ctlInputMethod { _ = previous // Stop clang-format from ruining the parameters of this function. hideTooltip() guard let client = client as? IMKTextInput else { - ctlCandidateCurrent?.visible = false + ctlCandidateCurrent.visible = false return } client.setMarkedText( @@ -484,14 +479,14 @@ extension ctlInputMethod { // 上面這句如果是 true 的話,就會是縱排;反之則為橫排。 } - ctlCandidateCurrent?.delegate = nil + ctlCandidateCurrent.delegate = nil if useVerticalMode { - ctlCandidateCurrent = .vertical + ctlCandidateCurrent.currentLayout = .vertical } else if mgrPrefs.useHorizontalCandidateList { - ctlCandidateCurrent = .horizontal + ctlCandidateCurrent.currentLayout = .horizontal } else { - ctlCandidateCurrent = .vertical + ctlCandidateCurrent.currentLayout = .vertical } // set the attributes for the candidate panel (which uses NSAttributedString) @@ -519,10 +514,10 @@ extension ctlInputMethod { return finalReturnFont } - ctlCandidateCurrent?.keyLabelFont = labelFont( + ctlCandidateCurrent.keyLabelFont = labelFont( name: mgrPrefs.candidateKeyLabelFontName, size: keyLabelSize ) - ctlCandidateCurrent?.candidateFont = candidateFont( + ctlCandidateCurrent.candidateFont = candidateFont( name: mgrPrefs.candidateTextFontName, size: textSize ) @@ -530,15 +525,15 @@ extension ctlInputMethod { let keyLabels = candidateKeys.count > 4 ? Array(candidateKeys) : Array(mgrPrefs.defaultCandidateKeys) let keyLabelSuffix = state is InputState.AssociatedPhrases ? "^" : "" - ctlCandidateCurrent?.keyLabels = keyLabels.map { + ctlCandidateCurrent.keyLabels = keyLabels.map { CandidateKeyLabel(key: String($0), displayedText: String($0) + keyLabelSuffix) } - ctlCandidateCurrent?.delegate = self - ctlCandidateCurrent?.reloadData() + ctlCandidateCurrent.delegate = self + ctlCandidateCurrent.reloadData() currentClient = client - ctlCandidateCurrent?.visible = true + ctlCandidateCurrent.visible = true var lineHeightRect = NSRect(x: 0.0, y: 0.0, width: 16.0, height: 16.0) var cursor = 0 @@ -558,14 +553,14 @@ extension ctlInputMethod { } if useVerticalMode { - ctlCandidateCurrent?.set( + ctlCandidateCurrent.set( windowTopLeftPoint: NSPoint( x: lineHeightRect.origin.x + lineHeightRect.size.width + 4.0, y: lineHeightRect.origin.y - 4.0 ), bottomOutOfScreenAdjustmentHeight: lineHeightRect.size.height + 4.0 ) } else { - ctlCandidateCurrent?.set( + ctlCandidateCurrent.set( windowTopLeftPoint: NSPoint(x: lineHeightRect.origin.x, y: lineHeightRect.origin.y - 4.0), bottomOutOfScreenAdjustmentHeight: lineHeightRect.size.height + 4.0 ) @@ -595,19 +590,13 @@ extension ctlInputMethod { // MARK: - extension ctlInputMethod: KeyHandlerDelegate { - func ctlCandidate(for keyHandler: KeyHandler) -> Any { - _ = keyHandler // Stop clang-format from ruining the parameters of this function. - return ctlCandidateCurrent ?? .vertical - } + func ctlCandidate() -> ctlCandidate { ctlCandidateCurrent } func keyHandler( - _ keyHandler: KeyHandler, didSelectCandidateAt index: Int, - ctlCandidate controller: Any + _: KeyHandler, didSelectCandidateAt index: Int, + ctlCandidate controller: ctlCandidate ) { - _ = keyHandler // Stop clang-format from ruining the parameters of this function. - if let controller = controller as? ctlCandidate { - ctlCandidate(controller, didSelectCandidateAtIndex: index) - } + ctlCandidate(controller, didSelectCandidateAtIndex: index) } func keyHandler(_ keyHandler: KeyHandler, didRequestWriteUserPhraseWith state: InputState)