2828import javafx .animation .Animation ;
2929import javafx .animation .Interpolator ;
3030import javafx .application .Platform ;
31- import javafx .beans .InvalidationListener ;
3231import javafx .beans .Observable ;
3332import javafx .beans .property .SimpleObjectProperty ;
3433import javafx .beans .value .ObservableValue ;
4342import javafx .util .Duration ;
4443
4544import static io .github .palexdev .mfxcore .input .WhenEvent .intercept ;
46- import static io .github .palexdev .mfxcore .observables .OnInvalidated .withListener ;
4745import static io .github .palexdev .mfxcore .observables .When .*;
4846
4947/**
@@ -138,7 +136,6 @@ protected void layoutChildren() {
138136 */
139137 private void addListeners () {
140138 VFXScrollPane pane = getSkinnable ();
141- InvalidationListener ll = l -> pane .requestLayout ();
142139
143140 // Bindings
144141 bvp = new BarsVisibilityProperty ();
@@ -184,11 +181,12 @@ private void addListeners() {
184181 .executeNow (() -> pane .getContent () != null ),
185182
186183 // Layout
187- withListener (pane .fitToWidthProperty (), ll ),
188- withListener (pane .fitToHeightProperty (), ll ),
189- withListener (pane .vBarPosProperty (), ll ),
190- withListener (pane .hBarPosProperty (), ll ),
191- withListener (pane .scrollBarsGapProperty (), ll ),
184+ observe (
185+ pane ::requestLayout ,
186+ pane .fitToWidthProperty (), pane .fitToHeightProperty (), pane .contentPaddingProperty (),
187+ pane .vBarPosProperty (), pane .hBarPosProperty (),
188+ pane .scrollBarsGapProperty ()
189+ ),
192190 // Animations
193191 onInvalidated (pane .minBarsOpacityProperty ())
194192 .then (v -> buildBarsAnimations ())
@@ -228,13 +226,16 @@ protected void layoutContent() {
228226
229227 double w = viewport .getWidth ();
230228 double h = viewport .getHeight ();
229+ Insets padding = pane .getContentPadding ();
231230 if (content instanceof VFXContainer <?>) {
232231 // Virtualized containers always take up all the space and thus ignore the alignment too
233- content . resizeRelocate ( 0 , 0 , w , h );
232+ layoutInArea ( content , 0 , 0 , w , h , 0 , padding , HPos . LEFT , VPos . TOP );
234233 } else {
235234 Pos alignment = pane .getAlignment ();
236235 VPos vAlign = alignment .getVpos ();
237236 HPos hAlign = alignment .getHpos ();
237+
238+ // Find content bounds and set it to be at least the same as the viewport size if fitTo configs are enabled
238239 double cw = LayoutUtils .snappedBoundWidth (content );
239240 double ch = LayoutUtils .snappedBoundHeight (content );
240241 cw = pane .isFitToWidth ()
@@ -244,12 +245,12 @@ protected void layoutContent() {
244245 ? Math .max (h , ch )
245246 : ch ;
246247
247- // If the content is larger than the viewport then the alignment is ignored
248- if (ch > h ) vAlign = VPos .TOP ;
248+ // If the content is larger than the viewport, then the alignment is ignored
249249 if (cw > w ) hAlign = HPos .LEFT ;
250+ if (ch > h ) vAlign = VPos .TOP ;
250251
251252 content .resize (cw , ch );
252- positionInArea (content , 0 , 0 , w , h , 0 , hAlign , vAlign );
253+ positionInArea (content , 0 , 0 , w , h , 0 , padding , hAlign , vAlign );
253254 }
254255
255256 updateVisualAmount (content );
@@ -276,24 +277,27 @@ protected void updateScrollBindings(Node oldContent, Node newContent) {
276277 if (newContent == null ) return ;
277278
278279 if (!(newContent instanceof VFXContainer <?> c )) {
280+ VFXScrollPane pane = getSkinnable ();
279281 newContent .translateXProperty ().bind (DoubleBindingBuilder .build ()
280282 .setMapper (() -> {
281- double cw = newContent . getLayoutBounds ().getWidth ();
283+ double cw = pane . getContentBounds ().width ();
282284 double vw = viewport .getWidth ();
283285 double maxScroll = Math .max (0 , cw - vw );
284286 return -maxScroll * hBar .getValue ();
285287 })
288+ .addSources (pane .contentPaddingProperty ())
286289 .addSources (newContent .layoutBoundsProperty (), viewport .widthProperty ())
287290 .addSources (hBar .valueProperty ())
288291 .get ()
289292 );
290293 newContent .translateYProperty ().bind (DoubleBindingBuilder .build ()
291294 .setMapper (() -> {
292- double ch = newContent . getLayoutBounds ().getHeight ();
295+ double ch = pane . getContentBounds ().height ();
293296 double vh = viewport .getHeight ();
294297 double maxScroll = Math .max (0 , ch - vh );
295298 return -maxScroll * vBar .getValue ();
296299 })
300+ .addSources (pane .contentPaddingProperty ())
297301 .addSources (newContent .layoutBoundsProperty (), viewport .heightProperty ())
298302 .addSources (vBar .valueProperty ())
299303 .get ()
0 commit comments