@@ -327,7 +327,7 @@ public Base(String[] args) throws Exception {
327327 boolean showEditor = parser .isGuiMode ();
328328 if (!parser .isForceSavePrefs ())
329329 PreferencesData .setDoSave (showEditor );
330- if (handleOpen (file , nextEditorLocation ( ), showEditor , false ) == null ) {
330+ if (handleOpen (file , retrieveSketchLocation ( ".default" ), showEditor , false ) == null ) {
331331 String mess = I18n .format (tr ("Failed to open sketch: \" {0}\" " ), path );
332332 // Open failure is fatal in upload/verify mode
333333 if (parser .isVerifyOrUploadMode ())
@@ -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 ++;
@@ -560,29 +528,56 @@ protected void storeSketches() {
560528 PreferencesData .setInteger ("last.screen.width" , screen .width );
561529 PreferencesData .setInteger ("last.screen.height" , screen .height );
562530
563- String untitledPath = untitledFolder .getAbsolutePath ();
531+ // If there is only one sketch opened save his position as default
532+ if (editors .size () == 1 ) {
533+ storeSketchLocation (editors .get (0 ), ".default" );
534+ }
564535
565536 // Save the sketch path and window placement for each open sketch
566- LinkedList <Editor > reverseEditors = new LinkedList <Editor >(editors );
567- Collections .reverse (reverseEditors );
537+ String untitledPath = untitledFolder .getAbsolutePath ();
538+ List <Editor > reversedEditors = new LinkedList <>(editors );
539+ Collections .reverse (reversedEditors );
568540 int index = 0 ;
569- for (Editor editor : reverseEditors ) {
570- String path = editor .getSketch (). getMainFilePath ();
571- // In case of a crash, save untitled sketches if they contain changes.
572- // (Added this for release 0158, may not be a good idea.)
573- if (path .startsWith (untitledPath ) && !editor . getSketch () .isModified ()) {
541+ for (Editor editor : reversedEditors ) {
542+ Sketch sketch = editor .getSketch ();
543+ String path = sketch . getMainFilePath ();
544+ // Skip untitled sketches if they do not contains changes.
545+ if (path .startsWith (untitledPath ) && !sketch .isModified ()) {
574546 continue ;
575547 }
576- PreferencesData .set ("last.sketch" + index + ".path" , path );
577-
578- int [] location = editor .getPlacement ();
579- String locationStr = PApplet .join (PApplet .str (location ), "," );
580- PreferencesData .set ("last.sketch" + index + ".location" , locationStr );
548+ storeSketchLocation (editor , "" + index );
581549 index ++;
582550 }
583551 PreferencesData .setInteger ("last.sketch.count" , index );
584552 }
585553
554+ private void storeSketchLocation (Editor editor , String index ) {
555+ String path = editor .getSketch ().getMainFilePath ();
556+ String loc = StringUtils .join (editor .getPlacement (), ',' );
557+ PreferencesData .set ("last.sketch" + index + ".path" , path );
558+ PreferencesData .set ("last.sketch" + index + ".location" , loc );
559+ }
560+
561+ private int [] retrieveSketchLocation (String index ) {
562+ if (PreferencesData .get ("last.screen.height" ) == null )
563+ return defaultEditorLocation ();
564+
565+ // if screen size has changed, the window coordinates no longer
566+ // make sense, so don't use them unless they're identical
567+ Dimension screen = Toolkit .getDefaultToolkit ().getScreenSize ();
568+ int screenW = PreferencesData .getInteger ("last.screen.width" );
569+ int screenH = PreferencesData .getInteger ("last.screen.height" );
570+
571+ if ((screen .width != screenW ) || (screen .height != screenH ))
572+ return defaultEditorLocation ();
573+
574+ String locationStr = PreferencesData
575+ .get ("last.sketch" + index + ".location" );
576+ if (locationStr == null )
577+ return defaultEditorLocation ();
578+ return PApplet .parseInt (PApplet .split (locationStr , ',' ));
579+ }
580+
586581 protected void storeRecentSketches (Sketch sketch ) {
587582 if (sketch .isUntitled ()) {
588583 return ;
@@ -624,49 +619,46 @@ protected void handleActivated(Editor whichEditor) {
624619 EditorConsole .setCurrentEditorConsole (activeEditor .console );
625620 }
626621
627-
628- protected int [] nextEditorLocation () {
622+ protected int [] defaultEditorLocation () {
629623 int defaultWidth = PreferencesData .getInteger ("editor.window.width.default" );
630624 int defaultHeight = PreferencesData .getInteger ("editor.window.height.default" );
625+ Rectangle screen = GraphicsEnvironment .getLocalGraphicsEnvironment ().getDefaultScreenDevice ().getDefaultConfiguration ().getBounds ();
626+ return new int []{
627+ (screen .width - defaultWidth ) / 2 ,
628+ (screen .height - defaultHeight ) / 2 ,
629+ defaultWidth , defaultHeight , 0
630+ };
631+ }
631632
633+ protected int [] nextEditorLocation () {
632634 if (activeEditor == null ) {
633- Rectangle screen = GraphicsEnvironment .getLocalGraphicsEnvironment ().getDefaultScreenDevice ().getDefaultConfiguration ().getBounds ();
634635 // If no current active editor, use default placement
635- return new int []{
636- (screen .width - defaultWidth ) / 2 ,
637- (screen .height - defaultHeight ) / 2 ,
638- defaultWidth , defaultHeight , 0
639- };
636+ return defaultEditorLocation ();
637+ }
640638
641- } else {
642- Dimension screen = Toolkit .getDefaultToolkit ().getScreenSize ();
643-
644- // With a currently active editor, open the new window
645- // using the same dimensions, but offset slightly.
646- synchronized (editors ) {
647- final int OVER = 50 ;
648- // In release 0160, don't
649- //location = activeEditor.getPlacement();
650- Editor lastOpened = activeEditor ;
651- int [] location = lastOpened .getPlacement ();
652- // Just in case the bounds for that window are bad
653- location [0 ] += OVER ;
654- location [1 ] += OVER ;
655-
656- if (location [0 ] == OVER ||
657- location [2 ] == OVER ||
658- location [0 ] + location [2 ] > screen .width ||
659- location [1 ] + location [3 ] > screen .height ) {
660- // Warp the next window to a randomish location on screen.
661- return new int []{
662- (int ) (Math .random () * (screen .width - defaultWidth )),
663- (int ) (Math .random () * (screen .height - defaultHeight )),
664- defaultWidth , defaultHeight , 0
665- };
666- }
639+ Dimension screen = Toolkit .getDefaultToolkit ().getScreenSize ();
640+
641+ // With a currently active editor, open the new window
642+ // using the same dimensions, but offset slightly.
643+ synchronized (editors ) {
644+ int [] location = activeEditor .getPlacement ();
667645
668- return location ;
646+ // Just in case the bounds for that window are bad
647+ final int OVER = 50 ;
648+ location [0 ] += OVER ;
649+ location [1 ] += OVER ;
650+
651+ if (location [0 ] == OVER || location [2 ] == OVER
652+ || location [0 ] + location [2 ] > screen .width
653+ || location [1 ] + location [3 ] > screen .height ) {
654+ // Warp the next window to a randomish location on screen.
655+ int [] l = defaultEditorLocation ();
656+ l [0 ] *= Math .random () * 2 ;
657+ l [1 ] *= Math .random () * 2 ;
658+ return l ;
669659 }
660+
661+ return location ;
670662 }
671663 }
672664
@@ -896,12 +888,7 @@ protected Editor handleOpen(File file, int[] storedLocation, int[] defaultLocati
896888 // now that we're ready, show the window
897889 // (don't do earlier, cuz we might move it based on a window being closed)
898890 if (showEditor ) {
899- SwingUtilities .invokeLater (new Runnable () {
900- @ Override
901- public void run () {
902- editor .setVisible (true );
903- }
904- });
891+ SwingUtilities .invokeLater (() -> editor .setVisible (true ));
905892 }
906893
907894 return editor ;
@@ -961,14 +948,15 @@ public boolean handleClose(Editor editor) {
961948 editor .internalCloseRunner ();
962949
963950 if (editors .size () == 1 ) {
951+ storeSketches ();
952+
964953 // This will store the sketch count as zero
965954 editors .remove (editor );
966955 try {
967956 Editor .serialMonitor .close ();
968957 } catch (Exception e ) {
969958 //ignore
970959 }
971- storeSketches ();
972960 rebuildRecentSketchesMenuItems ();
973961
974962 // Save out the current prefs state
0 commit comments