-
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
Open
atifmmahmud
wants to merge
5
commits into
master
Choose a base branch
from
atif-electron-prompt
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a098d09
The electron prompt appears on app launch
atifmmahmud e6ab9e7
Cleanup: using local files instead of npm package
atifmmahmud fc57cde
Changes based on PR comments
atifmmahmud 3241310
Moved files around - now have both main and rendrer process. Error: …
atifmmahmud bf76662
Renamed to main
atifmmahmud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
|
|
||
| export const main = require('./main') | ||
| export const main = require('./main') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, 'index.html'), | ||
| hash: id | ||
| }); | ||
|
|
||
| promptWindow.loadURL(promptUrl); | ||
| }); | ||
| } | ||
|
|
||
| module.exports = electronPrompt; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| 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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
|
|
||
| <html> | ||
| <head> | ||
| <link href="index.css" rel="stylesheet" /> | ||
| </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="index.js"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| const {ipcRenderer} = require('electron'); | ||
| const docReady = require('doc-ready'); | ||
|
|
||
| let promptId = null; | ||
| let promptOptions = null; | ||
|
|
||
| const promptError = e => { | ||
| if (e instanceof Error) { | ||
| e = e.message; | ||
| } | ||
| ipcRenderer.sendSync('prompt-error:' + promptId, e); | ||
| }; | ||
|
|
||
| const promptCancel = () => { | ||
| ipcRenderer.sendSync('prompt-post-data:' + promptId, null); | ||
| }; | ||
|
|
||
| const promptSubmit = () => { | ||
| const dataEl = document.getElementById('data'); | ||
| let data = null; | ||
|
|
||
| if (promptOptions.type === 'input') { | ||
| data = dataEl.value; | ||
| } else if (promptOptions.type === 'select') { | ||
| if (promptOptions.selectMultiple) { | ||
| data = dataEl.querySelectorAll('option[selected]').map(o => o.getAttribute('value')); | ||
| } else { | ||
| data = dataEl.value; | ||
| } | ||
| } | ||
| ipcRenderer.sendSync('prompt-post-data:' + promptId, data); | ||
| }; | ||
|
|
||
|
|
||
| window.addEventListener('error', error => { | ||
| if (promptId) { | ||
| promptError('An error has occured on the prompt window: \n' + error); | ||
| } | ||
| }); | ||
|
|
||
| docReady(() => { | ||
| promptId = document.location.hash.replace('#', ''); | ||
|
|
||
| try { | ||
| promptOptions = JSON.parse(ipcRenderer.sendSync('prompt-get-options:' + promptId)); | ||
| } catch (e) { | ||
| return promptError(e); | ||
| } | ||
|
|
||
| document.getElementById('label').textContent = promptOptions.label; | ||
| document.getElementById('ok').addEventListener('click', () => promptSubmit()); | ||
| document.getElementById('cancel').addEventListener('click', () => promptCancel()); | ||
|
|
||
| const dataContainerEl = document.getElementById('data-container'); | ||
|
|
||
| let dataEl; | ||
| if (promptOptions.type === 'input') { | ||
| dataEl = document.createElement('input'); | ||
| dataEl.setAttribute('type', 'text'); | ||
|
|
||
| if (promptOptions.value) { | ||
| dataEl.value = promptOptions.value; | ||
| } else { | ||
| dataEl.value = ''; | ||
| } | ||
|
|
||
| if (promptOptions.inputAttrs && typeof (promptOptions.inputAttrs) === 'object') { | ||
| for (const k in promptOptions.inputAttrs) { | ||
| if (!Object.prototype.hasOwnProperty.call(promptOptions.inputAttrs, k)) { | ||
| continue; | ||
| } | ||
|
|
||
| dataEl.setAttribute(k, promptOptions.inputAttrs[k]); | ||
| } | ||
| } | ||
|
|
||
| dataEl.addEventListener('keyup', e => { | ||
| e.which = e.which || e.keyCode; | ||
| if (e.which === 13) { | ||
| promptSubmit(); | ||
| } | ||
| if (e.which === 27) { | ||
| promptCancel(); | ||
| } | ||
| }); | ||
| } else if (promptOptions.type === 'select') { | ||
| dataEl = document.createElement('select'); | ||
| let optionEl; | ||
|
|
||
| for (const k in promptOptions.selectOptions) { | ||
| if (!Object.prototype.hasOwnProperty.call(promptOptions.selectOptions, k)) { | ||
| continue; | ||
| } | ||
|
|
||
| optionEl = document.createElement('option'); | ||
| optionEl.setAttribute('value', k); | ||
| optionEl.textContent = promptOptions.selectOptions[k]; | ||
| if (k === promptOptions.value) { | ||
| optionEl.setAttribute('selected', 'selected'); | ||
| } | ||
| dataEl.appendChild(optionEl); | ||
| } | ||
| } | ||
|
|
||
| dataContainerEl.appendChild(dataEl); | ||
| dataEl.setAttribute('id', 'data'); | ||
|
|
||
| dataEl.focus(); | ||
| if (promptOptions.type === 'input') { | ||
| dataEl.select(); | ||
| } | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Missing dependency?