Skip to content

Commit

Permalink
Add new theme parameter to GuiSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
tah5in committed Apr 4, 2024
1 parent 9cb2f34 commit 36343c3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
18 changes: 15 additions & 3 deletions src/main/java/scm/address/commons/core/GuiSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ public class GuiSettings implements Serializable {
private static final double DEFAULT_HEIGHT = 600;
private static final double DEFAULT_WIDTH = 740;

private static final String THEME_LIGHT = "light";
private static final String THEME_DARK = "dark";

private final double windowWidth;
private final double windowHeight;
private final Point windowCoordinates;
private final String theme;

/**
* Constructs a {@code GuiSettings} with the default height, width and position.
Expand All @@ -26,15 +30,17 @@ public GuiSettings() {
windowWidth = DEFAULT_WIDTH;
windowHeight = DEFAULT_HEIGHT;
windowCoordinates = null; // null represent no coordinates
theme = THEME_LIGHT;
}

/**
* Constructs a {@code GuiSettings} with the specified height, width and position.
*/
public GuiSettings(double windowWidth, double windowHeight, int xPosition, int yPosition) {
public GuiSettings(double windowWidth, double windowHeight, int xPosition, int yPosition, String theme) {
this.windowWidth = windowWidth;
this.windowHeight = windowHeight;
windowCoordinates = new Point(xPosition, yPosition);
this.theme = theme;
}

public double getWindowWidth() {
Expand All @@ -49,6 +55,10 @@ public Point getWindowCoordinates() {
return windowCoordinates != null ? new Point(windowCoordinates) : null;
}

public String getTheme() {
return theme;
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand All @@ -63,12 +73,13 @@ public boolean equals(Object other) {
GuiSettings otherGuiSettings = (GuiSettings) other;
return windowWidth == otherGuiSettings.windowWidth
&& windowHeight == otherGuiSettings.windowHeight
&& Objects.equals(windowCoordinates, otherGuiSettings.windowCoordinates);
&& Objects.equals(windowCoordinates, otherGuiSettings.windowCoordinates)
&& theme.equals(otherGuiSettings.theme);
}

@Override
public int hashCode() {
return Objects.hash(windowWidth, windowHeight, windowCoordinates);
return Objects.hash(windowWidth, windowHeight, windowCoordinates, theme);
}

@Override
Expand All @@ -77,6 +88,7 @@ public String toString() {
.add("windowWidth", windowWidth)
.add("windowHeight", windowHeight)
.add("windowCoordinates", windowCoordinates)
.add("theme", theme)
.toString();
}
}
8 changes: 6 additions & 2 deletions src/main/java/scm/address/logic/commands/ThemeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.requireNonNull;

import scm.address.commons.core.GuiSettings;
import scm.address.logic.commands.exceptions.CommandException;
import scm.address.model.Model;

Expand Down Expand Up @@ -34,7 +35,10 @@ public ThemeCommand(String themeName) {
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
model.setTheme(themeName);
return new CommandResult(String.format(MESSAGE_SUCCESS));
GuiSettings guiSettings = model.getGuiSettings();
model.setGuiSettings(new GuiSettings(guiSettings.getWindowWidth(), guiSettings.getWindowHeight(),
guiSettings.getWindowCoordinates().x, guiSettings.getWindowCoordinates().y, themeName));

return new CommandResult(String.format(MESSAGE_SUCCESS), false, false, true);
}
}
22 changes: 19 additions & 3 deletions src/main/java/scm/address/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
public class MainWindow extends UiPart<Stage> {

private static final String FXML = "MainWindow.fxml";
private static final String EXTENSIONS_CSS_FILE_PATH = "/view/Extensions.css";

private final Logger logger = LogsCenter.getLogger(getClass());

Expand Down Expand Up @@ -164,7 +165,7 @@ void show() {
@FXML
private void handleExit() {
GuiSettings guiSettings = new GuiSettings(primaryStage.getWidth(), primaryStage.getHeight(),
(int) primaryStage.getX(), (int) primaryStage.getY());
(int) primaryStage.getX(), (int) primaryStage.getY(), logic.getGuiSettings().getTheme());
logic.setGuiSettings(guiSettings);
helpWindow.hide();
primaryStage.hide();
Expand Down Expand Up @@ -193,6 +194,10 @@ private CommandResult executeCommand(String commandText) throws CommandException
handleExit();
}

if (commandResult.isChangeTheme()) {
handleChangeTheme();
}

return commandResult;
} catch (CommandException | ParseException e) {
logger.info("An error occurred while executing command: " + commandText);
Expand All @@ -201,16 +206,27 @@ private CommandResult executeCommand(String commandText) throws CommandException
}
}

private void handleChangeTheme() {

String theme = logic.getGuiSettings().getTheme();

if (theme.equals("light")) {
setCss("view/LightTheme.css");
} else if (theme.equals("dark")) {
setCss("view/DarkTheme.css");
}
}

/**
* Changes the CSS of the application.
*
* @param cssFilePath The file path of the CSS file.
*/
public void setCss(String cssFilePath) {
System.out.println(cssFilePath);
//String css = this.getClass().getResource(cssFilePath).toExternalForm();
Scene scene = primaryStage.getScene();
// scene.getStylesheets().clear();
scene.getStylesheets().clear();
scene.getStylesheets().add(cssFilePath);
scene.getStylesheets().add(EXTENSIONS_CSS_FILE_PATH);
}
}
3 changes: 0 additions & 3 deletions src/main/java/scm/address/ui/UiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ public void start(Stage primaryStage) {
mainWindow = new MainWindow(primaryStage, logic);
mainWindow.show(); //This should be called before creating other UI parts
mainWindow.fillInnerParts();

//have to call this to change theme.
mainWindow.setCss("view/LightTheme.css");
} catch (Throwable e) {
logger.severe(StringUtil.getDetails(e));
showFatalErrorDialogAndShutdown("Fatal error during initializing", e);
Expand Down

0 comments on commit 36343c3

Please sign in to comment.