@@ -87,6 +87,8 @@ var _initializing_margins := false
8787var _base_offset := Vector2 .ZERO
8888## True after first margin/layout initialization; gates follow_focus during startup
8989var _startup_done := false
90+ ## True if initial margin calculation was skipped due to being hidden
91+ var _initial_margins_skipped := false
9092## When true, `content_node`'s position is only set by dragging the h scroll bar
9193var h_scrollbar_dragging := false
9294## When true, `content_node`'s position is only set by dragging the v scroll bar
@@ -144,8 +146,14 @@ func _ready() -> void:
144146 get_h_scroll_bar ().mouse_exited .connect (_mouse_on_scroll_bar .bind (false ))
145147 get_viewport ().gui_focus_changed .connect (_on_focus_changed )
146148
149+ visibility_changed .connect (_visibility_changed )
147150 theme_changed .connect (_update_content_margins )
148- call_deferred ("_update_content_margins" )
151+
152+ # Check if we're initially hidden - if so, defer margin calculation until visible
153+ if visible :
154+ call_deferred ("_update_content_margins" )
155+ else :
156+ _initial_margins_skipped = true
149157
150158 for c in get_children ():
151159 if not c is ScrollBar :
@@ -337,6 +345,7 @@ func _scrollbar_hide_timer_timeout() -> void:
337345# Updates content margins from current StyleBox
338346# Captures baseline offset and clears velocity; keeps scroll math in margin-free space
339347func _update_content_margins () -> void :
348+ print ("updated" )
340349 _initializing_margins = true
341350
342351 var style_box = get_theme_stylebox ("panel" )
@@ -349,15 +358,30 @@ func _update_content_margins() -> void:
349358 content_margins = Vector4 .ZERO
350359
351360 if content_node :
352- # Capture new baseline offset from layout; rendering uses _base_offset + pos
353- _base_offset = content_node .position
354- velocity = Vector2 .ZERO
361+ if _initial_margins_skipped :
362+ _base_offset = Vector2 (content_margins .x , content_margins .y )
363+ pos = Vector2 .ZERO
364+ else :
365+ var current_scroll_pos = pos
366+ # Capture new baseline offset from layout; rendering uses _base_offset + pos
367+ _base_offset = content_node .position - current_scroll_pos
368+
369+ if not _startup_done :
370+ velocity = Vector2 .ZERO
355371
356372 call_deferred ("_end_margin_init" )
357373
358374func _end_margin_init () -> void :
359375 _initializing_margins = false
360376 _startup_done = true
377+ _initial_margins_skipped = false
378+
379+ func _visibility_changed () -> void :
380+ if visible and content_node :
381+ if _initial_margins_skipped :
382+ call_deferred ("_update_content_margins" )
383+ elif _startup_done :
384+ call_deferred ("_update_content_margins" )
361385
362386func _set_hide_scrollbar_over_time (value : bool ) -> bool :
363387 if value == false :
0 commit comments