IMKCandidates // Simplify certain conditions.

This commit is contained in:
ShikiSuen 2022-08-23 09:58:18 +08:00
parent f58b4d37c5
commit 15af0f74ef
1 changed files with 27 additions and 93 deletions

View File

@ -20,15 +20,7 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol {
} }
} }
public var visible: Bool = false { public var visible: Bool = false { didSet { visible ? show() : hide() } }
didSet {
if visible {
show()
} else {
hide()
}
}
}
public var windowTopLeftPoint: NSPoint { public var windowTopLeftPoint: NSPoint {
get { get {
@ -124,14 +116,13 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol {
private var currentPageIndex: Int = 0 private var currentPageIndex: Int = 0
private var pageCount: Int { private var pageCount: Int {
guard let delegate = delegate else { guard let delegate = delegate else { return 0 }
return 0
}
let totalCount = delegate.candidateCountForController(self) let totalCount = delegate.candidateCountForController(self)
let keyLabelCount = keyLabels.count let keyLabelCount = keyLabels.count
return totalCount / keyLabelCount + ((totalCount % keyLabelCount) != 0 ? 1 : 0) return totalCount / keyLabelCount + ((totalCount % keyLabelCount) != 0 ? 1 : 0)
} }
// IMK
public func showNextPage() -> Bool { public func showNextPage() -> Bool {
guard delegate != nil else { return false } guard delegate != nil else { return false }
if pageCount == 1 { return highlightNextCandidate() } if pageCount == 1 { return highlightNextCandidate() }
@ -139,15 +130,11 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol {
currentPageIndex = (currentPageIndex + 1 >= pageCount) ? 0 : currentPageIndex + 1 currentPageIndex = (currentPageIndex + 1 >= pageCount) ? 0 : currentPageIndex + 1
if selectedCandidateIndex == candidates(self).count - 1 { return false } if selectedCandidateIndex == candidates(self).count - 1 { return false }
selectedCandidateIndex = min(selectedCandidateIndex + keyCount, candidates(self).count - 1) selectedCandidateIndex = min(selectedCandidateIndex + keyCount, candidates(self).count - 1)
switch currentLayout { do { currentLayout == .vertical ? moveRight(self) : moveDown(self) }
case .horizontal:
moveDown(self)
case .vertical:
moveRight(self)
}
return true return true
} }
// IMK
public func showPreviousPage() -> Bool { public func showPreviousPage() -> Bool {
guard delegate != nil else { return false } guard delegate != nil else { return false }
if pageCount == 1 { return highlightPreviousCandidate() } if pageCount == 1 { return highlightPreviousCandidate() }
@ -155,68 +142,44 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol {
currentPageIndex = (currentPageIndex == 0) ? pageCount - 1 : currentPageIndex - 1 currentPageIndex = (currentPageIndex == 0) ? pageCount - 1 : currentPageIndex - 1
if selectedCandidateIndex == 0 { return true } if selectedCandidateIndex == 0 { return true }
selectedCandidateIndex = max(selectedCandidateIndex - keyCount, 0) selectedCandidateIndex = max(selectedCandidateIndex - keyCount, 0)
switch currentLayout { do { currentLayout == .vertical ? moveLeft(self) : moveUp(self) }
case .horizontal:
moveUp(self)
case .vertical:
moveLeft(self)
}
return true return true
} }
// IMK
public func highlightNextCandidate() -> Bool { public func highlightNextCandidate() -> Bool {
guard let delegate = delegate else { return false } guard let delegate = delegate else { return false }
selectedCandidateIndex = selectedCandidateIndex =
(selectedCandidateIndex + 1 >= delegate.candidateCountForController(self)) (selectedCandidateIndex + 1 >= delegate.candidateCountForController(self))
? 0 : selectedCandidateIndex + 1 ? 0 : selectedCandidateIndex + 1
switch currentLayout { do { currentLayout == .vertical ? moveDown(self) : moveRight(self) }
case .horizontal: return true
moveRight(self)
return true
case .vertical:
moveDown(self)
return true
}
} }
// IMK
public func highlightPreviousCandidate() -> Bool { public func highlightPreviousCandidate() -> Bool {
guard let delegate = delegate else { return false } guard let delegate = delegate else { return false }
selectedCandidateIndex = selectedCandidateIndex =
(selectedCandidateIndex == 0) (selectedCandidateIndex == 0)
? delegate.candidateCountForController(self) - 1 : selectedCandidateIndex - 1 ? delegate.candidateCountForController(self) - 1 : selectedCandidateIndex - 1
switch currentLayout { do { currentLayout == .vertical ? moveUp(self) : moveLeft(self) }
case .horizontal: return true
moveLeft(self)
return true
case .vertical:
moveUp(self)
return true
}
} }
public func candidateIndexAtKeyLabelIndex(_ index: Int) -> Int { public func candidateIndexAtKeyLabelIndex(_ index: Int) -> Int {
guard let delegate = delegate else { guard let delegate = delegate else { return Int.max }
return Int.max
}
let result = currentPageIndex * keyLabels.count + index let result = currentPageIndex * keyLabels.count + index
return result < delegate.candidateCountForController(self) ? result : Int.max return result < delegate.candidateCountForController(self) ? result : Int.max
} }
public var selectedCandidateIndex: Int { public var selectedCandidateIndex: Int {
get { get { selectedCandidate() }
selectedCandidate() set { selectCandidate(withIdentifier: newValue) }
}
set {
selectCandidate(withIdentifier: newValue)
}
} }
public func set(windowTopLeftPoint: NSPoint, bottomOutOfScreenAdjustmentHeight height: CGFloat) { public func set(windowTopLeftPoint: NSPoint, bottomOutOfScreenAdjustmentHeight height: CGFloat) {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()) { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()) {
self.doSet( self.doSet(windowTopLeftPoint: windowTopLeftPoint, bottomOutOfScreenAdjustmentHeight: height)
windowTopLeftPoint: windowTopLeftPoint, bottomOutOfScreenAdjustmentHeight: height
)
} }
} }
@ -235,9 +198,7 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol {
} }
} }
if adjustedHeight > screenFrame.size.height / 2.0 { if adjustedHeight > screenFrame.size.height / 2.0 { adjustedHeight = 0.0 }
adjustedHeight = 0.0
}
let windowSize = candidateFrame().size let windowSize = candidateFrame().size
@ -247,9 +208,7 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol {
} }
// top over the screen? // top over the screen?
if adjustedPoint.y >= screenFrame.maxY { if adjustedPoint.y >= screenFrame.maxY { adjustedPoint.y = screenFrame.maxY - 1.0 }
adjustedPoint.y = screenFrame.maxY - 1.0
}
// right // right
if adjustedPoint.x + windowSize.width >= screenFrame.maxX { if adjustedPoint.x + windowSize.width >= screenFrame.maxX {
@ -257,9 +216,7 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol {
} }
// left // left
if adjustedPoint.x < screenFrame.minX { if adjustedPoint.x < screenFrame.minX { adjustedPoint.x = screenFrame.minX }
adjustedPoint.x = screenFrame.minX
}
setCandidateFrameTopLeft(adjustedPoint) setCandidateFrameTopLeft(adjustedPoint)
} }
@ -274,35 +231,14 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol {
if input.isEsc || input.isBackSpace || input.isDelete || (input.isShiftHold && !input.isSpace) { if input.isEsc || input.isBackSpace || input.isDelete || (input.isShiftHold && !input.isSpace) {
_ = delegate.sharedEventHandler(event) _ = delegate.sharedEventHandler(event)
} else if input.isSymbolMenuPhysicalKey || input.isSpace { } else if input.isSymbolMenuPhysicalKey || input.isSpace {
if input.isShiftHold { switch currentLayout {
switch currentLayout { case .horizontal: input.isShiftHold ? moveUp(self) : moveDown(self)
case .horizontal: case .vertical: input.isShiftHold ? moveLeft(self) : moveRight(self)
moveUp(self)
case .vertical:
moveLeft(self)
}
} else {
switch currentLayout {
case .horizontal:
moveDown(self)
case .vertical:
moveRight(self)
}
} }
} else if input.isTab { } else if input.isTab {
switch currentLayout { switch currentLayout {
case .horizontal: case .horizontal: input.isShiftHold ? moveLeft(self) : moveRight(self)
if input.isShiftHold { case .vertical: input.isShiftHold ? moveUp(self) : moveDown(self)
moveLeft(self)
} else {
moveRight(self)
}
case .vertical:
if input.isShiftHold {
moveUp(self)
} else {
moveDown(self)
}
} }
} else { } else {
if let newChar = ctlCandidateIMK.defaultIMKSelectionKey[event.keyCode] { if let newChar = ctlCandidateIMK.defaultIMKSelectionKey[event.keyCode] {
@ -334,11 +270,9 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol {
} }
} }
if mgrPrefs.useSCPCTypingMode { if mgrPrefs.useSCPCTypingMode, !input.isReservedKey {
if !input.isReservedKey { _ = delegate.sharedEventHandler(event)
_ = delegate.sharedEventHandler(event) return
return
}
} }
if delegate.isAssociatedPhrasesState, if delegate.isAssociatedPhrasesState,