summarylogtreecommitdiffstats
path: root/main.js
blob: e69a6f2842d65131e1698fb24ed466c159002a9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const {app, BrowserWindow, ipcMain} = require('electron');
const path = require('path');

function createWindow () {
    const mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            preload: path.join(__dirname, 'prompt.js')
        }
    });

    mainWindow.loadFile('index.html');

    // Electon does not support prompt()!
    // Using prompt from https://github.com/konsumer/electron-prompt
    var promptResponse;
    ipcMain.on('prompt', function(eventRet, arg) {
        promptResponse = null;
        var promptWindow = new BrowserWindow({
            width: 300,
            height: 100,
            show: false,
            resizable: false,
            movable: false,
            alwaysOnTop: true,
            frame: false,
            webPreferences: {
                nodeIntegration: true
            }
        });
        arg.val = arg.val || '';
        const promptHtml =
            '<script>\
            function ok() {\
                require(\'electron\').ipcRenderer.send(\'prompt-response\', document.getElementById(\'val\').value);\
                window.close();\
            }\
            </script>\
            <label for="val">' + arg.title + '</label>\
            <input onkeypress="if (window.event.which == 10 || window.event.which == 13) {ok();}" id="val" value="' + arg.val + '" autofocus />\
            <button onclick="ok();">Ok</button>\
            <button onclick="window.close()">Cancel</button>\
            <style>body {font-family: sans-serif;} button {float:right; margin-left: 10px;} label,input {margin-bottom: 10px; width: 100%; display:block;}</style>\
            <script>document.getElementById("val").select();</script>';
        promptWindow.loadURL('data:text/html,' + promptHtml);
        promptWindow.show();
        promptWindow.on('closed', function() {
            eventRet.returnValue = promptResponse;
            promptWindow = null;
        });
    })
    ipcMain.on('prompt-response', function(event, arg) {
        if (arg === '') arg = null;
        promptResponse = arg
    })
}

app.whenReady().then(() => {
    createWindow();
    app.on('activate', function() {
        // On macOS 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 (BrowserWindow.getAllWindows().length === 0) createWindow();
    })
})

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