Parcourir la source

change status view from Label to Stackpane & adjust spinner size computation

Armin Schrenk il y a 3 ans
Parent
commit
58eb6a6dee

+ 8 - 4
src/main/java/org/cryptomator/ui/controls/FontAwesome5Spinner.java

@@ -18,10 +18,15 @@ public class FontAwesome5Spinner extends ProgressIndicator {
 
 	private static final double DEFAULT_GLYPH_SIZE = 12.0;
 
+	private final FontAwesome5IconView boundingBox;
 	private final RotateTransition animation;
-	private DoubleProperty glyphSize = new SimpleDoubleProperty(this, "glyphSize", DEFAULT_GLYPH_SIZE);
+	private final DoubleProperty glyphSize = new SimpleDoubleProperty(this, "glyphSize", DEFAULT_GLYPH_SIZE);
 
 	public FontAwesome5Spinner() {
+		this.boundingBox = new FontAwesome5IconView();
+		boundingBox.setGlyph(FontAwesome5Icon.SPINNER);
+		boundingBox.glyphSizeProperty().bind(glyphSize);
+
 		this.animation = new RotateTransition(Duration.millis(100), this);
 		animation.setInterpolator(Interpolator.DISCRETE);
 		animation.setByAngle(45);
@@ -33,12 +38,11 @@ public class FontAwesome5Spinner extends ProgressIndicator {
 		});
 
 		EasyBind.subscribe(this.visibleProperty(), this::reset);
-		EasyBind.subscribe(glyphSize, this::shrinkToGlyphSize);
+		EasyBind.subscribe(boundingBox.glyphSizeProperty(), this::shrinkToGlyphSize);
 	}
 
 	private void shrinkToGlyphSize(Number newValue) {
-		double sizeInPx = newValue.doubleValue() * 1.333;
-		setMaxSize(sizeInPx, sizeInPx);
+		setMaxSize(boundingBox.getBoundsInLocal().getWidth(), boundingBox.getBoundsInLocal().getHeight());
 	}
 
 	private void reset(boolean flag) {

+ 17 - 21
src/main/java/org/cryptomator/ui/controls/FontAwesome5StatusView.java

@@ -2,51 +2,47 @@ package org.cryptomator.ui.controls;
 
 import com.tobiasdiez.easybind.EasyBind;
 
+import javafx.beans.binding.BooleanBinding;
 import javafx.beans.property.DoubleProperty;
 import javafx.beans.property.ObjectProperty;
 import javafx.beans.property.SimpleDoubleProperty;
 import javafx.beans.property.SimpleObjectProperty;
-import javafx.scene.control.ContentDisplay;
-import javafx.scene.control.Label;
+import javafx.geometry.Pos;
+import javafx.scene.layout.StackPane;
 
 /**
  * Similar to the {@link FontAwesome5IconView}, except that if the spinner glyph is selected, an animated facsimile is used.
  */
-public class FontAwesome5StatusView extends Label {
+public class FontAwesome5StatusView extends StackPane {
 
 	private static final double DEFAULT_GLYPH_SIZE = 12.0;
 	private static final FontAwesome5Icon DEFAULT_GLYPH = FontAwesome5Icon.ANCHOR;
 
-	private FontAwesome5IconView staticIcon;
-	private FontAwesome5Spinner animatedSpinner;
+	private final FontAwesome5IconView staticIcon;
+	private final FontAwesome5Spinner animatedSpinner;
+	private final BooleanBinding isSpinnerGlyph;
+
+	private final ObjectProperty<FontAwesome5Icon> glyph = new SimpleObjectProperty<>(this, "glyph", DEFAULT_GLYPH);
+	private final DoubleProperty glyphSize = new SimpleDoubleProperty(this, "glyphSize", DEFAULT_GLYPH_SIZE);
 
-	private ObjectProperty<FontAwesome5Icon> glyph = new SimpleObjectProperty<>(this, "glyph", DEFAULT_GLYPH);
-	private DoubleProperty glyphSize = new SimpleDoubleProperty(this, "glyphSize", DEFAULT_GLYPH_SIZE);
 
 	public FontAwesome5StatusView() {
 		this.staticIcon = new FontAwesome5IconView();
 		this.animatedSpinner = new FontAwesome5Spinner();
-		setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
+		this.isSpinnerGlyph = glyphProperty().isEqualTo(FontAwesome5Icon.SPINNER);
+		setAlignment(Pos.CENTER);
+		getChildren().addAll(staticIcon, animatedSpinner);
 
 		staticIcon.glyphProperty().bind(glyph);
 		staticIcon.glyphSizeProperty().bind(glyphSize);
 		animatedSpinner.glyphSizeProperty().bind(glyphSize);
 
-		EasyBind.subscribe(glyphProperty(), this::spinnerOrIcon);
-		EasyBind.subscribe(glyphSize, this::shrinkToGlyphSize);
-	}
-
-	private void shrinkToGlyphSize(Number newValue) {
-		double sizeInPx = newValue.doubleValue() * 1.333;
-		setMaxSize(sizeInPx, sizeInPx);
+		EasyBind.subscribe(isSpinnerGlyph, this::showSpinner);
 	}
 
-	private void spinnerOrIcon(FontAwesome5Icon icon) {
-		if (icon == FontAwesome5Icon.SPINNER) {
-			this.setGraphic(animatedSpinner);
-		} else {
-			this.setGraphic(staticIcon);
-		}
+	private void showSpinner(boolean isSpinner) {
+		animatedSpinner.setVisible(isSpinner);
+		staticIcon.setVisible(!isSpinner);
 	}
 
 	public ObjectProperty<FontAwesome5Icon> glyphProperty() {