Skip to content

Commit 64c5a9d

Browse files
committed
Electron: Add hooks to Settings to launch file dialogs
When the input text elements for the Settings Compiler path or Sketch path are clicked it will launch the Electron native file and folder browsers to select a path for the selected setting.
1 parent b6afda1 commit 64c5a9d

File tree

3 files changed

+67
-18
lines changed

3 files changed

+67
-18
lines changed

ardublockly/ardublockly.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,32 +92,34 @@ Ardublockly.bindActionFunctions = function() {
9292
Ardublockly.bindClick_('button_load_xml', Ardublockly.XmlTextareaToBlocks);
9393
Ardublockly.bindClick_('button_toggle_toolbox', Ardublockly.toogleToolbox);
9494

95-
// Settings modal input field listeners
95+
// Settings modal input field listeners only if they can be edited
9696
var settingsPathInputListeners = function(elId, setValFunc, setHtmlCallback) {
9797
var el = document.getElementById(elId);
98-
// Event listener that send the data when the user presses 'Enter'
99-
el.onkeypress = function(e) {
100-
if (!e) e = window.event;
101-
var keyCode = e.keyCode || e.which;
102-
if (keyCode == '13') {
98+
if (el.readOnly === false) {
99+
// Event listener that send the data when the user presses 'Enter'
100+
el.onkeypress = function(e) {
101+
if (!e) e = window.event;
102+
var keyCode = e.keyCode || e.which;
103+
if (keyCode == '13') {
104+
setValFunc(el.value, function(jsonObj) {
105+
setHtmlCallback(ArdublocklyServer.jsonToHtmlTextInput(jsonObj));
106+
});
107+
return false;
108+
}
109+
};
110+
// Event listener that send the data when moving out of the input field
111+
el.onblur = function() {
103112
setValFunc(el.value, function(jsonObj) {
104113
setHtmlCallback(ArdublocklyServer.jsonToHtmlTextInput(jsonObj));
105114
});
106-
return false;
107-
}
108-
};
109-
// Event listener that send the data when moving out of the input field
110-
el.onblur = function(e) {
111-
setValFunc(el.value, function(jsonObj) {
112-
setHtmlCallback(ArdublocklyServer.jsonToHtmlTextInput(jsonObj));
113-
});
114-
};
115+
};
116+
}
115117
};
116118
settingsPathInputListeners('settings_compiler_location',
117119
ArdublocklyServer.setCompilerLocation,
118120
Ardublockly.setCompilerLocationHtml);
119121
settingsPathInputListeners('settings_sketch_location',
120-
ArdublocklyServer.setSketchLocation,
122+
ArdublocklyServer.setSketchLocationHtml,
121123
Ardublockly.setSketchLocationHtml);
122124
};
123125

ardublockly/ardublockly_desktop.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ Ardublockly.isRunningElectron = function() {
2828
*/
2929
(function loadJsInElectron(){
3030
if (Ardublockly.isRunningElectron()) {
31-
var projectLocator = require('electron').remote.require('./projectlocator.js');
31+
var projectLocator = require('electron').remote.require(
32+
'./projectlocator.js');
3233
var projectRoot = projectLocator.getProjectRootPath();
3334
window.$ = window.jQuery = require(projectRoot +
3435
'/ardublockly/js_libs/jquery-2.1.3.min.js');
@@ -72,6 +73,49 @@ Ardublockly.htmlPrompt = function(message, defaultValue, callback) {
7273
window.location.hash = '';
7374
};
7475

76+
/**
77+
* Add click listeners to the Compiler and Sketch input fields to launch the
78+
* Electron file/folder browsers.
79+
*/
80+
Ardublockly.bindSettingsPathInputs = function() {
81+
var dialog = require('electron').remote.dialog;
82+
83+
// Compiler path
84+
var compilerEl = document.getElementById('settings_compiler_location');
85+
compilerEl.readOnly = true;
86+
Ardublockly.bindClick_(compilerEl, function() {
87+
dialog.showOpenDialog({
88+
title: 'Select the Arduino IDE executable',
89+
buttonLabel: 'Select',
90+
properties: ['openFile']
91+
}, function (files) {
92+
if (files && files[0]) {
93+
ArdublocklyServer.setCompilerLocation(files[0], function(jsonObj) {
94+
Ardublockly.setCompilerLocationHtml(
95+
ArdublocklyServer.jsonToHtmlTextInput(jsonObj));
96+
});
97+
}
98+
})
99+
});
100+
// Sketch path
101+
var sketchEl = document.getElementById('settings_sketch_location');
102+
sketchEl.readOnly = true;
103+
Ardublockly.bindClick_(sketchEl, function() {
104+
dialog.showOpenDialog({
105+
title: 'Select the Arduino IDE executable',
106+
buttonLabel: 'Select',
107+
properties: ['openDirectory']
108+
}, function (folders) {
109+
if (folders && folders[0]) {
110+
ArdublocklyServer.setSketchLocation(folders[0], function(jsonObj) {
111+
Ardublockly.setSketchLocationHtml(
112+
ArdublocklyServer.jsonToHtmlTextInput(jsonObj));
113+
});
114+
}
115+
})
116+
});
117+
};
118+
75119
/** Initialize Ardublockly code required for Electron on page load. */
76120
window.addEventListener('load', function load(event) {
77121
window.removeEventListener('load', load, false);
@@ -80,6 +124,9 @@ window.addEventListener('load', function load(event) {
80124
Ardublockly.containerFullWidth();
81125
Ardublockly.hideSideMenuButton();
82126

127+
// Open the file or directory browsers when clicking on the Settings inputs
128+
Ardublockly.bindSettingsPathInputs();
129+
83130
// Prevent browser zoom changes like pinch-to-zoom
84131
var webFrame = require('electron').webFrame;
85132
webFrame.setZoomLevelLimits(1, 1);

package/electron/app/appmenu.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ var getExamplesMenuData = function() {
234234
BrowserWindow.getFocusedWindow().webContents
235235
.executeJavaScript(
236236
'Ardublockly.loadServerXmlFile("../examples/' +
237-
'serial_print_ascii_.xml");');
237+
'serial_print_ascii.xml");');
238238
}
239239
}, {
240240
label: 'Serial Repeat Game',

0 commit comments

Comments
 (0)