Electron:尝试设置“nativeTheme.themeSource”,但“nativeTheme”未定义

2023-12-21

我无法设置themeSource我的电子应用程序。平台是Windows 8.1。

const electron = require('electron');
const app = electron.app;
if (app) app.on('ready', function() {
  nativeTheme = electron.nativeTheme;
  nativeTheme.themeSource = 'dark';
});

这会在模式弹出警报中产生错误:nativeTheme未定义。

我肯定做错了什么,但我一辈子都看不到。

我在 Electron 中所做的其他一切都非常有魅力。

这是我的整个 app.js:

// server-side jquery
const jsdom = require('jsdom');
const jquery = require('jquery');
const { JSDOM } = jsdom;
const dom = new JSDOM('<!DOCTYPE html>');
const $ = jquery(dom.window);
global.jq = $;
// for priming the webapp
const request = require('request');
// electron config stuff
var backgroundColor = '#1A1A1A';
var width = 800, height = 600;
// get electron
const electron = require('electron');
// prime electron app
const app = electron.app;
// flags: don't enter GUI launch until both sails & electron report ready
var electronIsReady = false;
var sailsIsReady = false;
var gruntIsReady = false;
// block repeat launches (edge contingency)
var windowIsLaunching = false;
var splashIsUp = false;
var splashResponse = 'pong';
// electron window(s)
var mainWindow = null;
var splashWindow = null;
// delay after all preflight checks pass
var windowCreationDelay = 0;
// sails app address
const appAddress = 'http://127.0.0.1';
const appPort = 1337;

const BrowserWindow = electron.BrowserWindow;
if (app) app.on('ready', tryLaunchingForElectron);
else electronIsReady = true;

function tryLaunchingForSails() {
  sailsIsReady = true;
  try {
    // "prime" the webapp by requesting content early
    request(`${appAddress}:${appPort}`, (error,response,body) => {/*nada*/});
    if (app && electronIsReady && gruntIsReady) createWindow();
  }
  catch (e) { console.error(e); }
}
function tryLaunchingForElectron() {
  // try to prevent multiple instances of the app running
  app.requestSingleInstanceLock();
  electronIsReady = true;
  if (!splashIsUp) {
    splashIsUp = true;
    // show splash screen
    splashWindow = new BrowserWindow({
      width: width, height: height,
      transparent: true, frame: false, alwaysOnTop: true,
      focusable: false, fullscreenable: false,
      webPreferences: { nodeIntegration: true }
    });
    splashWindow.loadURL(`file://${__dirname}/splash.html`);
  }
  // enter UI phase if sails is also ready
  if (app && sailsIsReady && gruntIsReady) createWindow();
}

function createWindow() {
  if (windowIsLaunching === true) return -1;
  windowIsLaunching = true;
  // optional: give sails time to get it fully together
  setTimeout(function() {
    try {
      // tell the splash page to close
      splashResponse = 'close';
      // create main window
      mainWindow = new BrowserWindow({show: false, width: width, height: height,
        backgroundColor: backgroundColor
      });
      // hide menu bar where available
      mainWindow.setMenuBarVisibility(false);
      // maximize the window
      mainWindow.maximize();
      // bring to the front
      mainWindow.focus();
      // go to the sails app
      mainWindow.loadURL(`${appAddress}:${appPort}/`);
      // show javascript & DOM consoles
      mainWindow.webContents.openDevTools();
      // show browser only when it's ready to render itself
      mainWindow.once('ready-to-show', function() {
        // get the splash out of the way
        splashWindow.setAlwaysOnTop(false);
        // show the main window
        mainWindow.setAlwaysOnTop(true);
        mainWindow.show();
        mainWindow.setAlwaysOnTop(false);
        app.focus();
      });
      // setup close function
      mainWindow.on('closed', function() {
        mainWindow = null;
      });
    }
    catch (e) { console.error(e); }
  }, windowCreationDelay);
}

// tell the splash window when it's time to hide & close
if (app) app.on('ready', function() {
  var ipcMain = electron.ipcMain;
  ipcMain.on('splashPing', (event, arg) => {
    try {
      event.sender.send('splashPing', splashResponse);
    } catch (e) { console.log(e); }
    if (splashResponse === 'close') {
      //splashWindow = null;
      ipcMain.removeAllListeners('splashPing');
    }
    // console.log(`${arg}||${splashResponse}`);
  });
});

// quit when all windows are closed
if (app) app.on('window-all-closed', function() {
  if (process.platform !== 'darwin') {
    sails.lower(function (err) {
      if (err) {
        console.log(err);
        app.exit(1);
      } else
        app.quit();
      setTimeout(()=>{app.quit();},5000);
    });
  }
});

// probably for mobile
if (app) app.on('activate', function() {
  if (mainWindow === null) {
    createWindow();
  }
})

if (app) app.on('ready', function() {
  nativeTheme = electron.nativeTheme;
  nativeTheme.themeSource = 'dark';
});

// sails wants this
process.chdir(__dirname);

// import sails & rc
var sails;
var rc;
try {
    sails = require('sails');
  sails.on('hook:grunt:done', () => {
    gruntIsReady = true;
    tryLaunchingForSails();
  });
    rc = require('sails/accessible/rc');
} catch (err) {
    console.error(err);
}

// Start server
try {
  sails.lift(rc('sails'), tryLaunchingForSails );
}
catch (e) { console.log(e); }


nativeTheme = electron.nativeTheme;

这就是问题。你需要做:

const nativeTheme = electron.nativeTheme;

尽管在这种情况下不需要额外的变量 - 只需这样做electron.nativeTheme.themeSource = 'dark';.

I strongly建议你使用 Typescript - 它会告诉你:

编辑:我确信我在评论中提到了这一点,但它似乎已被以某种方式删除:您还需要确保您使用的是 Electron 7.0.0 或更高版本 -nativeTheme在此之前不可用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Electron:尝试设置“nativeTheme.themeSource”,但“nativeTheme”未定义 的相关文章

随机推荐