From ee9f273d22348d55f6301fa0b4ffe06552b425b2 Mon Sep 17 00:00:00 2001
From: Arkerthan <arkerthan@gmail.com>
Date: Tue, 17 Mar 2020 18:07:42 +0100
Subject: [PATCH] link themes to saves

---
 js/003-data/gameVariableData.js |  1 +
 src/data/onLoad.js              | 10 ++++++++++
 src/gui/theming.js              | 21 +++++++++++++++++----
 3 files changed, 28 insertions(+), 4 deletions(-)
 create mode 100644 src/data/onLoad.js

diff --git a/js/003-data/gameVariableData.js b/js/003-data/gameVariableData.js
index eb73d75d033..541af5e3460 100644
--- a/js/003-data/gameVariableData.js
+++ b/js/003-data/gameVariableData.js
@@ -164,6 +164,7 @@ App.Data.defaultGameStateVariables = {
 	useTabs: 0,
 	verboseDescriptions: 0,
 	verticalizeArcologyLinks: 0,
+	theme: 0,
 	weightAffectsAssets: 1,
 	curativeSideEffects: 1,
 	disableTiredness: 1,
diff --git a/src/data/onLoad.js b/src/data/onLoad.js
new file mode 100644
index 00000000000..349c62cad99
--- /dev/null
+++ b/src/data/onLoad.js
@@ -0,0 +1,10 @@
+/**
+ *
+ * @param {object} save
+ */
+Config.saves.onLoad = function(save) {
+	// get the old State.variables for easier access.
+	const V = save.state.history[0].variables;
+
+	App.UI.Theme.onLoad(V);
+};
diff --git a/src/gui/theming.js b/src/gui/theming.js
index 706d870670d..ac274203c91 100644
--- a/src/gui/theming.js
+++ b/src/gui/theming.js
@@ -1,13 +1,14 @@
 App.UI.Theme = (function() {
-	// NOTE: Due to browser limitations it is not possible to retrieve the path of selected files. We therefor expect
-	// all files to be located in the same directory as the HTML file. Selected files from somewhere else will simply
-	// not be loaded.
+	// NOTE: Due to browser limitations it is not possible to retrieve the path of selected files, only the filename.
+	// We therefore expect all files to be located in the same directory as the HTML file. Selected files from somewhere
+	// else will simply not be loaded or if a file in the correct place has the same name, it will be loaded instead.
 	let currentThemeElement = null;
 	let devTheme = null;
 
 	return {
 		selector: selector,
-		devTheme: reloadDevTheme
+		devTheme: reloadDevTheme,
+		onLoad: onLoad
 	};
 
 	/**
@@ -36,6 +37,8 @@ App.UI.Theme = (function() {
 	 * @param {string} filename or filepath relative to the HTML file.
 	 */
 	function load(filename) {
+		V.theme = filename;
+
 		currentThemeElement = document.createElement("link");
 		currentThemeElement.setAttribute("rel", "stylesheet");
 		currentThemeElement.setAttribute("type", "text/css");
@@ -48,6 +51,7 @@ App.UI.Theme = (function() {
 		if (currentThemeElement !== null) {
 			document.head.removeChild(currentThemeElement);
 			currentThemeElement = null;
+			V.theme = 0;
 		}
 	}
 
@@ -70,4 +74,13 @@ App.UI.Theme = (function() {
 			document.head.appendChild(devTheme);
 		}
 	}
+
+	/**
+	 * @param {object} V
+	 */
+	function onLoad(V) {
+		if (V.theme !== 0) {
+			load(V.theme);
+		}
+	}
 })();
-- 
GitLab