2017-08-25 109 views
1

我試圖做到的,是在一個意義上的「真正的」應用程序類似的行爲,當我結束在MacOS(按X的應用程序,因此應用程序仍然在碼頭),然後再次從碼頭打開應用程序,網頁內容應該立即在那裏。正如我試圖建立一個容器的web應用程序,我得到的行爲是我每次打開應用程序時,網頁加載再次引起了摩擦UX。飼養頁面內容加載中時電子關閉窗口

我已經卸載窗口之前嘗試了一些骯髒的解決方法,就像從演示過程中主窗口調用.hide():

const {remote} = require('electron'); 
const main = remote.require('./main.js'); 

window.onbeforeunload = e => { 
    main.hideWindow(); 
    e.returnValue = false; 
}; 

和主要工藝

exports.hideWindow =() => { 
    mainWindow.hide(); 
}; 

但是,我無法退出我的應用程序。

我考慮的另一個選擇是加載整個mainWindow DOM在內存中,然後打開應用程序時,在<webview>preload script加載緩存內容到webview中,一旦加載頁面,覆蓋webview內容,但它也似乎非常黑客。

我知道Slack的行爲完全如何,我希望我的應用程序的行爲,但我很努力地找到他們如何實現即時加載(或者可能永遠不會關閉,除非從Dock或Cmd + Q中選擇退出被擊中)。

回答

0

我最後不得不支配了「關閉」的行爲

let allowQuitting = false; 

標誌和處理這樣

function quitApp() { 
    if (app) { 
    allowQuitting = true; 
    app.quit(); 
    } 
} 
function closeApp() { 
    if (mainWindow && !mainWindow.isDestroyed()) { 
    mainWindow.hide(); 
    } 
} 

在收盤的最後,我聽在關閉事件

function createWindow() { 
    mainWindow.on('closed', function() { 
    closeApp(); 
    }); 

    mainWindow.on('close', event => { 
    if (allowQuitting === false) { 
     event.preventDefault(); 
     closeApp(); 
    } 
    }); 
    ... 
} 
app.on('ready', createWindow); 

在激活,我首先檢查窗口存在

app.on('activate', function() { 
    if (mainWindow === null) { 
    createWindow(); 
    } else { 
    mainWindow.show(); 
    } 
}); 

應用程序可以使用Cmd的+ Q由於加速器關閉:

const template = [{ 
    label: 'Application', 
    submenu: [ 
    ... 
    { 
     label: 'Quit', accelerator: 'Command+Q', click:() => quitApp() 
    } 
    ] 
}, 
... 
]; 
Menu.setApplicationMenu(Menu.buildFromTemplate(template)); 

這使我期望的結果,儘管有副作用。該應用程序可用Cmd + Q關閉,,但不能通過碼頭(通過選擇退出)或關閉系統(表示中斷關閉)關閉。

0

如果我理解你的問題正確的話,我覺得有幾個標準的變通辦法此的。特別是圍繞

...永遠不會關閉,退出時除外從Dock或 Cmd的+ Q選擇被擊中)

// Quit when all windows are closed. 
app.on('window-all-closed',() => { 
    // On OS X it is common for applications and their menu bar 
    // to stay active until the user quits explicitly with Cmd + Q 
    if (process.platform !== 'darwin') { 
    app.quit() 
    } 
}) 

而且......

再次打開從被告席上的應用程序,網頁內容時,應立即有 。

app.on('activate',() => { 
    // On OS X it's common to re-create a window in the app when the 
    // dock icon is clicked and there are no other windows open. 
    if (mainWindow === null) { 
    createWindow() 
    } 
}) 
+0

是的,你已經概述了快速啓動應用程序的行爲,導致在應用程序激活時重新加載頁面,這是我試圖避免的現象 – Lightheaded

+0

對不起,也許我誤解了,如果這是代碼導致你試圖避免的現象,那麼肯定你正在做類似的事情......並且需要刪除它。沒有? – Fraser

+0

問題是,窗口關閉(與關閉瀏覽器相同)。打開它後,調用'createWindow()',webview再次加載頁面,導致應用程序短暫延遲。我想通過緩存應用程序內容或以某種方式解決關閉窗口而不是隱藏它來擺脫這種延遲。 – Lightheaded