Skip to content

Commit eac9906

Browse files
authored
Merge pull request #499 from CatalystDevOrg/c38-custom-themes
Custom Themes
2 parents 90123b0 + bcd5f5a commit eac9906

File tree

5 files changed

+70
-5
lines changed

5 files changed

+70
-5
lines changed

main/main.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ const template = [{
109109
{
110110
label: 'Find',
111111
accelerator: 'CmdOrCtrl+F',
112-
click: function() {
112+
click: function () {
113113
mainWindow.webContents.executeJavaScript('toggleFind()');
114114
}
115115
},
@@ -119,7 +119,7 @@ const template = [{
119119
click: function () {
120120
mainWindow.webContents.toggleDevTools();
121121
},
122-
122+
123123
}/*
124124
{
125125
label: "Check for Updates",
@@ -158,7 +158,21 @@ ipcMain.handle('loadExt', async (event, ext) => {
158158

159159
ipcMain.handle('read-user-data', async (event, fileName) => {
160160
const path = app.getPath('userData');
161-
const buf = fs.readFileSync(`${path}/${fileName}`, { encoding: 'utf8', flag: 'r' });
161+
try {
162+
const buf = fs.readFileSync(`${path}/${fileName}`, { encoding: 'utf8', flag: 'r' });
163+
} catch {
164+
return;
165+
}
166+
return buf;
167+
});
168+
169+
if (!fs.existsSync(`${app.getPath('userData')}/themes`)) {
170+
fs.mkdirSync(`${app.getPath('userData')}/themes`)
171+
}
172+
173+
ipcMain.handle('get-themes', async (event) => {
174+
const path = app.getPath('userData');
175+
const buf = fs.readdirSync(`${path}/themes`, { encoding: 'utf8', flag: 'r' });
162176
return buf;
163177
});
164178

main/preload.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,33 @@ contextBridge.exposeInMainWorld('cat', {
2222
}
2323
);
2424
},
25+
getThemes: () => {
26+
const themes = ipcRenderer.invoke('get-themes').then(
27+
result => {
28+
themeSelect = document.getElementById('pref-theme')
29+
for (x in result) {
30+
let sel = document.createElement('option')
31+
sel.value = result[x]
32+
sel.innerText = result[x].replace(".css", "")
33+
themeSelect.appendChild(sel)
34+
}
35+
}
36+
)
37+
},
38+
loadTheme: (theme) => {
39+
const file = ipcRenderer.invoke('read-user-data', `themes/${theme}`).then(
40+
result => {
41+
let el = document.createElement('style');
42+
el.type = 'text/css';
43+
el.innerText = result;
44+
el.classList.add("theme")
45+
document.head.appendChild(el);
46+
}
47+
)
48+
},
49+
unloadTheme: () => {
50+
document.getElementsByClassName('theme')[0].remove()
51+
},
2552
enableAdBlocker: () => ipcRenderer.invoke('enable-ad-blocker'),
2653
ipcToggleFs: () => ipcRenderer.invoke('toggle-full-screen'),
2754
});

src/index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ <h1 class="font-bold text-xl">Preferences</h1>
8282
id="pref-darkmode" type="checkbox" class="" disabled />
8383
<p class="text-sm">Toggle dark mode</p>
8484
<br />
85+
<label for="pref-theme">Theme</label>
86+
<select name="pref-theme" id="pref-theme">
87+
<option value="0">Default</option>
88+
</select>
89+
<br />
8590
<label for="pref-autocomplete">Autocomplete </label>
8691
<input id="pref-autocomplete" type="checkbox" class="" />
8792
<p class="text-sm">Search Autocomplete</p>

src/preferences.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ function togglePreferences() {
2525
addCheckboxListener(document.getElementById('pref-adblk'), 'adblk');
2626
if (preferences.agent.toString().length > 1) {
2727
document.getElementById('pref-useragent').value =
28-
preferences.agent || 'Catalyst/{{version}}';
28+
preferences.agent || 'Catalyst/{{version}}';
2929
} else {
3030
document.getElementById('pref-useragent').value = preferences.agent;
3131
}
3232
addTextListener(document.getElementById('pref-useragent'), 'agent');
3333
addCheckboxListener(document.getElementById('pref-homewidgets'), 'homewidgets');
3434
document.getElementById('pref-homewidgets').checked = preferences.homewidgets;
35+
addSelectListener(document.getElementById('pref-theme'), 'theme')
3536
}
3637
}
3738

@@ -77,6 +78,13 @@ function addTextListener(element, prefKey) {
7778
});
7879
}
7980

81+
function addSelectListener(element, prefKey) {
82+
element.addEventListener('change', () => {
83+
preferences[prefKey] = element.value;
84+
updatePreferences();
85+
})
86+
}
87+
8088
/**
8189
* Updates the preferences in LocalStorage to the new preferences and evaluates the new ones
8290
*/
@@ -100,6 +108,15 @@ function evaluatePreferences() {
100108
if (preferences.adblk) {
101109
cat.enableAdBlocker();
102110
}
111+
if (preferences.theme) {
112+
if (document.getElementsByClassName('theme').length > 0) {
113+
cat.unloadTheme();
114+
}
115+
if (preferences.theme == 0) {
116+
return;
117+
}
118+
cat.loadTheme(preferences.theme)
119+
}
103120
}
104121

105122
var enginespref = document.querySelector('#se');

src/startup.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ if (!localStorage.getItem('home-postfix')) {
1717

1818
if (localStorage.getItem('bookmarks') < 1) {
1919
document.querySelector('#bookmarks').innerText = 'When you add bookmarks they will appear here!';
20-
}
20+
}
21+
22+
cat.getThemes();

0 commit comments

Comments
 (0)