mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2025-11-05 11:56:37 +00:00
auto dark mode (only mac)
This commit is contained in:
parent
36ee8add08
commit
ca07644bd0
3 changed files with 34 additions and 5 deletions
|
|
@ -369,8 +369,8 @@ public class AppController implements Initializable {
|
||||||
|
|
||||||
Theme configTheme = Config.get().getTheme();
|
Theme configTheme = Config.get().getTheme();
|
||||||
if(configTheme == null) {
|
if(configTheme == null) {
|
||||||
configTheme = Theme.LIGHT;
|
configTheme = Theme.SYSTEM_DEFAULT;
|
||||||
Config.get().setTheme(Theme.LIGHT);
|
Config.get().setTheme(Theme.SYSTEM_DEFAULT);
|
||||||
}
|
}
|
||||||
final Theme selectedTheme = configTheme;
|
final Theme selectedTheme = configTheme;
|
||||||
Optional<Toggle> selectedThemeToggle = theme.getToggles().stream().filter(toggle -> selectedTheme.equals(toggle.getUserData())).findFirst();
|
Optional<Toggle> selectedThemeToggle = theme.getToggles().stream().filter(toggle -> selectedTheme.equals(toggle.getUserData())).findFirst();
|
||||||
|
|
@ -2339,11 +2339,35 @@ public class AppController implements Initializable {
|
||||||
|
|
||||||
public void setTheme(ActionEvent event) {
|
public void setTheme(ActionEvent event) {
|
||||||
Theme selectedTheme = (Theme)theme.getSelectedToggle().getUserData();
|
Theme selectedTheme = (Theme)theme.getSelectedToggle().getUserData();
|
||||||
if(Config.get().getTheme() != selectedTheme) {
|
Theme themeToApply = selectedTheme;
|
||||||
|
if (selectedTheme == Theme.SYSTEM_DEFAULT) {
|
||||||
|
themeToApply = detectSystemTheme();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Config.get().getTheme() != selectedTheme) {
|
||||||
Config.get().setTheme(selectedTheme);
|
Config.get().setTheme(selectedTheme);
|
||||||
}
|
}
|
||||||
|
|
||||||
EventManager.get().post(new ThemeChangedEvent(selectedTheme));
|
EventManager.get().post(new ThemeChangedEvent(themeToApply));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Theme detectSystemTheme() {
|
||||||
|
String os = System.getProperty("os.name").toLowerCase();
|
||||||
|
if (os.contains("mac")) {
|
||||||
|
return isMacDarkMode() ? Theme.DARK : Theme.LIGHT;
|
||||||
|
}
|
||||||
|
return Theme.LIGHT;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isMacDarkMode() {
|
||||||
|
try (BufferedReader reader = new BufferedReader(
|
||||||
|
new InputStreamReader(
|
||||||
|
new ProcessBuilder("defaults", "read", "-g", "AppleInterfaceStyle").start().getInputStream()))) {
|
||||||
|
String result = reader.readLine();
|
||||||
|
return result != null && result.trim().equalsIgnoreCase("Dark");
|
||||||
|
} catch (IOException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void serverToggleStartAnimation() {
|
private void serverToggleStartAnimation() {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
package com.sparrowwallet.sparrow;
|
package com.sparrowwallet.sparrow;
|
||||||
|
|
||||||
public enum Theme {
|
public enum Theme {
|
||||||
LIGHT, DARK
|
SYSTEM_DEFAULT, LIGHT, DARK
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -101,6 +101,11 @@
|
||||||
</Menu>
|
</Menu>
|
||||||
<Menu mnemonicParsing="false" text="Theme">
|
<Menu mnemonicParsing="false" text="Theme">
|
||||||
<items>
|
<items>
|
||||||
|
<RadioMenuItem mnemonicParsing="false" text="System Default" toggleGroup="$theme" onAction="#setTheme">
|
||||||
|
<userData>
|
||||||
|
<Theme fx:constant="SYSTEM_DEFAULT" />
|
||||||
|
</userData>
|
||||||
|
</RadioMenuItem>
|
||||||
<RadioMenuItem mnemonicParsing="false" text="Light" toggleGroup="$theme" onAction="#setTheme">
|
<RadioMenuItem mnemonicParsing="false" text="Light" toggleGroup="$theme" onAction="#setTheme">
|
||||||
<userData>
|
<userData>
|
||||||
<Theme fx:constant="LIGHT" />
|
<Theme fx:constant="LIGHT" />
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue