improvements on the ui

This commit is contained in:
2026-11-25 07:14:49 +01:00
parent 3afdb6d67f
commit b7c72b9e16
249 changed files with 19702 additions and 17358 deletions
+44 -22
View File
@@ -85,8 +85,7 @@ public final class PageViewController: NSViewController, NSTextViewDelegate, NST
dragStrip.setDropTarget(textView)
textView.dragStrip = dragStrip
bookmarkIndex.isHidden = true
bookmarkIndex.onSelect = { [weak self] index in self?.showBookmark(entry: index) }
bookmarkIndex.onSelect = { [weak self] index in self?.selectIndexEntry(index) }
textView.bookmarkIndex = bookmarkIndex
root.addSubview(scrollView)
@@ -216,8 +215,9 @@ public final class PageViewController: NSViewController, NSTextViewDelegate, NST
public func textDidChange(_ notification: Notification) {
stampBlocks()
store.changed()
let room = bookmarkIndexRoom
textView.reloadBookmarks()
if textView.topSpace > PageMetrics.topInset || textView.frame.height <= clip.bounds.height * 2 {
if room != bookmarkIndexRoom || textView.topSpace > PageMetrics.topInset || textView.frame.height <= clip.bounds.height * 2 {
layoutPage()
} else {
layoutBookmarkIndex()
@@ -245,16 +245,25 @@ public final class PageViewController: NSViewController, NSTextViewDelegate, NST
// MARK: Layout
/// Puts the column in the middle; a short page gets more room on top so its end stays in the middle.
/// The room left of the column that the bookmark index needs.
private var bookmarkIndexRoom: CGFloat {
let end = NSAttributedString(string: BookmarkIndexView.endLabel, attributes: [.font: textView.bookmarkFont]).size().width
let gaps = PageMetrics.bookmarkIndexInset + PageMetrics.bookmarkGap * 2
return min(ceil(max(textView.widestBookmarkLabel, end)) + gaps, PageMetrics.bookmarkIndexRoomMaximum)
}
/// Puts the column in the middle, or further right when the bookmark index needs the room on its left.
/// A short page gets more room on top so its end stays in the middle.
private func layoutPage() {
let visible = clip.bounds.size
guard visible.width > 0, let container = textView.textContainer, let layoutManager = textView.layoutManager else { return }
let column = max(1, min(PageMetrics.columnWidth(bodySize: textSize.body, characters: columnWidth.characters), visible.width - PageMetrics.sideMargin * 2))
var column = max(1, min(PageMetrics.columnWidth(bodySize: textSize.body, characters: columnWidth.characters), visible.width - PageMetrics.sideMargin * 2))
let side = max(floor((visible.width - column) / 2), bookmarkIndexRoom)
column = max(1, min(column, visible.width - side - PageMetrics.sideMargin))
if container.size.width != column {
container.size = NSSize(width: column, height: CGFloat.greatestFiniteMagnitude)
}
layoutManager.ensureLayout(for: container)
let side = floor((visible.width - column) / 2)
let bottom = round(visible.height / 2)
let endMid = textView.lineRect(at: textView.length).midY - textView.topSpace
let top = max(PageMetrics.topInset, round(visible.height / 2 - endMid))
@@ -266,24 +275,37 @@ public final class PageViewController: NSViewController, NSTextViewDelegate, NST
layoutBookmarkIndex()
}
/// Shows the bookmark index at the top left when bookmarks are wide, as wide as the side space.
/// Shows the bookmark index at the top left, as wide as the room left of the column.
private func layoutBookmarkIndex() {
let labels = textView.bookmarksAreWide ? textView.bookmarks.suffix(PageMetrics.bookmarkIndexCount).map(\.label) : []
let wasHidden = bookmarkIndex.isHidden
bookmarkIndex.isHidden = labels.isEmpty
if !labels.isEmpty {
let inset = PageMetrics.bookmarkIndexInset
let width = max(0, textView.sideSpace - inset - PageMetrics.bookmarkGap * 2)
bookmarkIndex.show(labels: labels, font: textView.bookmarkFont, width: width)
let top = view.bounds.height - PageMetrics.topInset
let height = max(0, min(bookmarkIndex.contentHeight, top - inset))
let frame = NSRect(x: inset, y: top - height, width: width, height: height)
if bookmarkIndex.frame != frame { bookmarkIndex.frame = frame }
// When the entries do not fit, the lowest bookmarks stay in sight.
bookmarkIndex.contentView.scroll(to: NSPoint(x: 0, y: max(0, bookmarkIndex.contentHeight - height)))
bookmarkIndex.reflectScrolledClipView(bookmarkIndex.contentView)
let labels = textView.bookmarks.suffix(PageMetrics.bookmarkIndexCount).map(\.label) + [BookmarkIndexView.endLabel]
let inset = PageMetrics.bookmarkIndexInset
let width = max(0, textView.sideSpace - inset - PageMetrics.bookmarkGap * 2)
bookmarkIndex.show(labels: labels, font: textView.bookmarkFont, width: width)
let top = view.bounds.height - PageMetrics.topInset
let height = max(0, min(bookmarkIndex.contentHeight, top - inset))
let frame = NSRect(x: inset, y: top - height, width: width, height: height)
if bookmarkIndex.frame != frame { bookmarkIndex.frame = frame }
// When the entries do not fit, the lowest entries stay in sight.
bookmarkIndex.contentView.scroll(to: NSPoint(x: 0, y: max(0, bookmarkIndex.contentHeight - height)))
bookmarkIndex.reflectScrolledClipView(bookmarkIndex.contentView)
view.window?.invalidateCursorRects(for: textView)
}
/// A click on an entry of the bookmark index: a bookmark, or `end` below them.
func selectIndexEntry(_ entry: Int) {
if entry == min(textView.bookmarks.count, PageMetrics.bookmarkIndexCount) {
showPageEnd()
} else {
showBookmark(entry: entry)
}
if wasHidden != bookmarkIndex.isHidden || !labels.isEmpty { view.window?.invalidateCursorRects(for: textView) }
}
/// Puts the caret at the end of the page and scrolls the end to the vertical middle, as ⌘↓ does.
func showPageEnd() {
closeEditor()
view.window?.makeFirstResponder(textView)
textView.setSelectedRange(NSRange(location: textView.length, length: 0))
scrollEndToMiddle()
}
/// Puts the caret at the end of the bookmark line and scrolls that line to the vertical middle.