Skip to content

Commit 90a795b

Browse files
authored
在陶瓦联机页面添加切换账号菜单 (#5270)
1 parent 46f7e4d commit 90a795b

3 files changed

Lines changed: 90 additions & 34 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Hello Minecraft! Launcher
3+
* Copyright (C) 2026 huangyuhui <huanghongxun2008@126.com> and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
package org.jackhuang.hmcl.ui.account;
19+
20+
import com.jfoenix.controls.JFXPopup;
21+
import javafx.beans.InvalidationListener;
22+
import javafx.beans.WeakInvalidationListener;
23+
import javafx.beans.binding.Bindings;
24+
import javafx.beans.binding.BooleanBinding;
25+
import javafx.scene.Node;
26+
import javafx.scene.control.Label;
27+
import javafx.scene.layout.StackPane;
28+
import org.jackhuang.hmcl.auth.Account;
29+
import org.jackhuang.hmcl.setting.Accounts;
30+
import org.jackhuang.hmcl.ui.FXUtils;
31+
import org.jackhuang.hmcl.ui.construct.AdvancedListBox;
32+
33+
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
34+
35+
public final class AccountListPopupMenu extends StackPane {
36+
public static void show(Node owner, JFXPopup.PopupVPosition vAlign, JFXPopup.PopupHPosition hAlign,
37+
double initOffsetX, double initOffsetY) {
38+
var menu = new AccountListPopupMenu();
39+
JFXPopup popup = new JFXPopup(menu);
40+
popup.show(owner, vAlign, hAlign, initOffsetX, initOffsetY);
41+
}
42+
43+
@SuppressWarnings("FieldCanBeLocal")
44+
private final BooleanBinding isEmpty = Bindings.isEmpty(Accounts.getAccounts());
45+
@SuppressWarnings("FieldCanBeLocal")
46+
private final InvalidationListener listener;
47+
48+
public AccountListPopupMenu() {
49+
AdvancedListBox box = new AdvancedListBox();
50+
box.getStyleClass().add("no-padding");
51+
box.setPrefWidth(220);
52+
box.setPrefHeight(-1);
53+
box.setMaxHeight(260);
54+
55+
listener = o -> {
56+
box.clear();
57+
58+
for (Account account : Accounts.getAccounts()) {
59+
AccountAdvancedListItem item = new AccountAdvancedListItem(account);
60+
item.setOnAction(e -> {
61+
Accounts.setSelectedAccount(account);
62+
if (getScene().getWindow() instanceof JFXPopup popup)
63+
popup.hide();
64+
});
65+
box.add(item);
66+
}
67+
};
68+
listener.invalidated(null);
69+
Accounts.getAccounts().addListener(new WeakInvalidationListener(listener));
70+
71+
Label placeholder = new Label(i18n("account.empty"));
72+
placeholder.setStyle("-fx-padding: 10px; -fx-text-fill: -monet-on-surface-variant; -fx-font-style: italic;");
73+
74+
FXUtils.onChangeAndOperate(isEmpty, empty -> {
75+
getChildren().setAll(empty ? placeholder : box);
76+
});
77+
}
78+
79+
}

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

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919

2020
import com.jfoenix.controls.JFXPopup;
2121
import javafx.beans.property.ReadOnlyObjectProperty;
22-
import javafx.scene.control.Label;
2322
import org.jackhuang.hmcl.Metadata;
24-
import org.jackhuang.hmcl.auth.Account;
2523
import org.jackhuang.hmcl.event.EventBus;
2624
import org.jackhuang.hmcl.event.RefreshedVersionsEvent;
2725
import org.jackhuang.hmcl.game.HMCLGameRepository;
@@ -37,11 +35,11 @@
3735
import org.jackhuang.hmcl.ui.FXUtils;
3836
import org.jackhuang.hmcl.ui.SVG;
3937
import org.jackhuang.hmcl.ui.account.AccountAdvancedListItem;
38+
import org.jackhuang.hmcl.ui.account.AccountListPopupMenu;
4039
import org.jackhuang.hmcl.ui.animation.AnimationUtils;
4140
import org.jackhuang.hmcl.ui.construct.AdvancedListBox;
4241
import org.jackhuang.hmcl.ui.construct.AdvancedListItem;
4342
import org.jackhuang.hmcl.ui.construct.MessageDialogPane;
44-
import org.jackhuang.hmcl.ui.construct.PopupMenu;
4543
import org.jackhuang.hmcl.ui.decorator.DecoratorAnimatedPage;
4644
import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
4745
import org.jackhuang.hmcl.ui.download.ModpackInstallWizardProvider;
@@ -148,7 +146,7 @@ protected Skin(RootPage control) {
148146
// first item in left sidebar
149147
AccountAdvancedListItem accountListItem = new AccountAdvancedListItem();
150148
accountListItem.setOnAction(e -> Controllers.navigate(Controllers.getAccountListPage()));
151-
FXUtils.onSecondaryButtonClicked(accountListItem, () -> showAccountListPopupMenu(accountListItem));
149+
FXUtils.onSecondaryButtonClicked(accountListItem, () -> AccountListPopupMenu.show(accountListItem, JFXPopup.PopupVPosition.TOP, JFXPopup.PopupHPosition.LEFT, accountListItem.getWidth(), 0));
152150
accountListItem.accountProperty().bind(Accounts.selectedAccountProperty());
153151

154152
// second item in left sidebar
@@ -251,36 +249,6 @@ else if (Platform.SYSTEM_PLATFORM.equals(OperatingSystem.LINUX, Architecture.LOO
251249
setCenter(getSkinnable().getMainPage());
252250
}
253251

254-
public void showAccountListPopupMenu(
255-
AccountAdvancedListItem accountListItem
256-
) {
257-
PopupMenu popupMenu = new PopupMenu();
258-
JFXPopup popup = new JFXPopup(popupMenu);
259-
AdvancedListBox scrollPane = new AdvancedListBox();
260-
scrollPane.getStyleClass().add("no-padding");
261-
scrollPane.setPrefWidth(220);
262-
scrollPane.setPrefHeight(-1);
263-
scrollPane.setMaxHeight(260);
264-
265-
if (Accounts.getAccounts().isEmpty()) {
266-
Label placeholder = new Label(i18n("account.empty"));
267-
placeholder.setStyle("-fx-padding: 10px; -fx-text-fill: -monet-on-surface-variant; -fx-font-style: italic;");
268-
scrollPane.add(placeholder);
269-
} else {
270-
for (Account account : Accounts.getAccounts()) {
271-
AccountAdvancedListItem item = new AccountAdvancedListItem(account);
272-
item.setOnAction(e -> {
273-
Accounts.setSelectedAccount(account);
274-
popup.hide();
275-
});
276-
scrollPane.add(item);
277-
}
278-
}
279-
280-
popupMenu.getContent().add(scrollPane);
281-
popup.show(accountListItem, JFXPopup.PopupVPosition.TOP, JFXPopup.PopupHPosition.LEFT, accountListItem.getWidth(), 0);
282-
}
283-
284252
public void showGameListPopupMenu(AdvancedListItem gameListItem) {
285253
GameListPopupMenu.show(gameListItem,
286254
JFXPopup.PopupVPosition.TOP,

HMCL/src/main/java/org/jackhuang/hmcl/ui/terracotta/TerracottaPage.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@
2525
import javafx.scene.layout.BorderPane;
2626
import javafx.scene.layout.Priority;
2727
import javafx.scene.layout.VBox;
28+
import org.jackhuang.hmcl.setting.Accounts;
2829
import org.jackhuang.hmcl.setting.Profile;
2930
import org.jackhuang.hmcl.setting.Profiles;
3031
import org.jackhuang.hmcl.terracotta.TerracottaMetadata;
3132
import org.jackhuang.hmcl.ui.Controllers;
3233
import org.jackhuang.hmcl.ui.FXUtils;
3334
import org.jackhuang.hmcl.ui.SVG;
35+
import org.jackhuang.hmcl.ui.account.AccountAdvancedListItem;
36+
import org.jackhuang.hmcl.ui.account.AccountListPopupMenu;
3437
import org.jackhuang.hmcl.ui.animation.TransitionPane;
3538
import org.jackhuang.hmcl.ui.construct.*;
3639
import org.jackhuang.hmcl.ui.decorator.DecoratorAnimatedPage;
@@ -69,7 +72,13 @@ public TerracottaPage() {
6972
.addNavigationDrawerTab(tab, statusPage, i18n("terracotta.status"), SVG.TUNE);
7073
left.setTop(sideBar);
7174

75+
AccountAdvancedListItem accountListItem = new AccountAdvancedListItem();
76+
accountListItem.setOnAction(e -> Controllers.navigate(Controllers.getAccountListPage()));
77+
accountListItem.accountProperty().bind(Accounts.selectedAccountProperty());
78+
FXUtils.onSecondaryButtonClicked(accountListItem, () -> AccountListPopupMenu.show(accountListItem, JFXPopup.PopupVPosition.BOTTOM, JFXPopup.PopupHPosition.LEFT, accountListItem.getWidth(), 0));
79+
7280
AdvancedListBox toolbar = new AdvancedListBox()
81+
.add(accountListItem)
7382
.addNavigationDrawerItem(i18n("version.launch"), SVG.ROCKET_LAUNCH, () -> {
7483
Profile profile = Profiles.getSelectedProfile();
7584
Versions.launch(profile, profile.getSelectedVersion(), launcherHelper -> {

0 commit comments

Comments
 (0)