|
| 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 | +} |
0 commit comments