Repo // UInt -> Int.

- This should avoid a hell lot of unnecessary type conversions.
This commit is contained in:
ShikiSuen 2022-06-02 14:25:27 +08:00
parent f083538f31
commit 9977ef4846
9 changed files with 104 additions and 93 deletions

View File

@ -110,8 +110,11 @@ enum KeyCodeBlackListed: UInt16 {
} }
// CharCodes: https://theasciicode.com.ar/ascii-control-characters/horizontal-tab-ascii-code-9.html // CharCodes: https://theasciicode.com.ar/ascii-control-characters/horizontal-tab-ascii-code-9.html
enum CharCode: UInt /* 16 */ { enum CharCode: UInt16 {
case yajuusenpai = 114_514_191_191_810_893 case yajuusenpaiA = 114
case yajuusenpaiB = 514
case yajuusenpaiC = 1919
case yajuusenpaiD = 810
// CharCode is not reliable at all. KeyCode is the most appropriate choice due to its accuracy. // CharCode is not reliable at all. KeyCode is the most appropriate choice due to its accuracy.
// KeyCode doesn't give a phuque about the character sent through macOS keyboard layouts ... // KeyCode doesn't give a phuque about the character sent through macOS keyboard layouts ...
// ... but only focuses on which physical key is pressed. // ... but only focuses on which physical key is pressed.

View File

@ -110,9 +110,9 @@ class InputState {
/// Represents that the composing buffer is not empty. /// Represents that the composing buffer is not empty.
class NotEmpty: InputState { class NotEmpty: InputState {
private(set) var composingBuffer: String private(set) var composingBuffer: String
private(set) var cursorIndex: UInt private(set) var cursorIndex: Int = 0 { didSet { cursorIndex = max(cursorIndex, 0) } }
init(composingBuffer: String, cursorIndex: UInt) { init(composingBuffer: String, cursorIndex: Int) {
self.composingBuffer = composingBuffer self.composingBuffer = composingBuffer
self.cursorIndex = cursorIndex self.cursorIndex = cursorIndex
} }
@ -129,7 +129,7 @@ class InputState {
var poppedText: String = "" var poppedText: String = ""
var tooltip: String = "" var tooltip: String = ""
override init(composingBuffer: String, cursorIndex: UInt) { override init(composingBuffer: String, cursorIndex: Int) {
super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex) super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex)
} }
@ -156,7 +156,7 @@ class InputState {
/// Represents that the user is marking a range in the composing buffer. /// Represents that the user is marking a range in the composing buffer.
class Marking: NotEmpty { class Marking: NotEmpty {
private(set) var markerIndex: UInt private(set) var markerIndex: Int = 0 { didSet { markerIndex = max(markerIndex, 0) } }
private(set) var markedRange: NSRange private(set) var markedRange: NSRange
private var deleteTargetExists = false private var deleteTargetExists = false
var tooltip: String { var tooltip: String {
@ -176,7 +176,7 @@ class InputState {
return "" return ""
} }
let text = composingBuffer.substring(with: markedRange) let text = composingBuffer.utf16SubString(with: markedRange)
if markedRange.length < kMinMarkRangeLength { if markedRange.length < kMinMarkRangeLength {
ctlInputMethod.tooltipController.setColor(state: .denialInsufficiency) ctlInputMethod.tooltipController.setColor(state: .denialInsufficiency)
return String( return String(
@ -221,11 +221,11 @@ class InputState {
var tooltipForInputting: String = "" var tooltipForInputting: String = ""
private(set) var readings: [String] private(set) var readings: [String]
init(composingBuffer: String, cursorIndex: UInt, markerIndex: UInt, readings: [String]) { init(composingBuffer: String, cursorIndex: Int, markerIndex: Int, readings: [String]) {
self.markerIndex = markerIndex self.markerIndex = markerIndex
let begin = min(cursorIndex, markerIndex) let begin = min(cursorIndex, markerIndex)
let end = max(cursorIndex, markerIndex) let end = max(cursorIndex, markerIndex)
markedRange = NSRange(location: Int(begin), length: Int(end - begin)) markedRange = NSRange(location: begin, length: end - begin)
self.readings = readings self.readings = readings
super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex) super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex)
} }
@ -291,7 +291,7 @@ class InputState {
} }
var chkIfUserPhraseExists: Bool { var chkIfUserPhraseExists: Bool {
let text = composingBuffer.substring(with: markedRange) let text = composingBuffer.utf16SubString(with: markedRange)
let (exactBegin, _) = composingBuffer.utf16CharIndex(from: markedRange.location) let (exactBegin, _) = composingBuffer.utf16CharIndex(from: markedRange.location)
let (exactEnd, _) = composingBuffer.utf16CharIndex( let (exactEnd, _) = composingBuffer.utf16CharIndex(
from: markedRange.location + markedRange.length) from: markedRange.location + markedRange.length)
@ -303,7 +303,7 @@ class InputState {
} }
var userPhrase: String { var userPhrase: String {
let text = composingBuffer.substring(with: markedRange) let text = composingBuffer.utf16SubString(with: markedRange)
let (exactBegin, _) = composingBuffer.utf16CharIndex(from: markedRange.location) let (exactBegin, _) = composingBuffer.utf16CharIndex(from: markedRange.location)
let (exactEnd, _) = composingBuffer.utf16CharIndex( let (exactEnd, _) = composingBuffer.utf16CharIndex(
from: markedRange.location + markedRange.length) from: markedRange.location + markedRange.length)
@ -314,7 +314,7 @@ class InputState {
var userPhraseConverted: String { var userPhraseConverted: String {
let text = let text =
OpenCCBridge.crossConvert(composingBuffer.substring(with: markedRange)) ?? "" OpenCCBridge.crossConvert(composingBuffer.utf16SubString(with: markedRange)) ?? ""
let (exactBegin, _) = composingBuffer.utf16CharIndex(from: markedRange.location) let (exactBegin, _) = composingBuffer.utf16CharIndex(from: markedRange.location)
let (exactEnd, _) = composingBuffer.utf16CharIndex( let (exactEnd, _) = composingBuffer.utf16CharIndex(
from: markedRange.location + markedRange.length) from: markedRange.location + markedRange.length)
@ -332,7 +332,7 @@ class InputState {
private(set) var candidates: [String] private(set) var candidates: [String]
private(set) var useVerticalMode: Bool private(set) var useVerticalMode: Bool
init(composingBuffer: String, cursorIndex: UInt, candidates: [String], useVerticalMode: Bool) { init(composingBuffer: String, cursorIndex: Int, candidates: [String], useVerticalMode: Bool) {
self.candidates = candidates self.candidates = candidates
self.useVerticalMode = useVerticalMode self.useVerticalMode = useVerticalMode
super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex) super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex)

View File

@ -71,7 +71,7 @@ extension KeyHandler {
} }
delegate!.keyHandler( delegate!.keyHandler(
self, self,
didSelectCandidateAt: Int(ctlCandidateCurrent.selectedCandidateIndex), didSelectCandidateAt: ctlCandidateCurrent.selectedCandidateIndex,
ctlCandidate: ctlCandidateCurrent ctlCandidate: ctlCandidateCurrent
) )
return true return true
@ -260,11 +260,11 @@ extension KeyHandler {
return false return false
} else { // count > 0!isEmpty滿 } else { // count > 0!isEmpty滿
if input.isEnd || input.emacsKey == vChewingEmacsKey.end { if input.isEnd || input.emacsKey == vChewingEmacsKey.end {
if ctlCandidateCurrent.selectedCandidateIndex == UInt(candidates.count - 1) { if ctlCandidateCurrent.selectedCandidateIndex == candidates.count - 1 {
IME.prtDebugIntel("9B69AAAD") IME.prtDebugIntel("9B69AAAD")
errorCallback() errorCallback()
} else { } else {
ctlCandidateCurrent.selectedCandidateIndex = UInt(candidates.count - 1) ctlCandidateCurrent.selectedCandidateIndex = candidates.count - 1
} }
} }
} }
@ -294,10 +294,10 @@ extension KeyHandler {
} }
if index != NSNotFound { if index != NSNotFound {
let candidateIndex: UInt = ctlCandidateCurrent.candidateIndexAtKeyLabelIndex(UInt(index)) let candidateIndex = ctlCandidateCurrent.candidateIndexAtKeyLabelIndex(index)
if candidateIndex != UInt.max { if candidateIndex != Int.max {
delegate!.keyHandler( delegate!.keyHandler(
self, didSelectCandidateAt: Int(candidateIndex), ctlCandidate: ctlCandidateCurrent self, didSelectCandidateAt: candidateIndex, ctlCandidate: ctlCandidateCurrent
) )
return true return true
} }
@ -342,11 +342,11 @@ extension KeyHandler {
} }
if shouldAutoSelectCandidate { if shouldAutoSelectCandidate {
let candidateIndex: UInt = ctlCandidateCurrent.candidateIndexAtKeyLabelIndex(0) let candidateIndex = ctlCandidateCurrent.candidateIndexAtKeyLabelIndex(0)
if candidateIndex != UInt.max { if candidateIndex != Int.max {
delegate!.keyHandler( delegate!.keyHandler(
self, self,
didSelectCandidateAt: Int(candidateIndex), didSelectCandidateAt: candidateIndex,
ctlCandidate: ctlCandidateCurrent ctlCandidate: ctlCandidateCurrent
) )
clear() clear()

View File

@ -114,7 +114,7 @@ extension KeyHandler {
let composedText = head + reading + tail let composedText = head + reading + tail
let cursorIndex = composedStringCursorIndex + reading.utf16.count let cursorIndex = composedStringCursorIndex + reading.utf16.count
let stateResult = InputState.Inputting(composingBuffer: composedText, cursorIndex: UInt(cursorIndex)) let stateResult = InputState.Inputting(composingBuffer: composedText, cursorIndex: cursorIndex)
// Now we start weaving the contents of the tooltip. // Now we start weaving the contents of the tooltip.
if tooltipParameterRef[0].isEmpty, tooltipParameterRef[1].isEmpty { if tooltipParameterRef[0].isEmpty, tooltipParameterRef[1].isEmpty {
@ -206,7 +206,7 @@ extension KeyHandler {
if input.isCursorBackward || input.emacsKey == vChewingEmacsKey.backward, input.isShiftHold { if input.isCursorBackward || input.emacsKey == vChewingEmacsKey.backward, input.isShiftHold {
var index = state.markerIndex var index = state.markerIndex
if index > 0 { if index > 0 {
index = UInt(state.composingBuffer.utf16PreviousPosition(for: Int(index))) index = state.composingBuffer.utf16PreviousPosition(for: index)
let marking = InputState.Marking( let marking = InputState.Marking(
composingBuffer: state.composingBuffer, composingBuffer: state.composingBuffer,
cursorIndex: state.cursorIndex, cursorIndex: state.cursorIndex,
@ -227,7 +227,7 @@ extension KeyHandler {
if input.isCursorForward || input.emacsKey == vChewingEmacsKey.forward, input.isShiftHold { if input.isCursorForward || input.emacsKey == vChewingEmacsKey.forward, input.isShiftHold {
var index = state.markerIndex var index = state.markerIndex
if index < (state.composingBuffer.utf16.count) { if index < (state.composingBuffer.utf16.count) {
index = UInt(state.composingBuffer.utf16NextPosition(for: Int(index))) index = state.composingBuffer.utf16NextPosition(for: index)
let marking = InputState.Marking( let marking = InputState.Marking(
composingBuffer: state.composingBuffer, composingBuffer: state.composingBuffer,
cursorIndex: state.cursorIndex, cursorIndex: state.cursorIndex,
@ -565,11 +565,11 @@ extension KeyHandler {
// Shift + Right // Shift + Right
if currentState.cursorIndex < currentState.composingBuffer.utf16.count { if currentState.cursorIndex < currentState.composingBuffer.utf16.count {
let nextPosition = currentState.composingBuffer.utf16NextPosition( let nextPosition = currentState.composingBuffer.utf16NextPosition(
for: Int(currentState.cursorIndex)) for: currentState.cursorIndex)
let marking: InputState.Marking! = InputState.Marking( let marking: InputState.Marking! = InputState.Marking(
composingBuffer: currentState.composingBuffer, composingBuffer: currentState.composingBuffer,
cursorIndex: currentState.cursorIndex, cursorIndex: currentState.cursorIndex,
markerIndex: UInt(nextPosition), markerIndex: nextPosition,
readings: currentReadings readings: currentReadings
) )
marking.tooltipForInputting = currentState.tooltip marking.tooltipForInputting = currentState.tooltip
@ -614,11 +614,11 @@ extension KeyHandler {
// Shift + left // Shift + left
if currentState.cursorIndex > 0 { if currentState.cursorIndex > 0 {
let previousPosition = currentState.composingBuffer.utf16PreviousPosition( let previousPosition = currentState.composingBuffer.utf16PreviousPosition(
for: Int(currentState.cursorIndex)) for: currentState.cursorIndex)
let marking: InputState.Marking! = InputState.Marking( let marking: InputState.Marking! = InputState.Marking(
composingBuffer: currentState.composingBuffer, composingBuffer: currentState.composingBuffer,
cursorIndex: currentState.cursorIndex, cursorIndex: currentState.cursorIndex,
markerIndex: UInt(previousPosition), markerIndex: previousPosition,
readings: currentReadings readings: currentReadings
) )
marking.tooltipForInputting = currentState.tooltip marking.tooltipForInputting = currentState.tooltip

View File

@ -67,7 +67,7 @@ extension String {
(self as NSString).expandingTildeInPath (self as NSString).expandingTildeInPath
} }
public func substring(with nsRange: NSRange) -> String { public func utf16SubString(with range: NSRange) -> String {
(self as NSString).substring(with: nsRange) (self as NSString).substring(with: range)
} }
} }

View File

@ -373,7 +373,7 @@ extension ctlInputMethod {
// the selection range is where the cursor is, with the length being 0 and replacement range NSNotFound, // 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 // i.e. the client app needs to take care of where to put this composing buffer
client.setMarkedText( client.setMarkedText(
state.attributedString, selectionRange: NSRange(location: Int(state.cursorIndex), length: 0), state.attributedString, selectionRange: NSRange(location: state.cursorIndex, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound) replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
) )
if !state.tooltip.isEmpty { if !state.tooltip.isEmpty {
@ -395,7 +395,7 @@ extension ctlInputMethod {
// the selection range is where the cursor is, with the length being 0 and replacement range NSNotFound, // 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 // i.e. the client app needs to take care of where to put this composing buffer
client.setMarkedText( client.setMarkedText(
state.attributedString, selectionRange: NSRange(location: Int(state.cursorIndex), length: 0), state.attributedString, selectionRange: NSRange(location: state.cursorIndex, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound) replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
) )
@ -420,7 +420,7 @@ extension ctlInputMethod {
// the selection range is where the cursor is, with the length being 0 and replacement range NSNotFound, // 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 // i.e. the client app needs to take care of where to put this composing buffer
client.setMarkedText( client.setMarkedText(
state.attributedString, selectionRange: NSRange(location: Int(state.cursorIndex), length: 0), state.attributedString, selectionRange: NSRange(location: state.cursorIndex, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound) replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
) )
show(candidateWindowWith: state, client: client) show(candidateWindowWith: state, client: client)
@ -437,7 +437,7 @@ extension ctlInputMethod {
// the selection range is where the cursor is, with the length being 0 and replacement range NSNotFound, // 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 // i.e. the client app needs to take care of where to put this composing buffer
client.setMarkedText( client.setMarkedText(
state.attributedString, selectionRange: NSRange(location: Int(state.cursorIndex), length: 0), state.attributedString, selectionRange: NSRange(location: state.cursorIndex, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound) replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
) )
show(candidateWindowWith: state, client: client) show(candidateWindowWith: state, client: client)
@ -553,7 +553,7 @@ extension ctlInputMethod {
var cursor = 0 var cursor = 0
if let state = state as? InputState.ChoosingCandidate { if let state = state as? InputState.ChoosingCandidate {
cursor = Int(state.cursorIndex) cursor = state.cursorIndex
if cursor == state.composingBuffer.count, cursor != 0 { if cursor == state.composingBuffer.count, cursor != 0 {
cursor -= 1 cursor -= 1
} }
@ -581,9 +581,9 @@ extension ctlInputMethod {
} }
} }
private func show(tooltip: String, composingBuffer: String, cursorIndex: UInt, client: Any!) { private func show(tooltip: String, composingBuffer: String, cursorIndex: Int, client: Any!) {
var lineHeightRect = NSRect(x: 0.0, y: 0.0, width: 16.0, height: 16.0) var lineHeightRect = NSRect(x: 0.0, y: 0.0, width: 16.0, height: 16.0)
var cursor = Int(cursorIndex) var cursor = cursorIndex
if cursor == composingBuffer.count, cursor != 0 { if cursor == composingBuffer.count, cursor != 0 {
cursor -= 1 cursor -= 1
} }
@ -615,7 +615,7 @@ extension ctlInputMethod: KeyHandlerDelegate {
) { ) {
_ = keyHandler // Stop clang-format from ruining the parameters of this function. _ = keyHandler // Stop clang-format from ruining the parameters of this function.
if let controller = controller as? ctlCandidate { if let controller = controller as? ctlCandidate {
ctlCandidate(controller, didSelectCandidateAtIndex: UInt(index)) ctlCandidate(controller, didSelectCandidateAtIndex: index)
} }
} }
@ -651,34 +651,34 @@ extension ctlInputMethod: KeyHandlerDelegate {
// MARK: - // MARK: -
extension ctlInputMethod: ctlCandidateDelegate { extension ctlInputMethod: ctlCandidateDelegate {
func candidateCountForController(_ controller: ctlCandidate) -> UInt { func candidateCountForController(_ controller: ctlCandidate) -> Int {
_ = controller // Stop clang-format from ruining the parameters of this function. _ = controller // Stop clang-format from ruining the parameters of this function.
if let state = state as? InputState.ChoosingCandidate { if let state = state as? InputState.ChoosingCandidate {
return UInt(state.candidates.count) return state.candidates.count
} else if let state = state as? InputState.AssociatedPhrases { } else if let state = state as? InputState.AssociatedPhrases {
return UInt(state.candidates.count) return state.candidates.count
} }
return 0 return 0
} }
func ctlCandidate(_ controller: ctlCandidate, candidateAtIndex index: UInt) func ctlCandidate(_ controller: ctlCandidate, candidateAtIndex index: Int)
-> String -> String
{ {
_ = controller // Stop clang-format from ruining the parameters of this function. _ = controller // Stop clang-format from ruining the parameters of this function.
if let state = state as? InputState.ChoosingCandidate { if let state = state as? InputState.ChoosingCandidate {
return state.candidates[Int(index)] return state.candidates[index]
} else if let state = state as? InputState.AssociatedPhrases { } else if let state = state as? InputState.AssociatedPhrases {
return state.candidates[Int(index)] return state.candidates[index]
} }
return "" return ""
} }
func ctlCandidate(_ controller: ctlCandidate, didSelectCandidateAtIndex index: UInt) { func ctlCandidate(_ controller: ctlCandidate, didSelectCandidateAtIndex index: Int) {
_ = controller // Stop clang-format from ruining the parameters of this function. _ = controller // Stop clang-format from ruining the parameters of this function.
let client = currentClient let client = currentClient
if let state = state as? InputState.SymbolTable, if let state = state as? InputState.SymbolTable,
let node = state.node.children?[Int(index)] let node = state.node.children?[index]
{ {
if let children = node.children, !children.isEmpty { if let children = node.children, !children.isEmpty {
handle( handle(
@ -693,7 +693,7 @@ extension ctlInputMethod: ctlCandidateDelegate {
} }
if let state = state as? InputState.ChoosingCandidate { if let state = state as? InputState.ChoosingCandidate {
let selectedValue = state.candidates[Int(index)] let selectedValue = state.candidates[index]
keyHandler.fixNode(value: selectedValue, respectCursorPushing: true) keyHandler.fixNode(value: selectedValue, respectCursorPushing: true)
let inputting = keyHandler.buildInputtingState let inputting = keyHandler.buildInputtingState
@ -718,7 +718,7 @@ extension ctlInputMethod: ctlCandidateDelegate {
} }
if let state = state as? InputState.AssociatedPhrases { if let state = state as? InputState.AssociatedPhrases {
let selectedValue = state.candidates[Int(index)] let selectedValue = state.candidates[index]
handle(state: .Committing(poppedText: selectedValue), client: currentClient) handle(state: .Committing(poppedText: selectedValue), client: currentClient)
if mgrPrefs.associatedPhrasesEnabled, if mgrPrefs.associatedPhrasesEnabled,
let associatePhrases = keyHandler.buildAssociatePhraseState( let associatePhrases = keyHandler.buildAssociatePhraseState(

View File

@ -38,11 +38,11 @@ public class CandidateKeyLabel: NSObject {
} }
public protocol ctlCandidateDelegate: AnyObject { public protocol ctlCandidateDelegate: AnyObject {
func candidateCountForController(_ controller: ctlCandidate) -> UInt func candidateCountForController(_ controller: ctlCandidate) -> Int
func ctlCandidate(_ controller: ctlCandidate, candidateAtIndex index: UInt) func ctlCandidate(_ controller: ctlCandidate, candidateAtIndex index: Int)
-> String -> String
func ctlCandidate( func ctlCandidate(
_ controller: ctlCandidate, didSelectCandidateAtIndex index: UInt _ controller: ctlCandidate, didSelectCandidateAtIndex index: Int
) )
} }
@ -53,7 +53,7 @@ public class ctlCandidate: NSWindowController {
} }
} }
public var selectedCandidateIndex: UInt = .max public var selectedCandidateIndex: Int = .max
public var visible: Bool = false { public var visible: Bool = false {
didSet { didSet {
NSObject.cancelPreviousPerformRequests(withTarget: self) NSObject.cancelPreviousPerformRequests(withTarget: self)
@ -108,8 +108,8 @@ public class ctlCandidate: NSWindowController {
false false
} }
public func candidateIndexAtKeyLabelIndex(_: UInt) -> UInt { public func candidateIndexAtKeyLabelIndex(_: Int) -> Int {
UInt.max Int.max
} }
/// Sets the location of the candidate window. /// Sets the location of the candidate window.

View File

@ -27,7 +27,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import Cocoa import Cocoa
private class HorizontalCandidateView: NSView { private class HorizontalCandidateView: NSView {
var highlightedIndex: UInt = 0 var highlightedIndex: Int = 0 { didSet { highlightedIndex = max(highlightedIndex, 0) } }
var action: Selector? var action: Selector?
weak var target: AnyObject? weak var target: AnyObject?
@ -42,7 +42,9 @@ private class HorizontalCandidateView: NSView {
private var candidateAttrDict: [NSAttributedString.Key: AnyObject] = [:] private var candidateAttrDict: [NSAttributedString.Key: AnyObject] = [:]
private var candidateWithLabelAttrDict: [NSAttributedString.Key: AnyObject] = [:] private var candidateWithLabelAttrDict: [NSAttributedString.Key: AnyObject] = [:]
private var elementWidths: [CGFloat] = [] private var elementWidths: [CGFloat] = []
private var trackingHighlightedIndex: UInt = .max private var trackingHighlightedIndex: Int = .max {
didSet { trackingHighlightedIndex = max(trackingHighlightedIndex, 0) }
}
override var isFlipped: Bool { override var isFlipped: Bool {
true true
@ -196,26 +198,27 @@ private class HorizontalCandidateView: NSView {
} }
} }
private func findHitIndex(event: NSEvent) -> UInt? { private func findHitIndex(event: NSEvent) -> Int {
let location = convert(event.locationInWindow, to: nil) let location = convert(event.locationInWindow, to: nil)
if !bounds.contains(location) { if !bounds.contains(location) {
return nil return NSNotFound
} }
var accuWidth: CGFloat = 0.0 var accuWidth: CGFloat = 0.0
for (index, elementWidth) in elementWidths.enumerated() { for (index, elementWidth) in elementWidths.enumerated() {
let currentWidth = elementWidth let currentWidth = elementWidth
if location.x >= accuWidth, location.x <= accuWidth + currentWidth { if location.x >= accuWidth, location.x <= accuWidth + currentWidth {
return UInt(index) return index
} }
accuWidth += currentWidth + 1.0 accuWidth += currentWidth + 1.0
} }
return nil return NSNotFound
} }
override func mouseUp(with event: NSEvent) { override func mouseUp(with event: NSEvent) {
trackingHighlightedIndex = highlightedIndex trackingHighlightedIndex = highlightedIndex
guard let newIndex = findHitIndex(event: event) else { let newIndex = findHitIndex(event: event)
guard newIndex != NSNotFound else {
return return
} }
highlightedIndex = newIndex highlightedIndex = newIndex
@ -223,7 +226,8 @@ private class HorizontalCandidateView: NSView {
} }
override func mouseDown(with event: NSEvent) { override func mouseDown(with event: NSEvent) {
guard let newIndex = findHitIndex(event: event) else { let newIndex = findHitIndex(event: event)
guard newIndex != NSNotFound else {
return return
} }
var triggerAction = false var triggerAction = false
@ -247,7 +251,7 @@ public class ctlCandidateHorizontal: ctlCandidate {
private var candidateView: HorizontalCandidateView private var candidateView: HorizontalCandidateView
private var prevPageButton: NSButton private var prevPageButton: NSButton
private var nextPageButton: NSButton private var nextPageButton: NSButton
private var currentPageIndex: UInt = 0 private var currentPageIndex: Int = 0
public init() { public init() {
var contentRect = NSRect(x: 128.0, y: 128.0, width: 0.0, height: 0.0) var contentRect = NSRect(x: 128.0, y: 128.0, width: 0.0, height: 0.0)
@ -364,24 +368,24 @@ public class ctlCandidateHorizontal: ctlCandidate {
return true return true
} }
override public func candidateIndexAtKeyLabelIndex(_ index: UInt) -> UInt { override public func candidateIndexAtKeyLabelIndex(_ index: Int) -> Int {
guard let delegate = delegate else { guard let delegate = delegate else {
return UInt.max return Int.max
} }
let result = currentPageIndex * UInt(keyLabels.count) + index let result = currentPageIndex * keyLabels.count + index
return result < delegate.candidateCountForController(self) ? result : UInt.max return result < delegate.candidateCountForController(self) ? result : Int.max
} }
override public var selectedCandidateIndex: UInt { override public var selectedCandidateIndex: Int {
get { get {
currentPageIndex * UInt(keyLabels.count) + candidateView.highlightedIndex currentPageIndex * keyLabels.count + candidateView.highlightedIndex
} }
set { set {
guard let delegate = delegate else { guard let delegate = delegate else {
return return
} }
let keyLabelCount = UInt(keyLabels.count) let keyLabelCount = keyLabels.count
if newValue < delegate.candidateCountForController(self) { if newValue < delegate.candidateCountForController(self) {
currentPageIndex = newValue / keyLabelCount currentPageIndex = newValue / keyLabelCount
candidateView.highlightedIndex = newValue % keyLabelCount candidateView.highlightedIndex = newValue % keyLabelCount
@ -392,12 +396,12 @@ public class ctlCandidateHorizontal: ctlCandidate {
} }
extension ctlCandidateHorizontal { extension ctlCandidateHorizontal {
private var pageCount: UInt { 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 = UInt(keyLabels.count) let keyLabelCount = keyLabels.count
return totalCount / keyLabelCount + ((totalCount % keyLabelCount) != 0 ? 1 : 0) return totalCount / keyLabelCount + ((totalCount % keyLabelCount) != 0 ? 1 : 0)
} }
@ -409,7 +413,7 @@ extension ctlCandidateHorizontal {
candidateView.set(keyLabelFont: keyLabelFont, candidateFont: candidateFont) candidateView.set(keyLabelFont: keyLabelFont, candidateFont: candidateFont)
var candidates = [String]() var candidates = [String]()
let count = delegate.candidateCountForController(self) let count = delegate.candidateCountForController(self)
let keyLabelCount = UInt(keyLabels.count) let keyLabelCount = keyLabels.count
let begin = currentPageIndex * keyLabelCount let begin = currentPageIndex * keyLabelCount
for index in begin..<min(begin + keyLabelCount, count) { for index in begin..<min(begin + keyLabelCount, count) {

View File

@ -27,7 +27,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import Cocoa import Cocoa
private class VerticalCandidateView: NSView { private class VerticalCandidateView: NSView {
var highlightedIndex: UInt = 0 var highlightedIndex: Int = 0 { didSet { highlightedIndex = max(highlightedIndex, 0) } }
var action: Selector? var action: Selector?
weak var target: AnyObject? weak var target: AnyObject?
@ -44,7 +44,9 @@ private class VerticalCandidateView: NSView {
private var windowWidth: CGFloat = 0 private var windowWidth: CGFloat = 0
private var elementWidths: [CGFloat] = [] private var elementWidths: [CGFloat] = []
private var elementHeights: [CGFloat] = [] private var elementHeights: [CGFloat] = []
private var trackingHighlightedIndex: UInt = .max private var trackingHighlightedIndex: Int = .max {
didSet { trackingHighlightedIndex = max(trackingHighlightedIndex, 0) }
}
override var isFlipped: Bool { override var isFlipped: Bool {
true true
@ -201,26 +203,27 @@ private class VerticalCandidateView: NSView {
} }
} }
private func findHitIndex(event: NSEvent) -> UInt? { private func findHitIndex(event: NSEvent) -> Int {
let location = convert(event.locationInWindow, to: nil) let location = convert(event.locationInWindow, to: nil)
if !bounds.contains(location) { if !bounds.contains(location) {
return nil return NSNotFound
} }
var accuHeight: CGFloat = 0.0 var accuHeight: CGFloat = 0.0
for (index, elementHeight) in elementHeights.enumerated() { for (index, elementHeight) in elementHeights.enumerated() {
let currentHeight = elementHeight let currentHeight = elementHeight
if location.y >= accuHeight, location.y <= accuHeight + currentHeight { if location.y >= accuHeight, location.y <= accuHeight + currentHeight {
return UInt(index) return index
} }
accuHeight += currentHeight accuHeight += currentHeight
} }
return nil return NSNotFound
} }
override func mouseUp(with event: NSEvent) { override func mouseUp(with event: NSEvent) {
trackingHighlightedIndex = highlightedIndex trackingHighlightedIndex = highlightedIndex
guard let newIndex = findHitIndex(event: event) else { let newIndex = findHitIndex(event: event)
guard newIndex != NSNotFound else {
return return
} }
highlightedIndex = newIndex highlightedIndex = newIndex
@ -228,7 +231,8 @@ private class VerticalCandidateView: NSView {
} }
override func mouseDown(with event: NSEvent) { override func mouseDown(with event: NSEvent) {
guard let newIndex = findHitIndex(event: event) else { let newIndex = findHitIndex(event: event)
guard newIndex != NSNotFound else {
return return
} }
var triggerAction = false var triggerAction = false
@ -252,7 +256,7 @@ public class ctlCandidateVertical: ctlCandidate {
private var candidateView: VerticalCandidateView private var candidateView: VerticalCandidateView
private var prevPageButton: NSButton private var prevPageButton: NSButton
private var nextPageButton: NSButton private var nextPageButton: NSButton
private var currentPageIndex: UInt = 0 private var currentPageIndex: Int = 0
public init() { public init() {
var contentRect = NSRect(x: 128.0, y: 128.0, width: 0.0, height: 0.0) var contentRect = NSRect(x: 128.0, y: 128.0, width: 0.0, height: 0.0)
@ -369,24 +373,24 @@ public class ctlCandidateVertical: ctlCandidate {
return true return true
} }
override public func candidateIndexAtKeyLabelIndex(_ index: UInt) -> UInt { override public func candidateIndexAtKeyLabelIndex(_ index: Int) -> Int {
guard let delegate = delegate else { guard let delegate = delegate else {
return UInt.max return Int.max
} }
let result = currentPageIndex * UInt(keyLabels.count) + index let result = currentPageIndex * keyLabels.count + index
return result < delegate.candidateCountForController(self) ? result : UInt.max return result < delegate.candidateCountForController(self) ? result : Int.max
} }
override public var selectedCandidateIndex: UInt { override public var selectedCandidateIndex: Int {
get { get {
currentPageIndex * UInt(keyLabels.count) + candidateView.highlightedIndex currentPageIndex * keyLabels.count + candidateView.highlightedIndex
} }
set { set {
guard let delegate = delegate else { guard let delegate = delegate else {
return return
} }
let keyLabelCount = UInt(keyLabels.count) let keyLabelCount = keyLabels.count
if newValue < delegate.candidateCountForController(self) { if newValue < delegate.candidateCountForController(self) {
currentPageIndex = newValue / keyLabelCount currentPageIndex = newValue / keyLabelCount
candidateView.highlightedIndex = newValue % keyLabelCount candidateView.highlightedIndex = newValue % keyLabelCount
@ -397,12 +401,12 @@ public class ctlCandidateVertical: ctlCandidate {
} }
extension ctlCandidateVertical { extension ctlCandidateVertical {
private var pageCount: UInt { 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 = UInt(keyLabels.count) let keyLabelCount = keyLabels.count
return totalCount / keyLabelCount + ((totalCount % keyLabelCount) != 0 ? 1 : 0) return totalCount / keyLabelCount + ((totalCount % keyLabelCount) != 0 ? 1 : 0)
} }
@ -414,7 +418,7 @@ extension ctlCandidateVertical {
candidateView.set(keyLabelFont: keyLabelFont, candidateFont: candidateFont) candidateView.set(keyLabelFont: keyLabelFont, candidateFont: candidateFont)
var candidates = [String]() var candidates = [String]()
let count = delegate.candidateCountForController(self) let count = delegate.candidateCountForController(self)
let keyLabelCount = UInt(keyLabels.count) let keyLabelCount = keyLabels.count
let begin = currentPageIndex * keyLabelCount let begin = currentPageIndex * keyLabelCount
for index in begin..<min(begin + keyLabelCount, count) { for index in begin..<min(begin + keyLabelCount, count) {