improvements on the ui
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
import AppKit
|
||||
|
||||
/// The bookmark index at the top left of the window: one line per bookmark, the last bookmarks of the page only.
|
||||
/// The bookmark index at the top left of the window: one line per bookmark, the last bookmarks of the page only,
|
||||
/// and the `end` entry below them.
|
||||
final class BookmarkIndexView: NSScrollView {
|
||||
/// The last entry, which jumps to the end of the page; it is no bookmark.
|
||||
static let endLabel = "end"
|
||||
|
||||
let list = BookmarkIndexList()
|
||||
|
||||
/// A click on the entry at this index.
|
||||
@@ -50,6 +54,9 @@ final class BookmarkIndexList: NSView {
|
||||
private(set) var labels: [String] = []
|
||||
private(set) var font = PlakFonts.mono(10)
|
||||
var onSelect: ((Int) -> Void)?
|
||||
/// The entry under the pointer; it draws in the accent colour.
|
||||
private(set) var hoveredEntry: Int?
|
||||
private var hoverTracking: NSTrackingArea?
|
||||
|
||||
override var isFlipped: Bool { true }
|
||||
override var acceptsFirstResponder: Bool { false }
|
||||
@@ -61,6 +68,7 @@ final class BookmarkIndexList: NSView {
|
||||
guard labels != self.labels || font != self.font || width != frame.width else { return }
|
||||
self.labels = labels
|
||||
self.font = font
|
||||
if let hoveredEntry, !labels.indices.contains(hoveredEntry) { self.hoveredEntry = nil }
|
||||
setFrameSize(NSSize(width: width, height: CGFloat(labels.count) * lineHeight))
|
||||
needsDisplay = true
|
||||
window?.invalidateCursorRects(for: self)
|
||||
@@ -79,12 +87,13 @@ final class BookmarkIndexList: NSView {
|
||||
override func draw(_ dirtyRect: NSRect) {
|
||||
let style = NSMutableParagraphStyle()
|
||||
style.lineBreakMode = .byTruncatingTail
|
||||
let attributes: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: Theme.inkMuted, .paragraphStyle: style]
|
||||
var attributes: [NSAttributedString.Key: Any] = [.font: font, .paragraphStyle: style]
|
||||
let natural = PlakFonts.naturalLineHeight(font)
|
||||
for (index, label) in labels.enumerated() {
|
||||
let row = entryRect(index)
|
||||
guard row.intersects(dirtyRect) else { continue }
|
||||
let rect = NSRect(x: row.minX, y: row.minY + floor((row.height - natural) / 2), width: row.width, height: natural)
|
||||
attributes[.foregroundColor] = index == hoveredEntry ? Theme.accent : Theme.inkMuted
|
||||
NSAttributedString(string: label, attributes: attributes).draw(with: rect, options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine])
|
||||
}
|
||||
}
|
||||
@@ -94,6 +103,35 @@ final class BookmarkIndexList: NSView {
|
||||
onSelect?(index)
|
||||
}
|
||||
|
||||
/// Always active: a click works on a window that is not key, so the hover shows there too.
|
||||
override func updateTrackingAreas() {
|
||||
super.updateTrackingAreas()
|
||||
if let hoverTracking { removeTrackingArea(hoverTracking) }
|
||||
let area = NSTrackingArea(rect: .zero, options: [.mouseMoved, .mouseEnteredAndExited, .activeAlways, .inVisibleRect], owner: self)
|
||||
addTrackingArea(area)
|
||||
hoverTracking = area
|
||||
}
|
||||
|
||||
override func mouseEntered(with event: NSEvent) {
|
||||
hover(entry(at: convert(event.locationInWindow, from: nil)))
|
||||
}
|
||||
|
||||
override func mouseMoved(with event: NSEvent) {
|
||||
hover(entry(at: convert(event.locationInWindow, from: nil)))
|
||||
}
|
||||
|
||||
override func mouseExited(with event: NSEvent) {
|
||||
hover(nil)
|
||||
}
|
||||
|
||||
/// Redraws only the entry that loses the hover and the one that gets it.
|
||||
func hover(_ index: Int?) {
|
||||
guard index != hoveredEntry else { return }
|
||||
if let hoveredEntry { setNeedsDisplay(entryRect(hoveredEntry)) }
|
||||
hoveredEntry = index
|
||||
if let index { setNeedsDisplay(entryRect(index)) }
|
||||
}
|
||||
|
||||
override func resetCursorRects() {
|
||||
for index in labels.indices {
|
||||
let rect = entryRect(index).intersection(visibleRect)
|
||||
|
||||
Reference in New Issue
Block a user