Fixes minor layout issues.

This commit is contained in:
zonble 2022-01-11 00:47:48 +08:00
parent f7e927d67d
commit 867a828722
2 changed files with 42 additions and 41 deletions

View File

@ -26,7 +26,6 @@ fileprivate class HorizontalCandidateView: NSView {
result.width = elementWidths.reduce(0, +) result.width = elementWidths.reduce(0, +)
result.width += CGFloat(elementWidths.count) result.width += CGFloat(elementWidths.count)
result.height = keyLabelHeight + candidateTextHeight + 1.0 result.height = keyLabelHeight + candidateTextHeight + 1.0
} }
return result return result
} }
@ -41,8 +40,8 @@ fileprivate class HorizontalCandidateView: NSView {
let baseSize = NSSize(width: 10240.0, height: 10240.0) let baseSize = NSSize(width: 10240.0, height: 10240.0)
for index in 0..<count { for index in 0..<count {
let labelRect = (keyLabels[index] as NSString).boundingRect(with: baseSize, options: .usesLineFragmentOrigin, attributes: keyLabelAttrDict) let labelRect = (keyLabels[index] as NSString).boundingRect(with: baseSize, options: .usesLineFragmentOrigin, attributes: keyLabelAttrDict)
let candidateRect = (displayedCandidates[index] as NSString).boundingRect(with: baseSize, options: .usesLineFragmentOrigin, attributes: keyLabelAttrDict) let candidateRect = (displayedCandidates[index] as NSString).boundingRect(with: baseSize, options: .usesLineFragmentOrigin, attributes: candidateAttrDict)
let cellWidth = max(labelRect.size.width, candidateRect.size.width) let cellWidth = max(labelRect.size.width, candidateRect.size.width) + cellPadding;
newWidths.append(cellWidth) newWidths.append(cellWidth)
} }
elementWidths = newWidths elementWidths = newWidths
@ -90,8 +89,8 @@ fileprivate class HorizontalCandidateView: NSView {
var accuWidth: CGFloat = 0 var accuWidth: CGFloat = 0
for index in 0..<elementWidths.count { for index in 0..<elementWidths.count {
let currentWidth = elementWidths[index] let currentWidth = elementWidths[index]
let labelRect = NSRect(x: accuWidth, y: 0.0, width: currentWidth, height: keyLabelHeight); let labelRect = NSRect(x: accuWidth, y: 0.0, width: currentWidth, height: keyLabelHeight)
let candidateRect = NSRect(x: accuWidth, y: keyLabelHeight + 1.0, width: currentWidth, height: candidateTextHeight); let candidateRect = NSRect(x: accuWidth, y: keyLabelHeight + 1.0, width: currentWidth, height: candidateTextHeight)
(index == highlightedIndex ? darkGray : lightGray).setFill() (index == highlightedIndex ? darkGray : lightGray).setFill()
NSBezierPath.fill(labelRect) NSBezierPath.fill(labelRect)
(keyLabels[index] as NSString).draw(in: labelRect, withAttributes: keyLabelAttrDict) (keyLabels[index] as NSString).draw(in: labelRect, withAttributes: keyLabelAttrDict)
@ -146,7 +145,7 @@ fileprivate class HorizontalCandidateView: NSView {
if newIndex == highlightedIndex { if newIndex == highlightedIndex {
triggerAction = true triggerAction = true
} else { } else {
highlightedIndex = trackingHighlightedIndex; highlightedIndex = trackingHighlightedIndex
} }
trackingHighlightedIndex = 0 trackingHighlightedIndex = 0
@ -170,6 +169,8 @@ public class HorizontalCandidateController : CandidateController {
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)
let styleMask: NSWindow.StyleMask = [.borderless, .nonactivatingPanel] let styleMask: NSWindow.StyleMask = [.borderless, .nonactivatingPanel]
let panel = NSPanel(contentRect: contentRect, styleMask: styleMask, backing: .buffered, defer: false) let panel = NSPanel(contentRect: contentRect, styleMask: styleMask, backing: .buffered, defer: false)
panel.level = NSWindow.Level(Int(kCGPopUpMenuWindowLevel))
panel.hasShadow = true
contentRect.origin = NSPoint.zero contentRect.origin = NSPoint.zero
candidateView = HorizontalCandidateView(frame: contentRect) candidateView = HorizontalCandidateView(frame: contentRect)
@ -177,7 +178,14 @@ public class HorizontalCandidateController : CandidateController {
contentRect.size = NSSize(width: 36.0, height: 20.0) contentRect.size = NSSize(width: 36.0, height: 20.0)
nextPageButton = NSButton(frame: contentRect) nextPageButton = NSButton(frame: contentRect)
nextPageButton.setButtonType(.momentaryLight)
nextPageButton.bezelStyle = .smallSquare
nextPageButton.title = "»"
prevPageButton = NSButton(frame: contentRect) prevPageButton = NSButton(frame: contentRect)
prevPageButton.setButtonType(.momentaryLight)
prevPageButton.bezelStyle = .smallSquare
prevPageButton.title = "«"
panel.contentView?.addSubview(nextPageButton) panel.contentView?.addSubview(nextPageButton)
panel.contentView?.addSubview(prevPageButton) panel.contentView?.addSubview(prevPageButton)
@ -187,15 +195,9 @@ public class HorizontalCandidateController : CandidateController {
candidateView.target = self candidateView.target = self
candidateView.action = #selector(candidateViewMouseDidClick(_:)) candidateView.action = #selector(candidateViewMouseDidClick(_:))
nextPageButton.setButtonType(.momentaryLight)
nextPageButton.bezelStyle = .smallSquare
nextPageButton.title = "»"
nextPageButton.target = self nextPageButton.target = self
nextPageButton.action = #selector(pageButtonAction(_:)) nextPageButton.action = #selector(pageButtonAction(_:))
prevPageButton.setButtonType(.momentaryLight)
prevPageButton.bezelStyle = .smallSquare
prevPageButton.title = "«"
prevPageButton.target = self prevPageButton.target = self
prevPageButton.action = #selector(pageButtonAction(_:)) prevPageButton.action = #selector(pageButtonAction(_:))
} }
@ -215,14 +217,14 @@ public class HorizontalCandidateController : CandidateController {
return false return false
} }
if currentPage + 1 >= self.pageCount { if currentPage + 1 >= pageCount {
return false return false
} }
currentPage += 1 currentPage += 1
candidateView.highlightedIndex = 0 candidateView.highlightedIndex = 0
layoutCandidateView(); layoutCandidateView()
return true; return true
} }
public override func showPreviousPage() -> Bool { public override func showPreviousPage() -> Bool {
@ -236,8 +238,8 @@ public class HorizontalCandidateController : CandidateController {
currentPage -= 1 currentPage -= 1
candidateView.highlightedIndex = 0 candidateView.highlightedIndex = 0
layoutCandidateView(); layoutCandidateView()
return true; return true
} }
public override func highlightNextCandidate() -> Bool { public override func highlightNextCandidate() -> Bool {
@ -247,10 +249,10 @@ public class HorizontalCandidateController : CandidateController {
let currentIndex = selectedCandidateIndex let currentIndex = selectedCandidateIndex
if currentIndex + 1 >= delegate.candidateCountForController(self) { if currentIndex + 1 >= delegate.candidateCountForController(self) {
return false; return false
} }
selectedCandidateIndex = currentIndex + 1 selectedCandidateIndex = currentIndex + 1
return true; return true
} }
public override func highlightPreviousCandidate() -> Bool { public override func highlightPreviousCandidate() -> Bool {
@ -258,7 +260,7 @@ public class HorizontalCandidateController : CandidateController {
return false return false
} }
let currentIndex = self.selectedCandidateIndex; let currentIndex = selectedCandidateIndex
if currentIndex == 0 { if currentIndex == 0 {
return false return false
} }
@ -272,8 +274,8 @@ public class HorizontalCandidateController : CandidateController {
return UInt.max return UInt.max
} }
let result = currentPage * UInt(keyLabels.count) + index; let result = currentPage * UInt(keyLabels.count) + index
return result < delegate.candidateCountForController(self) ? result : UInt.max; return result < delegate.candidateCountForController(self) ? result : UInt.max
} }
@objc public override var selectedCandidateIndex: UInt { @objc public override var selectedCandidateIndex: UInt {
@ -286,8 +288,8 @@ public class HorizontalCandidateController : CandidateController {
} }
let keyLabelCount = UInt(keyLabels.count) let keyLabelCount = UInt(keyLabels.count)
if newValue < delegate.candidateCountForController(self) { if newValue < delegate.candidateCountForController(self) {
currentPage = newValue / keyLabelCount; currentPage = newValue / keyLabelCount
candidateView.highlightedIndex = newValue % keyLabelCount; candidateView.highlightedIndex = newValue % keyLabelCount
layoutCandidateView() layoutCandidateView()
} }
} }
@ -357,7 +359,7 @@ extension HorizontalCandidateController {
frameRect = window?.frame ?? NSRect.zero frameRect = window?.frame ?? NSRect.zero
let topLeftPoint = NSMakePoint(frameRect.origin.x, frameRect.origin.y + frameRect.size.height) let topLeftPoint = NSMakePoint(frameRect.origin.x, frameRect.origin.y + frameRect.size.height)
frameRect.size = newSize frameRect.size = newSize
frameRect.origin = NSMakePoint(topLeftPoint.x, topLeftPoint.y - frameRect.size.height) frameRect.origin = NSMakePoint(topLeftPoint.x, topLeftPoint.y - frameRect.size.height)
self.window?.setFrame(frameRect, display: false) self.window?.setFrame(frameRect, display: false)
@ -380,4 +382,3 @@ extension HorizontalCandidateController {
} }
} }

