Skip to content

Commit

Permalink
fix bug on reset button, reset font size when canvas settings is reset
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed Jul 16, 2024
1 parent 2cf75a4 commit 7e84549
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ public class SideMenuController {
""";

private SideMenuPanel sidebar;
private AppState app;

public SideMenuController(AppState app, SideMenuPanel sidebar) {
this.app = app;
this.sidebar = sidebar;

// set default values in canvas settings
sidebar.getGridLinesCheckBox().setSelected(app.getCanvasModel().isGuidelinesEnabled());
sidebar.getAxesCheckBox().setSelected(app.getCanvasModel().isAxesVisible());
sidebar.getFontSizeSelector().setSelectedItem(String.format("%d", app.getCanvasModel().getLabelFontSize()));
refreshCanvasSettingsUI();

// Initialize button listeners
initializeButtonListeners(app);
Expand All @@ -55,6 +54,16 @@ public void mouseMoved(MouseEvent e) {
});
}

/**
* Updates default values in canvas settings to match values from canvas model
*/
public void refreshCanvasSettingsUI() {
// set default values in canvas settings
sidebar.getGridLinesCheckBox().setSelected(app.getCanvasModel().isGuidelinesEnabled());
sidebar.getAxesCheckBox().setSelected(app.getCanvasModel().isAxesVisible());
sidebar.getFontSizeSelector().setSelectedItem(String.format("%d", app.getCanvasModel().getLabelFontSize()));
}

private void initializeButtonListeners(AppState app) {
CanvasModel canvasModel = app.getCanvasModel();

Expand Down Expand Up @@ -123,17 +132,27 @@ public void mousePressed(MouseEvent e) {
JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);

if (confirmation == JOptionPane.YES_OPTION) {
boolean settingsChanged = false;
// Reset guidelines checkbox and model
if (sidebar.getGridLinesCheckBox().isSelected()) {
sidebar.getGridLinesCheckBox().setSelected(false);
if (!canvasModel.isGuidelinesEnabled()) {
settingsChanged = true;
canvasModel.setGuidelinesEnabled(true);
}

// Reset axes checkbox and model
if (sidebar.getAxesCheckBox().isSelected()) {
sidebar.getAxesCheckBox().setSelected(false);
if (!canvasModel.isAxesVisible()) {
settingsChanged = true;
canvasModel.setAxesVisible(true);
}

// Reset font size
if (canvasModel.getLabelFontSize() != CanvasModel.DEFAULT_LABEL_FONT_SIZE) {
settingsChanged = true;
canvasModel.setLabelFontSize(CanvasModel.DEFAULT_LABEL_FONT_SIZE);
}

if (settingsChanged)
refreshCanvasSettingsUI();
}
});
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/github/creme332/model/CanvasModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ public class CanvasModel {
*/
int cellSize = Math.max(MIN_CELL_SIZE, Math.min(DEFAULT_CELL_SIZE, MAX_CELL_SIZE));

public static final int DEFAULT_LABEL_FONT_SIZE = 20;

/**
* Font size of labels on canvas in pixels.
*/
private int labelFontSize = 20;
private int labelFontSize = DEFAULT_LABEL_FONT_SIZE;

public static final Color labelForegroundColor = new Color(82, 82, 82);

Expand Down

0 comments on commit 7e84549

Please sign in to comment.