Merge branch 'save-on-close'

This commit is contained in:
Cristian Maglie 2016-01-25 17:54:24 +01:00
commit c3d2bbdb86
2 changed files with 76 additions and 87 deletions

View File

@ -327,7 +327,7 @@ public class Base {
boolean showEditor = parser.isGuiMode();
if (!parser.isForceSavePrefs())
PreferencesData.setDoSave(showEditor);
if (handleOpen(file, nextEditorLocation(), showEditor, false) == null) {
if (handleOpen(file, retrieveSketchLocation(".default"), showEditor, false) == null) {
String mess = I18n.format(tr("Failed to open sketch: \"{0}\""), path);
// Open failure is fatal in upload/verify mode
if (parser.isVerifyOrUploadMode())
@ -489,32 +489,6 @@ public class Base {
* @throws Exception
*/
protected boolean restoreSketches() throws Exception {
// figure out window placement
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
boolean windowPositionValid = true;
if (PreferencesData.get("last.screen.height") != null) {
// if screen size has changed, the window coordinates no longer
// make sense, so don't use them unless they're identical
int screenW = PreferencesData.getInteger("last.screen.width");
int screenH = PreferencesData.getInteger("last.screen.height");
if ((screen.width != screenW) || (screen.height != screenH)) {
windowPositionValid = false;
}
/*
int windowX = Preferences.getInteger("last.window.x");
int windowY = Preferences.getInteger("last.window.y");
if ((windowX < 0) || (windowY < 0) ||
(windowX > screenW) || (windowY > screenH)) {
windowPositionValid = false;
}
*/
} else {
windowPositionValid = false;
}
// Iterate through all sketches that were open last time p5 was running.
// If !windowPositionValid, then ignore the coordinates found for each.
@ -534,13 +508,7 @@ public class Base {
// path unchanged.
}
}
int[] location;
if (windowPositionValid) {
String locationStr = PreferencesData.get("last.sketch" + i + ".location");
location = PApplet.parseInt(PApplet.split(locationStr, ','));
} else {
location = nextEditorLocation();
}
int[] location = retrieveSketchLocation("" + i);
// If file did not exist, null will be returned for the Editor
if (handleOpen(new File(path), location, nextEditorLocation(), true, false, false) != null) {
opened++;
@ -560,29 +528,56 @@ public class Base {
PreferencesData.setInteger("last.screen.width", screen.width);
PreferencesData.setInteger("last.screen.height", screen.height);
String untitledPath = untitledFolder.getAbsolutePath();
// If there is only one sketch opened save his position as default
if (editors.size() == 1) {
storeSketchLocation(editors.get(0), ".default");
}
// Save the sketch path and window placement for each open sketch
LinkedList<Editor> reverseEditors = new LinkedList<Editor>(editors);
Collections.reverse(reverseEditors);
String untitledPath = untitledFolder.getAbsolutePath();
List<Editor> reversedEditors = new LinkedList<>(editors);
Collections.reverse(reversedEditors);
int index = 0;
for (Editor editor : reverseEditors) {
String path = editor.getSketch().getMainFilePath();
// In case of a crash, save untitled sketches if they contain changes.
// (Added this for release 0158, may not be a good idea.)
if (path.startsWith(untitledPath) && !editor.getSketch().isModified()) {
for (Editor editor : reversedEditors) {
Sketch sketch = editor.getSketch();
String path = sketch.getMainFilePath();
// Skip untitled sketches if they do not contains changes.
if (path.startsWith(untitledPath) && !sketch.isModified()) {
continue;
}
PreferencesData.set("last.sketch" + index + ".path", path);
int[] location = editor.getPlacement();
String locationStr = PApplet.join(PApplet.str(location), ",");
PreferencesData.set("last.sketch" + index + ".location", locationStr);
storeSketchLocation(editor, "" + index);
index++;
}
PreferencesData.setInteger("last.sketch.count", index);
}
private void storeSketchLocation(Editor editor, String index) {
String path = editor.getSketch().getMainFilePath();
String loc = StringUtils.join(editor.getPlacement(), ',');
PreferencesData.set("last.sketch" + index + ".path", path);
PreferencesData.set("last.sketch" + index + ".location", loc);
}
private int[] retrieveSketchLocation(String index) {
if (PreferencesData.get("last.screen.height") == null)
return defaultEditorLocation();
// if screen size has changed, the window coordinates no longer
// make sense, so don't use them unless they're identical
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int screenW = PreferencesData.getInteger("last.screen.width");
int screenH = PreferencesData.getInteger("last.screen.height");
if ((screen.width != screenW) || (screen.height != screenH))
return defaultEditorLocation();
String locationStr = PreferencesData
.get("last.sketch" + index + ".location");
if (locationStr == null)
return defaultEditorLocation();
return PApplet.parseInt(PApplet.split(locationStr, ','));
}
protected void storeRecentSketches(Sketch sketch) {
if (sketch.isUntitled()) {
return;
@ -624,49 +619,46 @@ public class Base {
EditorConsole.setCurrentEditorConsole(activeEditor.console);
}
protected int[] nextEditorLocation() {
protected int[] defaultEditorLocation() {
int defaultWidth = PreferencesData.getInteger("editor.window.width.default");
int defaultHeight = PreferencesData.getInteger("editor.window.height.default");
Rectangle screen = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getBounds();
return new int[]{
(screen.width - defaultWidth) / 2,
(screen.height - defaultHeight) / 2,
defaultWidth, defaultHeight, 0
};
}
protected int[] nextEditorLocation() {
if (activeEditor == null) {
Rectangle screen = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getBounds();
// If no current active editor, use default placement
return new int[]{
(screen.width - defaultWidth) / 2,
(screen.height - defaultHeight) / 2,
defaultWidth, defaultHeight, 0
};
return defaultEditorLocation();
}
} else {
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
// With a currently active editor, open the new window
// using the same dimensions, but offset slightly.
synchronized (editors) {
final int OVER = 50;
// In release 0160, don't
//location = activeEditor.getPlacement();
Editor lastOpened = activeEditor;
int[] location = lastOpened.getPlacement();
// Just in case the bounds for that window are bad
location[0] += OVER;
location[1] += OVER;
// With a currently active editor, open the new window
// using the same dimensions, but offset slightly.
synchronized (editors) {
int[] location = activeEditor.getPlacement();
if (location[0] == OVER ||
location[2] == OVER ||
location[0] + location[2] > screen.width ||
location[1] + location[3] > screen.height) {
// Warp the next window to a randomish location on screen.
return new int[]{
(int) (Math.random() * (screen.width - defaultWidth)),
(int) (Math.random() * (screen.height - defaultHeight)),
defaultWidth, defaultHeight, 0
};
}
// Just in case the bounds for that window are bad
final int OVER = 50;
location[0] += OVER;
location[1] += OVER;
return location;
if (location[0] == OVER || location[2] == OVER
|| location[0] + location[2] > screen.width
|| location[1] + location[3] > screen.height) {
// Warp the next window to a randomish location on screen.
int[] l = defaultEditorLocation();
l[0] *= Math.random() * 2;
l[1] *= Math.random() * 2;
return l;
}
return location;
}
}
@ -896,12 +888,7 @@ public class Base {
// now that we're ready, show the window
// (don't do earlier, cuz we might move it based on a window being closed)
if (showEditor) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
editor.setVisible(true);
}
});
SwingUtilities.invokeLater(() -> editor.setVisible(true));
}
return editor;
@ -961,6 +948,8 @@ public class Base {
editor.internalCloseRunner();
if (editors.size() == 1) {
storeSketches();
// This will store the sketch count as zero
editors.remove(editor);
try {
@ -968,7 +957,6 @@ public class Base {
} catch (Exception e) {
//ignore
}
storeSketches();
rebuildRecentSketchesMenuItems();
// Save out the current prefs state

View File

@ -1,6 +1,7 @@
ARDUINO 1.6.8
[ide]
* Editor position is saved when closing with Alt+F4 or clicking on the "X" button. Thanks @willie68
* Fixed a NullPointerException when dealing with some rare combination of package_*.json files
* Fixed incorrect key bindings handling for changing tab. Thanks @matthijskooijman
* MacOSX: Fixed handling of add indent/remove indent shortcuts (CMD+[ and CMD+])