Skip to content

Commit c97b4a2

Browse files
feat. added system theme
1 parent e4c712d commit c97b4a2

File tree

5 files changed

+125
-1
lines changed

5 files changed

+125
-1
lines changed

src/lib/systemConfiguration.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,25 @@ export function getSystemConfiguration() {
5252
cordova.exec(resolve, reject, "System", "get-configuration", []);
5353
});
5454
}
55+
56+
57+
export function isDeviceDarkTheme() {
58+
return new Promise((resolve, reject) => {
59+
if(window.cordova && typeof cordova.exec !== 'function'){
60+
resolve(true)
61+
return
62+
}
63+
cordova.exec(
64+
(result) => {
65+
resolve(result.isDark);
66+
},
67+
(error) => {
68+
console.warn(error);
69+
resolve(true);
70+
},
71+
'System',
72+
'getTheme',
73+
[]
74+
);
75+
});
76+
}

src/palettes/changeTheme/index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import "./style.scss";
22
import palette from "components/palette";
33
import appSettings from "lib/settings";
44
import themes from "theme/list";
5+
import { updateSystemTheme } from "theme/preInstalled";
6+
import { isDeviceDarkTheme } from "lib/systemConfiguration";
57

68
export default function changeTheme(type = "editor") {
79
palette(
@@ -56,11 +58,49 @@ function generateHints(type) {
5658
});
5759
}
5860

61+
62+
63+
64+
65+
let previousDark = await isDeviceDarkTheme();
66+
const updateTimeMs = 3000;
67+
68+
let intervalId = setInterval(async () => {
69+
if (appSettings.value.appTheme.toLowerCase() === "system") {
70+
const isDark = await isDeviceDarkTheme();
71+
if (isDark !== previousDark) {
72+
previousDark = isDark
73+
updateSystemTheme(isDark);
74+
}
75+
}
76+
}, updateTimeMs);
77+
5978
function onselect(value) {
6079
if (!value) return;
6180

6281
const selection = JSON.parse(value);
6382

83+
if (selection.theme === "system") {
84+
// Start interval if not already started
85+
if (!intervalId) {
86+
intervalId = setInterval(async () => {
87+
if (appSettings.value.appTheme.toLowerCase() === "system") {
88+
const isDark = await isDeviceDarkTheme();
89+
if (isDark !== previousDark) {
90+
previousDark = isDark
91+
updateSystemTheme(isDark);
92+
}
93+
}
94+
}, updateTimeMs);
95+
}
96+
} else {
97+
// Cancel interval if it's running
98+
if (intervalId) {
99+
clearInterval(intervalId);
100+
intervalId = null;
101+
}
102+
}
103+
64104
if (selection.type === "editor") {
65105
editorManager.editor.setTheme(selection.theme);
66106
appSettings.update(

src/plugins/system/android/com/foxdebug/system/System.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,24 @@ public boolean execute(
104104
final String arg5 = args.optString(4);
105105
final String arg6 = args.optString(5);
106106

107+
if (action.equals("getTheme")) {
108+
JSONObject theme = new JSONObject();
109+
110+
boolean isDark = (cordova.getActivity().getResources().getConfiguration().uiMode
111+
& Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
112+
113+
try {
114+
theme.put("isDark", isDark);
115+
} catch (Exception e) {
116+
callbackContext.error("Error setting 'isDark' JSON entry.");
117+
return true;
118+
}
119+
120+
callbackContext.success(theme);
121+
122+
return true;
123+
}
124+
107125
switch (action) {
108126
case "get-webkit-info":
109127
case "file-action":

src/theme/list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function add(theme) {
6868
* @param {string} id The name of the theme to apply
6969
* @param {boolean} init Whether or not this is the first time the theme is being applied
7070
*/
71-
async function apply(id, init) {
71+
export async function apply(id, init) {
7272
if (!DOES_SUPPORT_THEME) {
7373
id = "default";
7474
}

src/theme/preInstalled.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import appSettings from "lib/settings";
12
import { createBuiltInTheme } from "./builder";
3+
import { apply } from "./list";
4+
import { isDeviceDarkTheme } from "lib/systemConfiguration";
25

36
const WHITE = "rgb(255, 255, 255)";
47
const BLACK = "rgb(0, 0, 0)";
@@ -164,11 +167,52 @@ light.linkTextColor = "rgb(104, 103, 149)";
164167
light.borderColor = "rgb(153, 153, 153)";
165168
light.popupIconColor = "rgb(51, 62, 89)";
166169

170+
const system = createBuiltInTheme("System");
171+
172+
export function updateSystemTheme(darkTheme) {
173+
if (darkTheme) {
174+
system.primaryColor = "rgb(49, 49, 49)";
175+
system.primaryTextColor = WHITE;
176+
system.darkenedPrimaryColor = "rgb(29, 29, 29)";
177+
system.secondaryColor = "rgb(37, 37, 37)";
178+
system.secondaryTextColor = WHITE;
179+
system.activeColor = "rgb(51, 153, 255)";
180+
system.linkTextColor = "rgb(181, 180, 233)";
181+
system.borderColor = "rgba(230, 230, 230, 0.2)";
182+
system.popupIconColor = WHITE;
183+
184+
system.preferredEditorTheme = "ace/theme/clouds_midnight";
185+
186+
system.popupBackgroundColor = "rgb(49, 49, 49)";
187+
system.popupTextColor = WHITE;
188+
system.popupActiveColor = "rgb(255, 215, 0)";
189+
} else {
190+
system.darkenedPrimaryColor = "rgb(153, 153, 153)";
191+
system.primaryColor = WHITE;
192+
system.primaryTextColor = "rgb(51, 62, 89)";
193+
system.secondaryColor = WHITE;
194+
system.secondaryTextColor = "rgb(51, 62, 89)";
195+
system.activeColor = "rgb(51, 153, 255)";
196+
system.linkTextColor = "rgb(104, 103, 149)";
197+
system.borderColor = "rgb(153, 153, 153)";
198+
system.popupIconColor = "rgb(51, 62, 89)";
199+
200+
system.preferredEditorTheme = "ace/theme/crimson_editor";
201+
}
202+
203+
if (appSettings.value.appTheme.toLowerCase() === "system") {
204+
apply(system.id, true);
205+
}
206+
}
207+
208+
updateSystemTheme(await isDeviceDarkTheme());
209+
167210
const custom = createBuiltInTheme("Custom");
168211
custom.autoDarkened = true;
169212

170213
export default [
171214
createBuiltInTheme("default", "dark", "free"),
215+
system,
172216
dark,
173217
oled,
174218
ocean,

0 commit comments

Comments
 (0)