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 all 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
28 changes: 25 additions & 3 deletions megameklab/src/megameklab/MegaMekLab.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +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 io.sentry.Sentry;
import megamek.MMLoggingConstants;
Expand Down Expand Up @@ -99,7 +101,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,11 +129,31 @@ private static void setLookAndFeel() {
try {
String plaf = CConfig.getParam(CConfig.GUI_PLAF, UIManager.getSystemLookAndFeelClassName());
UIManager.setLookAndFeel(plaf);
updateAfterUiChange();
} catch (Exception ex) {
logger.error("setLookAndFeel() Exception {}", 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 @@ -608,9 +608,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 @@ -636,7 +637,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
4 changes: 2 additions & 2 deletions megameklab/src/megameklab/ui/battleArmor/BACriticalView.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* The Crit Slots view for a single suit of BattleArmor
*
* Original author - jtighe ([email protected])
*
*
* @author arlith
* @author Simon (Juliez)
*/
Expand Down Expand Up @@ -159,7 +159,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 @@ -139,10 +139,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 @@ -178,10 +177,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 @@ -190,10 +188,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 @@ -202,10 +199,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 @@ -214,10 +210,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 @@ -226,10 +221,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 @@ -240,11 +234,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 @@ -256,7 +249,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 @@ -193,6 +193,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
Loading