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

Add FlatLaf GUi scaling to MHQ #4990

Merged
merged 7 commits into from
Oct 11, 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
30 changes: 23 additions & 7 deletions MekHQ/src/mekhq/MekHQ.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
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 @@ -173,6 +174,8 @@ 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);
Expand Down Expand Up @@ -667,13 +670,7 @@ private static void setLookAndFeel(String themeName) {
addOSXKeyStrokes((InputMap) UIManager.get("TextArea.focusInputMap"));
}

for (final Frame frame : Frame.getFrames()) {
SwingUtilities.updateComponentTreeUI(frame);
}

for (Window window : Window.getWindows()) {
SwingUtilities.updateComponentTreeUI(window);
}
updateAfterUiChange();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
logger.error(e, "setLookAndFeel()");
Expand All @@ -682,6 +679,25 @@ private static void setLookAndFeel(String themeName) {
SwingUtilities.invokeLater(runnable);
}

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 {
@Override
public void propertyChange(PropertyChangeEvent evt) {
Expand Down
10 changes: 5 additions & 5 deletions MekHQ/src/mekhq/campaign/parts/AmmoStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* This will be a special type of part that will only exist as spares
* It will determine the amount of ammo of a particular type that
* is available
*
*
* @author Jay Lawson (jaylawson39 at yahoo.com)
*/
public class AmmoStorage extends EquipmentPart implements IAcquisitionWork {
Expand Down Expand Up @@ -130,7 +130,7 @@ public boolean isSamePartType(@Nullable Part part) {
/**
* Gets a value indicating whether or an {@code AmmoType} is
* the same as this instance's ammo.
*
*
* @param otherAmmoType The other {@code AmmoType}.
*/
public boolean isSameAmmoType(AmmoType otherAmmoType) {
Expand All @@ -142,7 +142,7 @@ public boolean isSameAmmoType(AmmoType otherAmmoType) {
/**
* Gets a value indicating whether or not an {@code AmmoType}
* is compatible with this instance's ammo.
*
*
* @param otherAmmoType The other {@code AmmoType}.
*/
public boolean isCompatibleAmmo(AmmoType otherAmmoType) {
Expand Down Expand Up @@ -229,7 +229,7 @@ public boolean needsFixing() {

@Override
public String getDesc() {
String toReturn = "<html><font size='2'";
String toReturn = "<html><font";
String scheduled = "";
if (getTech() != null) {
scheduled = " (scheduled) ";
Expand Down Expand Up @@ -284,7 +284,7 @@ public String failToFind() {

@Override
public String getAcquisitionDesc() {
String toReturn = "<html><font size='2'";
String toReturn = "<html><font";

toReturn += ">";
toReturn += "<b>" + getAcquisitionDisplayName() + "</b> " + getAcquisitionBonus() + "<br/>";
Expand Down
4 changes: 2 additions & 2 deletions MekHQ/src/mekhq/campaign/parts/Armor.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public String getDesc() {
if (getAllMods(null).getValue() > -1) {
bonus = '+' + bonus;
}
String toReturn = "<html><font size='3'";
String toReturn = "<html><font";

String scheduled = "";
if (getTech() != null) {
Expand Down Expand Up @@ -474,7 +474,7 @@ public boolean onBadHipOrShoulder() {

@Override
public String getAcquisitionDesc() {
String toReturn = "<html><font size='2'";
String toReturn = "<html><font";

toReturn += ">";
toReturn += "<b>" + getAcquisitionDisplayName() + "</b> " + getAcquisitionBonus() + "<br/>";
Expand Down
2 changes: 1 addition & 1 deletion MekHQ/src/mekhq/campaign/parts/MekLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ public String getDesc() {
if ((!isBreached() && !isBlownOff()) || isSalvaging()) {
return super.getDesc();
}
String toReturn = "<html><font size='3'";
String toReturn = "<html><font";
String scheduled = "";
if (getTech() != null) {
scheduled = " (scheduled) ";
Expand Down
4 changes: 2 additions & 2 deletions MekHQ/src/mekhq/campaign/parts/MissingPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public String getDesc() {
if (getAllMods(null).getValue() > -1) {
bonus = '+' + bonus;
}
String toReturn = "<html><font size='3'";
String toReturn = "<html><font";
String scheduled = "";
if (getTech() != null) {
scheduled = " (scheduled) ";
Expand Down Expand Up @@ -278,7 +278,7 @@ public TargetRoll getAllAcquisitionMods() {

@Override
public String getAcquisitionDesc() {
String toReturn = "<html><font size='2'";
String toReturn = "<html><font";

toReturn += ">";
toReturn += "<b>" + getAcquisitionDisplayName() + "</b> " + getAcquisitionBonus() + "<br/>";
Expand Down
2 changes: 1 addition & 1 deletion MekHQ/src/mekhq/campaign/parts/Part.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public String getDesc() {
if (getAllMods(null).getValue() > -1) {
bonus = '+' + bonus;
}
String toReturn = "<html><font size='3'";
String toReturn = "<html><font";
String action = "Repair ";
if (isSalvaging()) {
action = "Salvage ";
Expand Down
2 changes: 1 addition & 1 deletion MekHQ/src/mekhq/campaign/parts/PodSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public String getDesc() {
if (getAllMods(null).getValue() > -1) {
bonus = '+' + bonus;
}
String toReturn = "<html><font size='3'";
String toReturn = "<html><font";
String action = "Replace ";
if (isSalvaging()) {
action = "Salvage ";
Expand Down
2 changes: 1 addition & 1 deletion MekHQ/src/mekhq/campaign/parts/ProtoMekLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ public String getDesc() {
if ((!isBreached() && !isBlownOff()) || isSalvaging()) {
return super.getDesc();
}
String toReturn = "<html><font size='3'";
String toReturn = "<html><font";
String scheduled = "";
if (getTech() != null) {
scheduled = " (scheduled) ";
Expand Down
2 changes: 1 addition & 1 deletion MekHQ/src/mekhq/campaign/parts/TankLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public String getDesc() {
if (!isBreached() || isSalvaging()) {
return super.getDesc();
}
String toReturn = "<html><font size='2'";
String toReturn = "<html><font";
String scheduled = "";
if (getTech() != null) {
scheduled = " (scheduled) ";
Expand Down
4 changes: 2 additions & 2 deletions MekHQ/src/mekhq/campaign/parts/equipment/AmmoBin.java
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ public String getDesc() {
if (isSalvaging()) {
return super.getDesc();
}
String toReturn = "<html><font size='2'";
String toReturn = "<html><font";
String scheduled = "";
if (getTech() != null) {
scheduled = " (scheduled) ";
Expand Down Expand Up @@ -557,7 +557,7 @@ public boolean isEnoughSpareAmmoAvailable() {

@Override
public String getAcquisitionDesc() {
String toReturn = "<html><font size='2'";
String toReturn = "<html><font";

toReturn += ">";
toReturn += "<b>" + getAcquisitionDisplayName() + "</b> " + getAcquisitionBonus() + "<br/>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public AmmoStorage getNewPart() {

@Override
public String getAcquisitionDesc() {
String toReturn = "<html><font size='2'";
String toReturn = "<html><font";

toReturn += ">";
toReturn += "<b>" + getAcquisitionDisplayName() + "</b> " + getAcquisitionBonus() + "<br/>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public String getDesc() {
if (shotsNeeded >= 0) {
return super.getDesc();
}
String toReturn = "<html><font size='2'";
String toReturn = "<html><font";
String scheduled = "";
if (getTech() != null) {
scheduled = " (scheduled) ";
Expand Down
4 changes: 2 additions & 2 deletions MekHQ/src/mekhq/gui/BasicInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ public BasicInfo() {
}

public void setText(String s, String color) {
lblImage.setText("<html><font size='2' color='" + color + "'>" + s + "</font></html>");
lblImage.setText("<html><font color='" + color + "'>" + s + "</font></html>");
}

public void setText(String s) {
lblImage.setText("<html><font size='2'>" + s + "</font></html>");
lblImage.setText("<html>" + s + "</html>");
}

public void setHtmlText(String s) {
Expand Down
39 changes: 19 additions & 20 deletions MekHQ/src/mekhq/gui/CampaignGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import megamek.client.generator.RandomUnitGenerator;
import megamek.client.ui.preferences.JWindowPreference;
import megamek.client.ui.preferences.PreferencesNode;
import megamek.client.ui.swing.GUIPreferences;
import megamek.client.ui.swing.GameOptionsDialog;
import megamek.client.ui.swing.UnitLoadingDialog;
import megamek.client.ui.swing.dialog.AbstractUnitSelectorDialog;
Expand Down Expand Up @@ -1061,7 +1062,7 @@ private void initTopButtons() {
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = GridBagConstraints.NONE;
gridBagConstraints.weightx = 0.0;
gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = 0.0;
gridBagConstraints.gridheight = 2;
gridBagConstraints.anchor = GridBagConstraints.WEST;
Expand All @@ -1072,14 +1073,11 @@ private void initTopButtons() {
btnGMMode.setToolTipText(resourceMap.getString("btnGMMode.toolTipText"));
btnGMMode.setSelected(getCampaign().isGM());
btnGMMode.addActionListener(e -> getCampaign().setGMMode(btnGMMode.isSelected()));
btnGMMode.setMinimumSize(new Dimension(150, 25));
btnGMMode.setPreferredSize(new Dimension(150, 25));
btnGMMode.setMaximumSize(new Dimension(150, 25));
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = GridBagConstraints.NONE;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 0;
gridBagConstraints.weighty = 0.0;
gridBagConstraints.anchor = GridBagConstraints.EAST;
gridBagConstraints.insets = new Insets(3, 3, 3, 3);
Expand All @@ -1088,25 +1086,22 @@ private void initTopButtons() {
btnOvertime = new JToggleButton(resourceMap.getString("btnOvertime.text"));
btnOvertime.setToolTipText(resourceMap.getString("btnOvertime.toolTipText"));
btnOvertime.addActionListener(evt -> getCampaign().setOvertime(btnOvertime.isSelected()));
btnOvertime.setMinimumSize(new Dimension(150, 25));
btnOvertime.setPreferredSize(new Dimension(150, 25));
btnOvertime.setMaximumSize(new Dimension(150, 25));
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = GridBagConstraints.NONE;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 0;
gridBagConstraints.weighty = 0.0;
gridBagConstraints.anchor = GridBagConstraints.EAST;
gridBagConstraints.insets = new Insets(3, 3, 3, 3);
btnPanel.add(btnOvertime, gridBagConstraints);

// This button uses a mnemonic that is unique and listed in the initMenu JavaDoc
JButton btnAdvanceDay = new JButton(resourceMap.getString("btnAdvanceDay.text"));
String padding = " ";
JButton btnAdvanceDay = new JButton(padding + resourceMap.getString("btnAdvanceDay.text") + padding);
btnAdvanceDay.setToolTipText(resourceMap.getString("btnAdvanceDay.toolTipText"));
btnAdvanceDay.addActionListener(evt -> getCampaignController().advanceDay());
btnAdvanceDay.setMnemonic(KeyEvent.VK_A);
btnAdvanceDay.setPreferredSize(new Dimension(250, 50));
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 0;
Expand Down Expand Up @@ -1254,14 +1249,18 @@ private void refreshThemeChoices() {
menuThemes.removeAll();
JCheckBoxMenuItem miPlaf;
for (LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
miPlaf = new JCheckBoxMenuItem(laf.getName());
if (laf.getClassName().equalsIgnoreCase(MekHQ.getSelectedTheme().getValue())) {
miPlaf.setSelected(true);
}
// intentionally only limits the themes in the menu; as a last resort for GUI problems, other laf can be used by
// hand-editing mhq.preferences
if (GUIPreferences.isSupportedLookAndFeel(laf)) {
miPlaf = new JCheckBoxMenuItem(laf.getName());
if (laf.getClassName().equalsIgnoreCase(MekHQ.getSelectedTheme().getValue())) {
miPlaf.setSelected(true);
}

menuThemes.add(miPlaf);
miPlaf.setActionCommand(laf.getClassName());
miPlaf.addActionListener(this::changeTheme);
menuThemes.add(miPlaf);
miPlaf.setActionCommand(laf.getClassName());
miPlaf.addActionListener(this::changeTheme);
}
}
}

Expand Down
16 changes: 14 additions & 2 deletions MekHQ/src/mekhq/gui/HangarTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
import megamek.client.ui.preferences.JComboBoxPreference;
import megamek.client.ui.preferences.JTablePreference;
import megamek.client.ui.preferences.PreferencesNode;
import megamek.client.ui.swing.GUIPreferences;
import megamek.client.ui.swing.util.UIUtil;
import megamek.common.Entity;
import megamek.common.UnitType;
import megamek.common.event.Subscribe;
import megamek.common.preference.IPreferenceChangeListener;
import megamek.common.util.sorter.NaturalOrderComparator;
import megamek.logging.MMLogger;
import mekhq.MekHQ;
Expand Down Expand Up @@ -71,6 +74,8 @@ public final class HangarTab extends CampaignGuiTab {
private UnitTableModel unitModel;
private TableRowSorter<UnitTableModel> unitSorter;

private final IPreferenceChangeListener scalingChangeListener = e -> changeUnitView();

private static final ResourceBundle resourceMap = ResourceBundle.getBundle("mekhq.resources.CampaignGUI",
MekHQ.getMHQOptions().getLocale());

Expand All @@ -79,6 +84,7 @@ public HangarTab(CampaignGUI gui, String name) {
super(gui, name);
MekHQ.registerHandler(this);
setUserPreferences();
GUIPreferences.getInstance().addPreferenceChangeListener(scalingChangeListener);
}
// endregion Constructors

Expand Down Expand Up @@ -291,7 +297,7 @@ public static String getUnitViewName(int group) {
public void changeUnitView() {
int view = choiceUnitView.getSelectedIndex();
XTableColumnModel columnModel = (XTableColumnModel) unitTable.getColumnModel();
unitTable.setRowHeight(15);
unitTable.setRowHeight(UIUtil.scaleForGUI(15));

// set the renderer
TableColumn column;
Expand All @@ -311,7 +317,7 @@ public void changeUnitView() {
}

if (view == UV_GRAPHIC) {
unitTable.setRowHeight(80);
unitTable.setRowHeight(UIUtil.scaleForGUI(60));
columnModel.setColumnVisible(columnModel.getColumnByModelIndex(UnitTableModel.COL_NAME), false);
columnModel.setColumnVisible(columnModel.getColumnByModelIndex(UnitTableModel.COL_TYPE), false);
columnModel.setColumnVisible(columnModel.getColumnByModelIndex(UnitTableModel.COL_WCLASS), true);
Expand Down Expand Up @@ -404,6 +410,12 @@ public void changeUnitView() {
}
}

@Override
public void disposeTab() {
super.disposeTab();
GUIPreferences.getInstance().removePreferenceChangeListener(scalingChangeListener);
}

public void focusOnUnit(UUID id) {
splitUnit.resetToPreferredSizes();
int row = -1;
Expand Down
Loading