簡體   English   中英

為什么看起來像來自Electron中一個菜單項的IPC消息到達了我的窗口,但是卻不是從另一個菜單iteam發送時卻到達了我的窗口?

[英]Why does it look like IPC messages from one menu item in Electron reach my window, but not when sent from another menu iteam?

我有一個簡單的應用程序,需要運行一個后台進程來獲取一些數據。 我想在檢索數據時顯示一個加載指示器,但在完成任務時遇到了麻煩。

我正在使用ipcrenderer在主窗口中接收消息。 我的代碼如下:

//// main.js

const electron = require('electron');
const url = require('url');
const path = require('path');

const {
  app,
  BrowserWindow,
  Menu,
  ipcMain
} = electron

// SET ENV
//process.env.NODE_ENV = 'production';

let mainWindow;
let addWindow;
let workerWindow;

// Listen for app to be ready
app.on('ready', function () {
  // Create new window
  mainWindow = new BrowserWindow({});
  // Load the HTML file into the window
  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'mainWindow.html'),
    protocol: 'file',
    slashes: true
  }));

  // Quit app when closed
  mainWindow.on('closed', function () {
    app.quit();
  })

  // Build menu from menu template
  const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
  // Insert menu
  Menu.setApplicationMenu(mainMenu);
});

// Handle create worker window
function createWorkerWindow() {
  mainWindow.webContents.send('status:showLoading'); // This does NOT work
  // Create new window
  workerWindow = new BrowserWindow({
    width: 300,
    height: 200,
    title: 'workerWindow',
    show: process.env.NODE_ENV == 'production' ? false : true
  });
  // Load the HTML file into the window
  workerWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'worker.html'),
    protocol: 'file',
    slashes: true
  }));
  // Garbage collection
  workerWindow.on('close', function () {
    workerWindow = null;
  })
  mainWindow.webContents.send('status:hideLoading'); // This does NOT work
}

// Catch item:add-worker
ipcMain.on('item:add-worker', function(e, item){
  console.log(item);
  mainWindow.webContents.send('item:add-worker', item);
  workerWindow.close();
});


// Create menu template
const mainMenuTemplate = [{
  label: 'File',
  submenu: [
    {
      label: 'Add Item from Worker',
      click() {
        mainWindow.webContents.send('status:showLoading'); // This does NOT work
        createWorkerWindow();      
      }
    },
    {
      label: 'Clear Items',
      click(){
        mainWindow.webContents.send('item:clear'); // This works
      }
    },
    {
      label: 'Show Loading',
      click(){
        mainWindow.webContents.send('status:showLoading'); // This works
      }
    },
    {
      label: 'Hide Loading',
      click(){
        mainWindow.webContents.send('status:hideLoading') // This works
      }
    },
    {
      label: 'Quit',
      accelerator: process.platform == 'darwin' ? 'Command+Q' : 'Ctrl+Q',
      click() {
        app.quit();
      }
    }
  ]
}];

當我使用“ Show Loading和“ Hide Loading項”菜單項時,主窗口會收到消息並執行所需的操作。 但是,當我單擊“ Add Item from Worker菜單項時,該消息似乎未到達主窗口。

 <!-- mainWindow.html --> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Security-Policy" content="default-src file: https:"> <title>Shopping List</title> <!-- Compiled and minified CSS --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css"> <link rel="stylesheet" href="mainWindow.css"> </head> <body> <nav> <div class="nav-wrapper"> <a class="brand-logo center">Shopping List</a> <div id="loadingIndicator" class="progress"> <div class="indeterminate"></div> </div> </div> </nav> <ul></ul> <script src="mainWindow.js"></script> </body> </html> 

//// mainWindow.js

// Show Loading
ipcRenderer.on('status:showLoading', function(){
    document.getElementById('loadingIndicator').style.display = 'block';
}); 

// Hide Loading
ipcRenderer.on('status:hideLoading', function(){
    document.getElementById('loadingIndicator').style.display = 'none';
});

我試圖添加mainWindow.webContents.send('status:showLoading'); 線一側的click()菜單項和內部功能createWorkerWindow()函數。 兩者似乎都不起作用。

任何見解將不勝感激。

謝謝。

問題在於,在瀏覽器窗口中加載URL是不同步的,因此當它運行createWorkerWindow ,它將發送status:showLoading消息,然后將創建工作器窗口並發送status:hideLoading 如果要在工作程序窗口完成加載后隱藏加載窗口,可以使用:

workerWindow.webContents.on('did-finish-load', function () {
    mainWindow.webContents.send('status:hideLoading');
});

盡管請記住,這將在加載任何URL而不只是第一個URL時運行。

did-finish-load文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM