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",
"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",
@ -26,13 +28,24 @@
{
"label": "Chromium Browser 2",
"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 connect = require('react-redux').connect;
var actions = require('../../store/actions');
var dialog = require('remote').require('dialog');
var ProfileMenu = React.createClass({
@ -8,7 +9,6 @@ var ProfileMenu = React.createClass({
return (
<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-primary" onClick={this.handleSaveClick}>Enregistrer</button>
</div>
@ -18,9 +18,9 @@ var ProfileMenu = React.createClass({
handleOpenClick: function() {
var dispatch = this.props.dispatch;
this.showFileDialog()
this.showOpenProfileDialog()
.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 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) {
dispatch(actions.edit.saveProfile(profilePath, profile));
if(profilePath) dispatch(actions.edit.saveProfile(profilePath, profile));
});
},
showFileDialog: function(saveAs) {
var fileInput = this.refs.fileInput.getDOMNode();
// Toggle 'save as' feature
if(saveAs) {
fileInput.nwsaveas = true;
} else {
fileInput.removeAttribute('nwsaveas');
}
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);
}
showOpenProfileDialog: function() {
return new Promise(function(resolve) {
dialog.showOpenDialog(
{
title: 'Éditer un profil',
filters: [ {name: 'Profils Pitaya', extensions: ['json'] } ],
properties: ['openFile']
},
function(files) {
return resolve(files ? files[0] : null);
}
)
});
},
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,
'skip-taskbar': asDesktop,
frame: !asDesktop,
width: size.width,
height: size.height
width: asDesktop ? size.width : undefined,
height: asDesktop ? size.height : undefined,
x: asDesktop ? 0 : undefined,
y: asDesktop ? 0 : undefined,
});
if(process.env.NODE_ENV === 'development') {