From 2c089044c8073f5fc9029f7527e9aa4cb66bdcb3 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 12 Jan 2016 23:31:26 +0100 Subject: [PATCH 1/5] Small cosmetics --- app/src/processing/app/Base.java | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index f7af1aa33..75a720e79 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -563,21 +563,22 @@ public class Base { String untitledPath = untitledFolder.getAbsolutePath(); // Save the sketch path and window placement for each open sketch - LinkedList reverseEditors = new LinkedList(editors); - Collections.reverse(reverseEditors); + String untitledPath = untitledFolder.getAbsolutePath(); + List 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); + int[] placement = editor.getPlacement(); + String location = PApplet.join(PApplet.str(placement), ","); + PreferencesData.set("last.sketch" + index + ".location", location); index++; } PreferencesData.setInteger("last.sketch.count", index); @@ -896,12 +897,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; From c98c18089ddc9becf4e2e1e3000cf5c6421f1e0f Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 12 Jan 2016 23:31:45 +0100 Subject: [PATCH 2/5] Save window position when closing app from "x" button or Alt+F4 Fix #4404 Fix #4218 --- app/src/processing/app/Base.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 75a720e79..48f39bc03 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -957,6 +957,8 @@ public class Base { editor.internalCloseRunner(); if (editors.size() == 1) { + storeSketches(); + // This will store the sketch count as zero editors.remove(editor); try { @@ -964,7 +966,6 @@ public class Base { } catch (Exception e) { //ignore } - storeSketches(); rebuildRecentSketchesMenuItems(); // Save out the current prefs state From f5f478ced69dd3a78e348445e81bb3d97dc3480d Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 22 Jan 2016 17:47:14 +0100 Subject: [PATCH 3/5] Factored out method to save sketch location --- app/src/processing/app/Base.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 48f39bc03..921e065f2 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -574,16 +574,19 @@ public class Base { if (path.startsWith(untitledPath) && !sketch.isModified()) { continue; } - PreferencesData.set("last.sketch" + index + ".path", path); - - int[] placement = editor.getPlacement(); - String location = PApplet.join(PApplet.str(placement), ","); - PreferencesData.set("last.sketch" + index + ".location", location); + 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); + } + protected void storeRecentSketches(Sketch sketch) { if (sketch.isUntitled()) { return; From 61f11f60e151c0ddca037ced8483bfe1729f1063 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 22 Jan 2016 17:52:13 +0100 Subject: [PATCH 4/5] Factored out logic to retrieve editor placement The check for "resolution-changed" is performed when an editor location is retrieved from preferences. This commit rationalize access to PreferencesData and prepares for the next improvement. --- app/src/processing/app/Base.java | 117 ++++++++++++++----------------- 1 file changed, 51 insertions(+), 66 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 921e065f2..f08d61517 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -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++; @@ -587,6 +555,26 @@ public class Base { 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; @@ -628,49 +616,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; } } From 0bceb93b7c9d78ace1f2439fc9fcaa04130fb2e8 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 22 Jan 2016 17:55:27 +0100 Subject: [PATCH 5/5] When closing the last sketch, the window position is saved as default This allows the use-case with users editing one sketch at a time, that seems to be the most common scenario: 1. User position the editor as desired 2. User close the IDE 3. User opens the IDE by double clicking on another .ino file 4. The IDE is opened again at the same position See #4432 --- app/src/processing/app/Base.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index f08d61517..3120bdbda 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -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()) @@ -528,7 +528,10 @@ 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 String untitledPath = untitledFolder.getAbsolutePath();