GUI: fixed that server's lobby do not remember divider position after app restart (tables, matches and chat sizes)

This commit is contained in:
Oleg Agafonov 2024-06-23 15:55:46 +04:00
parent f78177a540
commit f0c38cdb87
7 changed files with 36 additions and 32 deletions

View file

@ -324,9 +324,7 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
// create default server lobby and hide it until connect
tablesPane = new TablesPane();
desktopPane.add(tablesPane, javax.swing.JLayeredPane.DEFAULT_LAYER);
SwingUtilities.invokeLater(() -> {
this.hideServerLobby();
});
SwingUtilities.invokeLater(this::hideServerLobby);
addTooltipContainer();
setBackground();
@ -1608,8 +1606,9 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
}
@Override
public void disconnected(final boolean askToReconnect) {
public void disconnected(boolean askToReconnect, boolean keepMySessionActive) {
if (SwingUtilities.isEventDispatchThread()) {
// TODO: need research, it can generate wrong logs due diff threads source (doInBackground, swing, server events, etc)
// REMOTE task, e.g. connecting
LOGGER.info("Disconnected from server side");
} else {
@ -1617,11 +1616,11 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
LOGGER.info("Disconnected from client side");
}
SwingUtilities.invokeLater(() -> {
Runnable runOnExit = () -> {
// user already disconnected, can't do any online actions like quite chat
// but try to keep session
// TODO: why it ignore askToReconnect here, but use custom reconnect dialog later?! Need research
SessionHandler.disconnect(false, true);
SessionHandler.disconnect(false, keepMySessionActive);
setConnectButtonText(NOT_CONNECTED_BUTTON);
disableButtons();
hideGames();
@ -1632,7 +1631,13 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
message.setButton2("Yes", PlayerAction.CLIENT_RECONNECT);
showUserRequestDialog(message);
}
});
};
if (SwingUtilities.isEventDispatchThread()) {
runOnExit.run();
} else {
SwingUtilities.invokeLater(runOnExit);
}
}
@Override

View file

@ -30,8 +30,6 @@ import org.apache.log4j.Logger;
import org.mage.card.arcane.CardRendererUtils;
import org.ocpsoft.prettytime.Duration;
import org.ocpsoft.prettytime.PrettyTime;
import org.ocpsoft.prettytime.TimeFormat;
import org.ocpsoft.prettytime.units.JustNow;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
@ -645,8 +643,24 @@ public class TablesPanel extends javax.swing.JPanel {
btnQuickStartMCTS.setFont(GUISizeHelper.menuFont);
}
private void restoreDividerLocations() {
Rectangle currentBounds = MageFrame.getDesktop().getBounds();
if (currentBounds != null) {
String firstDivider = PreferencesDialog.getCachedValue(KEY_TABLES_DIVIDER_LOCATION_1, null);
String tableDivider = PreferencesDialog.getCachedValue(KEY_TABLES_DIVIDER_LOCATION_2, null);
String chatDivider = PreferencesDialog.getCachedValue(KEY_TABLES_DIVIDER_LOCATION_3, null);
GuiDisplayUtil.restoreDividerLocations(currentBounds, firstDivider, jSplitPane1);
GuiDisplayUtil.restoreDividerLocations(currentBounds, tableDivider, jSplitPaneTables);
GuiDisplayUtil.restoreDividerLocations(currentBounds, chatDivider, chatPanelMain);
}
}
private void saveDividerLocations() {
// save divider locations and divider saveDividerLocations
// save divider locations
if (this.jSplitPane1.getDividerLocation() == -1) {
// server lobby hidden by default, so ignore that settings
return;
}
GuiDisplayUtil.saveCurrentBoundsToPrefs();
GuiDisplayUtil.saveDividerLocationToPrefs(KEY_TABLES_DIVIDER_LOCATION_1, this.jSplitPane1.getDividerLocation());
GuiDisplayUtil.saveDividerLocationToPrefs(KEY_TABLES_DIVIDER_LOCATION_2, this.jSplitPaneTables.getDividerLocation());
@ -663,18 +677,6 @@ public class TablesPanel extends javax.swing.JPanel {
TableUtil.saveColumnWidthAndOrderToPrefs(tableTables, KEY_TABLES_COLUMNS_WIDTH, KEY_TABLES_COLUMNS_ORDER);
}
private void restoreDividers() {
Rectangle currentBounds = MageFrame.getDesktop().getBounds();
if (currentBounds != null) {
String firstDivider = PreferencesDialog.getCachedValue(KEY_TABLES_DIVIDER_LOCATION_1, null);
String tableDivider = PreferencesDialog.getCachedValue(KEY_TABLES_DIVIDER_LOCATION_2, null);
String chatDivider = PreferencesDialog.getCachedValue(KEY_TABLES_DIVIDER_LOCATION_3, null);
GuiDisplayUtil.restoreDividerLocations(currentBounds, firstDivider, jSplitPane1);
GuiDisplayUtil.restoreDividerLocations(currentBounds, tableDivider, jSplitPaneTables);
GuiDisplayUtil.restoreDividerLocations(currentBounds, chatDivider, chatPanelMain);
}
}
public Map<String, JComponent> getUIComponents() {
Map<String, JComponent> components = new HashMap<>();
@ -778,12 +780,9 @@ public class TablesPanel extends javax.swing.JPanel {
//tableModel.setSession(session);
reloadServerMessages();
MageFrame.getUI().addButton(MageComponents.NEW_GAME_BUTTON, btnNewTable);
// divider locations have to be set with delay else values set are overwritten with system defaults
Executors.newSingleThreadScheduledExecutor().schedule(() -> restoreDividers(), 300, TimeUnit.MILLISECONDS);
restoreDividerLocations();
}
protected void reloadServerMessages() {

View file

@ -71,7 +71,7 @@ public class MultiConnectTest {
}
@Override
public void disconnected(boolean askToReconnect) {
public void disconnected(boolean askToReconnect, boolean keepMySessionActive) {
logger.info("disconnected");
}