142 lines
5.4 KiB
Swift
142 lines
5.4 KiB
Swift
import AppKit
|
|
|
|
/// 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.
|
|
var onSelect: ((Int) -> Void)? {
|
|
get { list.onSelect }
|
|
set { list.onSelect = newValue }
|
|
}
|
|
|
|
var labels: [String] { list.labels }
|
|
|
|
/// The height of all entries.
|
|
var contentHeight: CGFloat { list.frame.height }
|
|
|
|
init() {
|
|
super.init(frame: .zero)
|
|
drawsBackground = false
|
|
borderType = .noBorder
|
|
hasVerticalScroller = true
|
|
hasHorizontalScroller = false
|
|
autohidesScrollers = true
|
|
scrollerStyle = .overlay
|
|
automaticallyAdjustsContentInsets = false
|
|
contentInsets = NSEdgeInsetsZero
|
|
contentView.drawsBackground = false
|
|
documentView = list
|
|
}
|
|
|
|
@available(*, unavailable)
|
|
required init?(coder: NSCoder) { fatalError("init(coder:) is not supported") }
|
|
|
|
func show(labels: [String], font: NSFont, width: CGFloat) {
|
|
list.update(labels: labels, font: font, width: width)
|
|
}
|
|
|
|
/// The pointing hand over an entry, the arrow elsewhere.
|
|
func updateCursor(at locationInWindow: NSPoint) {
|
|
let point = list.convert(locationInWindow, from: nil)
|
|
(list.entry(at: point) != nil ? NSCursor.pointingHand : NSCursor.arrow).set()
|
|
}
|
|
|
|
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
|
|
}
|
|
|
|
/// The entries of the bookmark index, top to bottom.
|
|
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 }
|
|
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
|
|
|
|
var lineHeight: CGFloat { PageMetrics.lineHeight(font) }
|
|
|
|
func update(labels: [String], font: NSFont, width: CGFloat) {
|
|
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)
|
|
}
|
|
|
|
func entryRect(_ index: Int) -> NSRect {
|
|
NSRect(x: 0, y: CGFloat(index) * lineHeight, width: bounds.width, height: lineHeight)
|
|
}
|
|
|
|
func entry(at point: NSPoint) -> Int? {
|
|
guard bounds.contains(point) else { return nil }
|
|
let index = Int(point.y / lineHeight)
|
|
return labels.indices.contains(index) ? index : nil
|
|
}
|
|
|
|
override func draw(_ dirtyRect: NSRect) {
|
|
let style = NSMutableParagraphStyle()
|
|
style.lineBreakMode = .byTruncatingTail
|
|
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])
|
|
}
|
|
}
|
|
|
|
override func mouseDown(with event: NSEvent) {
|
|
guard let index = entry(at: convert(event.locationInWindow, from: nil)) else { return }
|
|
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)
|
|
if !rect.isEmpty { addCursorRect(rect, cursor: .pointingHand) }
|
|
}
|
|
}
|
|
}
|