瀏覽代碼

fix weird fxml bug with spinner, remove redundant functionality from autoanimator

Sebastian Stenzel 4 年之前
父節點
當前提交
fa5248c781

+ 2 - 26
src/main/java/org/cryptomator/ui/common/AutoAnimator.java

@@ -11,25 +11,21 @@ import javafx.beans.value.ObservableValue;
  * Animation which starts and stops automatically based on an observable condition.
  * <p>
  * During creation the consumer can optionally define actions to be executed everytime before the animation starts and after it stops.
- * The automatic playback of the animation based on the condition can be stopped by calling {@link #deactivateCondition()}. To reactivate it, {@link #activateCondition()} must be called.
  */
 public class AutoAnimator<T extends Animation> {
 
-
 	private final T animation;
 	private final ObservableValue<Boolean> condition;
 	private final Runnable beforeStart;
 	private final Runnable afterStop;
-
-	private Subscription sub;
+	private final Subscription sub;
 
 	AutoAnimator(T animation, ObservableValue<Boolean> condition, Runnable beforeStart, Runnable afterStop) {
 		this.animation = animation;
 		this.condition = condition;
 		this.beforeStart = beforeStart;
 		this.afterStop = afterStop;
-
-		activateCondition();
+		this.sub = EasyBind.subscribe(condition, this::togglePlay);
 	}
 
 	public void playFromStart() {
@@ -42,26 +38,6 @@ public class AutoAnimator<T extends Animation> {
 		afterStop.run();
 	}
 
-	/**
-	 * Deactivates activation on the condition.
-	 * No-op if condition is already deactivated.
-	 */
-	public void deactivateCondition() {
-		if (sub != null) {
-			sub.unsubscribe();
-		}
-	}
-
-	/**
-	 * Activates the condition
-	 * No-op if condition is already activated.
-	 */
-	public void activateCondition() {
-		if (sub == null) {
-			this.sub = EasyBind.subscribe(condition, this::togglePlay);
-		}
-	}
-
 	private void togglePlay(boolean play) {
 		if (play) {
 			this.playFromStart();

+ 20 - 12
src/main/java/org/cryptomator/ui/controls/FontAwesome5Spinner.java

@@ -3,9 +3,9 @@ package org.cryptomator.ui.controls;
 import org.cryptomator.ui.common.Animations;
 import org.cryptomator.ui.common.AutoAnimator;
 
-import javafx.beans.NamedArg;
-import javafx.beans.value.ObservableValue;
-import java.util.Optional;
+import javafx.beans.property.BooleanProperty;
+import javafx.beans.property.ObjectProperty;
+import javafx.beans.property.SimpleBooleanProperty;
 
 /**
  * An animated progress spinner using the {@link FontAwesome5IconView} with the spinner glyph.
@@ -14,23 +14,31 @@ import java.util.Optional;
  */
 public class FontAwesome5Spinner extends FontAwesome5IconView {
 
+	protected final BooleanProperty spinning = new SimpleBooleanProperty(this, "spinning", true);
+
 	private AutoAnimator animator;
 
 	public FontAwesome5Spinner() {
-		new FontAwesome5Spinner(Optional.empty());
-	}
-
-	public FontAwesome5Spinner(@NamedArg("spinning") ObservableValue<Boolean> spinning) {
-		new FontAwesome5Spinner(Optional.of(spinning));
-	}
-
-	private FontAwesome5Spinner(Optional<ObservableValue<Boolean>> animateCondition) {
 		setGlyph(FontAwesome5Icon.SPINNER);
 		var animation = Animations.createDiscrete360Rotation(this);
 		this.animator = AutoAnimator.animate(animation) //
 				.afterStop(() -> setRotate(0)) //
-				.onCondition(animateCondition.orElse(visibleProperty())) //
+				.onCondition(spinning.and(visibleProperty())) //
 				.build();
 	}
 
+	/* Getter/Setter */
+
+	public BooleanProperty spinningProperty() {
+		return spinning;
+	}
+
+	public boolean isSpinning() {
+		return spinning.get();
+	}
+
+	public void setSpinning(boolean spinning) {
+		this.spinning.set(spinning);
+	}
+
 }