Use Electron API for file dialogs

This commit is contained in:
William Petit 2015-10-10 14:34:14 +02:00
parent e41329e28f
commit 2b419997f1
3 changed files with 57 additions and 37 deletions

View File

@ -11,9 +11,11 @@
{ {
"label": "Chromium Browser 1", "label": "Chromium Browser 1",
"icon": "chromium-browser", "icon": "chromium-browser",
"exec": "/usr/bin/chromium-browser" "exec": "/usr/bin/chromium-browser",
"_key": "item_1444480285022_3"
} }
] ],
"_key": "item_1444480285022_2"
}, },
{ {
"label": "Level 2-2", "label": "Level 2-2",
@ -26,13 +28,24 @@
{ {
"label": "Chromium Browser 2", "label": "Chromium Browser 2",
"icon": "chromium-browser", "icon": "chromium-browser",
"exec": "/usr/bin/chromium-browser" "exec": "/usr/bin/chromium-browser",
"_key": "item_1444480285022_6"
},
{
"label": "Atom",
"icon": "atom",
"exec": "/usr/share/atom/atom %U",
"_key": "item_1444480288996_7"
} }
] ],
"_key": "item_1444480285022_5"
} }
] ],
"_key": "item_1444480285022_4"
} }
] ],
"_key": "item_1444480285022_1"
} }
] ],
"_key": "item_1444480285021_0"
} }

View File

@ -1,6 +1,7 @@
var React = require('react'); var React = require('react');
var connect = require('react-redux').connect; var connect = require('react-redux').connect;
var actions = require('../../store/actions'); var actions = require('../../store/actions');
var dialog = require('remote').require('dialog');
var ProfileMenu = React.createClass({ var ProfileMenu = React.createClass({
@ -8,7 +9,6 @@ var ProfileMenu = React.createClass({
return ( return (
<div className="profile-menu"> <div className="profile-menu">
<input ref="fileInput" style={{display: 'none'}} filter=".json" type="file" />
<button className="btn btn-default" onClick={this.handleOpenClick}>Ouvrir</button> <button className="btn btn-default" onClick={this.handleOpenClick}>Ouvrir</button>
<button className="btn btn-primary" onClick={this.handleSaveClick}>Enregistrer</button> <button className="btn btn-primary" onClick={this.handleSaveClick}>Enregistrer</button>
</div> </div>
@ -18,9 +18,9 @@ var ProfileMenu = React.createClass({
handleOpenClick: function() { handleOpenClick: function() {
var dispatch = this.props.dispatch; var dispatch = this.props.dispatch;
this.showFileDialog() this.showOpenProfileDialog()
.then(function(profilePath) { .then(function(profilePath) {
dispatch(actions.common.loadProfile(profilePath)); if(profilePath) dispatch(actions.common.loadProfile(profilePath));
}) })
; ;
}, },
@ -31,39 +31,44 @@ var ProfileMenu = React.createClass({
var profile = this.props.profile; var profile = this.props.profile;
var profilePath = this.props.profilePath; var profilePath = this.props.profilePath;
var promise = profilePath ? Promise.resolve(profilePath) : this.showFileDialog(true); var promise = profilePath ? Promise.resolve(profilePath) : this.showSaveProfileDialog();
promise.then(function(profilePath) { promise.then(function(profilePath) {
dispatch(actions.edit.saveProfile(profilePath, profile)); if(profilePath) dispatch(actions.edit.saveProfile(profilePath, profile));
}); });
}, },
showFileDialog: function(saveAs) { showOpenProfileDialog: function() {
var fileInput = this.refs.fileInput.getDOMNode(); return new Promise(function(resolve) {
dialog.showOpenDialog(
// Toggle 'save as' feature {
if(saveAs) { title: 'Éditer un profil',
fileInput.nwsaveas = true; filters: [ {name: 'Profils Pitaya', extensions: ['json'] } ],
} else { properties: ['openFile']
fileInput.removeAttribute('nwsaveas'); },
function(files) {
return resolve(files ? files[0] : null);
} }
)
return new Promise(function(resolve, reject) {
fileInput.addEventListener('change', handleChange, false);
fileInput.click();
function handleChange(evt) {
fileInput.removeEventListener('change', handleChange);
var value = this.value;
this.value = null;
resolve(value);
}
}); });
},
showSaveProfileDialog: function() {
return new Promise(function(resolve) {
dialog.showSaveDialog(
{
title: 'Éditer un profil',
filters: [ {name: 'Profils Pitaya', extensions: ['json'] } ]
},
function(file) {
return resolve(file);
}
)
});
} }

View File

@ -21,8 +21,10 @@ app.on('ready', function() {
type: asDesktop ? 'desktop' : undefined, type: asDesktop ? 'desktop' : undefined,
'skip-taskbar': asDesktop, 'skip-taskbar': asDesktop,
frame: !asDesktop, frame: !asDesktop,
width: size.width, width: asDesktop ? size.width : undefined,
height: size.height height: asDesktop ? size.height : undefined,
x: asDesktop ? 0 : undefined,
y: asDesktop ? 0 : undefined,
}); });
if(process.env.NODE_ENV === 'development') { if(process.env.NODE_ENV === 'development') {