From 38c5e809c9afcff18169f71a1c03ea04806db4a8 Mon Sep 17 00:00:00 2001 From: zonble Date: Tue, 28 Nov 2023 20:46:23 +0800 Subject: [PATCH 1/2] Uses label color instead of secondary label color on key caps. Sometimes if a user is inputting on an app with a dark background in the light mode, it could be very hard to read the text on the key caps. --- .../Sources/CandidateUI/HorizontalCandidateController.swift | 2 +- .../Sources/CandidateUI/VerticalCandidateController.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Packages/CandidateUI/Sources/CandidateUI/HorizontalCandidateController.swift b/Packages/CandidateUI/Sources/CandidateUI/HorizontalCandidateController.swift index 7538b636..0e2c797a 100644 --- a/Packages/CandidateUI/Sources/CandidateUI/HorizontalCandidateController.swift +++ b/Packages/CandidateUI/Sources/CandidateUI/HorizontalCandidateController.swift @@ -97,7 +97,7 @@ fileprivate class HorizontalCandidateView: NSView { keyLabelAttrDict = [.font: labelFont, .paragraphStyle: paraStyle, - .foregroundColor: NSColor.secondaryLabelColor] + .foregroundColor: NSColor.labelColor] candidateAttrDict = [.font: candidateFont, .paragraphStyle: paraStyle, .foregroundColor: NSColor.labelColor] diff --git a/Packages/CandidateUI/Sources/CandidateUI/VerticalCandidateController.swift b/Packages/CandidateUI/Sources/CandidateUI/VerticalCandidateController.swift index 88e03311..f09e2025 100644 --- a/Packages/CandidateUI/Sources/CandidateUI/VerticalCandidateController.swift +++ b/Packages/CandidateUI/Sources/CandidateUI/VerticalCandidateController.swift @@ -48,7 +48,7 @@ fileprivate class VerticalKeyLabelStripView: NSView { let textAttr: [NSAttributedString.Key: AnyObject] = [ .font: keyLabelFont, - .foregroundColor: NSColor.secondaryLabelColor, + .foregroundColor: NSColor.labelColor, .paragraphStyle: paraStyle] let textAttrHighlighted: [NSAttributedString.Key: AnyObject] = [ .font: keyLabelFont, From ed66196041dfb216866e720fcd4ad4b3657bcf8a Mon Sep 17 00:00:00 2001 From: zonble Date: Tue, 28 Nov 2023 22:06:44 +0800 Subject: [PATCH 2/2] Fine-tunes the scroller for the vertical candidate window. When the scroller stops, it checks if the highlighted row is visible and highlights another row if not. If also tries to snap the scroller. --- .../VerticalCandidateController.swift | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/Packages/CandidateUI/Sources/CandidateUI/VerticalCandidateController.swift b/Packages/CandidateUI/Sources/CandidateUI/VerticalCandidateController.swift index f09e2025..83efe12c 100644 --- a/Packages/CandidateUI/Sources/CandidateUI/VerticalCandidateController.swift +++ b/Packages/CandidateUI/Sources/CandidateUI/VerticalCandidateController.swift @@ -134,6 +134,7 @@ public class VerticalCandidateController: CandidateController { scrollView.verticalScrollElasticity = .none scrollView.drawsBackground = false scrollView.contentView.drawsBackground = false + scrollView.contentView.postsBoundsChangedNotifications = true tableView = NSTableView(frame: contentRect) let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "candidate")) @@ -171,6 +172,11 @@ public class VerticalCandidateController: CandidateController { tableView.delegate = self tableView.doubleAction = #selector(rowDoubleClicked(_:)) tableView.target = self + + NotificationCenter.default.addObserver(self, + selector: #selector(boundsChange), + name: NSView.boundsDidChangeNotification, + object: scrollView.contentView) } required init?(coder: NSCoder) { @@ -256,6 +262,27 @@ public class VerticalCandidateController: CandidateController { tableView.selectRowIndexes(IndexSet(integer: Int(newIndex)), byExtendingSelection: false) } } + + var scrollTimer: Timer? + + @objc func boundsChange() { + let visibleRect = tableView.visibleRect + let visibleRowIndexes = tableView.rows(in: visibleRect) + let selected = selectedCandidateIndex + + 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 + } + } + } } extension VerticalCandidateController: NSTableViewDataSource, NSTableViewDelegate { @@ -414,7 +441,6 @@ extension VerticalCandidateController: NSTableViewDataSource, NSTableViewDelegat return } - var tooltipHeight: CGFloat = 0 var tooltipWidth: CGFloat = 0 @@ -483,6 +509,8 @@ extension VerticalCandidateController: NSTableViewDataSource, NSTableViewDelegat tooltipView.frame = NSRect(x: tooltipPadding, y: windowHeight - tooltipHeight + tooltipPadding, width: windowWidth, height: tooltipHeight) self.window?.setFrame(frameRect, display: false) } + + } extension NSImage {