Skip to content

Commit 5862545

Browse files
committed
♻️ (components, core) Review, move and rename InsetsBuilder utility
1 parent 68213ad commit 5862545

9 files changed

Lines changed: 92 additions & 98 deletions

File tree

modules/components/src/main/java/io/github/palexdev/mfxcomponents/skins/MFXButtonsGroupSkin.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package io.github.palexdev.mfxcomponents.skins;
2020

2121
import io.github.palexdev.mfxcomponents.controls.MFXButtonsGroup;
22-
import io.github.palexdev.mfxcore.builders.InsetsBuilder;
2322
import io.github.palexdev.mfxcore.controls.MFXSkinBase;
2423
import io.github.palexdev.mfxcore.observables.When;
2524
import io.github.palexdev.mfxcore.utils.fx.LayoutUtils;
@@ -34,6 +33,8 @@
3433
import javafx.scene.layout.Region;
3534
import javafx.scene.paint.Color;
3635

36+
import static io.github.palexdev.mfxcore.utils.fx.InsetsUtils.uniform;
37+
3738
/// Default skin implementation for all [MFXButtonsGroups][MFXButtonsGroup].
3839
///
3940
/// The layout is simple: every button is [MFXButtonsGroup#getButtons()] is positioned in a row spaced by
@@ -71,7 +72,7 @@ protected void buildClip() {
7172
Region clip = new Region();
7273
clip.setBackground(new Background(new BackgroundFill(
7374
Color.WHITE,
74-
InsetsBuilder.uniform(999.0).toRadius(false),
75+
uniform(999.0).toRadius(false),
7576
Insets.EMPTY))
7677
);
7778
getSkinnable().setClip(clip);

modules/components/src/main/java/io/github/palexdev/mfxcomponents/skins/MFXCheckboxSkin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import io.github.palexdev.mfxcomponents.controls.MFXSurface;
2323
import io.github.palexdev.mfxcomponents.skins.base.MFXLabeledSkin;
2424
import io.github.palexdev.mfxcore.behavior.MFXBehavior;
25-
import io.github.palexdev.mfxcore.builders.InsetsBuilder;
2625
import io.github.palexdev.mfxcore.controls.BoundLabel;
2726
import io.github.palexdev.mfxcore.controls.MFXLabeled;
2827
import io.github.palexdev.mfxcore.utils.fx.LayoutUtils;
@@ -46,6 +45,7 @@
4645

4746
import static io.github.palexdev.mfxcore.input.WhenEvent.intercept;
4847
import static io.github.palexdev.mfxcore.observables.When.observe;
48+
import static io.github.palexdev.mfxcore.utils.fx.InsetsUtils.uniform;
4949

5050
/// Default skin implementation for all [MFXCheckboxes][MFXCheckbox]. Extends [MFXLabeledSkin].
5151
///
@@ -88,7 +88,7 @@ public MFXCheckboxSkin(MFXCheckbox checkbox) {
8888
rg.setClipSupplier(() -> {
8989
Region clip = new Region();
9090
clip.setBackground(new Background(new BackgroundFill(
91-
Color.WHITE, InsetsBuilder.uniform(999.0).toRadius(false), Insets.EMPTY))
91+
Color.WHITE, uniform(999.0).toRadius(false), Insets.EMPTY))
9292
);
9393
return clip;
9494
});

modules/core/src/main/java/io/github/palexdev/mfxcore/utils/fx/CSSFragment.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import java.util.Base64;
2222
import java.util.Objects;
2323

24-
import io.github.palexdev.mfxcore.builders.InsetsBuilder;
2524
import io.github.palexdev.mfxcore.controls.Text;
25+
import io.github.palexdev.mfxcore.utils.fx.InsetsUtils.InsetsBuilder;
2626
import javafx.application.Application;
2727
import javafx.collections.ObservableList;
2828
import javafx.css.Styleable;
@@ -391,7 +391,7 @@ public Builder backgroundInsets(String val) {
391391
}
392392

393393
public Builder backgroundInsets(Insets val) {
394-
style("-fx-background-insets: " + InsetsBuilder.stringify(val));
394+
style("-fx-background-insets: " + InsetsUtils.stringify(val));
395395
return this;
396396
}
397397

