-
Notifications
You must be signed in to change notification settings - Fork 0
Electron Prompt on Startup #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
a098d09
e6ab9e7
fc57cde
3241310
bf76662
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
|
|
||
| body { | ||
| font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; | ||
| line-height: 1.5em; | ||
| color: #333; | ||
| background-color: #fff; | ||
| } | ||
|
|
||
| #container { | ||
| align-items: center; | ||
| justify-content: center; | ||
| /* display: flex; */ | ||
| height: 100%; | ||
| overflow: auto; | ||
|
|
||
| } | ||
|
|
||
| #form { | ||
| width: 100%; | ||
| } | ||
|
|
||
| #label { | ||
| max-width: 100%; | ||
| max-height: 100%; | ||
| margin-bottom: .8em; | ||
| padding: 0 .5em; | ||
| /* white-space: nowrap; */ | ||
| /* overflow: hidden; */ | ||
| /* text-overflow: ellipsis; */ | ||
| } | ||
|
|
||
| #data { | ||
| border-radius: 2px; | ||
| background: #fff; | ||
| width: 90%; | ||
| padding: .4em .5em; | ||
| border: 1px solid black; | ||
| min-height: 2em; | ||
| margin: 0 0 1.2em; | ||
| } | ||
|
|
||
| select#data { | ||
| height: 2em; | ||
| } | ||
|
|
||
| #data-container { | ||
| text-align: center; | ||
| } | ||
|
|
||
| #buttons { | ||
| text-align: right; | ||
| padding: 0 .5em 0 0; | ||
| } | ||
|
|
||
| #buttons > button { | ||
| border-radius: 2px; | ||
| border: 0; | ||
| margin: 0 0 0 .5em; | ||
| font-size: .8em; | ||
| line-height: 1em; | ||
| padding: .6em 1em | ||
| } | ||
|
|
||
| #ok { | ||
| background-color: #3879D9; | ||
| color: white; | ||
| } | ||
|
|
||
| #cancel { | ||
| background-color: #DDD; | ||
| color: black; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
|
|
||
| <head> | ||
| <link href="prompt.css" rel="stylesheet" /> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. index.css |
||
| </head> | ||
| <body> | ||
| <div id="container"> | ||
| <div id="form"> | ||
| <div id="label">...</div> | ||
| <div id="data-container"></div> | ||
| <div id="buttons"> | ||
| <button id="cancel">Cancel</button> | ||
| <button id="ok">OK</button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| <script src="prompt.js"></script> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. index.js? |
||
| </body> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| const electron = require('electron'); | ||
|
|
||
| const BrowserWindow = electron.BrowserWindow || electron.remote.BrowserWindow; | ||
| const ipcMain = electron.ipcMain || electron.remote.ipcMain; | ||
| const url = require('url'); | ||
| const path = require('path'); | ||
|
|
||
| function electronPrompt(options, parentWindow) { | ||
| return new Promise((resolve, reject) => { | ||
| const id = `${new Date().getTime()}-${Math.random()}`; | ||
|
|
||
| const opts = Object.assign( | ||
| { | ||
| width: 370, | ||
| height: 130, | ||
| resizable: false, | ||
| title: 'Prompt', | ||
| label: 'Please input a value:', | ||
| alwaysOnTop: false, | ||
| value: null, | ||
| type: 'input', | ||
| selectOptions: null, | ||
| icon: null | ||
| }, | ||
| options || {} | ||
| ); | ||
|
|
||
| if (opts.type === 'select' && (opts.selectOptions === null || typeof opts.selectOptions !== 'object')) { | ||
| return reject(new Error('"selectOptions" must be an object')); | ||
| } | ||
|
|
||
| let promptWindow = new BrowserWindow({ | ||
| width: opts.width, | ||
| height: opts.height, | ||
| resizable: opts.resizable, | ||
| parent: parentWindow, | ||
| skipTaskbar: true, | ||
| alwaysOnTop: opts.alwaysOnTop, | ||
| useContentSize: true, | ||
| modal: Boolean(parentWindow), | ||
| title: opts.title, | ||
| icon: opts.icon | ||
| }); | ||
|
|
||
| promptWindow.setMenu(null); | ||
|
|
||
| const getOptionsListener = event => { | ||
| event.returnValue = JSON.stringify(opts); | ||
| }; | ||
|
|
||
| const cleanup = () => { | ||
| if (promptWindow) { | ||
| promptWindow.close(); | ||
| promptWindow = null; | ||
| } | ||
| }; | ||
|
|
||
| const postDataListener = (event, value) => { | ||
| resolve(value); | ||
| event.returnValue = null; | ||
| cleanup(); | ||
| }; | ||
|
|
||
| const unresponsiveListener = () => { | ||
| reject(new Error('Window was unresponsive')); | ||
| cleanup(); | ||
| }; | ||
|
|
||
| const errorListener = (event, message) => { | ||
| reject(new Error(message)); | ||
| event.returnValue = null; | ||
| cleanup(); | ||
| }; | ||
|
|
||
| ipcMain.on('prompt-get-options:' + id, getOptionsListener); | ||
| ipcMain.on('prompt-post-data:' + id, postDataListener); | ||
| ipcMain.on('prompt-error:' + id, errorListener); | ||
| promptWindow.on('unresponsive', unresponsiveListener); | ||
|
|
||
| promptWindow.on('closed', () => { | ||
| ipcMain.removeListener('prompt-get-options:' + id, getOptionsListener); | ||
| ipcMain.removeListener('prompt-post-data:' + id, postDataListener); | ||
| ipcMain.removeListener('prompt-error:' + id, postDataListener); | ||
| resolve(null); | ||
| }); | ||
|
|
||
| const promptUrl = url.format({ | ||
| protocol: 'file', | ||
| slashes: true, | ||
| pathname: path.join(__dirname, 'page', 'prompt.html'), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no 'page' |
||
| hash: id | ||
| }); | ||
|
|
||
| promptWindow.loadURL(promptUrl); | ||
| }); | ||
| } | ||
|
|
||
| module.exports = electronPrompt; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. export default electronPrompt |
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pass in Windows.main as the 2nd param (parentWindow)