Skip to content

Commit

Permalink
添加动画开关 (#2111)
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo authored Feb 14, 2023
1 parent 389d430 commit ab26495
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 46 deletions.
15 changes: 15 additions & 0 deletions HMCL/src/main/java/org/jackhuang/hmcl/setting/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ public static Config fromJson(String json) throws JsonParseException {
@SerializedName("preferredLoginType")
private StringProperty preferredLoginType = new SimpleStringProperty();

@SerializedName("animationDisabled")
private BooleanProperty animationDisabled = new SimpleBooleanProperty();

private transient ObservableHelper helper = new ObservableHelper(this);

public Config() {
Expand Down Expand Up @@ -601,6 +604,18 @@ public StringProperty preferredLoginTypeProperty() {
return preferredLoginType;
}

public boolean isAnimationDisabled() {
return animationDisabled.get();
}

public BooleanProperty animationDisabledProperty() {
return animationDisabled;
}

public void setAnimationDisabled(boolean animationDisabled) {
this.animationDisabled.set(animationDisabled);
}

public boolean isTitleTransparent() {
return titleTransparent.get();
}
Expand Down
2 changes: 2 additions & 0 deletions HMCL/src/main/java/org/jackhuang/hmcl/setting/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javafx.beans.binding.Bindings;
import org.jackhuang.hmcl.Metadata;
import org.jackhuang.hmcl.game.HMCLCacheRepository;
import org.jackhuang.hmcl.ui.animation.AnimationUtils;
import org.jackhuang.hmcl.util.CacheRepository;
import org.jackhuang.hmcl.util.io.FileUtils;

Expand Down Expand Up @@ -54,6 +55,7 @@ private Settings() {
Accounts.init();
Profiles.init();
AuthlibInjectorServers.init();
AnimationUtils.init();

CacheRepository.setInstance(HMCLCacheRepository.REPOSITORY);
HMCLCacheRepository.REPOSITORY.directoryProperty().bind(Bindings.createStringBinding(() -> {
Expand Down
4 changes: 3 additions & 1 deletion HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import javafx.util.Duration;
import javafx.util.StringConverter;
import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.ui.animation.AnimationUtils;
import org.jackhuang.hmcl.ui.construct.JFXHyperlink;
import org.jackhuang.hmcl.util.Holder;
import org.jackhuang.hmcl.util.Logging;
Expand Down Expand Up @@ -275,7 +276,8 @@ public static Node limitingSize(Node node, double width, double height) {
}

public static void smoothScrolling(ScrollPane scrollPane) {
ScrollUtils.addSmoothScrolling(scrollPane);
if (AnimationUtils.isAnimationEnabled())
ScrollUtils.addSmoothScrolling(scrollPane);
}

public static void installFastTooltip(Node node, Tooltip tooltip) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.jackhuang.hmcl.ui.animation;

import org.jackhuang.hmcl.setting.ConfigHolder;

public final class AnimationUtils {

private AnimationUtils() {
}

/**
* Trigger initialization of this class.
* Should be called from {@link org.jackhuang.hmcl.setting.Settings#init()}.
*/
@SuppressWarnings("JavadocReference")
public static void init() {
}

private static final boolean enabled = !ConfigHolder.config().isAnimationDisabled();

public static boolean isAnimationEnabled() {
return enabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,23 @@ public void setContent(Node newView, AnimationProducer transition, Duration dura
return;
}

transition.init(this);

// runLater or "init" will not work
Platform.runLater(() -> {
Timeline newAnimation = new Timeline();
newAnimation.getKeyFrames().addAll(transition.animate(this));
newAnimation.getKeyFrames().add(new KeyFrame(duration, e -> {
setMouseTransparent(false);
getChildren().remove(previousNode);
}));
FXUtils.playAnimation(this, "transition_pane", newAnimation);
});
if (AnimationUtils.isAnimationEnabled()) {
transition.init(this);

// runLater or "init" will not work
Platform.runLater(() -> {
Timeline newAnimation = new Timeline();
newAnimation.getKeyFrames().addAll(transition.animate(this));
newAnimation.getKeyFrames().add(new KeyFrame(duration, e -> {
setMouseTransparent(false);
getChildren().remove(previousNode);
}));
FXUtils.playAnimation(this, "transition_pane", newAnimation);
});
} else {
setMouseTransparent(false);
getChildren().remove(previousNode);
}
}

private void updateContent(Node newView) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import javafx.application.Platform;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
Expand All @@ -35,6 +38,7 @@
import org.jackhuang.hmcl.setting.Theme;
import org.jackhuang.hmcl.ui.FXUtils;
import org.jackhuang.hmcl.ui.SVG;
import org.jackhuang.hmcl.ui.animation.AnimationUtils;

/**
* @author huangyuhui
Expand Down Expand Up @@ -70,6 +74,7 @@ protected void layoutChildren() {
}
}

@SuppressWarnings("unchecked")
private void updateLayout() {
if (content instanceof ComponentList) {
ComponentList list = (ComponentList) content;
Expand Down Expand Up @@ -134,42 +139,66 @@ private void updateLayout() {
container.getChildren().setAll(content);
groupNode.getChildren().add(container);

Runnable onExpand = () -> {
if (expandAnimation != null && expandAnimation.getStatus() == Animation.Status.RUNNING) {
expandAnimation.stop();
}
EventHandler<Event> onExpand;
if (AnimationUtils.isAnimationEnabled()) {
onExpand = e -> {
if (expandAnimation != null && expandAnimation.getStatus() == Animation.Status.RUNNING) {
expandAnimation.stop();
}

setExpanded(!isExpanded());
setExpanded(!isExpanded());

if (isExpanded()) {
list.onExpand();
list.layout();
}
if (isExpanded()) {
list.onExpand();
list.layout();
}

Platform.runLater(() -> {
double newAnimatedHeight = (content.prefHeight(-1) + 8 + 10) * (isExpanded() ? 1 : -1);
double newHeight = isExpanded() ? getHeight() + newAnimatedHeight : prefHeight(-1);
double contentHeight = isExpanded() ? newAnimatedHeight : 0;

if (isExpanded()) {
updateClip(newHeight);
}

expandAnimation = new Timeline(new KeyFrame(new Duration(320.0),
new KeyValue(container.minHeightProperty(), contentHeight, FXUtils.SINE),
new KeyValue(container.maxHeightProperty(), contentHeight, FXUtils.SINE)
));

if (!isExpanded()) {
expandAnimation.setOnFinished(e2 -> updateClip(newHeight));
}

expandAnimation.play();
});
};
} else {
onExpand = e -> {
setExpanded(!isExpanded());

Platform.runLater(() -> {
double newAnimatedHeight = (content.prefHeight(-1) + 8 + 10) * (isExpanded() ? 1 : -1);
double newHeight = isExpanded() ? getHeight() + newAnimatedHeight : prefHeight(-1);
double contentHeight = isExpanded() ? newAnimatedHeight : 0;

if (isExpanded()) {
list.onExpand();
list.layout();
updateClip(newHeight);
}

expandAnimation = new Timeline(new KeyFrame(new Duration(320.0),
new KeyValue(container.minHeightProperty(), contentHeight, FXUtils.SINE),
new KeyValue(container.maxHeightProperty(), contentHeight, FXUtils.SINE)
));
container.setMinHeight(contentHeight);
container.setMaxHeight(contentHeight);

if (!isExpanded()) {
expandAnimation.setOnFinished(e2 -> updateClip(newHeight));
updateClip(newHeight);
}
};
}

expandAnimation.play();
});
};

headerRippler.setOnMouseClicked(e -> onExpand.run());
expandButton.setOnMouseClicked(e -> onExpand.run());
headerRippler.setOnMouseClicked(onExpand);
expandButton.setOnAction((EventHandler<ActionEvent>) (Object) onExpand);

expandedProperty().addListener((a, b, newValue) ->
expandIcon.setRotate(newValue ? 180 : 0));
Expand Down
29 changes: 17 additions & 12 deletions HMCL/src/main/java/org/jackhuang/hmcl/ui/main/MainPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.jackhuang.hmcl.ui.Controllers;
import org.jackhuang.hmcl.ui.FXUtils;
import org.jackhuang.hmcl.ui.SVG;
import org.jackhuang.hmcl.ui.animation.AnimationUtils;
import org.jackhuang.hmcl.ui.construct.AnnouncementCard;
import org.jackhuang.hmcl.ui.construct.MessageDialogPane;
import org.jackhuang.hmcl.ui.construct.PopupMenu;
Expand Down Expand Up @@ -254,18 +255,22 @@ private void showUpdate(boolean show) {
}

private void doAnimation(boolean show) {
Duration duration = Duration.millis(320);
Timeline nowAnimation = new Timeline();
nowAnimation.getKeyFrames().addAll(
new KeyFrame(Duration.ZERO,
new KeyValue(updatePane.translateXProperty(), show ? 260 : 0, SINE)),
new KeyFrame(duration,
new KeyValue(updatePane.translateXProperty(), show ? 0 : 260, SINE)));
if (show) nowAnimation.getKeyFrames().add(
new KeyFrame(Duration.ZERO, e -> updatePane.setVisible(true)));
else nowAnimation.getKeyFrames().add(
new KeyFrame(duration, e -> updatePane.setVisible(false)));
nowAnimation.play();
if (AnimationUtils.isAnimationEnabled()) {
Duration duration = Duration.millis(320);
Timeline nowAnimation = new Timeline();
nowAnimation.getKeyFrames().addAll(
new KeyFrame(Duration.ZERO,
new KeyValue(updatePane.translateXProperty(), show ? 260 : 0, SINE)),
new KeyFrame(duration,
new KeyValue(updatePane.translateXProperty(), show ? 0 : 260, SINE)));
if (show) nowAnimation.getKeyFrames().add(
new KeyFrame(Duration.ZERO, e -> updatePane.setVisible(true)));
else nowAnimation.getKeyFrames().add(
new KeyFrame(duration, e -> updatePane.setVisible(false)));
nowAnimation.play();
} else {
updatePane.setVisible(show);
}
}

private void launch() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public PersonalizationPage() {
titleTransparentButton.selectedProperty().bindBidirectional(config().titleTransparentProperty());
titleTransparentButton.setTitle(i18n("settings.launcher.title_transparent"));
}
{
OptionToggleButton animationButton = new OptionToggleButton();
themeList.getContent().add(animationButton);
animationButton.selectedProperty().bindBidirectional(config().animationDisabledProperty());
animationButton.setTitle(i18n("settings.launcher.turn_off_animations"));
}
content.getChildren().addAll(ComponentList.createComponentListTitle(i18n("settings.launcher.appearance")), themeList);

{
Expand Down
1 change: 1 addition & 0 deletions HMCL/src/main/resources/assets/lang/I18N.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,7 @@ settings.launcher.proxy.socks=SOCKS
settings.launcher.proxy.username=Username
settings.launcher.theme=Theme
settings.launcher.title_transparent=Transparent titlebar
settings.launcher.turn_off_animations=Turn off animation (applies after restart)
settings.launcher.version_list_source=Version List

settings.memory=Memory
Expand Down
1 change: 1 addition & 0 deletions HMCL/src/main/resources/assets/lang/I18N_zh.properties
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,7 @@ settings.launcher.proxy.socks=Socks
settings.launcher.proxy.username=帳戶
settings.launcher.theme=主題
settings.launcher.title_transparent=標題欄透明
settings.launcher.turn_off_animations=關閉動畫 (重啟後生效)
settings.launcher.version_list_source=版本列表來源

settings.memory=遊戲記憶體
Expand Down
1 change: 1 addition & 0 deletions HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@ settings.launcher.proxy.socks=Socks
settings.launcher.proxy.username=帐户
settings.launcher.theme=主题
settings.launcher.title_transparent=标题栏透明
settings.launcher.turn_off_animations=关闭动画 (重启后生效)
settings.launcher.version_list_source=版本列表源

settings.memory=游戏内存
Expand Down

0 comments on commit ab26495

Please sign in to comment.