Skip to content

Commit

Permalink
[Components] ♻️ Move min sizes defined by components' specs to their …
Browse files Browse the repository at this point in the history
…skin as protected static members (not final! makes them easily changeable)

Signed-off-by: palexdev <[email protected]>
  • Loading branch information
palexdev committed Jun 7, 2024
1 parent ba70f30 commit a29f326
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public class MFXButtonSkin<T extends MFXButtonBase<B>, B extends MFXButtonBehavi
//================================================================================
protected final MaterialSurface surface;

// Specs
protected static double MIN_HEIGHT = 40.0;

//================================================================================
// Constructors
//================================================================================
Expand Down Expand Up @@ -131,6 +134,11 @@ protected void initBehavior(B behavior) {
);
}

@Override
public double computeMinHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) {
return Math.max(MIN_HEIGHT, super.computeMinHeight(width, topInset, rightInset, bottomInset, leftInset));
}

@Override
public double computePrefWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset) {
T button = getSkinnable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public class MFXFabSkin extends MFXButtonSkin<MFXFabBase, MFXButtonBehaviorBase<
//================================================================================
// Properties
//================================================================================
// TODO define min sizes in Skins
private Animation ecAnimation;
private Animation sAnimation;
private final Scale scale = new Scale(1, 1);
Expand All @@ -82,6 +81,15 @@ public class MFXFabSkin extends MFXButtonSkin<MFXFabBase, MFXButtonBehaviorBase<
protected Duration OPACITY_DURATION_EXTENDED = M3Motion.EXTRA_LONG2;
protected Interpolator RESIZE_CURVE = M3Motion.EMPHASIZED;

// Specs
protected static double MIN_HEIGHT = 56.0;
protected static double MIN_WIDTH = 56.0;
protected static double MIN_WIDTH_EXTENDED = 80.0;

// Specs Small
protected static double MIN_SIZE_SMALL = 40.0;
protected static double MIN_SIZE_LARGE = 96.0;

//================================================================================
// Constructors
//================================================================================
Expand Down Expand Up @@ -219,6 +227,29 @@ protected double computeLabelDisplacement(double w) {
return snapPositionX(((w - iW) / 2.0) - label.getLayoutX());
}

/**
* Returns the appropriate minimum width as specified by the Material Design 3 guidelines for "collapsed" FABs
* according to the variant:
* <p> - {@link FABVariants#SMALL} -> {@link #MIN_SIZE_SMALL}
* <p> - {@link FABVariants#LARGE} -> {@link #MIN_SIZE_LARGE}
* <p> - Standard -> {@link #MIN_WIDTH}
* <p>
* Little note: since this skin is for any FAB implementation starting from {@link MFXFabBase}, and because variants
* are only managed by the default implementation {@link MFXFab}, this will also check if the {@link #getSkinnable()}
* instance is the default implementation to use {@link WithVariants#isVariantApplied(Variant)}.
* Otherwise always returns {@link #MIN_WIDTH}.
*/
protected double getSpecsMinWidth() {
MFXFabBase base = getSkinnable();
if (base instanceof MFXFab) {
MFXFab fab = (MFXFab) base;
return fab.isVariantApplied(FABVariants.SMALL) ? MIN_SIZE_SMALL :
fab.isVariantApplied(FABVariants.LARGE) ? MIN_SIZE_LARGE :
MIN_WIDTH;
}
return MIN_WIDTH;
}

//================================================================================
// Overridden Methods
//================================================================================
Expand Down Expand Up @@ -277,15 +308,16 @@ public double computeMinWidth(double height, double topInset, double rightInset,
MFXFontIcon icon = fab.getIcon();
double iW = (icon != null) ? LayoutUtils.getWidth(icon) + leftInset + rightInset : 0.0;
double iH = (icon != null) ? LayoutUtils.getHeight(icon) + topInset + bottomInset : 0.0;
return Math.max(Math.max(iW, iH), 56.0);
double minW = getSpecsMinWidth();
return Math.max(Math.max(iW, iH), minW);
}
return 80.0;
return MIN_WIDTH_EXTENDED;
}

@Override
public double computeMinHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) {
MFXFabBase fab = getSkinnable();
return !fab.isExtended() ? computeMinWidth(-1) : 56.0;
return !fab.isExtended() ? computeMinWidth(-1) : MIN_HEIGHT;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public class MFXSegmentedButtonSkin extends MFXSkinBase<MFXSegmentedButton, MFXS
protected WeakReference<MFXSegment> first;
protected WeakReference<MFXSegment> last;

// Specs
protected static double MIN_SEGMENT_WIDTH = 48.0;
protected static double MIN_HEIGHT = 40.0;

//================================================================================
// Constructors
//================================================================================
Expand Down Expand Up @@ -119,6 +123,14 @@ protected void updateFirstLast() {
@Override
protected void initBehavior(MFXSegmentedButtonBehavior behavior) {}

@Override
public double computeMinHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) {
MFXSegmentedButton button = getSkinnable();
int density = button.getDensity();
double minH = MIN_HEIGHT - (4.0 * density);
return Math.max(minH, super.computeMinHeight(width, topInset, rightInset, bottomInset, leftInset));
}

@Override
public double computePrefWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset) {
ObservableList<MFXSegment> segments = getSkinnable().getSegments();
Expand Down Expand Up @@ -240,7 +252,7 @@ public void setIcon(MFXFontIcon icon) {
/**
* Default skin used by {@link MFXSegment} and simple extension of {@link MFXButtonSkin}.
* <p>
* What changes is the layout strategy. According to MD3 guidelines a segment's label (text + icon) is always centered.
* What changes is the layout strategy. According to MD3 guidelines, a segment's label (text + icon) is always centered.
*/
public static class MFXSegmentSkin extends MFXButtonSkin<MFXSegment, MFXSelectableBehaviorBase<MFXSegment>> {

Expand All @@ -251,6 +263,11 @@ public MFXSegmentSkin(MFXSegment button) {
@Override
protected void addListeners() {}

@Override
public double computeMinWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset) {
return Math.max(MIN_SEGMENT_WIDTH, super.computeMinWidth(height, topInset, rightInset, bottomInset, leftInset));
}

@Override
public double computePrefWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset) {
MFXSegment segment = getSkinnable();
Expand Down

0 comments on commit a29f326

Please sign in to comment.