From f9942cecccb6a3cdacd87ac58e2aaf78a85a172d Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Tue, 23 Aug 2022 09:58:18 +0800 Subject: [PATCH] IMKCandidates // Simplify certain conditions. --- .../CandidateUI/ctlCandidateIMK.swift | 120 ++++-------------- 1 file changed, 27 insertions(+), 93 deletions(-) diff --git a/Source/Modules/UIModules/CandidateUI/ctlCandidateIMK.swift b/Source/Modules/UIModules/CandidateUI/ctlCandidateIMK.swift index cc7fb02a..6a71b98e 100644 --- a/Source/Modules/UIModules/CandidateUI/ctlCandidateIMK.swift +++ b/Source/Modules/UIModules/CandidateUI/ctlCandidateIMK.swift @@ -20,15 +20,7 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol { } } - public var visible: Bool = false { - didSet { - if visible { - show() - } else { - hide() - } - } - } + public var visible: Bool = false { didSet { visible ? show() : hide() } } public var windowTopLeftPoint: NSPoint { get { @@ -124,14 +116,13 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol { private var currentPageIndex: Int = 0 private var pageCount: Int { - guard let delegate = delegate else { - return 0 - } + guard let delegate = delegate else { return 0 } let totalCount = delegate.candidateCountForController(self) let keyLabelCount = keyLabels.count return totalCount / keyLabelCount + ((totalCount % keyLabelCount) != 0 ? 1 : 0) } + // 該函式會影響 IMK 選字窗。 public func showNextPage() -> Bool { guard delegate != nil else { return false } if pageCount == 1 { return highlightNextCandidate() } @@ -139,15 +130,11 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol { currentPageIndex = (currentPageIndex + 1 >= pageCount) ? 0 : currentPageIndex + 1 if selectedCandidateIndex == candidates(self).count - 1 { return false } selectedCandidateIndex = min(selectedCandidateIndex + keyCount, candidates(self).count - 1) - switch currentLayout { - case .horizontal: - moveDown(self) - case .vertical: - moveRight(self) - } + do { currentLayout == .vertical ? moveRight(self) : moveDown(self) } return true } + // 該函式會影響 IMK 選字窗。 public func showPreviousPage() -> Bool { guard delegate != nil else { return false } if pageCount == 1 { return highlightPreviousCandidate() } @@ -155,68 +142,44 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol { currentPageIndex = (currentPageIndex == 0) ? pageCount - 1 : currentPageIndex - 1 if selectedCandidateIndex == 0 { return true } selectedCandidateIndex = max(selectedCandidateIndex - keyCount, 0) - switch currentLayout { - case .horizontal: - moveUp(self) - case .vertical: - moveLeft(self) - } + do { currentLayout == .vertical ? moveLeft(self) : moveUp(self) } return true } + // 該函式會影響 IMK 選字窗。 public func highlightNextCandidate() -> Bool { guard let delegate = delegate else { return false } selectedCandidateIndex = (selectedCandidateIndex + 1 >= delegate.candidateCountForController(self)) ? 0 : selectedCandidateIndex + 1 - switch currentLayout { - case .horizontal: - moveRight(self) - return true - case .vertical: - moveDown(self) - return true - } + do { currentLayout == .vertical ? moveDown(self) : moveRight(self) } + return true } + // 該函式會影響 IMK 選字窗。 public func highlightPreviousCandidate() -> Bool { guard let delegate = delegate else { return false } selectedCandidateIndex = (selectedCandidateIndex == 0) ? delegate.candidateCountForController(self) - 1 : selectedCandidateIndex - 1 - switch currentLayout { - case .horizontal: - moveLeft(self) - return true - case .vertical: - moveUp(self) - return true - } + do { currentLayout == .vertical ? moveUp(self) : moveLeft(self) } + return true } public func candidateIndexAtKeyLabelIndex(_ index: Int) -> Int { - guard let delegate = delegate else { - return Int.max - } - + guard let delegate = delegate else { return Int.max } let result = currentPageIndex * keyLabels.count + index return result < delegate.candidateCountForController(self) ? result : Int.max } public var selectedCandidateIndex: Int { - get { - selectedCandidate() - } - set { - selectCandidate(withIdentifier: newValue) - } + get { selectedCandidate() } + set { selectCandidate(withIdentifier: newValue) } } public func set(windowTopLeftPoint: NSPoint, bottomOutOfScreenAdjustmentHeight height: CGFloat) { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()) { - self.doSet( - windowTopLeftPoint: windowTopLeftPoint, bottomOutOfScreenAdjustmentHeight: height - ) + self.doSet(windowTopLeftPoint: windowTopLeftPoint, bottomOutOfScreenAdjustmentHeight: height) } } @@ -235,9 +198,7 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol { } } - if adjustedHeight > screenFrame.size.height / 2.0 { - adjustedHeight = 0.0 - } + if adjustedHeight > screenFrame.size.height / 2.0 { adjustedHeight = 0.0 } let windowSize = candidateFrame().size @@ -247,9 +208,7 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol { } // top over the screen? - if adjustedPoint.y >= screenFrame.maxY { - adjustedPoint.y = screenFrame.maxY - 1.0 - } + if adjustedPoint.y >= screenFrame.maxY { adjustedPoint.y = screenFrame.maxY - 1.0 } // right if adjustedPoint.x + windowSize.width >= screenFrame.maxX { @@ -257,9 +216,7 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol { } // left - if adjustedPoint.x < screenFrame.minX { - adjustedPoint.x = screenFrame.minX - } + if adjustedPoint.x < screenFrame.minX { adjustedPoint.x = screenFrame.minX } setCandidateFrameTopLeft(adjustedPoint) } @@ -274,35 +231,14 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol { if input.isEsc || input.isBackSpace || input.isDelete || (input.isShiftHold && !input.isSpace) { _ = delegate.sharedEventHandler(event) } else if input.isSymbolMenuPhysicalKey || input.isSpace { - if input.isShiftHold { - switch currentLayout { - case .horizontal: - moveUp(self) - case .vertical: - moveLeft(self) - } - } else { - switch currentLayout { - case .horizontal: - moveDown(self) - case .vertical: - moveRight(self) - } + switch currentLayout { + case .horizontal: input.isShiftHold ? moveUp(self) : moveDown(self) + case .vertical: input.isShiftHold ? moveLeft(self) : moveRight(self) } } else if input.isTab { switch currentLayout { - case .horizontal: - if input.isShiftHold { - moveLeft(self) - } else { - moveRight(self) - } - case .vertical: - if input.isShiftHold { - moveUp(self) - } else { - moveDown(self) - } + case .horizontal: input.isShiftHold ? moveLeft(self) : moveRight(self) + case .vertical: input.isShiftHold ? moveUp(self) : moveDown(self) } } else { if let newChar = ctlCandidateIMK.defaultIMKSelectionKey[event.keyCode] { @@ -334,11 +270,9 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol { } } - if mgrPrefs.useSCPCTypingMode { - if !input.isReservedKey { - _ = delegate.sharedEventHandler(event) - return - } + if mgrPrefs.useSCPCTypingMode, !input.isReservedKey { + _ = delegate.sharedEventHandler(event) + return } if delegate.isAssociatedPhrasesState,