import AppKit /// The bookmark index at the top left of the window: one line per bookmark, the last bookmarks of the page only. final class BookmarkIndexView: NSScrollView { 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)? 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 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 let attributes: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: Theme.inkMuted, .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) 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) } override func resetCursorRects() { for index in labels.indices { let rect = entryRect(index).intersection(visibleRect) if !rect.isEmpty { addCursorRect(rect, cursor: .pointingHand) } } } }