mirror of
https://github.com/tobspr/shapez.io.git
synced 2024-10-27 20:34:29 +00:00
Implement system theme preference
This commit is contained in:
parent
52629c5bb2
commit
fe35220145
@ -1,6 +1,16 @@
|
|||||||
/* eslint-disable quotes,no-undef */
|
/* eslint-disable quotes,no-undef */
|
||||||
|
|
||||||
const { app, BrowserWindow, Menu, MenuItem, ipcMain, shell, dialog, session } = require("electron");
|
const {
|
||||||
|
app,
|
||||||
|
BrowserWindow,
|
||||||
|
Menu,
|
||||||
|
MenuItem,
|
||||||
|
ipcMain,
|
||||||
|
shell,
|
||||||
|
dialog,
|
||||||
|
session,
|
||||||
|
nativeTheme,
|
||||||
|
} = require("electron");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const url = require("url");
|
const url = require("url");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
@ -97,6 +107,10 @@ function createWindow() {
|
|||||||
win.webContents.session.clearCache();
|
win.webContents.session.clearCache();
|
||||||
win.webContents.session.clearStorageData();
|
win.webContents.session.clearStorageData();
|
||||||
|
|
||||||
|
nativeTheme.on("updated", () => {
|
||||||
|
win.webContents.send("system-theme-updated");
|
||||||
|
});
|
||||||
|
|
||||||
////// SECURITY
|
////// SECURITY
|
||||||
|
|
||||||
// Disable permission requests
|
// Disable permission requests
|
||||||
@ -381,6 +395,10 @@ ipcMain.handle("get-mods", async () => {
|
|||||||
return mods;
|
return mods;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
ipcMain.handle("get-system-theme", async () => {
|
||||||
|
return nativeTheme.shouldUseDarkColors ? "dark" : "light";
|
||||||
|
});
|
||||||
|
|
||||||
steam.init(isDev);
|
steam.init(isDev);
|
||||||
|
|
||||||
// Only allow achievements and puzzle DLC if no mods are loaded
|
// Only allow achievements and puzzle DLC if no mods are loaded
|
||||||
|
@ -4,7 +4,42 @@ export const THEMES = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export let THEME = THEMES.light;
|
export let THEME = THEMES.light;
|
||||||
|
let currentThemePreference = "light";
|
||||||
|
|
||||||
|
THEMES.system = THEMES.light;
|
||||||
|
ipcRenderer.on("system-theme-updated", detectSystemTheme);
|
||||||
|
|
||||||
|
export function detectSystemTheme() {
|
||||||
|
return ipcRenderer
|
||||||
|
.invoke("get-system-theme")
|
||||||
|
.then(theme => (THEMES.system = THEMES[theme]))
|
||||||
|
.catch(() => (THEMES.system = THEMES.light))
|
||||||
|
.then(() => {
|
||||||
|
// Re-apply the theme, this only affects system
|
||||||
|
applyGameTheme();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export function applyGameTheme(id) {
|
export function applyGameTheme(id) {
|
||||||
THEME = THEMES[id];
|
if (id === undefined) {
|
||||||
|
id = currentThemePreference;
|
||||||
|
}
|
||||||
|
|
||||||
|
const isSystem = id === "system";
|
||||||
|
const themeId = isSystem ? THEMES.system.id : id;
|
||||||
|
|
||||||
|
if (!isSystem && id === undefined) {
|
||||||
|
// Re-applying light/dark themes is not needed
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (document.body.id != "state_InGameState") {
|
||||||
|
// Only set the theme if not playing, otherwise this causes bugs
|
||||||
|
// Main menu re-applies the theme anyway
|
||||||
|
THEME = THEMES[themeId];
|
||||||
|
document.documentElement.setAttribute("data-theme", themeId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep the theme to re-apply system theme
|
||||||
|
currentThemePreference = id;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
{
|
{
|
||||||
|
"id": "dark",
|
||||||
|
|
||||||
"map": {
|
"map": {
|
||||||
"background": "#3e3f47",
|
"background": "#3e3f47",
|
||||||
"gridRegular": "rgba(255, 255, 255, 0.02)",
|
"gridRegular": "rgba(255, 255, 255, 0.02)",
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
{
|
{
|
||||||
|
"id": "light",
|
||||||
|
|
||||||
"map": {
|
"map": {
|
||||||
"background": "#eceef2",
|
"background": "#eceef2",
|
||||||
|
|
||||||
|
@ -399,6 +399,8 @@ export class ModInterface {
|
|||||||
*/
|
*/
|
||||||
registerGameTheme({ id, name, theme }) {
|
registerGameTheme({ id, name, theme }) {
|
||||||
THEMES[id] = theme;
|
THEMES[id] = theme;
|
||||||
|
theme.id = id;
|
||||||
|
|
||||||
this.registerTranslations("en", {
|
this.registerTranslations("en", {
|
||||||
settings: {
|
settings: {
|
||||||
labels: {
|
labels: {
|
||||||
|
@ -6,7 +6,7 @@ import { ReadWriteProxy } from "../core/read_write_proxy";
|
|||||||
import { BoolSetting, EnumSetting, RangeSetting, BaseSetting } from "./setting_types";
|
import { BoolSetting, EnumSetting, RangeSetting, BaseSetting } from "./setting_types";
|
||||||
import { createLogger } from "../core/logging";
|
import { createLogger } from "../core/logging";
|
||||||
import { ExplainedResult } from "../core/explained_result";
|
import { ExplainedResult } from "../core/explained_result";
|
||||||
import { THEMES, applyGameTheme } from "../game/theme";
|
import { THEMES, applyGameTheme, detectSystemTheme } from "../game/theme";
|
||||||
import { T } from "../translations";
|
import { T } from "../translations";
|
||||||
import { LANGUAGES } from "../languages";
|
import { LANGUAGES } from "../languages";
|
||||||
|
|
||||||
@ -209,14 +209,7 @@ function initializeSettings() {
|
|||||||
textGetter: theme => T.settings.labels.theme.themes[theme],
|
textGetter: theme => T.settings.labels.theme.themes[theme],
|
||||||
category: enumCategories.userInterface,
|
category: enumCategories.userInterface,
|
||||||
restartRequired: false,
|
restartRequired: false,
|
||||||
changeCb:
|
changeCb: (app, id) => applyGameTheme(id),
|
||||||
/**
|
|
||||||
* @param {Application} app
|
|
||||||
*/
|
|
||||||
(app, id) => {
|
|
||||||
applyGameTheme(id);
|
|
||||||
document.documentElement.setAttribute("data-theme", id);
|
|
||||||
},
|
|
||||||
enabledCb: /**
|
enabledCb: /**
|
||||||
* @param {Application} app
|
* @param {Application} app
|
||||||
*/ app => app.restrictionMgr.getHasExtendedSettings(),
|
*/ app => app.restrictionMgr.getHasExtendedSettings(),
|
||||||
@ -298,7 +291,7 @@ class SettingsStorage {
|
|||||||
this.soundVolume = 1.0;
|
this.soundVolume = 1.0;
|
||||||
this.musicVolume = 1.0;
|
this.musicVolume = 1.0;
|
||||||
|
|
||||||
this.theme = "light";
|
this.theme = "system";
|
||||||
this.refreshRate = "60";
|
this.refreshRate = "60";
|
||||||
this.scrollWheelSensitivity = "regular";
|
this.scrollWheelSensitivity = "regular";
|
||||||
this.movementSpeed = "regular";
|
this.movementSpeed = "regular";
|
||||||
@ -344,6 +337,10 @@ export class ApplicationSettings extends ReadWriteProxy {
|
|||||||
initialize() {
|
initialize() {
|
||||||
// Read and directly write latest data back
|
// Read and directly write latest data back
|
||||||
return this.readAsync()
|
return this.readAsync()
|
||||||
|
.then(() => {
|
||||||
|
// Make sure it's ready
|
||||||
|
return detectSystemTheme();
|
||||||
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
// Apply default setting callbacks
|
// Apply default setting callbacks
|
||||||
const settings = this.getAllSettings();
|
const settings = this.getAllSettings();
|
||||||
|
@ -16,6 +16,7 @@ import {
|
|||||||
waitNextFrame,
|
waitNextFrame,
|
||||||
} from "../core/utils";
|
} from "../core/utils";
|
||||||
import { HUDModalDialogs } from "../game/hud/parts/modal_dialogs";
|
import { HUDModalDialogs } from "../game/hud/parts/modal_dialogs";
|
||||||
|
import { detectSystemTheme } from "../game/theme";
|
||||||
import { MODS } from "../mods/modloader";
|
import { MODS } from "../mods/modloader";
|
||||||
import { PlatformWrapperImplBrowser } from "../platform/browser/wrapper";
|
import { PlatformWrapperImplBrowser } from "../platform/browser/wrapper";
|
||||||
import { PlatformWrapperImplElectron } from "../platform/electron/wrapper";
|
import { PlatformWrapperImplElectron } from "../platform/electron/wrapper";
|
||||||
@ -348,6 +349,9 @@ export class MainMenuState extends GameState {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply the system theme if returning from InGameState
|
||||||
|
detectSystemTheme();
|
||||||
|
|
||||||
if (G_IS_DEV && globalConfig.debug.testPuzzleMode) {
|
if (G_IS_DEV && globalConfig.debug.testPuzzleMode) {
|
||||||
this.onPuzzleModeButtonClicked(true);
|
this.onPuzzleModeButtonClicked(true);
|
||||||
return;
|
return;
|
||||||
|
@ -6,6 +6,7 @@ import { createLogger } from "../core/logging";
|
|||||||
import { getLogoSprite } from "../core/utils";
|
import { getLogoSprite } from "../core/utils";
|
||||||
import { getRandomHint } from "../game/hints";
|
import { getRandomHint } from "../game/hints";
|
||||||
import { HUDModalDialogs } from "../game/hud/parts/modal_dialogs";
|
import { HUDModalDialogs } from "../game/hud/parts/modal_dialogs";
|
||||||
|
import { detectSystemTheme } from "../game/theme";
|
||||||
import { PlatformWrapperImplBrowser } from "../platform/browser/wrapper";
|
import { PlatformWrapperImplBrowser } from "../platform/browser/wrapper";
|
||||||
import { autoDetectLanguageId, T, updateApplicationLanguage } from "../translations";
|
import { autoDetectLanguageId, T, updateApplicationLanguage } from "../translations";
|
||||||
|
|
||||||
@ -108,6 +109,7 @@ export class PreloadState extends GameState {
|
|||||||
.then(() => this.fetchDiscounts())
|
.then(() => this.fetchDiscounts())
|
||||||
|
|
||||||
.then(() => this.setStatus("Initializing settings", 20))
|
.then(() => this.setStatus("Initializing settings", 20))
|
||||||
|
.then(() => detectSystemTheme())
|
||||||
.then(() => {
|
.then(() => {
|
||||||
return this.app.settings.initialize();
|
return this.app.settings.initialize();
|
||||||
})
|
})
|
||||||
|
@ -1267,6 +1267,7 @@ settings:
|
|||||||
themes:
|
themes:
|
||||||
dark: Dark
|
dark: Dark
|
||||||
light: Light
|
light: Light
|
||||||
|
system: System
|
||||||
|
|
||||||
refreshRate:
|
refreshRate:
|
||||||
title: Tick Rate
|
title: Tick Rate
|
||||||
|
Loading…
Reference in New Issue
Block a user