All files / app/main register-theme-mode-handlers.ts

0% Statements 0/17
0% Branches 0/4
0% Functions 0/6
0% Lines 0/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35                                                                     
import { ipcMain, nativeTheme, WebContents } from 'electron';
 
export function registerThemeModeHandlers(webContents: WebContents) {
  nativeTheme.on('updated', () => {
    webContents.send('message-event', {
      type: 'theme-event',
      shouldUseDarkMode: nativeTheme.shouldUseDarkColors,
    });
  });
 
  webContents.on('destroyed', () => {
    nativeTheme.removeAllListeners();
    ipcMain.removeHandler('theme:toggle-mode');
    ipcMain.removeHandler('theme:set-system-mode');
  });
 
  ipcMain.on(
    'theme:get-current',
    e => (e.returnValue = nativeTheme.shouldUseDarkColors ? 'dark' : 'light')
  );
 
  ipcMain.handle('theme:toggle-mode', () => {
    if (nativeTheme.shouldUseDarkColors) {
      nativeTheme.themeSource = 'light';
    } else {
      nativeTheme.themeSource = 'dark';
    }
    return nativeTheme.shouldUseDarkColors;
  });
 
  ipcMain.handle('theme:set-system-mode', () => {
    nativeTheme.themeSource = 'system';
  });
}