Electron์ beforeunload
์ด๋ฒคํธ๋ ๋ธ๋ผ์ฐ์ ์ ๋ค๋ฅด๊ฒ ์๋ํฉ๋๋ค. ๋ฌธ์์ด์ ๋ฐํํ๋ฉด ํ์ธ ๋ํ ์์์ ํ์๋์ง ์์ต๋๋ค. ๋๋์์ ๊ฒ์๋ณด๊ณ BrowserWindow
์ฌ์ฉ์ ์๋ฅผ ์ ๊ณต beforeunload
ํ๊ณ ๋น์ ์ด ๊ทธ๊ฒ์ ํ์ธ ๋ํ ์์๋ฅผ ํ์ ํ ์ ์๋ค๋ ์๊ฒฌ์ ์ธ๊ธ ๊ทธ๊ฒ์๋ฅผ :
// ๋ํ API๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์๊ฐ ์์ฉ ํ๋ก๊ทธ๋จ ๋ซ๊ธฐ๋ฅผ ํ์ธํ๋๋ก ํ ์๋ ์์ต๋๋ค.
Dialog API๋ ๋ฉ์ธ ํ๋ก์ธ์ค์์๋ง ์ฌ์ฉํ ์ ์๋์?
์ ์์ ์ธ ๋ธ๋ผ์ฐ์ ๋์์ ๋ณต์ ํ๋ ๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ ๋ฌด์์
๋๊น?
๊ด์ฐฎ์์. ๋๋ ๊ทธ๊ฒ์ ์์.
๋์ค์ ์ฐธ์กฐํ ์ ์๋๋ก ์๋ฅผ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
window.onbeforeunload = function(e) {
var remote = require('remote');
var dialog = remote.require('dialog');
var choice = dialog.showMessageBox(
remote.getCurrentWindow(),
{
type: 'question',
buttons: ['Yes', 'No'],
title: 'Confirm',
message: 'Are you sure you want to quit?'
});
return choice === 0;
};
์๋
ํ์ธ์(:
์์์ ์ค๋ช
ํ ์๋ฃจ์
์ ์ ์๊ฒ ํจ๊ณผ๊ฐ ์์ต๋๋ค.
์ฌ์ ํ ๋ธ๋ผ์ฐ์ ์ "onbeforunload"๋ฅผ ์ทจํ๊ธฐ ๋๋ฌธ์ ...
์ด๋ค ์๊ฐ?
@shneorasa ๊ทํ๊ฐ ์์ฒญํ ์ง ๋ช ๊ฐ์์ด window.onbeforeload
๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๊ณผ ๋ฐ๋๋ก ์ด ๋ฌธ์ ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ ๋์ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค
beforeunload
์ด๋ฒคํธ๋ฅผ ๋ฎ์ด์ฐ๋ฉด ํฅํ ๋ํ๊ฐ ์๋ง์ด ๋์ด ์ฑ์ด ์์๋ ์ ์์ต๋๋ค. ๋์ ๋ด๊ฐ ํ ๊ฒ์ฒ๋ผ ์ฑ ๋ด๋ถ์ ๊ฐ๋จํ ๋ถ์ธ ๊ฐ์ ์ ์ฅํ ์ ์์ต๋๋ค.
// Inside main/index.js
import { app, BrowserWindow, dialog } from 'electron'
app.showExitPrompt = true
์ด๋ฅผ ํตํด ๋ค์๊ณผ ๊ฐ์ด BrowserWindow
์ close
์ด๋ฒคํธ๋ฅผ ์ค๋จํ ์ ์์ต๋๋ค.
// Inside main/index.js, where BrowserWindow is initialized
mainWindow.on('close', (e) => {
if (app.showExitPrompt) {
e.preventDefault() // Prevents the window from closing
dialog.showMessageBox({
type: 'question',
buttons: ['Yes', 'No'],
title: 'Confirm',
message: 'Unsaved data will be lost. Are you sure you want to quit?'
}, function (response) {
if (response === 0) { // Runs the following if 'Yes' is clicked
app.showExitPrompt = false
mainWindow.close()
}
})
}
})
Vue.js๋ฅผ ์ฌ์ฉํ์ฌ ์ด ์๋ฃจ์
์ Electron ์ฑ์ ๊ตฌํํ์ผ๋ฉฐ ๋ํ ์์ ํ์/์จ๊ธฐ๊ธฐ๋ app
๊ฐ์ ธ์ค๊ธฐ๋งํผ ๊ฐ๋จํฉ๋๋ค.
const { app } = require('electron').remote
์ ์ฅ๋ ๋ถ์ธ ๊ฐ์ ๋ฎ์ด์๋๋ค.
app.showExitPrompt = true
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
@shneorasa ๊ทํ๊ฐ ์์ฒญํ ์ง ๋ช ๊ฐ์์ด
window.onbeforeload
๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๊ณผ ๋ฐ๋๋ก ์ด ๋ฌธ์ ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ ๋์ ๋ฐฉ๋ฒ์ด ์์ต๋๋คbeforeunload
์ด๋ฒคํธ๋ฅผ ๋ฎ์ด์ฐ๋ฉด ํฅํ ๋ํ๊ฐ ์๋ง์ด ๋์ด ์ฑ์ด ์์๋ ์ ์์ต๋๋ค. ๋์ ๋ด๊ฐ ํ ๊ฒ์ฒ๋ผ ์ฑ ๋ด๋ถ์ ๊ฐ๋จํ ๋ถ์ธ ๊ฐ์ ์ ์ฅํ ์ ์์ต๋๋ค.์ด๋ฅผ ํตํด ๋ค์๊ณผ ๊ฐ์ด
BrowserWindow
์close
์ด๋ฒคํธ๋ฅผ ์ค๋จํ ์ ์์ต๋๋ค.Vue.js๋ฅผ ์ฌ์ฉํ์ฌ ์ด ์๋ฃจ์ ์ Electron ์ฑ์ ๊ตฌํํ์ผ๋ฉฐ ๋ํ ์์ ํ์/์จ๊ธฐ๊ธฐ๋
app
๊ฐ์ ธ์ค๊ธฐ๋งํผ ๊ฐ๋จํฉ๋๋ค.์ ์ฅ๋ ๋ถ์ธ ๊ฐ์ ๋ฎ์ด์๋๋ค.