Skip to content

Commit 4e3d245

Browse files
authored
Merge pull request #103 from CodeDead/feature/more_themes
feature/more_themes
2 parents 3bd564c + edc5f6e commit 4e3d245

File tree

5 files changed

+68
-46
lines changed

5 files changed

+68
-46
lines changed

src/main/java/com/codedead/opal/OpalApplication.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.codedead.opal;
22

3-
import atlantafx.base.theme.*;
3+
import com.codedead.opal.controller.ThemeController;
44
import com.codedead.opal.controller.UpdateController;
55
import com.codedead.opal.utils.FxUtils;
66
import com.codedead.opal.utils.SharedVariables;
@@ -83,16 +83,7 @@ public void start(final Stage primaryStage) {
8383
final Properties properties = settingsController.getProperties();
8484
final String languageTag = properties.getProperty("locale", DEFAULT_LOCALE);
8585

86-
final String theme = properties.getProperty("theme", "light");
87-
switch (theme.toLowerCase()) {
88-
case "nordlight" -> Application.setUserAgentStylesheet(new NordLight().getUserAgentStylesheet());
89-
case "norddark" -> Application.setUserAgentStylesheet(new NordDark().getUserAgentStylesheet());
90-
case "dark" -> Application.setUserAgentStylesheet(new PrimerDark().getUserAgentStylesheet());
91-
case "cupertinodark" -> Application.setUserAgentStylesheet(new CupertinoDark().getUserAgentStylesheet());
92-
case "cuptertinolight" -> Application.setUserAgentStylesheet(new CupertinoLight().getUserAgentStylesheet());
93-
case "dracula" -> Application.setUserAgentStylesheet(new Dracula().getUserAgentStylesheet());
94-
default -> Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
95-
}
86+
ThemeController.setTheme(properties.getProperty("theme", "light"));
9687

9788
final Locale locale = Locale.forLanguageTag(languageTag);
9889
final ResourceBundle translationBundle = ResourceBundle.getBundle("translations.OpalApplication", locale);

src/main/java/com/codedead/opal/controller/MainWindowController.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package com.codedead.opal.controller;
22

3-
import atlantafx.base.theme.*;
43
import com.codedead.opal.domain.*;
54
import com.codedead.opal.interfaces.IAudioTimer;
65
import com.codedead.opal.interfaces.IRunnableHelper;
76
import com.codedead.opal.interfaces.TrayIconListener;
87
import com.codedead.opal.utils.*;
98
import com.fasterxml.jackson.core.type.TypeReference;
109
import com.fasterxml.jackson.databind.ObjectMapper;
11-
import javafx.application.Application;
1210
import javafx.application.Platform;
1311
import javafx.fxml.FXML;
1412
import javafx.fxml.FXMLLoader;
@@ -407,17 +405,7 @@ private void settingsAction() {
407405
primaryStage.getIcons().add(new Image(Objects.requireNonNull(getClass().getResourceAsStream(SharedVariables.ICON_URL))));
408406
primaryStage.setScene(new Scene(root));
409407

410-
primaryStage.setOnHiding(event -> {
411-
switch (settingsController.getProperties().getProperty("theme", "Light").toLowerCase()) {
412-
case "nordlight" -> Application.setUserAgentStylesheet(new NordLight().getUserAgentStylesheet());
413-
case "norddark" -> Application.setUserAgentStylesheet(new NordDark().getUserAgentStylesheet());
414-
case "dark" -> Application.setUserAgentStylesheet(new PrimerDark().getUserAgentStylesheet());
415-
case "cupertinodark" -> Application.setUserAgentStylesheet(new CupertinoDark().getUserAgentStylesheet());
416-
case "cuptertinolight" -> Application.setUserAgentStylesheet(new CupertinoLight().getUserAgentStylesheet());
417-
case "dracula" -> Application.setUserAgentStylesheet(new Dracula().getUserAgentStylesheet());
418-
default -> Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
419-
}
420-
});
408+
primaryStage.setOnHiding(event -> ThemeController.setTheme(settingsController.getProperties().getProperty("theme", "Light").toLowerCase()));
421409

422410
logger.info("Showing the SettingsWindow");
423411
primaryStage.show();

src/main/java/com/codedead/opal/controller/SettingsWindowController.java

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,9 @@ public SettingsWindowController() {
7373
*/
7474
@FXML
7575
private void initialize() {
76-
cboTheme.getSelectionModel().selectedItemProperty().addListener((options, oldValue, newValue) -> {
77-
switch (cboTheme.getSelectionModel().getSelectedIndex()) {
78-
case 0 -> Application.setUserAgentStylesheet(new CupertinoDark().getUserAgentStylesheet());
79-
case 1 -> Application.setUserAgentStylesheet(new CupertinoLight().getUserAgentStylesheet());
80-
case 2 -> Application.setUserAgentStylesheet(new Dracula().getUserAgentStylesheet());
81-
case 4 -> Application.setUserAgentStylesheet(new PrimerDark().getUserAgentStylesheet());
82-
case 5 -> Application.setUserAgentStylesheet(new NordLight().getUserAgentStylesheet());
83-
case 6 -> Application.setUserAgentStylesheet(new NordDark().getUserAgentStylesheet());
84-
default -> Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
85-
}
86-
});
76+
cboTheme.getSelectionModel()
77+
.selectedItemProperty()
78+
.addListener((options, oldValue, newValue) -> ThemeController.setTheme(cboTheme.getValue()));
8779
}
8880

8981
/**
@@ -167,16 +159,6 @@ private void loadSettings() {
167159
default -> cboLogLevel.getSelectionModel().select(4);
168160
}
169161

170-
final int themeIndex = switch (settingsController.getProperties().getProperty("theme", "light").toLowerCase()) {
171-
case "cupertinodark" -> 0;
172-
case "cupertinolight" -> 1;
173-
case "dracula" -> 2;
174-
case "dark" -> 4;
175-
case "nordlight" -> 5;
176-
case "norddark" -> 6;
177-
default -> 3;
178-
};
179-
180162
final long correctDelay = switch (delayType) {
181163
case 0 -> TimeUnit.MILLISECONDS.toSeconds(timerDelay);
182164
case 1 -> TimeUnit.MILLISECONDS.toMinutes(timerDelay);
@@ -190,7 +172,7 @@ private void loadSettings() {
190172
chbTrayIcon.setSelected(Boolean.parseBoolean(settingsController.getProperties().getProperty("trayIcon", "false")));
191173
chbTimerApplicationShutdown.setSelected(Boolean.parseBoolean(settingsController.getProperties().getProperty("timerApplicationShutdown", "false")));
192174
cboDelayType.getSelectionModel().select(delayType);
193-
cboTheme.getSelectionModel().select(themeIndex);
175+
cboTheme.getSelectionModel().select(ThemeController.getThemeIndex(settingsController.getProperties().getProperty("theme", "light")));
194176
numDelay.setText(String.valueOf(correctDelay));
195177
chbTimerComputerShutdown.setSelected(Boolean.parseBoolean(settingsController.getProperties().getProperty("timerComputerShutdown", "false")));
196178

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.codedead.opal.controller;
2+
3+
import atlantafx.base.theme.*;
4+
import javafx.application.Application;
5+
import org.apache.logging.log4j.LogManager;
6+
import org.apache.logging.log4j.Logger;
7+
8+
public final class ThemeController {
9+
10+
private static final Logger logger = LogManager.getLogger(ThemeController.class);
11+
12+
/**
13+
* Initialize a new ThemeController
14+
*/
15+
private ThemeController() {
16+
// Default constructor
17+
}
18+
19+
/**
20+
* Change the application theme
21+
*
22+
* @param themeName The theme name
23+
*/
24+
public static void setTheme(final String themeName) {
25+
logger.info("Setting theme to {}", themeName);
26+
27+
switch (themeName.toLowerCase()) {
28+
case "modena" -> Application.setUserAgentStylesheet(Application.STYLESHEET_MODENA);
29+
case "caspian" -> Application.setUserAgentStylesheet(Application.STYLESHEET_CASPIAN);
30+
case "nordlight" -> Application.setUserAgentStylesheet(new NordLight().getUserAgentStylesheet());
31+
case "norddark" -> Application.setUserAgentStylesheet(new NordDark().getUserAgentStylesheet());
32+
case "dark" -> Application.setUserAgentStylesheet(new PrimerDark().getUserAgentStylesheet());
33+
case "cupertinodark" -> Application.setUserAgentStylesheet(new CupertinoDark().getUserAgentStylesheet());
34+
case "cuptertinolight" -> Application.setUserAgentStylesheet(new CupertinoLight().getUserAgentStylesheet());
35+
case "dracula" -> Application.setUserAgentStylesheet(new Dracula().getUserAgentStylesheet());
36+
default -> Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
37+
}
38+
}
39+
40+
/**
41+
* Get the theme index from the theme name
42+
*
43+
* @param themeName The theme name
44+
* @return The theme index
45+
*/
46+
public static int getThemeIndex(final String themeName) {
47+
return switch (themeName.toLowerCase()) {
48+
case "cupertinodark" -> 0;
49+
case "cupertinolight" -> 1;
50+
case "dracula" -> 2;
51+
case "dark" -> 4;
52+
case "nordlight" -> 5;
53+
case "norddark" -> 6;
54+
case "modena" -> 7;
55+
case "caspian" -> 8;
56+
default -> 3;
57+
};
58+
}
59+
}

src/main/resources/windows/SettingsWindow.fxml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@
116116
<String fx:value="Dark"/>
117117
<String fx:value="NordLight"/>
118118
<String fx:value="NordDark"/>
119+
<String fx:value="Modena"/>
120+
<String fx:value="Caspian"/>
119121
</FXCollections>
120122
</items>
121123
</ComboBox>

0 commit comments

Comments
 (0)