@@ -131,6 +131,63 @@ final class CodeTextCoreTests: XCTestCase {
131131 CGPoint ( x: 7 , y: 140 )
132132 )
133133 }
134+
135+ @MainActor
136+ func testMacViewerKeepsFirstLineBelowToolbarWhileWindowResizes( ) throws {
137+ let highlights = CodeHighlightStore ( grammars: [ ] )
138+ let sourceCode = ( 1 ... 200 )
139+ . map {
140+ " let line \( $0) = \( $0) // " + String( repeating: " x " , count: 60 )
141+ }
142+ . joined ( separator: " \n " )
143+ let hostingView = NSHostingView (
144+ rootView: CodeViewer (
145+ documentID: " resize-test " ,
146+ sourceCode: sourceCode,
147+ highlightStore: highlights,
148+ language: " plain "
149+ )
150+ . ignoresSafeArea ( )
151+ )
152+ let window = NSWindow (
153+ contentRect: NSRect ( x: 0 , y: 0 , width: 700 , height: 500 ) ,
154+ styleMask: [ . titled, . resizable, . fullSizeContentView] ,
155+ backing: . buffered,
156+ defer: false
157+ )
158+ window. titleVisibility = . hidden
159+ window. titlebarAppearsTransparent = true
160+ window. toolbar = NSToolbar ( identifier: " CodeViewerKit.ResizeTest " )
161+ window. contentView = hostingView
162+
163+ hostingView. layoutSubtreeIfNeeded ( )
164+
165+ let scrollView = try XCTUnwrap (
166+ hostingView. firstDescendant ( ofType: NSScrollView . self)
167+ )
168+ XCTAssertGreaterThan ( scrollView. contentInsets. top, 0 )
169+ XCTAssertFalse ( scrollView. automaticallyAdjustsContentInsets)
170+ XCTAssertFalse ( scrollView. contentView. automaticallyAdjustsContentInsets)
171+ XCTAssertEqual (
172+ scrollView. contentView. bounds. minY + scrollView. contentInsets. top,
173+ 0 ,
174+ accuracy: 0.5
175+ )
176+
177+ for size in [
178+ NSSize ( width: 300 , height: 350 ) ,
179+ NSSize ( width: 900 , height: 600 )
180+ ] {
181+ window. setContentSize ( size)
182+ hostingView. layoutSubtreeIfNeeded ( )
183+
184+ XCTAssertEqual (
185+ scrollView. contentView. bounds. minY + scrollView. contentInsets. top,
186+ 0 ,
187+ accuracy: 0.5
188+ )
189+ }
190+ }
134191 #endif
135192
136193 func testPlainTextStyleUsesAnAppearanceAwareDefault( ) {
@@ -175,3 +232,22 @@ final class CodeTextCoreTests: XCTestCase {
175232 #endif
176233 }
177234}
235+
236+ #if os(macOS)
237+ private extension NSView {
238+ func firstDescendant< ViewType: NSView > (
239+ ofType type: ViewType . Type
240+ ) -> ViewType ? {
241+ if let match = self as? ViewType {
242+ return match
243+ }
244+
245+ for subview in subviews {
246+ if let match = subview. firstDescendant ( ofType: type) {
247+ return match
248+ }
249+ }
250+ return nil
251+ }
252+ }
253+ #endif
0 commit comments