Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatlaf UI scaling #1610

Merged
merged 9 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions megameklab/src/megameklab/MegaMekLab.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
*/
package megameklab;

import java.awt.*;
import java.io.File;
import java.io.ObjectInputFilter;
import java.util.Locale;

import javax.swing.ToolTipManager;
import javax.swing.UIManager;
import javax.swing.*;

import megamek.client.ui.swing.GUIPreferences;
import org.apache.logging.log4j.LogManager;

import io.sentry.Sentry;
Expand Down Expand Up @@ -99,7 +100,7 @@ private static void startup() {
// TODO : Individual localizations
Locale.setDefault(getMMLOptions().getLocale());

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

// Create a startup frame and display it
switch (CConfig.getStartUpType()) {
Expand Down Expand Up @@ -127,12 +128,32 @@ private static void setLookAndFeel() {
try {
String plaf = CConfig.getParam(CConfig.GUI_PLAF, UIManager.getSystemLookAndFeelClassName());
UIManager.setLookAndFeel(plaf);
updateAfterUiChange();
} catch (Exception ex) {
Sentry.captureException(ex);
LogManager.getLogger().error("", ex);
}
}

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() {
return mmlPreferences;
}
Expand Down
5 changes: 3 additions & 2 deletions megameklab/src/megameklab/ui/MenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,10 @@ private JMenu createThemesMenu() {
}
final JCheckBoxMenuItem miLookAndFeel = new JCheckBoxMenuItem(laf.getName());
miLookAndFeel.setName("miLookAndFeel");
miLookAndFeel.setSelected(laf.getName().equalsIgnoreCase(UIManager.getLookAndFeel().getName()));
miLookAndFeel.setSelected(laf.getClassName().equals(UIManager.getLookAndFeel().getClass().getName()));
miLookAndFeel.addActionListener(evt -> {
owner.changeTheme(laf);
CConfig.setParam(CConfig.GUI_PLAF, laf.getClassName());
for (int i = 0; i < themesMenu.getItemCount(); i++) {
final JMenuItem item = themesMenu.getItem(i);
if (item instanceof JCheckBoxMenuItem) {
Expand All @@ -624,7 +625,7 @@ private JMenu createThemesMenu() {
}
}

String text = "<HTML>" + fileNumber + ". " + recent.getName() + "<BR><FONT SIZE=\"-2\">" + path;
String text = "<HTML><NOBR>" + fileNumber + ". " + recent.getName() + "<BR><FONT SIZE=\"-2\">" + path;
final JMenuItem miCConfig = new JMenuItem(text);
miCConfig.setName("miCConfig");
miCConfig.addActionListener(evt -> loadUnitFromFile(fileNumber));
Expand Down
4 changes: 3 additions & 1 deletion megameklab/src/megameklab/ui/MenuBarOwner.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
*/
package megameklab.ui;

import megamek.MegaMek;
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;
Expand Down Expand Up @@ -126,7 +128,7 @@ default void changeTheme(String lookAndFeelInfo) {
SwingUtilities.invokeLater(() -> {
try {
UIManager.setLookAndFeel(lookAndFeelInfo);
SwingUtilities.updateComponentTreeUI(getFrame());
MegaMekLab.updateAfterUiChange();
} catch (Exception ex) {
PopupMessages.showLookAndFeelError(getFrame(), ex.getMessage());
}
Expand Down
15 changes: 5 additions & 10 deletions megameklab/src/megameklab/ui/battleArmor/BAChassisView.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,49 +82,44 @@ private void initUI() {
gbc.gridx = 0;
gbc.gridy = 0;
add(createLabel(resourceMap, "lblChassisType", "BAChassisView.cbChassisType.text",
"BAChassisView.cbChassisType.tooltip", labelSize), gbc);
"BAChassisView.cbChassisType.tooltip"), gbc);
gbc.gridx = 1;
setFieldSize(cbChassisType, controlSize);
cbChassisType.setToolTipText(resourceMap.getString("BAChassisView.cbChassisType.tooltip"));
add(cbChassisType, gbc);
cbChassisType.addActionListener(this);

gbc.gridx = 0;
gbc.gridy++;
add(createLabel(resourceMap, "lblWeightClass", "BAChassisView.cbWeightClass.text",
"BAChassisView.cbWeightClass.tooltip", labelSize), gbc);
"BAChassisView.cbWeightClass.tooltip"), gbc);
gbc.gridx = 1;
setFieldSize(cbWeightClass, controlSize);
cbWeightClass.setToolTipText(resourceMap.getString("BAChassisView.cbWeightClass.tooltip"));
add(cbWeightClass, gbc);
cbWeightClass.addActionListener(this);

gbc.gridx = 0;
gbc.gridy++;
add(createLabel(resourceMap, "lblSquadSize", "BAChassisView.spnSquadSize.text",
"BAChassisView.spnSquadSize.tooltip", labelSize), gbc);
"BAChassisView.spnSquadSize.tooltip"), gbc);
gbc.gridx = 1;
setFieldSize(spnSquadSize, controlSize);
spnSquadSize.setToolTipText(resourceMap.getString("BAChassisView.spnSquadSize.tooltip"));
add(spnSquadSize, gbc);
spnSquadSize.addChangeListener(this);

gbc.gridx = 0;
gbc.gridy++;
add(createLabel(resourceMap, "lblTurretType", "BAChassisView.cbTurretType.text",
"BAChassisView.cbTurretType.tooltip", labelSize), gbc);
"BAChassisView.cbTurretType.tooltip"), gbc);
gbc.gridx = 1;
setFieldSize(cbTurretType, controlSize);
cbTurretType.setToolTipText(resourceMap.getString("BAChassisView.cbTurretType.tooltip"));
add(cbTurretType, gbc);
cbTurretType.addActionListener(this);

gbc.gridx = 0;
gbc.gridy++;
add(createLabel(resourceMap, "lblTurretSize", "BAChassisView.spnTurretSize.text",
"BAChassisView.spnTurretSize.tooltip", labelSize), gbc);
"BAChassisView.spnTurretSize.tooltip"), gbc);
gbc.gridx = 1;
setFieldSize(spnTurretSize, controlSize);
spnTurretSize.setToolTipText(resourceMap.getString("BAChassisView.spnTurretSize.tooltip"));
add(spnTurretSize, gbc);
spnTurretSize.addChangeListener(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void refresh() {
criticalSlotList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
criticalSlotList.setName(location + ":" + trooper);
criticalSlotList.setBorder(BorderFactory.createLineBorder(CritCellUtil.CRITCELL_BORDER_COLOR));

criticalSlotList.setPrototypeCellValue(CritCellUtil.CRITCELL_WIDTH_STRING);
switch (location) {
case BattleArmor.MOUNT_LOC_LARM:
leftArmPanel.add(criticalSlotList);
Expand Down
22 changes: 7 additions & 15 deletions megameklab/src/megameklab/ui/combatVehicle/CVChassisView.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,9 @@ private void initUI() {
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.WEST;
add(createLabel(resourceMap, "lblTonnage", "CVChassisView.spnTonnage.text",
"CVChassisView.spnTonnage.tooltip", labelSize), gbc);
"CVChassisView.spnTonnage.tooltip"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
setFieldSize(spnTonnage, spinnerSize);
spnTonnage.setToolTipText(resourceMap.getString("CVChassisView.spnTonnage.tooltip"));
add(spnTonnage, gbc);
spnTonnage.addChangeListener(this);
Expand Down Expand Up @@ -157,10 +156,9 @@ private void initUI() {
cbMotiveType.setModel(new DefaultComboBoxModel<>(MOTIVE_TYPES));
gbc.gridx = 0;
add(createLabel(resourceMap, "lblMotiveType", "CVChassisView.cbMotiveType.text",
"CVChassisView.cbMotiveType.tooltip", labelSize), gbc);
"CVChassisView.cbMotiveType.tooltip"), gbc);
gbc.gridx = 1;
gbc.gridwidth = 3;
setFieldSize(cbMotiveType, controlSize);
cbMotiveType.setToolTipText(resourceMap.getString("CVChassisView.cbMotiveType.tooltip"));
add(cbMotiveType, gbc);
cbMotiveType.addActionListener(this);
Expand All @@ -169,10 +167,9 @@ private void initUI() {
gbc.gridx = 0;
gbc.gridwidth = 1;
add(createLabel(resourceMap, "lblEngine", "CVChassisView.cbEngine.text",
"CVChassisView.cbEngine.tooltip", labelSize), gbc);
"CVChassisView.cbEngine.tooltip"), gbc);
gbc.gridx = 1;
gbc.gridwidth = 3;
setFieldSize(cbEngine, controlSize);
cbEngine.setToolTipText(resourceMap.getString("CVChassisView.cbEngine.tooltip"));
add(cbEngine, gbc);
cbEngine.addActionListener(this);
Expand All @@ -181,10 +178,9 @@ private void initUI() {
gbc.gridx = 0;
gbc.gridwidth = 2;
add(createLabel(resourceMap, "lblExtraSeats", "CVChassisView.spnExtraSeats.text",
"CVChassisView.spnExtraSeats.tooltip", labelSize), gbc);
"CVChassisView.spnExtraSeats.tooltip"), gbc);
gbc.gridx = 2;
gbc.gridwidth = 2;
setFieldSize(spnExtraSeats, spinnerSize);
spnExtraSeats.setToolTipText(resourceMap.getString("CVChassisView.spnExtraSeats.tooltip"));
add(spnExtraSeats, gbc);
spnExtraSeats.addChangeListener(this);
Expand All @@ -193,10 +189,9 @@ private void initUI() {
gbc.gridx = 0;
gbc.gridwidth = 1;
add(createLabel(resourceMap, "lblTurrets", "CVChassisView.cbTurrets.text",
"CVChassisView.cbTurrets.tooltip", labelSize), gbc);
"CVChassisView.cbTurrets.tooltip"), gbc);
gbc.gridx = 1;
gbc.gridwidth = 3;
setFieldSize(cbTurrets, controlSize);
cbTurrets.setToolTipText(resourceMap.getString("CVChassisView.cbTurrets.tooltip"));
add(cbTurrets, gbc);
cbTurrets.addActionListener(this);
Expand All @@ -205,10 +200,9 @@ private void initUI() {
gbc.gridx = 0;
gbc.gridwidth = 3;
JLabel lbl = createLabel(resourceMap, "lblTurretWt", "CVChassisView.spnTurretWt.text",
"CVChassisView.spnTurretWt.tooltip", labelSize);
"CVChassisView.spnTurretWt.tooltip");
add(lbl, gbc);
gbc.gridx = 3;
setFieldSize(spnChassisTurretWt, spinnerSize);
spnChassisTurretWt.setToolTipText(resourceMap.getString("CVChassisView.spnTurretWt.tooltip"));
add(spnChassisTurretWt, gbc);
spnChassisTurretWt.addChangeListener(this);
Expand All @@ -219,11 +213,10 @@ private void initUI() {
gbc.gridx = 0;
gbc.gridwidth = 3;
lbl = createLabel(resourceMap, "lblTurret2Wt", "CVChassisView.spnTurret2Wt.text",
"CVChassisView.spnTurret2Wt.tooltip", labelSize);
"CVChassisView.spnTurret2Wt.tooltip");
add(lbl, gbc);
gbc.gridx = 3;
gbc.gridwidth = 1;
setFieldSize(spnChassisTurret2Wt, spinnerSize);
spnChassisTurret2Wt.setToolTipText(resourceMap.getString("CVChassisView.spnTurret2Wt.tooltip"));
add(spnChassisTurret2Wt, gbc);
spnChassisTurret2Wt.addChangeListener(this);
Expand All @@ -235,7 +228,6 @@ private void initUI() {
btnResetChassis.setActionCommand(CMD_RESET_CHASSIS);
gbc.gridx = 1;
gbc.gridwidth = 3;
setFieldSize(btnResetChassis, controlSize);
btnResetChassis.setToolTipText(resourceMap.getString("CVChassisView.btnResetChassis.tooltip"));
add(btnResetChassis, gbc);
btnResetChassis.addActionListener(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public void refresh() {
criticalSlotList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
criticalSlotList.setName(location + "");
criticalSlotList.setBorder(BorderFactory.createLineBorder(Color.BLACK));
criticalSlotList.setPrototypeCellValue(CritCellUtil.CRITCELL_WIDTH_STRING);
if (isVTOL()) {
if (vtolLocations.containsKey(location)) {
vtolLocations.get(location).add(criticalSlotList);
Expand Down
12 changes: 4 additions & 8 deletions megameklab/src/megameklab/ui/combatVehicle/CVTransportView.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,21 @@ private void initUI() {
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.WEST;
add(createLabel(resourceMap, "lblFixed", "CVTransportView.lblFixed.text", labelSize), gbc);
add(createLabel(resourceMap, "lblFixed", "CVTransportView.lblFixed.text"), gbc);

gbc.gridx = 2;
gbc.gridy = 0;
add(createLabel(resourceMap, "lblPod", "CVTransportView.lblPod.text", labelSize), gbc);
add(createLabel(resourceMap, "lblPod", "CVTransportView.lblPod.text"), gbc);

gbc.gridx = 0;
gbc.gridy = 1;
add(createLabel(resourceMap, "lblTroopSpace", "CVTransportView.lblTroopSpace.text", labelSizeLg), gbc);
add(createLabel(resourceMap, "lblTroopSpace", "CVTransportView.lblTroopSpace.text"), gbc);

gbc.gridx = 1;
setFieldSize(spnFixedTroop, editorSize);
add(spnFixedTroop, gbc);
spnFixedTroop.addChangeListener(this);

gbc.gridx = 2;
setFieldSize(spnPodTroop, editorSize);
add(spnPodTroop, gbc);
spnPodTroop.addChangeListener(this);

Expand All @@ -99,7 +97,7 @@ private void initUI() {
1 / bayType.getWeight());
gbc.gridx = 0;
gbc.gridy++;
final JLabel lblBayType = createLabel("lbl" + bayType.name(), bayType.getDisplayName(), labelSizeLg);
final JLabel lblBayType = createLabel("lbl" + bayType.name(), bayType.getDisplayName());
lblBayType.setToolTipText(tooltip);
add(lblBayType, gbc);

Expand All @@ -108,7 +106,6 @@ private void initUI() {
JSpinner spinner = new JSpinner(model);
spinner.setToolTipText(tooltip);
spinner.setName(bayType.toString());
setFieldSize(spinner, editorSize);
fixedSpinnerModels.put(bayType, model);
fixedSpinners.put(bayType, spinner);
add(spinner, gbc);
Expand All @@ -119,7 +116,6 @@ private void initUI() {
spinner = new JSpinner(model);
spinner.setToolTipText(tooltip);
spinner.setName(bayType.toString());
setFieldSize(spinner, editorSize);
podSpinnerModels.put(bayType, model);
podSpinners.put(bayType, spinner);
add(spinner, gbc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import megamek.client.ui.Messages;
import megamek.client.ui.baseComponents.MMComboBox;
import megamek.client.ui.swing.CommonSettingsDialog;
import megamek.client.ui.swing.GUIPreferences;
import megamek.client.ui.swing.HelpDialog;
import megamek.common.preference.PreferenceManager;
import megameklab.ui.MMLStartUp;
Expand All @@ -35,9 +36,7 @@
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.*;

/**
* A panel allowing to change MML's general preferences
Expand All @@ -49,6 +48,7 @@ public class MiscSettingsPanel extends JPanel {
private final JCheckBox chkSummaryFormatTRO = new JCheckBox();
private final JCheckBox chkSkipSavePrompts = new JCheckBox();
private final JTextField txtUserDir = new JTextField(20);
private final JSlider guiScale = new JSlider();

MiscSettingsPanel(JFrame parent) {
startUpMMComboBox.setRenderer(startUpRenderer);
Expand Down Expand Up @@ -99,13 +99,33 @@ public class MiscSettingsPanel extends JPanel {
chkSkipSavePrompts.setToolTipText(resources.getString("ConfigurationDialog.chkSkipSavePrompts.tooltip"));
chkSkipSavePrompts.setSelected(CConfig.getBooleanParam(CConfig.MISC_SKIP_SAFETY_PROMPTS));

guiScale.setMajorTickSpacing(3);
guiScale.setMinimum(7);
guiScale.setMaximum(24);
Hashtable<Integer, JComponent> table = new Hashtable<>();
table.put(7, new JLabel("70%"));
table.put(10, new JLabel("100%"));
table.put(16, new JLabel("160%"));
table.put(22, new JLabel("220%"));
guiScale.setLabelTable(table);
guiScale.setPaintTicks(true);
guiScale.setPaintLabels(true);
guiScale.setValue((int) (GUIPreferences.getInstance().getGUIScale() * 10));
guiScale.setToolTipText(Messages.getString("CommonSettingsDialog.guiScaleTT"));
JLabel guiScaleLabel = new JLabel(Messages.getString("CommonSettingsDialog.guiScale"));
JPanel scaleLine = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
scaleLine.add(guiScaleLabel);
scaleLine.add(Box.createHorizontalStrut(5));
scaleLine.add(guiScale);

JPanel gridPanel = new JPanel(new SpringLayout());
gridPanel.add(startUpLine);
gridPanel.add(userDirLine);
gridPanel.add(chkSummaryFormatTRO);
gridPanel.add(chkSkipSavePrompts);
gridPanel.add(scaleLine);

SpringUtilities.makeCompactGrid(gridPanel, 4, 1, 0, 0, 15, 10);
SpringUtilities.makeCompactGrid(gridPanel, 5, 1, 0, 0, 15, 10);
gridPanel.setBorder(new EmptyBorder(20, 30, 20, 30));
setLayout(new FlowLayout(FlowLayout.LEFT));
add(gridPanel);
Expand All @@ -119,14 +139,18 @@ Map<String, String> getMiscSettings() {
? MMLStartUp.SPLASH_SCREEN
: startUpMMComboBox.getSelectedItem();
miscSettings.put(CConfig.MISC_STARTUP, startUp.name());
// The user directory is stored in MM's client settings, not in CConfig, therefore not added here
// User directory and gui scale are stored in MM's client settings, not in CConfig, therefore not added here
return miscSettings;
}

String getUserDir() {
return txtUserDir.getText();
}

float guiScale() {
return 0.1f * guiScale.getValue();
}

DefaultListCellRenderer startUpRenderer = new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Expand Down
Loading