Skip to content

Save windows position when closed with Alt+F4 or "X" button #4432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 25, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 77 additions & 89 deletions app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public Base(String[] args) throws Exception {
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())
Expand Down Expand Up @@ -489,32 +489,6 @@ private void installKeyboardInputMap() {
* @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.

Expand All @@ -534,13 +508,7 @@ protected boolean restoreSketches() throws Exception {
// 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++;
Expand All @@ -560,29 +528,56 @@ protected void storeSketches() {
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;
Expand Down Expand Up @@ -624,49 +619,46 @@ protected void handleActivated(Editor whichEditor) {
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();

// 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;

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
};
}
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

// With a currently active editor, open the new window
// using the same dimensions, but offset slightly.
synchronized (editors) {
int[] location = activeEditor.getPlacement();

return location;
// Just in case the bounds for that window are bad
final int OVER = 50;
location[0] += OVER;
location[1] += OVER;

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;
}
}

Expand Down Expand Up @@ -896,12 +888,7 @@ protected Editor handleOpen(File file, int[] storedLocation, int[] defaultLocati
// 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;
Expand Down Expand Up @@ -961,14 +948,15 @@ public boolean handleClose(Editor editor) {
editor.internalCloseRunner();

if (editors.size() == 1) {
storeSketches();

// This will store the sketch count as zero
editors.remove(editor);
try {
Editor.serialMonitor.close();
} catch (Exception e) {
//ignore
}
storeSketches();
rebuildRecentSketchesMenuItems();

// Save out the current prefs state
Expand Down