소스 검색

create custom spinner

Armin Schrenk 4 년 전
부모
커밋
4167fdc850

+ 63 - 0
src/main/java/org/cryptomator/ui/controls/FontAwesomeSpinner.java

@@ -0,0 +1,63 @@
+package org.cryptomator.ui.controls;
+
+import com.tobiasdiez.easybind.EasyBind;
+
+import javafx.animation.AnimationTimer;
+import javafx.scene.Node;
+import javafx.scene.control.ProgressIndicator;
+import java.time.Duration;
+
+/**
+ * A progress indicator in the shape of {@link FontAwesome5Icon#SPINNER}. The single spinner segements are defined in the css in the `progress-indicator` class.
+ *
+ * See also https://docs.oracle.com/javase/8/javafx/api/javafx/scene/doc-files/cssref.html#progressindicator
+ */
+public class FontAwesomeSpinner extends ProgressIndicator {
+
+	private final Animator animation;
+
+	public FontAwesomeSpinner() {
+		this.animation = new Animator(this);
+
+		EasyBind.subscribe(this.visibleProperty(), this::startStopAnimation);
+	}
+
+	private void startStopAnimation(boolean flag){
+		if(flag){
+			animation.start();
+		} else {
+			animation.stop();
+		}
+	}
+
+	private static class Animator extends AnimationTimer {
+
+		private static final long STATIC_TIMEFRAME = Duration.ofMillis(1000).toNanos();
+		private static int SEGMENT_COUNT = 8;
+		private static final int ROTATION_ANGLE = 360/SEGMENT_COUNT;
+
+		private final Node toRotate;
+
+		private long lastChange = 0;
+		private int rotation_count = 0;
+
+		Animator(Node toRotate){
+			this.toRotate = toRotate;
+		}
+
+		@Override
+		public void handle(long now) {
+			if(now - lastChange > STATIC_TIMEFRAME) {
+				lastChange = now;
+
+				toRotate.setRotate(ROTATION_ANGLE * rotation_count);
+
+				rotation_count++;
+				if( rotation_count == SEGMENT_COUNT) {
+					rotation_count =0;
+				}
+			}
+		}
+	}
+
+}

+ 1 - 1
src/main/resources/css/dark_theme.css

@@ -797,7 +797,7 @@
 
 .progress-indicator {
 	-fx-indeterminate-segment-count: 8;
-	-fx-spin-enabled: true;
+	-fx-spin-enabled: false;
 }
 
 .progress-indicator:indeterminate > .spinner {

+ 2 - 1
src/main/resources/fxml/vault_detail_locked.fxml

@@ -7,6 +7,7 @@
 <?import javafx.scene.layout.Region?>
 <?import javafx.scene.layout.VBox?>
 <?import javafx.scene.control.ProgressIndicator?>
+<?import org.cryptomator.ui.controls.FontAwesomeSpinner?>
 <VBox xmlns:fx="http://javafx.com/fxml"
 	  xmlns="http://javafx.com/javafx"
 	  fx:controller="org.cryptomator.ui.mainwindow.VaultDetailLockedController"
@@ -30,7 +31,7 @@
 				<FontAwesome5IconView glyph="LOCK"/>
 			</graphic>
 		</Hyperlink>
-		<ProgressIndicator />
+		<FontAwesomeSpinner />
 
 		<Region VBox.vgrow="ALWAYS"/>