Skip to content

Commit

Permalink
Merge pull request #97 from AY2324S2-CS2103T-W08-3/light-theme
Browse files Browse the repository at this point in the history
Added Light theme
  • Loading branch information
wilsonwid authored Apr 5, 2024
2 parents e6b8709 + 9fda39c commit aaa6e84
Show file tree
Hide file tree
Showing 19 changed files with 384 additions and 39 deletions.
21 changes: 19 additions & 2 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ Shows a message explaining how to access the help page. The help page is located

Format: `help`

### Changing the theme : `theme`

Changes the theme of the application. Useful if you wish to change the appearance of the application. Currently supports `light` and `dark` themes.

![light theme](images/lightTheme.png)

Format: `theme THEME_NAME`

Examples:
* `theme light`
* `theme dark`


### Adding a person: `add`

Expand Down Expand Up @@ -164,6 +176,10 @@ Format: `find_and_export TAG [n/NAME] [a/ADDRESS] [f/FILENAME]`
Examples:
* `find_and_export cs2103t`
* `find_and_export cs2103t n/john a/olive street 42 f/output1.json`
* `find_and_export cs2103t n/john a/olive street 42 f/output1.csv`

Tip: `FILENAME` is optional. If not provided, has to be a valid filename with a `.json` or `.csv` extension.


### Importing a datafile: `import`

Expand All @@ -175,8 +191,9 @@ Format: `import f/FILENAME_1 [f/FILENAME_2] [f/FILENAME_3] ...`

Examples:
* `import f/export.json`
* `import f/contacts_export.json`
* `import f/contacts_export1.json f/contacts_export2.json`
* `import f/contactsExport.csv`
* `import f/contactsExport1.json f/contacts_export2.csv`


### Deleting a person : `delete`

Expand Down
Binary file added docs/images/lightTheme.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 4 additions & 5 deletions src/main/java/scm/address/commons/core/GuiSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Objects;

import scm.address.commons.util.ToStringBuilder;
import scm.address.model.theme.ThemeCollection;

/**
* A Serializable class that contains the GUI settings.
Expand All @@ -15,9 +16,7 @@ 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 static final String DEFAULT_THEME_NAME = ThemeCollection.getDarkTheme().getThemeName();
private final double windowWidth;
private final double windowHeight;
private final Point windowCoordinates;
Expand All @@ -30,7 +29,7 @@ public GuiSettings() {
windowWidth = DEFAULT_WIDTH;
windowHeight = DEFAULT_HEIGHT;
windowCoordinates = null; // null represent no coordinates
theme = THEME_LIGHT;
theme = DEFAULT_THEME_NAME;
}

/**
Expand All @@ -39,7 +38,7 @@ public GuiSettings() {
public GuiSettings(double windowWidth, double windowHeight, int xPosition, int yPosition, String theme) {
this.windowWidth = windowWidth;
this.windowHeight = windowHeight;
windowCoordinates = new Point(xPosition, yPosition);
this.windowCoordinates = new Point(xPosition, yPosition);
this.theme = theme;
}

Expand Down
18 changes: 12 additions & 6 deletions src/main/java/scm/address/logic/commands/ThemeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import scm.address.commons.core.GuiSettings;
import scm.address.logic.commands.exceptions.CommandException;
import scm.address.model.Model;
import scm.address.model.theme.Theme;


/**
Expand All @@ -20,24 +21,29 @@ public class ThemeCommand extends Command {
+ "Example: " + COMMAND_WORD + " light\n"
+ "Supported themes: light, dark";
public static final String MESSAGE_INVALID_THEME = "Invalid theme value";
private final String themeName;
private final Theme theme;

/**
* Creates a ThemeCommand to change the theme of the application.
*
* @param themeName The name of the theme to change to.
* @param theme The theme to change to.
*/
public ThemeCommand(String themeName) {
assert themeName != null;
this.themeName = themeName;
public ThemeCommand(Theme theme) {
assert theme != null;
this.theme = theme;
}

public Theme getTheme() {
return theme;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
GuiSettings guiSettings = model.getGuiSettings();

model.setGuiSettings(new GuiSettings(guiSettings.getWindowWidth(), guiSettings.getWindowHeight(),
guiSettings.getWindowCoordinates().x, guiSettings.getWindowCoordinates().y, themeName));
guiSettings.getWindowCoordinates().x, guiSettings.getWindowCoordinates().y, theme.getThemeName()));

return new CommandResult(String.format(MESSAGE_SUCCESS), false, false, true);
}
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/scm/address/logic/parser/ThemeCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import scm.address.logic.commands.ThemeCommand;
import scm.address.logic.parser.exceptions.ParseException;
import scm.address.model.theme.Theme;
import scm.address.model.theme.ThemeCollection;


