Skip to content

Commit

Permalink
Merge pull request #393 from openvanilla/dev/fix-int-crasher
Browse files Browse the repository at this point in the history
Fixes a boundary condition regarding integer conversion, and some fine-tunes
  • Loading branch information
zonble authored Dec 2, 2023
2 parents 0e34add + 17ac91e commit 217fcfc
Showing 1 changed file with 15 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fileprivate class VerticalKeyLabelStripView: NSView {
var keyLabelFont: NSFont = NSFont.systemFont(ofSize: NSFont.smallSystemFontSize)
var labelOffsetY: CGFloat = 0
var keyLabels: [String] = []
var highlightedIndex: UInt = UInt.max
var highlightedIndex: Int = -1

override var isFlipped: Bool {
true
Expand Down Expand Up @@ -68,15 +68,6 @@ fileprivate class VerticalKeyLabelStripView: NSView {
}
}

fileprivate class VerticalCandidateTableView: NSTableView {
override func adjustScroll(_ newVisible: NSRect) -> NSRect {
var scrollRect = newVisible
let rowHeightPlusSpacing = rowHeight + intercellSpacing.height
scrollRect.origin.y = (scrollRect.origin.y / rowHeightPlusSpacing) * rowHeightPlusSpacing
return scrollRect
}
}

private let kCandidateTextPadding: CGFloat = 24.0
private let kCandidateTextLeftMargin: CGFloat = 8.0
private let kCandidateTextPaddingWithMandatedTableViewPadding: CGFloat = 18.0
Expand Down Expand Up @@ -184,7 +175,7 @@ public class VerticalCandidateController: CandidateController {
}

public override func reloadData() {
maxCandidateAttrStringWidth = ceil(candidateFont.pointSize * 2.0 + candidateTextPadding)
maxCandidateAttrStringWidth = ceil(candidateFont.pointSize + candidateTextPadding)
tableView.reloadData()
layoutCandidateView()
if delegate?.candidateCountForController(self) ?? 0 > 0 {
Expand Down Expand Up @@ -270,17 +261,16 @@ public class VerticalCandidateController: CandidateController {
let visibleRowIndexes = tableView.rows(in: visibleRect)
let selected = selectedCandidateIndex

if selected == UInt.max || visibleRowIndexes.contains(Int(selected)) == false {
keyLabelStripView.highlightedIndex = -1
keyLabelStripView.setNeedsDisplay(keyLabelStripView.frame)
}

scrollTimer?.invalidate()
if visibleRowIndexes.contains(Int(selected)) == false {
selectedCandidateIndex = UInt(visibleRowIndexes.lowerBound)
tableView.scrollRowToVisible(visibleRowIndexes.lowerBound)
} else {
scrollTimer = Timer.scheduledTimer(withTimeInterval: 0.25, repeats: false) { timer in
self.tableView.reloadData()
self.tableView.scrollRowToVisible(visibleRowIndexes.lowerBound)
self.scrollTimer?.invalidate()
self.scrollTimer = nil
}
scrollTimer = Timer.scheduledTimer(withTimeInterval: 0.25, repeats: false) { timer in
self.tableView.scrollRowToVisible(visibleRowIndexes.lowerBound)
self.scrollTimer?.invalidate()
self.scrollTimer = nil
}
}
}
Expand Down Expand Up @@ -333,7 +323,7 @@ extension VerticalCandidateController: NSTableViewDataSource, NSTableViewDelegat
}

if newHilightIndex != keyLabelStripView.highlightedIndex && newHilightIndex >= 0 {
keyLabelStripView.highlightedIndex = UInt(newHilightIndex)
keyLabelStripView.highlightedIndex = newHilightIndex
keyLabelStripView.setNeedsDisplay(keyLabelStripView.frame)
}

Expand All @@ -348,9 +338,9 @@ extension VerticalCandidateController: NSTableViewDataSource, NSTableViewDelegat
let firstVisibleRow = tableView.row(at: scrollView.documentVisibleRect.origin)
// firstVisibleRow cannot be larger than selectedRow.
if selectedRow >= firstVisibleRow {
keyLabelStripView.highlightedIndex = UInt(selectedRow - firstVisibleRow)
keyLabelStripView.highlightedIndex = selectedRow - firstVisibleRow
} else {
keyLabelStripView.highlightedIndex = UInt.max
keyLabelStripView.highlightedIndex = -1
}

keyLabelStripView.setNeedsDisplay(keyLabelStripView.frame)
Expand Down Expand Up @@ -458,21 +448,14 @@ extension VerticalCandidateController: NSTableViewDataSource, NSTableViewDelegat
let keyLabelFontSize = ceil(keyLabelFont.pointSize)
let fontSize = max(candidateFontSize, keyLabelFontSize)

let controlSize: NSControl.ControlSize = fontSize > 36.0 ? .regular : .small

var keyLabelCount = UInt(keyLabels.count)
var scrollerWidth: CGFloat = 0.0
if count <= keyLabelCount {
keyLabelCount = count
scrollView.hasVerticalScroller = false
} else {
scrollView.hasVerticalScroller = true
let verticalScroller = scrollView.verticalScroller
verticalScroller?.controlSize = controlSize
verticalScroller?.scrollerStyle = NSScroller.preferredScrollerStyle
if NSScroller.preferredScrollerStyle == .legacy {
scrollerWidth = NSScroller.scrollerWidth(for: controlSize, scrollerStyle: NSScroller.preferredScrollerStyle)
}
scrollerWidth = NSScroller.scrollerWidth(for: .regular, scrollerStyle: NSScroller.preferredScrollerStyle)
}

keyLabelStripView.keyLabelFont = keyLabelFont
Expand Down

0 comments on commit 217fcfc

Please sign in to comment.