@@ -327,7 +327,7 @@ public Base(String[] args) throws Exception {
327
327
boolean showEditor = parser .isGuiMode ();
328
328
if (!parser .isForceSavePrefs ())
329
329
PreferencesData .setDoSave (showEditor );
330
- if (handleOpen (file , nextEditorLocation ( ), showEditor , false ) == null ) {
330
+ if (handleOpen (file , retrieveSketchLocation ( ".default" ), showEditor , false ) == null ) {
331
331
String mess = I18n .format (tr ("Failed to open sketch: \" {0}\" " ), path );
332
332
// Open failure is fatal in upload/verify mode
333
333
if (parser .isVerifyOrUploadMode ())
@@ -489,32 +489,6 @@ private void installKeyboardInputMap() {
489
489
* @throws Exception
490
490
*/
491
491
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
-
518
492
// Iterate through all sketches that were open last time p5 was running.
519
493
// If !windowPositionValid, then ignore the coordinates found for each.
520
494
@@ -534,13 +508,7 @@ protected boolean restoreSketches() throws Exception {
534
508
// path unchanged.
535
509
}
536
510
}
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 );
544
512
// If file did not exist, null will be returned for the Editor
545
513
if (handleOpen (new File (path ), location , nextEditorLocation (), true , false , false ) != null ) {
546
514
opened ++;
@@ -560,29 +528,56 @@ protected void storeSketches() {
560
528
PreferencesData .setInteger ("last.screen.width" , screen .width );
561
529
PreferencesData .setInteger ("last.screen.height" , screen .height );
562
530
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
+ }
564
535
565
536
// 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 );
568
540
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 ()) {
574
546
continue ;
575
547
}
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 );
581
549
index ++;
582
550
}
583
551
PreferencesData .setInteger ("last.sketch.count" , index );
584
552
}
585
553
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
+
586
581
protected void storeRecentSketches (Sketch sketch ) {
587
582
if (sketch .isUntitled ()) {
588
583
return ;
@@ -624,49 +619,46 @@ protected void handleActivated(Editor whichEditor) {
624
619
EditorConsole .setCurrentEditorConsole (activeEditor .console );
625
620
}
626
621
627
-
628
- protected int [] nextEditorLocation () {
622
+ protected int [] defaultEditorLocation () {
629
623
int defaultWidth = PreferencesData .getInteger ("editor.window.width.default" );
630
624
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
+ }
631
632
633
+ protected int [] nextEditorLocation () {
632
634
if (activeEditor == null ) {
633
- Rectangle screen = GraphicsEnvironment .getLocalGraphicsEnvironment ().getDefaultScreenDevice ().getDefaultConfiguration ().getBounds ();
634
635
// 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
+ }
640
638
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 ();
667
645
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 ;
669
659
}
660
+
661
+ return location ;
670
662
}
671
663
}
672
664
@@ -896,12 +888,7 @@ protected Editor handleOpen(File file, int[] storedLocation, int[] defaultLocati
896
888
// now that we're ready, show the window
897
889
// (don't do earlier, cuz we might move it based on a window being closed)
898
890
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 ));
905
892
}
906
893
907
894
return editor ;
@@ -961,14 +948,15 @@ public boolean handleClose(Editor editor) {
961
948
editor .internalCloseRunner ();
962
949
963
950
if (editors .size () == 1 ) {
951
+ storeSketches ();
952
+
964
953
// This will store the sketch count as zero
965
954
editors .remove (editor );
966
955
try {
967
956
Editor .serialMonitor .close ();
968
957
} catch (Exception e ) {
969
958
//ignore
970
959
}
971
- storeSketches ();
972
960
rebuildRecentSketchesMenuItems ();
973
961
974
962
// Save out the current prefs state
0 commit comments