/**
Expand All @@ -19,7 +21,7 @@ public ThemeCommand parse(String args) throws ParseException {

String themeValue = argMultimap.getPreamble().trim();

String theme = "";
Theme theme;
try {
theme = parseTheme(themeValue);
} catch (ParseException e) {
Expand All @@ -35,11 +37,17 @@ public ThemeCommand parse(String args) throws ParseException {
* @return The theme.
* @throws ParseException If the theme value is invalid.
*/
private String parseTheme(String theme) throws ParseException {
if (theme.equals("light") || theme.equals("dark")) {
return theme;
} else {
throw new ParseException("Invalid theme value");
private Theme parseTheme(String theme) throws ParseException {
String temp = theme.trim().toLowerCase();

switch (temp) {
case "light":
return ThemeCollection.getLightTheme();
case "dark":
return ThemeCollection.getDarkTheme();
default:
//Message does not matter as exception is caught and rethrown
throw new ParseException(ThemeCommand.MESSAGE_INVALID_THEME);
}
}
}
36 changes: 36 additions & 0 deletions src/main/java/scm/address/model/theme/DarkTheme.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package scm.address.model.theme;

import com.fasterxml.jackson.annotation.JsonProperty;


/**
* Represents the Dark Theme of the application.
*/
class DarkTheme implements Theme {
private static final String DARK_THEME_NAME = "dark";
private static final String DARK_THEME_CSS_PATH = "/view/DarkTheme.css";
private static final String DARK_THEME_EXTENSIONS_CSS_PATH = "/view/Extensions.css";
@JsonProperty
private String theme = "dark";

@Override
public String getThemeName() {
return DARK_THEME_NAME;
}

@Override
public String getThemeCssPath() {
return DARK_THEME_CSS_PATH;
}

@Override
public String getThemeExtensionsCssPath() {
return DARK_THEME_EXTENSIONS_CSS_PATH;
}

@Override
public boolean equals(Object obj) {
return obj instanceof DarkTheme;
}

}
31 changes: 31 additions & 0 deletions src/main/java/scm/address/model/theme/LightTheme.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package scm.address.model.theme;

/**
* Represents the Light Theme of the application.
*/
class LightTheme implements Theme {
private static final String LIGHT_THEME_NAME = "light";
private static final String LIGHT_THEME_CSS_PATH = "/view/LightTheme.css";
private static final String LIGHT_THEME_EXTENSIONS_CSS_PATH = "/view/Extensions.css";


@Override
public String getThemeName() {
return LIGHT_THEME_NAME;
}

@Override
public String getThemeCssPath() {
return LIGHT_THEME_CSS_PATH;
}

@Override
public String getThemeExtensionsCssPath() {
return LIGHT_THEME_EXTENSIONS_CSS_PATH;
}

@Override
public boolean equals(Object obj) {
return obj instanceof LightTheme;
}
}
10 changes: 10 additions & 0 deletions src/main/java/scm/address/model/theme/Theme.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package scm.address.model.theme;

/**
* Represents a Theme in the application.
*/
public interface Theme {
String getThemeName();
String getThemeCssPath();
String getThemeExtensionsCssPath();
}
30 changes: 30 additions & 0 deletions src/main/java/scm/address/model/theme/ThemeCollection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package scm.address.model.theme;

/**
* Contains the collection of themes available in the application.
*/
public class ThemeCollection {
public static final String MESSAGE_INVALID_THEME_NAME = "Invalid theme name provided.";
private static final Theme darkTheme = new DarkTheme();
private static final Theme lightTheme = new LightTheme();

public static Theme getDarkTheme() {
return darkTheme;
}

public static Theme getLightTheme() {
return lightTheme;
}

public static Theme getTheme(String themeName) throws IllegalArgumentException {
String temp = themeName.toLowerCase();

if (temp.equals(darkTheme.getThemeName())) {
return darkTheme;
} else if (temp.equals(lightTheme.getThemeName())) {
return lightTheme;
} else {
throw new IllegalArgumentException(MESSAGE_INVALID_THEME_NAME);
}
}
}
39 changes: 29 additions & 10 deletions src/main/java/scm/address/ui/MainWindow.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package scm.address.ui;

import static java.util.Objects.requireNonNull;

import java.util.logging.Logger;

import javafx.event.ActionEvent;
Expand All @@ -17,6 +19,8 @@
import scm.address.logic.commands.CommandResult;
import scm.address.logic.commands.exceptions.CommandException;
import scm.address.logic.parser.exceptions.ParseException;
import scm.address.model.theme.Theme;
import scm.address.model.theme.ThemeCollection;

/**
* The Main Window. Provides the basic application layout containing
Expand Down Expand Up @@ -206,27 +210,42 @@ private CommandResult executeCommand(String commandText) throws CommandException
}
}

/**
* Changes the theme of the application.
*/
// GCOVR_EXCL_START
// This method cannot be tested as it works on the UI. It is excluded from testing.
private void handleChangeTheme() {

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

if (theme.equals("light")) {
setCss("view/LightTheme.css");
} else if (theme.equals("dark")) {
setCss("view/DarkTheme.css");
try {
Theme theme = ThemeCollection.getTheme(themeName);
setTheme(theme);
} catch (Exception e) {
logger.warning("Error changing theme: " + e.getMessage());
resultDisplay.setFeedbackToUser("Error changing theme: " + e.getMessage());
}
}
// GCOVR_EXCL_STOP

/**
* Changes the CSS of the application.
* Changes the CSS of the application
*
* @param cssFilePath The file path of the CSS file.
* @param theme The theme to be set
*/
public void setCss(String cssFilePath) {
System.out.println(cssFilePath);
// GCOVR_EXCL_START
// This method cannot be tested as it works on the UI. It is excluded from testing.
private void setTheme(Theme theme) {
requireNonNull(theme);

String cssFilePath = theme.getThemeCssPath();
String extensionsCssPath = theme.getThemeExtensionsCssPath();

Scene scene = primaryStage.getScene();
scene.getStylesheets().clear();
scene.getStylesheets().add(cssFilePath);
scene.getStylesheets().add(EXTENSIONS_CSS_FILE_PATH);
scene.getStylesheets().add(extensionsCssPath);
}
// GCOVR_EXCL_STOP
}
4 changes: 2 additions & 2 deletions src/main/resources/view/LightTheme.css
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,11 @@
}

.scroll-bar {
-fx-background-color: derive(#99A98F, 20%);
-fx-background-color: derive(#e4e5f1, 20%);
}

.scroll-bar .thumb {
-fx-background-color: derive(#D6E8DB, 50%);
-fx-background-color: derive(#484b6a, 50%);
-fx-background-insets: 3;
}

Expand Down
Loading

0 comments on commit aaa6e84

Please sign in to comment.