From dca68e8b511ce7db790b52fa245a12dd3b8ee4c1 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 14 Oct 2024 23:01:53 +0200 Subject: [PATCH] gui scaling fixes (no window packing, unify methods, MML menu) --- megameklab/src/megameklab/MegaMekLab.java | 18 ++---------------- megameklab/src/megameklab/ui/MenuBar.java | 11 ++++++----- megameklab/src/megameklab/ui/MenuBarOwner.java | 7 +++---- 3 files changed, 11 insertions(+), 25 deletions(-) diff --git a/megameklab/src/megameklab/MegaMekLab.java b/megameklab/src/megameklab/MegaMekLab.java index db1eb6bf9..b4f82fecc 100644 --- a/megameklab/src/megameklab/MegaMekLab.java +++ b/megameklab/src/megameklab/MegaMekLab.java @@ -16,7 +16,6 @@ */ package megameklab; -import java.awt.*; import java.io.File; import java.io.ObjectInputFilter; import java.util.Locale; @@ -30,6 +29,7 @@ import megamek.MegaMek; import megamek.SuiteConstants; import megamek.client.ui.preferences.SuitePreferences; +import megamek.client.ui.swing.util.UIUtil; import megamek.common.Entity; import megamek.common.EquipmentType; import megamek.common.MekFileParser; @@ -129,7 +129,7 @@ private static void setLookAndFeel() { try { String plaf = CConfig.getParam(CConfig.GUI_PLAF, UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel(plaf); - updateAfterUiChange(); + UIUtil.updateAfterUiChange(); } catch (Exception ex) { logger.error("setLookAndFeel() Exception {}", ex); } @@ -138,20 +138,6 @@ private static void setLookAndFeel() { public static void updateGuiScaling() { System.setProperty("flatlaf.uiScale", Double.toString(GUIPreferences.getInstance().getGUIScale())); setLookAndFeel(); - 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(); - } } public static SuitePreferences getMMLPreferences() { diff --git a/megameklab/src/megameklab/ui/MenuBar.java b/megameklab/src/megameklab/ui/MenuBar.java index 706f949b4..b82ab09eb 100644 --- a/megameklab/src/megameklab/ui/MenuBar.java +++ b/megameklab/src/megameklab/ui/MenuBar.java @@ -39,6 +39,7 @@ import megamek.client.ui.dialogs.WeightDisplayDialog; import megamek.client.ui.swing.GUIPreferences; import megamek.client.ui.swing.UnitLoadingDialog; +import megamek.client.ui.swing.util.UIUtil; import megamek.common.*; import megamek.common.annotations.Nullable; import megamek.common.loaders.BLKFile; @@ -625,8 +626,7 @@ private JMenu createThemesMenu() { return themesMenu; } - private @Nullable JMenuItem createCConfigMenuItem(final String recentFileName, - final int fileNumber) { + private @Nullable JMenuItem createCConfigMenuItem(final String recentFileName, final int fileNumber) { File recent = new File(recentFileName); String path = recent.getParent(); String mmlDirectory = System.getProperty("user.dir"); @@ -636,9 +636,10 @@ private JMenu createThemesMenu() { path = path.substring(0, 40) + "..."; } } - - String text = "" + fileNumber + ". " + recent.getName() + "
" + path; - final JMenuItem miCConfig = new JMenuItem(text); + String html = "%s"; + String style = ".small { font-size:smaller; color:gray; }"; + String content = "
%d. %s
".formatted(fileNumber, recent.getName()) + UIUtil.spanCSS("small", path); + final JMenuItem miCConfig = new JMenuItem(html.formatted(style, content)); miCConfig.setName("miCConfig"); miCConfig.addActionListener(evt -> loadUnitFromFile(fileNumber)); miCConfig.setMnemonic(48 + fileNumber); // the number itself, i.e. 1, 2, 3 etc. diff --git a/megameklab/src/megameklab/ui/MenuBarOwner.java b/megameklab/src/megameklab/ui/MenuBarOwner.java index 4dfbb1fb7..04a1feae9 100644 --- a/megameklab/src/megameklab/ui/MenuBarOwner.java +++ b/megameklab/src/megameklab/ui/MenuBarOwner.java @@ -18,10 +18,9 @@ */ package megameklab.ui; -import megamek.MegaMek; +import megamek.client.ui.swing.util.UIUtil; import megamek.common.Entity; import megamek.common.annotations.Nullable; -import megameklab.MegaMekLab; import megameklab.ui.dialog.UiLoader; import megameklab.ui.util.AppCloser; import megameklab.util.CConfig; @@ -128,10 +127,10 @@ default void changeTheme(String lookAndFeelInfo) { SwingUtilities.invokeLater(() -> { try { UIManager.setLookAndFeel(lookAndFeelInfo); - MegaMekLab.updateAfterUiChange(); + UIUtil.updateAfterUiChange(); } catch (Exception ex) { PopupMessages.showLookAndFeelError(getFrame(), ex.getMessage()); } }); } -} \ No newline at end of file +}