iOS: redraw only the blocks when the band widens !231

merged merged by cmc on 2026-10-11 03:14 UTC · krz/orgstar:claude/gracious-lalande-edc4f9 into main

2 files changed, +22 −9

Layout: unified · split

Sources/OrgstarMobile/AgendaScreen.swift +1 −1
@@ -846,7 +846,7 @@ struct DateSheet: View {
846 } 846 }
847} 847}
848 848
849extension AgendaItem: @retroactive Identifiable { 849extension AgendaItem: Identifiable {
850 public var id: String { "\(path ?? "")|\(headingOffset ?? -1)|\(kind.rawValue)|\(text)" } 850 public var id: String { "\(path ?? "")|\(headingOffset ?? -1)|\(kind.rawValue)|\(text)" }
851 851
852 /// An entry, not a time-grid line. 852 /// An entry, not a time-grid line.
Sources/OrgstarMobile/MobileEditor.swift +21 −8
@@ -548,13 +548,18 @@ final class MobileEditor: NSObject, DocumentBuffer {
548 let range = selected.length > 0 ? selected.location..<NSMaxRange(selected) : nil 548 let range = selected.length > 0 ? selected.location..<NSMaxRange(selected) : nil
549 let old = fragmentStyle.selection?.range 549 let old = fragmentStyle.selection?.range
550 fragmentStyle.set(selection: range, color: selectionColor) 550 fragmentStyle.set(selection: range, color: selectionColor)
551 guard let manager = textView.textLayoutManager, let content = manager.textContentManager else { return }
552 let touched = [old, range].compactMap { $0 } 551 let touched = [old, range].compactMap { $0 }
553 for block in shaded.all where touched.contains(where: { $0.overlaps(block) }) { 552 redraw(blocks: shaded.all.filter { block in touched.contains { $0.overlaps(block) } })
553 }
554
555 /// Lays `blocks` out again and asks their fragments, which are kept across layout passes,
556 /// to draw again.
557 private func redraw(blocks: [Range<Int>]) {
558 guard let manager = textView.textLayoutManager, let content = manager.textContentManager else { return }
559 for block in blocks {
554 guard let start = content.location(content.documentRange.location, offsetBy: block.lowerBound), 560 guard let start = content.location(content.documentRange.location, offsetBy: block.lowerBound),
555 let end = content.location(start, offsetBy: block.count), 561 let end = content.location(start, offsetBy: block.count),
556 let textRange = NSTextRange(location: start, end: end) else { continue } 562 let textRange = NSTextRange(location: start, end: end) else { continue }
557 // Fragments are kept across layout passes, so they are asked to draw again too.
558 manager.invalidateRenderingAttributes(for: textRange) 563 manager.invalidateRenderingAttributes(for: textRange)
559 manager.invalidateLayout(for: textRange) 564 manager.invalidateLayout(for: textRange)
560 } 565 }
@@ -562,7 +567,9 @@ final class MobileEditor: NSObject, DocumentBuffer {
562 } 567 }
563 568
564 /// The band behind blocks spans the view's width, or the widest line's when truncated; 569 /// The band behind blocks spans the view's width, or the widest line's when truncated;
565 /// images are as wide as the view allows. 570 /// images are as wide as the view allows. Only the blocks read the band's width, so a
571 /// wider line in a truncated file, which widens the band on each layout pass, redraws
572 /// the blocks alone: laying the whole file out again moves the text under the view.
566 private func viewResized() { 573 private func viewResized() {
567 let band = fragmentStyle.blockBand 574 let band = fragmentStyle.blockBand
568 let width = max(textView.bounds.width, truncatesLines ? textView.contentSize.width : 0) 575 let width = max(textView.bounds.width, truncatesLines ? textView.contentSize.width : 0)
@@ -570,10 +577,16 @@ final class MobileEditor: NSObject, DocumentBuffer {
570 guard band.width != width || resized else { return } 577 guard band.width != width || resized else { return }
571 fragmentStyle.set(blockBackground: band.color, width: width) 578 fragmentStyle.set(blockBackground: band.color, width: width)
572 imageWidth = textView.bounds.width 579 imageWidth = textView.bounds.width
573 if showsImages, resized { restyle(0..<textView.textStorage.length) } 580 guard resized else {
574 if let manager = textView.textLayoutManager { 581 redraw(blocks: shaded.all)
575 manager.invalidateRenderingAttributes(for: manager.documentRange) 582 return
576 manager.invalidateLayout(for: manager.documentRange) 583 }
584 keepingTopLine {
585 if showsImages { restyle(0..<textView.textStorage.length) }
586 if let manager = textView.textLayoutManager {
587 manager.invalidateRenderingAttributes(for: manager.documentRange)
588 manager.invalidateLayout(for: manager.documentRange)
589 }
577 } 590 }
578 } 591 }
579 592