3
3
/*
4
4
Part of the Processing project - http://processing.org
5
5
6
- Copyright (c) 2012-19 The Processing Foundation
6
+ Copyright (c) 2012-21 The Processing Foundation
7
7
Copyright (c) 2011-12 Ben Fry and Casey Reas
8
8
9
9
This program is free software; you can redistribute it and/or modify
27
27
import java .awt .GraphicsDevice ;
28
28
import java .awt .GraphicsEnvironment ;
29
29
import java .awt .Rectangle ;
30
- import java .io .*;
31
30
import java .util .List ;
32
31
33
32
import processing .app .Preferences ;
34
- import processing .core .PApplet ;
35
-
36
- //import processing.core.PApplet;
37
-
38
-
39
- // scenarios:
40
- // 1) new untitled sketch (needs device, needs bounds)
41
- // 2) restoring sketch from recent menu
42
- // - device cannot be found
43
- // - device is found but it's a different size
44
- // - device is found and size is correct
45
- // 3) re-opening sketch in a new mode
46
33
47
34
35
+ /**
36
+ * Stores state information (location, size, display device) for an Editor window.
37
+ * Originally was used to restore sketch windows on startup, though that was removed
38
+ * at some point (3.0?) because it was unreliable and not always appreciated.
39
+ *
40
+ * This version is primarily just used to get the location (and display) to open
41
+ * a new Editor window, though some of the vestigial bits are still in there in case
42
+ * we want to restore the ability to restore windows on startup.
43
+ *
44
+ * Previous scenarios:
45
+ * <ul>
46
+ * <li>new untitled sketch (needs device, needs bounds)</li>
47
+ * <li>restoring sketch from recent menu
48
+ * <ul>
49
+ * <li>device is found and size matches</li>
50
+ * <li>device cannot be found</li>
51
+ * <li>device is found but its size has changed</li>
52
+ * </ul>
53
+ * </li>
54
+ * <li>re-opening sketch in a new mode</li>
55
+ * </ul>
56
+ */
48
57
public class EditorState {
49
- // path to the main .pde file for the sketch
50
- // String path;
51
- // placement of the window
52
- // int windowX, windowY, windowW, windowH;
53
58
Rectangle editorBounds ;
54
59
int dividerLocation ;
55
- // width/height of the screen on which this window was placed
56
- // int displayW, displayH;
57
- // String deviceName; // not really useful b/c it's more about bounds anyway
58
- Rectangle deviceBounds ;
59
60
boolean isMaximized ;
61
+ GraphicsConfiguration deviceConfig ;
60
62
61
- // how far to offset a new window from the previous window
63
+ /** How far to offset a new window from the previous window */
62
64
static final int WINDOW_OFFSET = 28 ;
63
65
66
+ /**
67
+ * Keep a reference to the last device config so we know which display to
68
+ * use when creating a new window after all windows have been closed.
69
+ */
70
+ static GraphicsConfiguration lastConfig ;
71
+
64
72
65
73
/**
66
74
* Create a fresh editor state object from the default screen device and
67
75
* set its placement relative to the last opened window.
68
76
* @param editors List of active editor objects
69
77
*/
70
- public EditorState (List <Editor > editors ) {
71
- defaultConfig ();
72
- defaultLocation (editors );
78
+ static public EditorState nextEditor (List <Editor > editors ) {
79
+ Editor lastEditor = null ;
80
+ int editorCount = editors .size ();
81
+ if (editorCount > 0 ) {
82
+ lastEditor = editors .get (editorCount -1 );
83
+ }
84
+
85
+ // update lastConfig so it can be set for this Editor and
86
+ // for the next Editor created if the last window is closed.
87
+ if (lastEditor != null ) {
88
+ lastConfig = lastEditor .getGraphicsConfiguration ();
89
+ }
90
+ if (lastConfig == null ) {
91
+ lastConfig = getDefaultConfig ();
92
+ }
93
+
94
+ EditorState outgoing = new EditorState ();
95
+ outgoing .initLocation (lastConfig , lastEditor );
96
+ return outgoing ;
73
97
}
74
98
75
99
76
- // EditorState(BufferedReader reader) throws IOException {
77
- // String line = reader.readLine();
78
- // EditorState(String[] pieces) throws IOException {
100
+ /*
79
101
EditorState(String info) throws IOException {
80
- // String line = reader.readLine();
81
- // String[] pieces = PApplet.split(line, '\t');
82
- String [] pieces = PApplet .split (info , ',' );
83
- // path = pieces[0];
84
-
102
+ String[] pieces = PApplet.split(info, ',');
85
103
editorBounds = new Rectangle(Integer.parseInt(pieces[0]),
86
104
Integer.parseInt(pieces[1]),
87
105
Integer.parseInt(pieces[2]),
@@ -93,15 +111,8 @@ public EditorState(List<Editor> editors) {
93
111
Integer.parseInt(pieces[6]),
94
112
Integer.parseInt(pieces[7]),
95
113
Integer.parseInt(pieces[8]));
96
-
97
- // windowX = Integer.parseInt(pieces[1]);
98
- // windowY = Integer.parseInt(pieces[2]);
99
- // windowW = Integer.parseInt(pieces[3]);
100
- // windowH = Integer.parseInt(pieces[4]);
101
-
102
- // displayW = Integer.parseInt(pieces[5]);
103
- // displayH = Integer.parseInt(pieces[6]);
104
114
}
115
+ */
105
116
106
117
107
118
public String toString () {
@@ -110,19 +121,14 @@ public String toString() {
110
121
editorBounds .width + "," +
111
122
editorBounds .height + "," +
112
123
dividerLocation + "," +
113
- deviceBounds .x + "," +
114
- deviceBounds .y + "," +
115
- deviceBounds .width + "," +
116
- deviceBounds .height );
124
+ deviceConfig );
117
125
}
118
126
119
-
120
- /**
121
- * Returns a GraphicsConfiguration so that a new Editor Frame can be
122
- * constructed. First tries to match the bounds for this state information
123
- * to an existing config (nominally, a display) and if that doesn't work,
124
- * then returns the default configuration/default display.
125
- */
127
+ /*
128
+ // * Returns a GraphicsConfiguration so that a new Editor Frame can be
129
+ // * constructed. First tries to match the bounds for this state information
130
+ // * to an existing config (nominally, a display) and if that doesn't work,
131
+ // * then returns the default configuration/default display.
126
132
GraphicsConfiguration checkConfig() {
127
133
if (deviceBounds != null) {
128
134
GraphicsEnvironment graphicsEnvironment =
@@ -133,42 +139,53 @@ GraphicsConfiguration checkConfig() {
133
139
for (GraphicsConfiguration config : configurations) {
134
140
// if (config.getDevice().getIDstring().equals(deviceName)) {
135
141
if (config.getBounds().equals(deviceBounds)) {
142
+ System.out.println("found config " + config + " " + deviceBounds);
143
+ System.out.println("device name is " + config.getDevice().getIDstring());
136
144
return config;
137
145
}
138
146
}
139
147
}
140
148
}
149
+ System.out.println("using default config");
141
150
// otherwise go to the default config
142
151
return defaultConfig();
143
152
}
153
+ */
144
154
145
155
146
- GraphicsConfiguration defaultConfig () {
156
+ public GraphicsConfiguration getConfig () {
157
+ return deviceConfig ;
158
+ }
159
+
160
+
161
+ static GraphicsConfiguration getDefaultConfig () {
147
162
GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment ();
148
163
GraphicsDevice device = ge .getDefaultScreenDevice ();
149
164
GraphicsConfiguration config = device .getDefaultConfiguration ();
150
- // deviceName = device.getIDstring();
151
- deviceBounds = config .getBounds ();
165
+ // deviceName = device.getIDstring();
166
+ // deviceBounds = config.getBounds();
152
167
return config ;
153
168
}
154
169
155
170
156
171
/**
157
172
* Figure out the next location by sizing up the last editor in the list.
158
173
* If no editors are opened, it'll just open on the main screen.
159
- * @param editors List of editors currently opened
174
+ * @param lastOpened The last Editor opened, used to determine display and location
160
175
*/
161
- void defaultLocation (List <Editor > editors ) {
176
+ void initLocation (GraphicsConfiguration lastConfig , Editor lastOpened ) {
177
+ deviceConfig = lastConfig ;
178
+ Rectangle deviceBounds = deviceConfig .getBounds ();
179
+
162
180
int defaultWidth =
163
181
Toolkit .zoom (Preferences .getInteger ("editor.window.width.default" ));
164
182
int defaultHeight =
165
183
Toolkit .zoom (Preferences .getInteger ("editor.window.height.default" ));
166
184
167
185
defaultWidth = Math .min (defaultWidth , deviceBounds .width );
168
186
defaultHeight = Math .min (defaultHeight , deviceBounds .height );
169
- //System.out.println("default w/h = " + defaultWidth + "/" + defaultHeight);
170
187
171
- if (editors . size () == 0 ) {
188
+ if (lastOpened == null ) {
172
189
// If no current active editor, use default placement.
173
190
// Center the window on ths screen, taking into account that the
174
191
// upper-left corner of the device may have a non (0, 0) origin.
@@ -183,39 +200,40 @@ void defaultLocation(List<Editor> editors) {
183
200
} else {
184
201
// With a currently active editor, open the new window using the same
185
202
// dimensions and divider location, but offset slightly.
186
- synchronized ( editors ) {
187
- Editor lastOpened = editors . get ( editors . size () - 1 );
188
- isMaximized = ( lastOpened . getExtendedState () == Frame . MAXIMIZED_BOTH );
189
- editorBounds = lastOpened .getBounds ( );
190
- editorBounds . x += WINDOW_OFFSET ;
191
- editorBounds .y += WINDOW_OFFSET ;
192
- dividerLocation = lastOpened . getDividerLocation () ;
193
-
194
- if (! deviceBounds . contains ( editorBounds )) {
195
- // Warp the next window to a randomish location on screen.
196
- editorBounds . x = deviceBounds . x +
197
- ( int ) ( Math . random () * ( deviceBounds .width - defaultWidth ));
198
- editorBounds . y = deviceBounds .y +
199
- ( int ) ( Math . random () * ( deviceBounds .height - defaultHeight ));
200
- }
201
- if ( isMaximized ) {
202
- editorBounds . width = defaultWidth ;
203
- editorBounds .height = defaultHeight ;
204
- }
203
+ //GraphicsDevice device = lastOpened.getGraphicsConfiguration().getDevice();
204
+ //System.out.println("last opened device is " + device );
205
+
206
+ isMaximized = ( lastOpened .getExtendedState () == Frame . MAXIMIZED_BOTH );
207
+ editorBounds = lastOpened . getBounds () ;
208
+ editorBounds .x += WINDOW_OFFSET ;
209
+ editorBounds . y += WINDOW_OFFSET ;
210
+ dividerLocation = lastOpened . getDividerLocation ();
211
+
212
+ if (! deviceBounds . contains ( editorBounds )) {
213
+ // Warp the next window to a randomish location on screen.
214
+ editorBounds . x = deviceBounds .x +
215
+ ( int ) ( Math . random () * ( deviceBounds .width - defaultWidth ));
216
+ editorBounds . y = deviceBounds .y +
217
+ ( int ) ( Math . random () * ( deviceBounds . height - defaultHeight ));
218
+ }
219
+ if ( isMaximized ) {
220
+ editorBounds .width = defaultWidth ;
221
+ editorBounds . height = defaultHeight ;
205
222
}
206
223
}
207
224
}
208
225
209
226
227
+ /*
210
228
void update(Editor editor) {
211
- // path = editor.getSketch().getMainFilePath();
212
229
editorBounds = editor.getBounds();
213
230
dividerLocation = editor.getDividerLocation();
214
231
GraphicsConfiguration config = editor.getGraphicsConfiguration();
215
- // GraphicsDevice device = config.getDevice();
216
232
deviceBounds = config.getBounds();
217
- // deviceName = device.getIDstring();
233
+ // GraphicsDevice device = config.getDevice();
234
+ deviceName = config.getDevice().getIDstring();
218
235
}
236
+ */
219
237
220
238
221
239
void apply (Editor editor ) {
@@ -229,6 +247,10 @@ void apply(Editor editor) {
229
247
if (isMaximized ) {
230
248
editor .setExtendedState (Frame .MAXIMIZED_BOTH );
231
249
}
250
+
251
+ // note: doesn't do anything with the device, though that could be
252
+ // added if it's something that would be necessary (i.e. to store windows and
253
+ // re-open them on when re-opening Processing)
232
254
}
233
255
234
256
0 commit comments