Commit 564370e071
Verified · cmc
Layout: unified · split
Tests/OrgEditorAppKitTests/TwoViewsSpikeTests.swift added +117
| @@ -0,0 +1,117 @@ | |||
| 1 | import AppKit | ||
| 2 | import Testing | ||
| 3 | |||
| 4 | /// Spike for windows and panes (docs/plans/2026-10-10-windows-and-panes.md, Task 0): two | ||
| 5 | /// `NSTextView`s, each with its own `NSTextLayoutManager`, on one `NSTextContentStorage`. | ||
| 6 | /// Findings are the test names. Two more: the layout manager's delegate must be in place before | ||
| 7 | /// its first layout (`invalidateLayout(for:)` afterwards kept the old fragments here), and IME | ||
| 8 | /// composition is not covered headlessly. | ||
| 9 | @MainActor | ||
| 10 | struct TwoViewsSpikeTests { | ||
| 11 | @MainActor final class Shared: NSObject, NSTextViewDelegate { | ||
| 12 | let undo = UndoManager() | ||
| 13 | override init() { super.init() } | ||
| 14 | func undoManager(for view: NSTextView) -> UndoManager? { undo } | ||
| 15 | } | ||
| 16 | |||
| 17 | final class MarkedFragment: NSTextLayoutFragment, @unchecked Sendable {} | ||
| 18 | |||
| 19 | final class FirstMarked: NSObject, NSTextLayoutManagerDelegate, @unchecked Sendable { | ||
| 20 | nonisolated func textLayoutManager(_ manager: NSTextLayoutManager, textLayoutFragmentFor location: NSTextLocation, in element: NSTextElement) -> NSTextLayoutFragment { | ||
| 21 | let content = manager.textContentManager! | ||
| 22 | if content.offset(from: content.documentRange.location, to: location) == 0 { | ||
| 23 | return MarkedFragment(textElement: element, range: element.elementRange) | ||
| 24 | } | ||
| 25 | return NSTextLayoutFragment(textElement: element, range: element.elementRange) | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | @MainActor struct Pair { | ||
| 30 | let storage: NSTextContentStorage | ||
| 31 | let shared: Shared | ||
| 32 | let a: NSTextView | ||
| 33 | let b: NSTextView | ||
| 34 | let windows: [NSWindow] | ||
| 35 | |||
| 36 | init(_ text: String, delegateForB: NSTextLayoutManagerDelegate? = nil) { | ||
| 37 | _ = NSApplication.shared | ||
| 38 | storage = NSTextContentStorage() | ||
| 39 | shared = Shared() | ||
| 40 | let storage = self.storage | ||
| 41 | storage.textStorage?.setAttributedString(NSAttributedString(string: text)) | ||
| 42 | func view(_ delegate: NSTextLayoutManagerDelegate? = nil) -> NSTextView { | ||
| 43 | let layout = NSTextLayoutManager() | ||
| 44 | layout.delegate = delegate | ||
| 45 | storage.addTextLayoutManager(layout) | ||
| 46 | let container = NSTextContainer(size: CGSize(width: 600, height: 100_000)) | ||
| 47 | layout.textContainer = container | ||
| 48 | let view = NSTextView(frame: NSRect(x: 0, y: 0, width: 600, height: 800), textContainer: container) | ||
| 49 | view.allowsUndo = true | ||
| 50 | return view | ||
| 51 | } | ||
| 52 | a = view(); b = view(delegateForB) | ||
| 53 | a.delegate = shared; b.delegate = shared | ||
| 54 | windows = [a, b].map { view in | ||
| 55 | let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 600, height: 800), styleMask: [.titled], backing: .buffered, defer: false) | ||
| 56 | window.contentView = view | ||
| 57 | window.makeFirstResponder(view) | ||
| 58 | return window | ||
| 59 | } | ||
| 60 | for view in [a, b] { view.textLayoutManager!.ensureLayout(for: view.textLayoutManager!.documentRange) } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | @Test func typingInOneViewShowsInTheOther() { | ||
| 65 | let pair = Pair("* a\nbody\n") | ||
| 66 | pair.a.insertText("X", replacementRange: NSRange(location: 0, length: 0)) | ||
| 67 | #expect(pair.b.string == "X* a\nbody\n") | ||
| 68 | #expect(pair.b.textLayoutManager!.textContentManager === pair.a.textLayoutManager!.textContentManager) | ||
| 69 | } | ||
| 70 | |||
| 71 | @Test func undoFromTheOtherViewRevertsTyping() { | ||
| 72 | let pair = Pair("* a\nbody\n") | ||
| 73 | pair.a.insertText("X", replacementRange: NSRange(location: 0, length: 0)) | ||
| 74 | #expect(pair.shared.undo.canUndo) | ||
| 75 | pair.b.undoManager?.undo() | ||
| 76 | #expect(pair.a.string == "* a\nbody\n") | ||
| 77 | #expect(pair.b.string == "* a\nbody\n") | ||
| 78 | } | ||
| 79 | |||
| 80 | @Test func fragmentsDifferPerLayoutManager() { | ||
| 81 | let delegate = FirstMarked() | ||
| 82 | let pair = Pair("* a\nbody\n", delegateForB: delegate) | ||
| 83 | func kinds(_ view: NSTextView) -> [String] { | ||
| 84 | let layout = view.textLayoutManager! | ||
| 85 | layout.ensureLayout(for: layout.documentRange) | ||
| 86 | var out: [String] = [] | ||
| 87 | layout.enumerateTextLayoutFragments(from: layout.documentRange.location, options: [.ensuresLayout]) { out.append(String(describing: type(of: $0))); return true } | ||
| 88 | return out | ||
| 89 | } | ||
| 90 | #expect(kinds(pair.b).first == "MarkedFragment") | ||
| 91 | #expect(kinds(pair.a).first == "NSTextLayoutFragment") | ||
| 92 | } | ||
| 93 | |||
| 94 | @Test func renderingAttributesStayWithTheirLayoutManager() { | ||
| 95 | let pair = Pair("* a\nbody\n") | ||
| 96 | let layoutA = pair.a.textLayoutManager!, layoutB = pair.b.textLayoutManager! | ||
| 97 | let range = NSTextRange(location: layoutA.documentRange.location, end: layoutA.location(layoutA.documentRange.location, offsetBy: 3))! | ||
| 98 | layoutA.addRenderingAttribute(.foregroundColor, value: NSColor.red, for: range) | ||
| 99 | func hasRed(_ layout: NSTextLayoutManager) -> Bool { | ||
| 100 | var found = false | ||
| 101 | layout.enumerateRenderingAttributes(from: layout.documentRange.location, reverse: false) { _, attributes, _ in | ||
| 102 | if attributes[.foregroundColor] as? NSColor == .red { found = true } | ||
| 103 | return !found | ||
| 104 | } | ||
| 105 | return found | ||
| 106 | } | ||
| 107 | #expect(hasRed(layoutA)) | ||
| 108 | #expect(!hasRed(layoutB)) | ||
| 109 | } | ||
| 110 | |||
| 111 | @Test func selectionInTheOtherViewMovesWithAnInsertionBeforeIt() { | ||
| 112 | let pair = Pair("* a\nbody\n") | ||
| 113 | pair.b.setSelectedRange(NSRange(location: 5, length: 0)) | ||
| 114 | pair.a.insertText("XY", replacementRange: NSRange(location: 0, length: 0)) | ||
| 115 | #expect(pair.b.selectedRange() == NSRange(location: 7, length: 0)) | ||
| 116 | } | ||
| 117 | } | ||