Skip to content

Commit ae144aa

Browse files
authored
预加载页面以减少动画卡顿 (#4779)
1 parent 343c09b commit ae144aa

4 files changed

Lines changed: 55 additions & 6 deletions

File tree

HMCL/src/main/java/org/jackhuang/hmcl/ui/Controllers.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ public final class Controllers {
103103
});
104104
private static Lazy<RootPage> rootPage = new Lazy<>(RootPage::new);
105105
private static DecoratorController decorator;
106-
private static Lazy<DownloadPage> downloadPage = new Lazy<>(DownloadPage::new);
106+
private static DownloadPage downloadPage;
107107
private static Lazy<AccountListPage> accountListPage = new Lazy<>(() -> {
108108
AccountListPage accountListPage = new AccountListPage();
109109
accountListPage.selectedAccountProperty().bindBidirectional(Accounts.selectedAccountProperty());
110110
accountListPage.accountsProperty().bindContent(Accounts.getAccounts());
111111
accountListPage.authServersProperty().bindContentBidirectional(config().getAuthlibInjectorServers());
112112
return accountListPage;
113113
});
114-
private static Lazy<LauncherSettingsPage> settingsPage = new Lazy<>(LauncherSettingsPage::new);
114+
private static LauncherSettingsPage settingsPage;
115115
private static Lazy<TerracottaPage> terracottaPage = new Lazy<>(TerracottaPage::new);
116116

117117
private Controllers() {
@@ -142,7 +142,18 @@ public static RootPage getRootPage() {
142142

143143
// FXThread
144144
public static LauncherSettingsPage getSettingsPage() {
145-
return settingsPage.get();
145+
if (settingsPage == null) {
146+
settingsPage = new LauncherSettingsPage();
147+
}
148+
return settingsPage;
149+
}
150+
151+
@FXThread
152+
public static void prepareSettingsPage() {
153+
if (settingsPage == null) {
154+
LOG.info("Prepare the settings page");
155+
settingsPage = FXUtils.prepareNode(new LauncherSettingsPage());
156+
}
146157
}
147158

148159
// FXThread
@@ -152,7 +163,18 @@ public static AccountListPage getAccountListPage() {
152163

153164
// FXThread
154165
public static DownloadPage getDownloadPage() {
155-
return downloadPage.get();
166+
if (downloadPage == null) {
167+
downloadPage = new DownloadPage();
168+
}
169+
return downloadPage;
170+
}
171+
172+
@FXThread
173+
public static void prepareDownloadPage() {
174+
if (downloadPage == null) {
175+
LOG.info("Prepare the download page");
176+
downloadPage = FXUtils.prepareNode(new DownloadPage());
177+
}
156178
}
157179

158180
// FXThread
@@ -533,6 +555,7 @@ public static void shutdown() {
533555
downloadPage = null;
534556
accountListPage = null;
535557
settingsPage = null;
558+
terracottaPage = null;
536559
decorator = null;
537560
stage = null;
538561
scene = null;

HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import javafx.collections.ObservableMap;
3131
import javafx.event.Event;
3232
import javafx.event.EventDispatcher;
33+
import javafx.event.EventHandler;
3334
import javafx.event.EventType;
3435
import javafx.geometry.Bounds;
3536
import javafx.geometry.Pos;
@@ -54,6 +55,7 @@
5455
import javafx.util.Callback;
5556
import javafx.util.Duration;
5657
import javafx.util.StringConverter;
58+
import org.jackhuang.hmcl.setting.StyleSheets;
5759
import org.jackhuang.hmcl.setting.Theme;
5860
import org.jackhuang.hmcl.task.CacheFileTask;
5961
import org.jackhuang.hmcl.task.Schedulers;
@@ -1367,6 +1369,24 @@ public static void onClicked(Node node, Runnable action) {
13671369
});
13681370
}
13691371

1372+
public static <N extends Parent> N prepareNode(N node) {
1373+
Scene dummyScene = new Scene(node);
1374+
StyleSheets.init(dummyScene);
1375+
node.applyCss();
1376+
node.layout();
1377+
return node;
1378+
}
1379+
1380+
public static void prepareOnMouseEnter(Node node, Runnable action) {
1381+
node.addEventFilter(MouseEvent.MOUSE_ENTERED, new EventHandler<>() {
1382+
@Override
1383+
public void handle(MouseEvent e) {
1384+
node.removeEventFilter(MouseEvent.MOUSE_ENTERED, this);
1385+
action.run();
1386+
}
1387+
});
1388+
}
1389+
13701390
public static <T> void onScroll(Node node, List<T> list,
13711391
ToIntFunction<List<T>> finder,
13721392
Consumer<T> updater

HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TabControl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private Tab<?> findNearestAvailableTab(int tabIndex, boolean doSelect) {
174174
}
175175
}
176176

177-
class Tab<T extends Node> {
177+
final class Tab<T extends Node> {
178178
private final StringProperty id = new SimpleStringProperty(this, "id");
179179
private final StringProperty text = new SimpleStringProperty(this, "text");
180180
private final ReadOnlyBooleanWrapper selected = new ReadOnlyBooleanWrapper(this, "selected");

HMCL/src/main/java/org/jackhuang/hmcl/ui/main/RootPage.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.jackhuang.hmcl.ui.FXUtils;
3636
import org.jackhuang.hmcl.ui.SVG;
3737
import org.jackhuang.hmcl.ui.account.AccountAdvancedListItem;
38+
import org.jackhuang.hmcl.ui.animation.AnimationUtils;
3839
import org.jackhuang.hmcl.ui.construct.AdvancedListBox;
3940
import org.jackhuang.hmcl.ui.construct.AdvancedListItem;
4041
import org.jackhuang.hmcl.ui.construct.MessageDialogPane;
@@ -175,20 +176,25 @@ protected Skin(RootPage control) {
175176
downloadItem.setTitle(i18n("download"));
176177
downloadItem.setOnAction(e -> Controllers.navigate(Controllers.getDownloadPage()));
177178
FXUtils.installFastTooltip(downloadItem, i18n("download.hint"));
179+
if (AnimationUtils.isAnimationEnabled()) {
180+
FXUtils.prepareOnMouseEnter(downloadItem, Controllers::prepareDownloadPage);
181+
}
178182

179183
// fifth item in left sidebar
180184
AdvancedListItem launcherSettingsItem = new AdvancedListItem();
181185
launcherSettingsItem.setLeftGraphic(wrap(SVG.SETTINGS));
182186
launcherSettingsItem.setActionButtonVisible(false);
183187
launcherSettingsItem.setTitle(i18n("settings"));
184188
launcherSettingsItem.setOnAction(e -> Controllers.navigate(Controllers.getSettingsPage()));
189+
if (AnimationUtils.isAnimationEnabled()) {
190+
FXUtils.prepareOnMouseEnter(launcherSettingsItem, Controllers::prepareSettingsPage);
191+
}
185192

186193
// sixth item in left sidebar
187194
AdvancedListItem terracottaItem = new AdvancedListItem();
188195
terracottaItem.setLeftGraphic(wrap(SVG.GRAPH2));
189196
terracottaItem.setActionButtonVisible(false);
190197
terracottaItem.setTitle(i18n("terracotta"));
191-
192198
terracottaItem.setOnAction(e -> {
193199
if (TerracottaMetadata.PROVIDER != null) {
194200
Controllers.navigate(Controllers.getTerracottaPage());

0 commit comments

Comments
 (0)