View File

@ -34,10 +34,10 @@ fileprivate class VerticalKeyLabelStripView: NSView {
.paragraphStyle: paraStyle] .paragraphStyle: paraStyle]
for index in 0..<count { for index in 0..<count {
let textRect = NSRect(x: 0.0, y: CGFloat(index) * cellHeight + labelOffsetY, width: bounds.size.width, height: cellHeight - labelOffsetY) let textRect = NSRect(x: 0.0, y: CGFloat(index) * cellHeight + labelOffsetY, width: bounds.size.width, height: cellHeight - labelOffsetY)
var cellRect = NSRect(x: 0.0, y: CGFloat(index) * cellHeight, width: bounds.size.width, height: cellHeight - 1); var cellRect = NSRect(x: 0.0, y: CGFloat(index) * cellHeight, width: bounds.size.width, height: cellHeight - 1)
if index + 1 >= count { if index + 1 >= count {
cellRect.size.height += 1.0; cellRect.size.height += 1.0
} }
(index == highlightedIndex ? darkGray : lightGray).setFill() (index == highlightedIndex ? darkGray : lightGray).setFill()
@ -51,14 +51,14 @@ fileprivate class VerticalKeyLabelStripView: NSView {
fileprivate class VerticalCandidateTableView: NSTableView { fileprivate class VerticalCandidateTableView: NSTableView {
override func adjustScroll(_ newVisible: NSRect) -> NSRect { override func adjustScroll(_ newVisible: NSRect) -> NSRect {
var scrollRect = newVisible var scrollRect = newVisible
let rowHeightPlusSpacing = rowHeight + intercellSpacing.height; let rowHeightPlusSpacing = rowHeight + intercellSpacing.height
scrollRect.origin.y = (scrollRect.origin.y / rowHeightPlusSpacing) * rowHeightPlusSpacing; scrollRect.origin.y = (scrollRect.origin.y / rowHeightPlusSpacing) * rowHeightPlusSpacing
return scrollRect return scrollRect
} }
} }
private let kCandidateTextPadding = 24.0; private let kCandidateTextPadding = 24.0
private let kCandidateTextLeftMargin = 8.0; private let kCandidateTextLeftMargin = 8.0
private let kCandidateTextPaddingWithMandatedTableViewPadding = 18.0 private let kCandidateTextPaddingWithMandatedTableViewPadding = 18.0
private let kCandidateTextLeftMarginWithMandatedTableViewPadding = 0.0 private let kCandidateTextLeftMarginWithMandatedTableViewPadding = 0.0
@ -167,9 +167,9 @@ public class VerticalCandidateController: CandidateController {
let firstVisibleRow = tableView.row(at: scrollView.documentVisibleRect.origin) let firstVisibleRow = tableView.row(at: scrollView.documentVisibleRect.origin)
if firstVisibleRow != -1 { if firstVisibleRow != -1 {
let result = UInt(firstVisibleRow) + index; let result = UInt(firstVisibleRow) + index
if result < delegate.candidateCountForController(self) { if result < delegate.candidateCountForController(self) {
return result; return result
} }
} }
@ -239,7 +239,7 @@ extension VerticalCandidateController: NSTableViewDataSource, NSTableViewDelegat
// expand the window width if text overflows // expand the window width if text overflows
let boundingRect = attrString.boundingRect(with: NSSize(width: 10240.0, height: 10240.0) , options: .usesLineFragmentOrigin) let boundingRect = attrString.boundingRect(with: NSSize(width: 10240.0, height: 10240.0) , options: .usesLineFragmentOrigin)
let textWidth = boundingRect.size.width + candidateTextPadding; let textWidth = boundingRect.size.width + candidateTextPadding
if textWidth > maxCandidateAttrStringWidth { if textWidth > maxCandidateAttrStringWidth {
maxCandidateAttrStringWidth = textWidth maxCandidateAttrStringWidth = textWidth
layoutCandidateView() layoutCandidateView()
@ -254,17 +254,17 @@ extension VerticalCandidateController: NSTableViewDataSource, NSTableViewDelegat
if keyLabelStripView.highlightedIndex != -1 && if keyLabelStripView.highlightedIndex != -1 &&
(row >= selectedRow + Int(count) || (selectedRow > count && row <= selectedRow - Int(count))) { (row >= selectedRow + Int(count) || (selectedRow > count && row <= selectedRow - Int(count))) {
newHilightIndex = -1; newHilightIndex = -1
} else { } else {
let firstVisibleRow = tableView.row(at: scrollView.documentVisibleRect.origin) let firstVisibleRow = tableView.row(at: scrollView.documentVisibleRect.origin)
newHilightIndex = selectedRow - firstVisibleRow; newHilightIndex = selectedRow - firstVisibleRow
if newHilightIndex < -1 { if newHilightIndex < -1 {
newHilightIndex = -1 newHilightIndex = -1
} }
} }
if newHilightIndex != keyLabelStripView.highlightedIndex && newHilightIndex >= 0 { if newHilightIndex != keyLabelStripView.highlightedIndex && newHilightIndex >= 0 {
keyLabelStripView.highlightedIndex = UInt(newHilightIndex); keyLabelStripView.highlightedIndex = UInt(newHilightIndex)
keyLabelStripView.setNeedsDisplay(keyLabelStripView.frame) keyLabelStripView.setNeedsDisplay(keyLabelStripView.frame)
} }
@ -277,7 +277,7 @@ extension VerticalCandidateController: NSTableViewDataSource, NSTableViewDelegat
if selectedRow != -1 { if selectedRow != -1 {
// keep track of the highlighted index in the key label strip // keep track of the highlighted index in the key label strip
let firstVisibleRow = tableView.row(at: scrollView.documentVisibleRect.origin) let firstVisibleRow = tableView.row(at: scrollView.documentVisibleRect.origin)
keyLabelStripView.highlightedIndex = UInt(selectedRow - firstVisibleRow); keyLabelStripView.highlightedIndex = UInt(selectedRow - firstVisibleRow)
keyLabelStripView.setNeedsDisplay(keyLabelStripView.frame) keyLabelStripView.setNeedsDisplay(keyLabelStripView.frame)
// fix a subtle OS X "bug" that, since we force the scroller to appear, // fix a subtle OS X "bug" that, since we force the scroller to appear,
@ -413,8 +413,8 @@ extension VerticalCandidateController: NSTableViewDataSource, NSTableViewDelegat
var frameRect = self.window?.frame ?? NSRect.zero var frameRect = self.window?.frame ?? NSRect.zero
let topLeftPoint = NSMakePoint(frameRect.origin.x, frameRect.origin.y + frameRect.size.height) let topLeftPoint = NSMakePoint(frameRect.origin.x, frameRect.origin.y + frameRect.size.height)
frameRect.size = NSMakeSize(windowWidth, windowHeight); frameRect.size = NSMakeSize(windowWidth, windowHeight)
frameRect.origin = NSMakePoint(topLeftPoint.x, topLeftPoint.y - frameRect.size.height); frameRect.origin = NSMakePoint(topLeftPoint.x, topLeftPoint.y - frameRect.size.height)
keyLabelStripView.frame = NSRect(x: 0.0, y: 0.0, width: stripWidth, height: windowHeight) keyLabelStripView.frame = NSRect(x: 0.0, y: 0.0, width: stripWidth, height: windowHeight)
scrollView.frame = NSRect(x: stripWidth + 1.0, y: 0.0, width: tableViewStartWidth, height: windowHeight) scrollView.frame = NSRect(x: stripWidth + 1.0, y: 0.0, width: tableViewStartWidth, height: windowHeight)