使用窗口管理器中的Electron窗口对象

时间:2017-07-12 01:53:48

标签: window electron

我试图将消息发送到我的主窗口,如显示和隐藏,但它一直说我的主窗口未定义。我不确定为什么。当我尝试在窗口管理器上分配open的结果时,api说它返回了对象,但这似乎并不能理解我的理解。你能帮助我吗?

const electron = require('electron')
const Menu = require('electron').Menu
var path = require('electron').path
var windowManager = require('electron-window-manager');
const app = electron.app
const BrowserWindow = electron.BrowserWindow
var Tray = require('electron').Tray
var nativeImage = require('electron').nativeImage
var tray = null
var myValue;
var mainWindow;
const notifier = require('node-notifier');
var notified = 0;

// Modify the user agent for all requests to the following urls.
const filter = {
  urls: ['https://*.github.com/*', '*://electron.github.io']
}

app.on('ready', function(){
    var icon1 = nativeImage.createFromPath(`${__dirname}/../build/timer.png`)
    tray = new Tray(icon1)
    tray.setToolTip('Timer')
    var icon2 = nativeImage.createFromPath(`${__dirname}/../build/timer.png`)

    var contextMenu = Menu.buildFromTemplate([

    { label: 'Open Window', click:  function(){
        windowManager.get('home').show();
    } },
    { label: 'Exit', click:  function(){
        app.isQuiting = true;
        app.quit();

    } 
    }
]);
    tray.setContextMenu(contextMenu)
    tray.setImage(icon1)
    tray.on('click', () => {
    windowManager.open('home', 'welcome', `file://${__dirname}/index.html`)
})
        windowManager.init({
        'onclose': function(event){
            event.preventDefault()
        }

    });
    // Open a window
    windowManager.open('home', 'welcome', `file://${__dirname}/index.html`);
    //mainWindow.loadURL(`file://${__dirname}/index.html`)

    notifier.on('click', function(notifierObject, options) {
               windowManager.get("home").show();
               notified = 0;
    })

    notifier.on('timeout', function (notifierObject, options) {
  // Triggers if `wait: true` and notification closes 
        mainWindow.show();
               notified = 0;
});
        /*
    mainWindow.on('minimize',function(event){
        event.preventDefault()
        mainWindow.hide();
    });

mainWindow.on('close', function (event) {
    if( !app.isQuiting){
        event.preventDefault()
        mainWindow.hide();
    }
    return false;
});
*/
})

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 windowManager.createNew(....)只给你一个窗口对象,为了得到一个浏览器窗口,它允许你显示,隐藏和添加监听器,你做这样的事情

mainWindow = windowManager.createNew('home', 'welcome', file://${__dirname}/index.html);
mainWindow.create();
mainWindow.object.show();