Use event system for GUI related events

This commit is contained in:
RaphiMC 2023-09-16 20:55:31 +02:00
parent 1a7c557adf
commit 9f4fbe0031
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
8 changed files with 80 additions and 23 deletions

View File

@ -47,6 +47,7 @@ import net.raphimc.viaproxy.saves.SaveManager;
import net.raphimc.viaproxy.tasks.LoaderTask; import net.raphimc.viaproxy.tasks.LoaderTask;
import net.raphimc.viaproxy.tasks.UpdateCheckTask; import net.raphimc.viaproxy.tasks.UpdateCheckTask;
import net.raphimc.viaproxy.ui.ViaProxyUI; import net.raphimc.viaproxy.ui.ViaProxyUI;
import net.raphimc.viaproxy.ui.events.UIInitEvent;
import net.raphimc.viaproxy.util.ClassLoaderPriorityUtil; import net.raphimc.viaproxy.util.ClassLoaderPriorityUtil;
import net.raphimc.viaproxy.util.logging.Logger; import net.raphimc.viaproxy.util.logging.Logger;
@ -143,7 +144,7 @@ public class ViaProxy {
Logger.LOGGER.info("Waiting for UI to be initialized..."); Logger.LOGGER.info("Waiting for UI to be initialized...");
Thread.sleep(1000); Thread.sleep(1000);
} }
ui.setReady(); ViaProxyUI.EVENT_MANAGER.call(new UIInitEvent());
Logger.LOGGER.info("ViaProxy started successfully!"); Logger.LOGGER.info("ViaProxy started successfully!");
return; return;
} }

View File

@ -44,10 +44,4 @@ public abstract class AUITab {
protected abstract void init(final JPanel contentPane); protected abstract void init(final JPanel contentPane);
public void setReady() {
}
public void onClose() {
}
} }

View File

@ -18,8 +18,14 @@
package net.raphimc.viaproxy.ui; package net.raphimc.viaproxy.ui;
import com.formdev.flatlaf.FlatDarkLaf; import com.formdev.flatlaf.FlatDarkLaf;
import net.lenni0451.lambdaevents.EventHandler;
import net.lenni0451.lambdaevents.LambdaManager;
import net.lenni0451.lambdaevents.generator.LambdaMetaFactoryGenerator;
import net.lenni0451.reflect.JavaBypass;
import net.lenni0451.reflect.stream.RStream; import net.lenni0451.reflect.stream.RStream;
import net.raphimc.viaproxy.ViaProxy; import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.ui.events.UICloseEvent;
import net.raphimc.viaproxy.ui.events.UIInitEvent;
import net.raphimc.viaproxy.ui.impl.AccountsTab; import net.raphimc.viaproxy.ui.impl.AccountsTab;
import net.raphimc.viaproxy.ui.impl.AdvancedTab; import net.raphimc.viaproxy.ui.impl.AdvancedTab;
import net.raphimc.viaproxy.ui.impl.GeneralTab; import net.raphimc.viaproxy.ui.impl.GeneralTab;
@ -35,6 +41,8 @@ import java.util.List;
public class ViaProxyUI extends JFrame { public class ViaProxyUI extends JFrame {
public static final LambdaManager EVENT_MANAGER = LambdaManager.threadSafe(new LambdaMetaFactoryGenerator(JavaBypass.TRUSTED_LOOKUP));
public static final int BORDER_PADDING = 10; public static final int BORDER_PADDING = 10;
public static final int BODY_BLOCK_PADDING = 10; public static final int BODY_BLOCK_PADDING = 10;
@ -59,6 +67,8 @@ public class ViaProxyUI extends JFrame {
ToolTipManager.sharedInstance().setDismissDelay(10_000); ToolTipManager.sharedInstance().setDismissDelay(10_000);
SwingUtilities.updateComponentTreeUI(this); SwingUtilities.updateComponentTreeUI(this);
this.setVisible(true); this.setVisible(true);
EVENT_MANAGER.register(this);
} }
private void setLookAndFeel() { private void setLookAndFeel() {
@ -83,7 +93,7 @@ public class ViaProxyUI extends JFrame {
this.addWindowListener(new WindowAdapter() { this.addWindowListener(new WindowAdapter() {
@Override @Override
public void windowClosing(WindowEvent e) { public void windowClosing(WindowEvent e) {
for (AUITab tab : ViaProxyUI.this.tabs) tab.onClose(); EVENT_MANAGER.call(new UICloseEvent());
} }
}); });
this.setSize(500, 360); this.setSize(500, 360);
@ -98,18 +108,20 @@ public class ViaProxyUI extends JFrame {
.fields() .fields()
.filter(field -> AUITab.class.isAssignableFrom(field.type())) .filter(field -> AUITab.class.isAssignableFrom(field.type()))
.forEach(field -> { .forEach(field -> {
AUITab tab = field.get(); final AUITab tab = field.get();
this.tabs.add(field.get()); this.tabs.add(field.get());
tab.add(this.contentPane); tab.add(this.contentPane);
EVENT_MANAGER.register(tab);
}); });
this.contentPane.setEnabledAt(this.contentPane.indexOfTab(this.accountsTab.getName()), false); this.contentPane.setEnabledAt(this.contentPane.indexOfTab(this.accountsTab.getName()), false);
} }
@EventHandler
public void setReady() { private void onInit(final UIInitEvent event) {
for (AUITab tab : this.tabs) tab.setReady(); for (int i = 0; i < this.contentPane.getTabCount(); i++) {
for (int i = 0; i < this.contentPane.getTabCount(); i++) this.contentPane.setEnabledAt(i, true); this.contentPane.setEnabledAt(i, true);
}
} }
public void openURL(final String url) { public void openURL(final String url) {

View File

@ -0,0 +1,21 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2023 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viaproxy.ui.events;
public class UICloseEvent {
}

View File

@ -0,0 +1,21 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2023 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viaproxy.ui.events;
public class UIInitEvent {
}

View File

@ -17,6 +17,7 @@
*/ */
package net.raphimc.viaproxy.ui.impl; package net.raphimc.viaproxy.ui.impl;
import net.lenni0451.lambdaevents.EventHandler;
import net.raphimc.mcauth.MinecraftAuth; import net.raphimc.mcauth.MinecraftAuth;
import net.raphimc.mcauth.step.msa.StepMsaDeviceCode; import net.raphimc.mcauth.step.msa.StepMsaDeviceCode;
import net.raphimc.mcauth.util.MicrosoftConstants; import net.raphimc.mcauth.util.MicrosoftConstants;
@ -27,6 +28,7 @@ import net.raphimc.viaproxy.saves.impl.accounts.BedrockAccount;
import net.raphimc.viaproxy.saves.impl.accounts.MicrosoftAccount; import net.raphimc.viaproxy.saves.impl.accounts.MicrosoftAccount;
import net.raphimc.viaproxy.ui.AUITab; import net.raphimc.viaproxy.ui.AUITab;
import net.raphimc.viaproxy.ui.ViaProxyUI; import net.raphimc.viaproxy.ui.ViaProxyUI;
import net.raphimc.viaproxy.ui.events.UIInitEvent;
import net.raphimc.viaproxy.ui.popups.AddAccountPopup; import net.raphimc.viaproxy.ui.popups.AddAccountPopup;
import net.raphimc.viaproxy.util.GBC; import net.raphimc.viaproxy.util.GBC;
import net.raphimc.viaproxy.util.TFunction; import net.raphimc.viaproxy.util.TFunction;
@ -216,8 +218,8 @@ public class AccountsTab extends AUITab {
contentPane.add(body, BorderLayout.CENTER); contentPane.add(body, BorderLayout.CENTER);
} }
@Override @EventHandler
public void setReady() { private void onInit(final UIInitEvent event) {
ViaProxy.saveManager.accountsSave.getAccounts().forEach(this::addAccount); ViaProxy.saveManager.accountsSave.getAccounts().forEach(this::addAccount);
DefaultListModel<Account> model = (DefaultListModel<Account>) this.accountsList.getModel(); DefaultListModel<Account> model = (DefaultListModel<Account>) this.accountsList.getModel();
if (!model.isEmpty()) this.markSelected(0); if (!model.isEmpty()) this.markSelected(0);

View File

@ -20,10 +20,13 @@ package net.raphimc.viaproxy.ui.impl;
import com.viaversion.viaversion.util.DumpUtil; import com.viaversion.viaversion.util.DumpUtil;
import gs.mclo.api.MclogsClient; import gs.mclo.api.MclogsClient;
import gs.mclo.api.response.UploadLogResponse; import gs.mclo.api.response.UploadLogResponse;
import net.lenni0451.lambdaevents.EventHandler;
import net.raphimc.viaproxy.ViaProxy; import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.saves.impl.UISave; import net.raphimc.viaproxy.saves.impl.UISave;
import net.raphimc.viaproxy.ui.AUITab; import net.raphimc.viaproxy.ui.AUITab;
import net.raphimc.viaproxy.ui.ViaProxyUI; import net.raphimc.viaproxy.ui.ViaProxyUI;
import net.raphimc.viaproxy.ui.events.UICloseEvent;
import net.raphimc.viaproxy.ui.events.UIInitEvent;
import net.raphimc.viaproxy.util.GBC; import net.raphimc.viaproxy.util.GBC;
import net.raphimc.viaproxy.util.logging.Logger; import net.raphimc.viaproxy.util.logging.Logger;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -200,15 +203,15 @@ public class AdvancedTab extends AUITab {
container.add(padding, BorderLayout.CENTER); container.add(padding, BorderLayout.CENTER);
} }
@Override @EventHandler
public void setReady() { private void onInit(final UIInitEvent event) {
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {
this.viaVersionDumpButton.setEnabled(true); this.viaVersionDumpButton.setEnabled(true);
}); });
} }
@Override @EventHandler
public void onClose() { private void onClose(final UICloseEvent event) {
UISave save = ViaProxy.saveManager.uiSave; UISave save = ViaProxy.saveManager.uiSave;
save.put("bind_port", String.valueOf(this.bindPort.getValue())); save.put("bind_port", String.valueOf(this.bindPort.getValue()));
save.put("proxy", this.proxy.getText()); save.put("proxy", this.proxy.getText());

View File

@ -18,6 +18,7 @@
package net.raphimc.viaproxy.ui.impl; package net.raphimc.viaproxy.ui.impl;
import com.google.common.net.HostAndPort; import com.google.common.net.HostAndPort;
import net.lenni0451.lambdaevents.EventHandler;
import net.raphimc.vialoader.util.VersionEnum; import net.raphimc.vialoader.util.VersionEnum;
import net.raphimc.viaproxy.ViaProxy; import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.cli.options.Options; import net.raphimc.viaproxy.cli.options.Options;
@ -27,6 +28,8 @@ import net.raphimc.viaproxy.saves.impl.UISave;
import net.raphimc.viaproxy.saves.impl.accounts.OfflineAccount; import net.raphimc.viaproxy.saves.impl.accounts.OfflineAccount;
import net.raphimc.viaproxy.ui.AUITab; import net.raphimc.viaproxy.ui.AUITab;
import net.raphimc.viaproxy.ui.ViaProxyUI; import net.raphimc.viaproxy.ui.ViaProxyUI;
import net.raphimc.viaproxy.ui.events.UICloseEvent;
import net.raphimc.viaproxy.ui.events.UIInitEvent;
import net.raphimc.viaproxy.util.GBC; import net.raphimc.viaproxy.util.GBC;
import net.raphimc.viaproxy.util.logging.Logger; import net.raphimc.viaproxy.util.logging.Logger;
import org.jdesktop.swingx.VerticalLayout; import org.jdesktop.swingx.VerticalLayout;
@ -185,16 +188,16 @@ public class GeneralTab extends AUITab {
parent.add(footer); parent.add(footer);
} }
@Override @EventHandler
public void setReady() { private void setReady(final UIInitEvent event) {
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {
this.stateButton.setText("Start"); this.stateButton.setText("Start");
this.stateButton.setEnabled(true); this.stateButton.setEnabled(true);
}); });
} }
@Override @EventHandler
public void onClose() { private void onClose(final UICloseEvent event) {
UISave save = ViaProxy.saveManager.uiSave; UISave save = ViaProxy.saveManager.uiSave;
save.put("server_address", this.serverAddress.getText()); save.put("server_address", this.serverAddress.getText());
save.put("server_version", String.valueOf(this.serverVersion.getSelectedIndex())); save.put("server_version", String.valueOf(this.serverVersion.getSelectedIndex()));