@@ -406,7 +406,7 @@ public Builder backgroundRadius(String val) {
406406

407407
// Insets cover the vast majority of use cases
408408
public Builder backgroundRadius(Insets val) {
409-
style("-fx-background-radius: " + InsetsBuilder.stringify(val));
409+
style("-fx-background-radius: " + InsetsUtils.stringify(val));
410410
return this;
411411
}
412412

@@ -431,7 +431,7 @@ public Builder borderInsets(String val) {
431431
}
432432

433433
public Builder borderInsets(Insets val) {
434-
style("-fx-border-insets: " + InsetsBuilder.stringify(val));
434+
style("-fx-border-insets: " + InsetsUtils.stringify(val));
435435
return this;
436436
}
437437

@@ -446,7 +446,7 @@ public Builder borderRadius(String val) {
446446

447447
// Insets cover the vast majority of use cases
448448
public Builder borderRadius(Insets val) {
449-
style("-fx-border-radius: " + InsetsBuilder.stringify(val));
449+
style("-fx-border-radius: " + InsetsUtils.stringify(val));
450450
return this;
451451
}
452452

@@ -466,7 +466,7 @@ public Builder padding(String val) {
466466
}
467467

468468
public Builder padding(Insets val) {
469-
style("-fx-padding: " + InsetsBuilder.stringify(val));
469+
style("-fx-padding: " + InsetsUtils.stringify(val));
470470
return this;
471471
}
472472

modules/core/src/main/java/io/github/palexdev/mfxcore/builders/InsetsBuilder.java renamed to modules/core/src/main/java/io/github/palexdev/mfxcore/utils/fx/InsetsUtils.java

Lines changed: 58 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2025 Parisi Alessandro - alessandro.parisi406@gmail.com
2+
* Copyright (C) 2026 Parisi Alessandro - alessandro.parisi406@gmail.com
33
* This file is part of MaterialFX (https://github.com/palexdev/MaterialFX)
44
*
55
* MaterialFX is free software: you can redistribute it and/or
@@ -16,38 +16,26 @@
1616
* along with MaterialFX. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
package io.github.palexdev.mfxcore.builders;
19+
package io.github.palexdev.mfxcore.utils.fx;
2020

2121
import javafx.geometry.Insets;
2222
import javafx.scene.layout.CornerRadii;
2323

24-
/// Convenience class to build [Insets] objects.
25-
public class InsetsBuilder {
26-
//================================================================================
27-
// Properties
28-
//================================================================================
29-
private Insets insets;
24+
/// Utility class for [Insets], also hosts the [InsetsBuilder] inner class.
25+
public class InsetsUtils {
3026

3127
//================================================================================
3228
// Constructors
3329
//================================================================================
34-
public InsetsBuilder() {
35-
this(Insets.EMPTY);
36-
}
3730

38-
public InsetsBuilder(Insets insets) {
39-
this.insets = insets;
40-
}
31+
private InsetsUtils() {}
4132

4233
//================================================================================
4334
// Static Methods
4435
//================================================================================
45-
public static InsetsBuilder build() {
46-
return new InsetsBuilder();
47-
}
4836

49-
public static Insets of(double top, double right, double bottom, double left) {
50-
return new Insets(top, right, bottom, left);
37+
public static InsetsUtils insets() {
38+
return new InsetsUtils();
5139
}
5240

5341
public static InsetsBuilder uniform(double all) {
@@ -77,67 +65,73 @@ public static boolean isUniform(Insets insets) {
7765
}
7866

7967
public static String stringify(Insets insets) {
80-
if (isUniform(insets)) {
81-
return String.valueOf(insets.getTop());
82-
}
68+
if (isUniform(insets)) return String.valueOf(insets.getTop());
8369
return insets.getTop() + " " +
8470
insets.getRight() + " " +
8571
insets.getBottom() + " " +
8672
insets.getLeft();
8773
}
8874

8975
//================================================================================
90-
// Methods
76+
// Inner Classes
9177
//================================================================================
92-
public InsetsBuilder withTop(double top) {
93-
insets = new Insets(top, insets.getRight(), insets.getBottom(), insets.getLeft());
94-
return this;
95-
}
9678

97-
public InsetsBuilder withRight(double right) {
98-
insets = new Insets(insets.getTop(), right, insets.getBottom(), insets.getLeft());
99-
return this;
100-
}
79+
public static class InsetsBuilder {
10180

102-
public InsetsBuilder withVertical(double topBottom) {
103-
insets = new Insets(topBottom, insets.getRight(), topBottom, insets.getLeft());
104-
return this;
105-
}
81+
private Insets insets;
10682

107-
public InsetsBuilder withBottom(double bottom) {
108-
return new InsetsBuilder(new Insets(insets.getTop(), insets.getRight(), bottom, insets.getLeft()));
109-
}
83+
public InsetsBuilder() {
84+
this(Insets.EMPTY);
85+
}
11086

111-
public InsetsBuilder withLeft(double left) {
112-
insets = new Insets(insets.getTop(), insets.getRight(), insets.getBottom(), left);
113-
return this;
114-
}
87+
public InsetsBuilder(Insets insets) {
88+
this.insets = insets;
89+
}
11590

116-
public InsetsBuilder withHorizontal(double leftRight) {
117-
insets = new Insets(insets.getTop(), leftRight, insets.getBottom(), leftRight);
118-
return this;
119-
}
91+
public InsetsBuilder top(double top) {
92+
insets = new Insets(top, insets.getRight(), insets.getBottom(), insets.getLeft());
93+
return this;
94+
}
12095

121-
public Insets get() {
122-
return insets;
123-
}
96+
public InsetsBuilder right(double right) {
97+
insets = new Insets(insets.getTop(), right, insets.getBottom(), insets.getLeft());
98+
return this;
99+
}
124100

125-
public CornerRadii toRadius(boolean asPercent) {
126-
return new CornerRadii(insets.getTop(), insets.getRight(), insets.getBottom(), insets.getLeft(), asPercent);
127-
}
101+
public InsetsBuilder bottom(double bottom) {
102+
insets = new Insets(insets.getTop(), insets.getRight(), bottom, insets.getLeft());
103+
return this;
104+
}
128105

129-
public boolean isUniform() {
130-
return isUniform(insets);
131-
}
106+
public InsetsBuilder left(double left) {
107+
insets = new Insets(insets.getTop(), insets.getRight(), insets.getBottom(), left);
108+
return this;
109+
}
132110

133-
public String stringify() {
134-
if (isUniform(insets)) {
135-
return String.valueOf(insets.getTop());
111+
public InsetsBuilder vertical(double topBottom) {
112+
insets = new Insets(topBottom, insets.getRight(), topBottom, insets.getLeft());
113+
return this;
114+
}
115+
116+
public InsetsBuilder horizontal(double leftRight) {
117+
insets = new Insets(insets.getTop(), leftRight, insets.getBottom(), leftRight);
118+
return this;
119+
}
120+
121+
public Insets get() {
122+
return insets;
123+
}
124+
125+
public CornerRadii toRadius(boolean asPercent) {
126+
return new CornerRadii(insets.getTop(), insets.getRight(), insets.getBottom(), insets.getLeft(), asPercent);
136127
}
137-
return insets.getTop() + " " +
138-
insets.getRight() + " " +
139-
insets.getBottom() + " " +
140-
insets.getLeft();
141-
}
142-
}
143128

129+
public boolean isUniform() {
130+
return InsetsUtils.isUniform(insets);
131+
}
132+
133+
public String stringify() {
134+
return InsetsUtils.stringify(insets);
135+
}
136+
}
137+
}

modules/core/src/main/java/io/github/palexdev/mfxcore/utils/fx/LayoutUtils.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package io.github.palexdev.mfxcore.utils.fx;
2020

2121
import io.github.palexdev.mfxcore.base.beans.Position;
22-
import io.github.palexdev.mfxcore.builders.InsetsBuilder;
2322
import javafx.geometry.*;
2423
import javafx.scene.Node;
2524
import javafx.scene.Parent;
@@ -40,11 +39,11 @@ public static Position computePosition(Region parent, Node child, double areaX,
4039

4140
Insets snappedMargin = margin == null ? Insets.EMPTY : margin;
4241
if (snapToPixel && snappedMargin != Insets.EMPTY) {
43-
snappedMargin = InsetsBuilder.build()
44-
.withTop(parent.snapSpaceY(snappedMargin.getTop()))
45-
.withRight(parent.snapSpaceX(snappedMargin.getRight()))
46-
.withBottom(parent.snapSpaceY(snappedMargin.getBottom()))
47-
.withLeft(parent.snapSpaceX(snappedMargin.getLeft()))
42+
snappedMargin = InsetsUtils.insets()
43+
.top(parent.snapSpaceY(snappedMargin.getTop()))
44+
.right(parent.snapSpaceX(snappedMargin.getRight()))
45+
.bottom(parent.snapSpaceY(snappedMargin.getBottom()))
46+
.left(parent.snapSpaceX(snappedMargin.getLeft()))
4847
.get();
4948
}
5049

@@ -56,11 +55,11 @@ public static Position computePosition(Region parent, Node child, double areaX,
5655
public static double computeXPosition(Region parent, Node child, double areaX, double areaWidth, Insets margin, boolean snapMargin, HPos hAlignment, boolean snapToPixel, boolean computeSizes) {
5756
Insets snappedMargin = margin == null ? Insets.EMPTY : margin;
5857
if (snapMargin && snappedMargin != Insets.EMPTY) {
59-
snappedMargin = InsetsBuilder.build()
60-
.withTop(parent.snapSpaceY(snappedMargin.getTop()))
61-
.withRight(parent.snapSpaceX(snappedMargin.getRight()))
62-
.withBottom(parent.snapSpaceY(snappedMargin.getBottom()))
63-
.withLeft(parent.snapSpaceX(snappedMargin.getLeft()))
58+
snappedMargin = InsetsUtils.insets()
59+
.top(parent.snapSpaceY(snappedMargin.getTop()))
60+
.right(parent.snapSpaceX(snappedMargin.getRight()))
61+
.bottom(parent.snapSpaceY(snappedMargin.getBottom()))
62+
.left(parent.snapSpaceX(snappedMargin.getLeft()))
6463
.get();
6564
}
6665

@@ -74,11 +73,11 @@ public static double computeXPosition(Region parent, Node child, double areaX, d
7473
public static double computeYPosition(Region parent, Node child, double areaY, double areaHeight, double areaBaselineOffset, Insets margin, boolean snapMargin, VPos vAlignment, boolean snapToPixel, boolean computeSizes) {
7574
Insets snappedMargin = margin == null ? Insets.EMPTY : margin;
7675
if (snapMargin) {
77-
snappedMargin = InsetsBuilder.build()
78-
.withTop(parent.snapSpaceY(snappedMargin.getTop()))
79-
.withRight(parent.snapSpaceX(snappedMargin.getRight()))
80-
.withBottom(parent.snapSpaceY(snappedMargin.getBottom()))
81-
.withLeft(parent.snapSpaceX(snappedMargin.getLeft()))
76+
snappedMargin = InsetsUtils.insets()
77+
.top(parent.snapSpaceY(snappedMargin.getTop()))
78+
.right(parent.snapSpaceX(snappedMargin.getRight()))
79+
.bottom(parent.snapSpaceY(snappedMargin.getBottom()))
80+
.left(parent.snapSpaceX(snappedMargin.getLeft()))
8281
.get();
8382
}
8483

modules/core/src/main/java/module-info.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
exports io.github.palexdev.mfxcore.behavior;
2626

2727
// Builders
28-
exports io.github.palexdev.mfxcore.builders;
2928
exports io.github.palexdev.mfxcore.builders.base;
3029
exports io.github.palexdev.mfxcore.builders.bindings;
3130
exports io.github.palexdev.mfxcore.builders.nodes;

modules/core/src/test/java/app/Playground.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.stream.Collectors;
2424
import java.util.stream.IntStream;
2525

26-
import io.github.palexdev.mfxcore.builders.InsetsBuilder;
2726
import io.github.palexdev.mfxcore.input.WhenEvent;
2827
import io.github.palexdev.mfxcore.popups.notifications.MFXNotificationPane;
2928
import io.github.palexdev.mfxcore.popups.notifications.Notification;
@@ -37,6 +36,8 @@
3736
import javafx.scene.layout.StackPane;
3837
import javafx.stage.Stage;
3938

39+
import static io.github.palexdev.mfxcore.utils.fx.InsetsUtils.uniform;
40+
4041
public class Playground extends Application {
4142

4243
@Override
@@ -47,7 +48,7 @@ public void start(Stage stage) {
4748

4849
MFXNotificationPane np = new MFXNotificationPane();
4950
np.setAlignment(Pos.TOP_RIGHT);
50-
np.setPadding(InsetsBuilder.uniform(10.0).get());
51+
np.setPadding(uniform(10.0).get());
5152
StackPane.setAlignment(np, Pos.TOP_RIGHT);
5253
np.setMaxVisible(3);
5354

modules/core/src/test/java/app/PopupPlacementTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.function.Function;
2424

2525
import io.github.palexdev.mfxcore.base.beans.Position;
26-
import io.github.palexdev.mfxcore.builders.InsetsBuilder;
2726
import io.github.palexdev.mfxcore.popups.MFXPopup;
2827
import io.github.palexdev.mfxcore.popups.MFXPopups;
2928
import io.github.palexdev.mfxcore.utils.fx.AnchorHandlers;
@@ -43,6 +42,7 @@
4342
import javafx.stage.Stage;
4443

4544
import static io.github.palexdev.mfxcore.observables.When.onInvalidated;
45+
import static io.github.palexdev.mfxcore.utils.fx.InsetsUtils.top;
4646

4747
public class PopupPlacementTest extends Application {
4848
static final Label placementInfoLabel = new Label("");
@@ -64,7 +64,7 @@ public void start(Stage stage) {
6464
);
6565

6666
StackPane.setAlignment(placementInfoLabel, Pos.TOP_CENTER);
67-
StackPane.setMargin(placementInfoLabel, InsetsBuilder.top(8.0).get());
67+
StackPane.setMargin(placementInfoLabel, top(8.0).get());
6868

6969
StackPane root = new StackPane(region, placementInfoLabel);
7070
Scene scene = new Scene(root, 400, 400);

0 commit comments

Comments
 (0)