Skip to content

Commit 61f11f6

Browse files
committed
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.
1 parent f5f478c commit 61f11f6

File tree

1 file changed

+53
-68
lines changed

1 file changed

+53
-68
lines changed

app/src/processing/app/Base.java

+53-68
Original file line numberDiff line numberDiff line change
@@ -489,32 +489,6 @@ private void installKeyboardInputMap() {
489489
* @throws Exception
490490
*/
491491
protected boolean restoreSketches() throws Exception {
492-
// figure out window placement
493-
494-
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
495-
boolean windowPositionValid = true;
496-
497-
if (PreferencesData.get("last.screen.height") != null) {
498-
// if screen size has changed, the window coordinates no longer
499-
// make sense, so don't use them unless they're identical
500-
int screenW = PreferencesData.getInteger("last.screen.width");
501-
int screenH = PreferencesData.getInteger("last.screen.height");
502-
503-
if ((screen.width != screenW) || (screen.height != screenH)) {
504-
windowPositionValid = false;
505-
}
506-
/*
507-
int windowX = Preferences.getInteger("last.window.x");
508-
int windowY = Preferences.getInteger("last.window.y");
509-
if ((windowX < 0) || (windowY < 0) ||
510-
(windowX > screenW) || (windowY > screenH)) {
511-
windowPositionValid = false;
512-
}
513-
*/
514-
} else {
515-
windowPositionValid = false;
516-
}
517-
518492
// Iterate through all sketches that were open last time p5 was running.
519493
// If !windowPositionValid, then ignore the coordinates found for each.
520494

@@ -534,13 +508,7 @@ protected boolean restoreSketches() throws Exception {
534508
// path unchanged.
535509
}
536510
}
537-
int[] location;
538-
if (windowPositionValid) {
539-
String locationStr = PreferencesData.get("last.sketch" + i + ".location");
540-
location = PApplet.parseInt(PApplet.split(locationStr, ','));
541-
} else {
542-
location = nextEditorLocation();
543-
}
511+
int[] location = retrieveSketchLocation("" + i);
544512
// If file did not exist, null will be returned for the Editor
545513
if (handleOpen(new File(path), location, nextEditorLocation(), true, false, false) != null) {
546514
opened++;
@@ -587,6 +555,26 @@ private void storeSketchLocation(Editor editor, String index) {
587555
PreferencesData.set("last.sketch" + index + ".location", loc);
588556
}
589557

558+
private int[] retrieveSketchLocation(String index) {
559+
if (PreferencesData.get("last.screen.height") == null)
560+
return defaultEditorLocation();
561+
562+
// if screen size has changed, the window coordinates no longer
563+
// make sense, so don't use them unless they're identical
564+
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
565+
int screenW = PreferencesData.getInteger("last.screen.width");
566+
int screenH = PreferencesData.getInteger("last.screen.height");
567+
568+
if ((screen.width != screenW) || (screen.height != screenH))
569+
return defaultEditorLocation();
570+
571+
String locationStr = PreferencesData
572+
.get("last.sketch" + index + ".location");
573+
if (locationStr == null)
574+
return defaultEditorLocation();
575+
return PApplet.parseInt(PApplet.split(locationStr, ','));
576+
}
577+
590578
protected void storeRecentSketches(Sketch sketch) {
591579
if (sketch.isUntitled()) {
592580
return;
@@ -628,49 +616,46 @@ protected void handleActivated(Editor whichEditor) {
628616
EditorConsole.setCurrentEditorConsole(activeEditor.console);
629617
}
630618

631-
632-
protected int[] nextEditorLocation() {
619+
protected int[] defaultEditorLocation() {
633620
int defaultWidth = PreferencesData.getInteger("editor.window.width.default");
634621
int defaultHeight = PreferencesData.getInteger("editor.window.height.default");
622+
Rectangle screen = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getBounds();
623+
return new int[]{
624+
(screen.width - defaultWidth) / 2,
625+
(screen.height - defaultHeight) / 2,
626+
defaultWidth, defaultHeight, 0
627+
};
628+
}
635629

630+
protected int[] nextEditorLocation() {
636631
if (activeEditor == null) {
637-
Rectangle screen = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getBounds();
638632
// If no current active editor, use default placement
639-
return new int[]{
640-
(screen.width - defaultWidth) / 2,
641-
(screen.height - defaultHeight) / 2,
642-
defaultWidth, defaultHeight, 0
643-
};
633+
return defaultEditorLocation();
634+
}
644635

645-
} else {
646-
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
647-
648-
// With a currently active editor, open the new window
649-
// using the same dimensions, but offset slightly.
650-
synchronized (editors) {
651-
final int OVER = 50;
652-
// In release 0160, don't
653-
//location = activeEditor.getPlacement();
654-
Editor lastOpened = activeEditor;
655-
int[] location = lastOpened.getPlacement();
656-
// Just in case the bounds for that window are bad
657-
location[0] += OVER;
658-
location[1] += OVER;
659-
660-
if (location[0] == OVER ||
661-
location[2] == OVER ||
662-
location[0] + location[2] > screen.width ||
663-
location[1] + location[3] > screen.height) {
664-
// Warp the next window to a randomish location on screen.
665-
return new int[]{
666-
(int) (Math.random() * (screen.width - defaultWidth)),
667-
(int) (Math.random() * (screen.height - defaultHeight)),
668-
defaultWidth, defaultHeight, 0
669-
};
670-
}
636+
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
671637

672-
return location;
638+
// With a currently active editor, open the new window
639+
// using the same dimensions, but offset slightly.
640+
synchronized (editors) {
641+
int[] location = activeEditor.getPlacement();
642+
643+
// Just in case the bounds for that window are bad
644+
final int OVER = 50;
645+
location[0] += OVER;
646+
location[1] += OVER;
647+
648+
if (location[0] == OVER || location[2] == OVER
649+
|| location[0] + location[2] > screen.width
650+
|| location[1] + location[3] > screen.height) {
651+
// Warp the next window to a randomish location on screen.
652+
int[] l = defaultEditorLocation();
653+
l[0] *= Math.random() * 2;
654+
l[1] *= Math.random() * 2;
655+
return l;
673656
}
657+
658+
return location;
674659
}
675660
}
676661

0 commit comments

Comments
 (0)