Skip to content

Instantly share code, notes, and snippets.

@dsanders11
Created December 21, 2022 18:17
Show Gist options
  • Save dsanders11/1a3aad29ba8468f47539c0e176c17845 to your computer and use it in GitHub Desktop.
Save dsanders11/1a3aad29ba8468f47539c0e176c17845 to your computer and use it in GitHub Desktop.
Theme Raptor API
<!DOCTYPE html>
<html>
<head>
<title>Starship</title>
</head>
<body>
<h1>Starship</h1>
<p id="current-theme"></p>
<nav>
<button id="theme-dark">Set Dark Theme</button>
<button id="theme-light">Set Light Theme</button>
<button id="theme-auto">Set Auto Theme</button>
</nav>
<script src="./renderer.js"></script>
</body>
</html>
const {app} = require('electron')
const { Window } = require('./starship')
function createWindow () {
// Create the Starship window.
const mainWindow = new Window({
width: 800,
height: 600,
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
{
"name": "milky-power-groan-dvhp2",
"productName": "milky-power-groan-dvhp2",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "dsanders11",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "18.3.15"
}
}
const { contextBridge, ipcRenderer } = require('electron')
async function getTheme() {
return ipcRenderer.invoke('starship.theme.get')
}
async function setTheme(theme) {
ipcRenderer.send('starship.theme.set', theme)
}
contextBridge.exposeInMainWorld(
'starship',
{
theme: {
get: getTheme,
set: setTheme,
}
}
)
window.onload = () => {
const matcher = window.matchMedia('(prefers-color-scheme: dark)');
document.documentElement.setAttribute('data-theme', matcher.matches ? 'dark' : 'light')
matcher.addEventListener('change', (event) => {
document.documentElement.setAttribute('data-theme', event.matches ? 'dark' : 'light')
})
};
const css = `
[data-theme='dark'] {
color-scheme: only dark;
}
[data-theme='light'] {
color-scheme: only light;
}
`
window.addEventListener('DOMContentLoaded', () => {
const style = document.createElement('style')
document.head.appendChild(style)
style.type = 'text/css'
style.appendChild(document.createTextNode(css))
})
function getTheme() {
starship.theme.get().then((currentTheme) => {
document.getElementById("current-theme").innerHTML = currentTheme;
}).catch((error) => {
throw error;
});
}
document.getElementById("theme-dark").onclick = function () {
return starship.theme.set('dark').then(getTheme);
};
document.getElementById("theme-light").onclick = function () {
return starship.theme.set('light').then(getTheme);
};
document.getElementById("theme-auto").onclick = function () {
return starship.theme.set('auto').then(getTheme);
};
getTheme();
const {ipcMain, nativeTheme, BrowserWindow} = require('electron')
const path = require('path')
let theme = nativeTheme.themeSource;
ipcMain.handle('starship.theme.get', async () => {
if (theme === 'system') return 'auto'
return theme
})
ipcMain.on('starship.theme.set', async (event, newTheme) => {
if (newTheme === 'auto') {
nativeTheme.themeSource = 'system'
} else {
nativeTheme.themeSource = newTheme
}
theme = newTheme
})
class Window {
constructor(options) {
return new BrowserWindow({
...options,
webPreferences: {
...options.webPreferences,
preload: path.join(__dirname, 'preload.js')
}
})
}
}
module.exports = {
Window
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment