Theme
The page owns its theme and reports it to the shell as a ThemeState (@adecore/shell/bridge). createTheme paints what only the shell can. It sets each window's background, so a reload or resize never flashes the other theme. It colors the native window controls off macOS. It sets the prefers-color-scheme Chromium reports, so a page follows the app instead of the system.
import { createTheme } from '@adecore/shell';
const theme = createTheme({
nativeTheme,
windows: () => windows.all(),
background: '#131316',
overlay: {
height: 48,
colors: { dark: { color: '#1b1b1f', symbolColor: '#ececf1' }, light: { color: '#ffffff', symbolColor: '#18181b' } }
}
});
// `ipc` is the app's createAppIpc, which checks the sender first.
ipc.on('window:theme', (_event, state: ThemeState) => theme.apply(state));
const window = new BrowserWindow({ ...bounds, ...theme.windowOptions(), show: false });windowOptions() returns a new window's chrome and background. On macOS that is the inset traffic lights (trafficLights, 17 by 17 by default). Elsewhere it is a hidden title bar with the overlay controls drawn over the page, or no controls without overlay. With nativeFrame, a window off macOS keeps the system's own title bar and menu bar, and the overlay colors are never applied. A new window opens in the current theme, or dark with background before any page reported one.
overlay holds one color pair per theme. A page whose background changes within a theme, such as a palette picker, can report its control colors in ThemeState.overlay. Those win over the theme's pair on every window until a state without them arrives. The shell reads only color and symbolColor. macOS ignores them.
// In the page, on every change of the theme or the palette.
desktop.setTheme({
resolved,
followsSystem,
background: tokenOf('--bg'),
overlay: { color: tokenOf('--bg'), symbolColor: tokenOf('--fg') }
});nativeTheme.themeSource is system only while the app follows the system. Otherwise it is the app's choice, so a page inside a <webview> follows the app too.
Room for the window controls
A page that reaches the top of a macOS window keeps the start of its first column free for the traffic lights, and takes that room back in fullscreen, where macOS hides them. windowControls(window) returns a WindowControlsState (@adecore/shell/bridge): its inset is the width to keep free, as far past the lights as trafficLights sets them from the edge (86 pixels by default). It is null off macOS and in fullscreen.
followWindowControls(window, send) calls send with a new state each time the window goes in or out of fullscreen, and returns what stops it. The shell registers no channel, so the app sends the state on one of its own and answers a page that asks for it. On the page, WindowControlsSpacer draws the room.
windowControlsOf(platform, options) (@adecore/shell/bridge) computes the same state without a window: options takes fullscreen, trafficLights and offset, a WindowControlsOptions. A page starts from it with the platform its preload reports, so the first frame clears the traffic lights before the shell answers.
// `ipc` is the app's createAppIpc, which checks the sender first.
ipc.handle('window:controls', (event) => theme.windowControls(ipc.senderWindow(event)!));
theme.followWindowControls(window, (state) => window.webContents.send('window:controls', state));Beside a left rail
A page with a rail at its left edge and no title bar over it, such as a Workspace with rails= and no top, wants the traffic lights beside the rail. The offset is the rail's width, 48 pixels. The lights then stand at trafficLights plus the offset, 65 by 17 by default, and the inset grows by the offset alone, to 134 pixels from the window's edge. With a title bar, or without a left rail, the offset is 0.
windowControlsOffset sets the offset of every new window, which windowOptions() passes to Electron as trafficLightPosition. When the page drops or adds its title bar while the window is open, it tells the main process on a channel of the app's own, and placeWindowControls(window, offset) moves the lights with setWindowButtonPosition and sends the new state to whoever follows the window. Fullscreen still hides the room, and a custom trafficLights keeps its place relative to the rail. Off macOS both do nothing. trafficLightsAt(offset, lights?) (@adecore/shell/bridge) returns where the lights go.
const theme = createTheme({ nativeTheme, windows: () => windows.all(), background: '#131316', windowControlsOffset: 48 });
// `ipc` is the app's createAppIpc, which checks the sender first.
ipc.on('window:controls-offset', (event, offset: number) => theme.placeWindowControls(ipc.senderWindow(event)!, offset === 48 ? 48 : 0));current() returns the last ThemeState applied, or null before the first. createTheme takes ThemeOptions, whose overlay is a TitleBarOverlay, and returns a Theme. The reference lists the options.