Skip to content

Commit

Permalink
gui scaling fixes (no window packing, unify methods, MML menu)
Browse files Browse the repository at this point in the history
  • Loading branch information
SJuliez committed Oct 14, 2024
1 parent d1cffdd commit 4a9cce8
Showing 1 changed file with 6 additions and 20 deletions.
26 changes: 6 additions & 20 deletions MekHQ/src/mekhq/MekHQ.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
import megamek.client.ui.swing.GUIPreferences;
import megamek.client.ui.swing.gameConnectionDialogs.ConnectDialog;
import megamek.client.ui.swing.gameConnectionDialogs.HostDialog;
import megamek.client.ui.swing.util.UIUtil;
import megamek.common.event.*;
import megamek.common.net.marshalling.SanityInputFilter;
import megamek.logging.MMLogger;
import megamek.server.Server;
import megamek.server.totalwarfare.TWGameManager;
import megameklab.MegaMekLab;
import megameklab.util.CConfig;
import mekhq.campaign.Campaign;
import mekhq.campaign.CampaignController;
import mekhq.campaign.Kill;
Expand Down Expand Up @@ -72,7 +72,6 @@
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.ObjectInputFilter;
import java.io.ObjectInputFilter.Config;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -174,14 +173,13 @@ private MekHQ() {
* At startup create and show the main frame of the application.
*/
protected void startup() {
updateGuiScaling(); // also sets the look-and-feel

// Setup user preferences
MegaMek.getMMPreferences().loadFromFile(SuiteConstants.MM_PREFERENCES_FILE);
MegaMekLab.getMMLPreferences().loadFromFile(SuiteConstants.MML_PREFERENCES_FILE);
getMHQPreferences().loadFromFile(SuiteConstants.MHQ_PREFERENCES_FILE);

setUserPreferences();
updateGuiScaling(); // also sets the look-and-feel

initEventHandlers();
// create a start-up frame and display it
Expand Down Expand Up @@ -658,9 +656,11 @@ private void initEventHandlers() {
}

private static void setLookAndFeel(String themeName) {
final String theme = themeName.isBlank() ? "com.formdev.flatlaf.FlatDarculaLaf" : themeName;

Runnable runnable = () -> {
try {
UIManager.setLookAndFeel(themeName);
UIManager.setLookAndFeel(theme);
if (System.getProperty("os.name", "").startsWith("Mac OS X")) {
// Ensure OSX key bindings are used for copy, paste etc
addOSXKeyStrokes((InputMap) UIManager.get("EditorPane.focusInputMap"));
Expand All @@ -670,7 +670,7 @@ private static void setLookAndFeel(String themeName) {
addOSXKeyStrokes((InputMap) UIManager.get("TextArea.focusInputMap"));
}

updateAfterUiChange();
UIUtil.updateAfterUiChange();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
logger.error(e, "setLookAndFeel()");
Expand All @@ -682,20 +682,6 @@ private static void setLookAndFeel(String themeName) {
public static void updateGuiScaling() {
System.setProperty("flatlaf.uiScale", Double.toString(GUIPreferences.getInstance().getGUIScale()));
setLookAndFeel(GUIPreferences.getInstance().getUITheme());
updateAfterUiChange();
}

/**
* Updates all existing windows and frames. Use after a gui scale change or look-and-feel change.
*/
public static void updateAfterUiChange() {
for (Window window : Window.getWindows()) {
SwingUtilities.updateComponentTreeUI(window);
window.pack();
window.invalidate();
window.validate();
window.repaint();
}
}

private static class MekHqPropertyChangedListener implements PropertyChangeListener {
Expand Down

0 comments on commit 4a9cce8

Please sign in to comment.