Skip to content

Commit 86bb5b0

Browse files
committed
Reviewed and fixed window bounds check, added maximized state persistence
1 parent 4cd8be0 commit 86bb5b0

7 files changed

Lines changed: 181 additions & 164 deletions

File tree

spin-tools/src/main/java/com/maccasoft/propeller/Preferences.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Set;
2525

2626
import org.eclipse.swt.graphics.Point;
27-
import org.eclipse.swt.graphics.Rectangle;
2827

2928
import com.fasterxml.jackson.databind.DeserializationFeature;
3029
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -173,15 +172,12 @@ public void removePropertyChangeListener(String propertyName, PropertyChangeList
173172
changeSupport.removePropertyChangeListener(propertyName, listener);
174173
}
175174

176-
public Rectangle getWindowBounds() {
177-
if (preferences.window == null) {
178-
return null;
179-
}
180-
return new Rectangle(preferences.window.x, preferences.window.y, preferences.window.width, preferences.window.height);
175+
public Bounds getWindowBounds() {
176+
return preferences.window;
181177
}
182178

183-
public void setWindowBounds(Rectangle rect) {
184-
preferences.window = new Bounds(rect.x, rect.y, rect.width, rect.height);
179+
public void setWindowBounds(Bounds bounds) {
180+
preferences.window = bounds;
185181
}
186182

187183
public String getWindowFont() {
@@ -757,25 +753,25 @@ public void setOpenTabs(File[] openTabs) {
757753
}
758754
}
759755

760-
public Rectangle getTerminalWindow() {
756+
public Bounds getTerminalWindow() {
761757
if (preferences.terminal.window == null) {
762-
return new Rectangle(-1, -1, 100, 30);
758+
return new Bounds(-1, -1, 100, 30);
763759
}
764-
return new Rectangle(preferences.terminal.window.x, preferences.terminal.window.y, preferences.terminal.window.width, preferences.terminal.window.height);
760+
return preferences.terminal.window;
765761
}
766762

767-
public void setTerminalWindow(Rectangle rect) {
768-
preferences.terminal.window = new Bounds(rect.x, rect.y, rect.width, rect.height);
763+
public void setTerminalWindow(Bounds bounds) {
764+
preferences.terminal.window = bounds;
769765
}
770766

771767
public Point getTerminalSize() {
772-
Rectangle rect = getTerminalWindow();
768+
Bounds rect = getTerminalWindow();
773769
return new Point(rect.width, rect.height);
774770

775771
}
776772

777773
public void setTerminalSize(int width, int height) {
778-
Rectangle rect = getTerminalWindow();
774+
Bounds rect = getTerminalWindow();
779775
if (rect.width != width || rect.height != height) {
780776
rect.width = width;
781777
rect.height = height;

spin-tools/src/main/java/com/maccasoft/propeller/SerialTerminal.java

Lines changed: 69 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import org.eclipse.swt.dnd.Transfer;
3030
import org.eclipse.swt.events.ControlAdapter;
3131
import org.eclipse.swt.events.ControlEvent;
32-
import org.eclipse.swt.events.DisposeEvent;
33-
import org.eclipse.swt.events.DisposeListener;
3432
import org.eclipse.swt.events.KeyAdapter;
3533
import org.eclipse.swt.events.KeyEvent;
3634
import org.eclipse.swt.events.MouseAdapter;
@@ -60,9 +58,7 @@
6058
import org.eclipse.swt.widgets.Combo;
6159
import org.eclipse.swt.widgets.Composite;
6260
import org.eclipse.swt.widgets.Display;
63-
import org.eclipse.swt.widgets.Event;
6461
import org.eclipse.swt.widgets.Label;
65-
import org.eclipse.swt.widgets.Listener;
6662
import org.eclipse.swt.widgets.ScrollBar;
6763
import org.eclipse.swt.widgets.Shell;
6864

@@ -77,7 +73,7 @@
7773
import com.maccasoft.propeller.internal.ColorRegistry;
7874
import com.maccasoft.propeller.internal.ImageRegistry;
7975
import com.maccasoft.propeller.internal.PngImageTransfer;
80-
import com.maccasoft.propeller.internal.Utils;
76+
import com.maccasoft.propeller.preferences.Bounds;
8177

8278
import jssc.SerialPort;
8379

@@ -326,12 +322,14 @@ public void propertyChange(PropertyChangeEvent evt) {
326322
gc.dispose();
327323
}
328324

329-
Rectangle rect = preferences.getTerminalWindow();
330-
GridData gridData = (GridData) canvas.getLayoutData();
331-
gridData.widthHint = rect.width * characterWidth;
332-
gridData.heightHint = rect.height * characterHeight;
325+
if (!shell.getMaximized()) {
326+
Bounds bounds = preferences.getTerminalWindow();
327+
GridData gridData = (GridData) canvas.getLayoutData();
328+
gridData.widthHint = bounds.width * characterWidth;
329+
gridData.heightHint = bounds.height * characterHeight;
330+
shell.pack();
331+
}
333332

334-
shell.pack();
335333
redraw();
336334
break;
337335
}
@@ -394,9 +392,7 @@ public SerialTerminal(Display display, Preferences preferences) {
394392
public void open() {
395393
create();
396394

397-
shell.pack();
398395
shell.open();
399-
400396
shell.addShellListener(new ShellAdapter() {
401397

402398
@Override
@@ -437,47 +433,71 @@ void create() {
437433
createContents(shell);
438434
applyTheme(preferences.getTheme());
439435

440-
Rectangle rect = preferences.getTerminalWindow();
441-
if (rect.x != -1 && rect.y != -1) {
442-
rect.width *= characterWidth;
443-
rect.height *= characterHeight;
444-
if (!Utils.offscreen(rect, display.getBounds())) {
445-
shell.setLocation(rect.x, rect.y);
446-
}
447-
}
448-
449436
preferences.addPropertyChangeListener(preferencesChangeListener);
450437

451-
shell.addListener(SWT.Traverse, new Listener() {
438+
shell.pack();
452439

453-
@Override
454-
public void handleEvent(Event e) {
455-
if (e.detail == SWT.TRAVERSE_ESCAPE) {
456-
e.doit = false;
457-
}
458-
}
459-
});
440+
Bounds bounds = preferences.getTerminalWindow();
441+
if (bounds.x != -1 && bounds.y != -1) {
442+
Rectangle windowBounds = shell.getBounds();
443+
Rectangle clientArea = shell.getClientArea();
444+
Rectangle displayBounds = display.getBounds();
460445

461-
shell.addDisposeListener(new DisposeListener() {
446+
boolean offscreen = false;
447+
if (bounds.x >= displayBounds.x + displayBounds.width) {
448+
offscreen = true;
449+
}
450+
else if (bounds.x + windowBounds.width < displayBounds.x) {
451+
offscreen = true;
452+
}
453+
else if (bounds.y + (windowBounds.height - clientArea.height) < displayBounds.y || bounds.y >= displayBounds.y + displayBounds.height) {
454+
offscreen = true;
455+
}
456+
if (!offscreen) {
457+
shell.setLocation(bounds.x, bounds.y);
458+
}
462459

463-
@Override
464-
public void widgetDisposed(DisposeEvent e) {
465-
preferences.removePropertyChangeListener(preferencesChangeListener);
466-
try {
467-
if (comPort.isOpened()) {
468-
comPort.closePort();
460+
if (bounds.maximized) {
461+
display.asyncExec(() -> {
462+
if (!shell.isDisposed()) {
463+
shell.setMaximized(bounds.maximized);
469464
}
470-
} catch (Exception ex) {
465+
});
466+
}
467+
}
468+
else {
469+
Rectangle rect = shell.getBounds();
470+
preferences.setTerminalWindow(new Bounds(rect.x, rect.y, screenWidth, screenHeight));
471+
}
472+
473+
shell.addListener(SWT.Traverse, e -> {
474+
if (e.detail == SWT.TRAVERSE_ESCAPE) {
475+
e.doit = false;
476+
}
477+
});
471478

479+
shell.addDisposeListener(e -> {
480+
preferences.removePropertyChangeListener(preferencesChangeListener);
481+
try {
482+
if (comPort.isOpened()) {
483+
comPort.closePort();
472484
}
485+
} catch (Exception ex) {
473486

474-
Rectangle rect = shell.getBounds();
475-
rect.width = screenWidth;
476-
rect.height = screenHeight;
477-
preferences.setTerminalWindow(rect);
487+
}
478488

479-
font.dispose();
489+
if (shell.getMaximized()) {
490+
Bounds currentBounds = preferences.getTerminalWindow();
491+
Bounds bounds1 = new Bounds(currentBounds.x, currentBounds.y, currentBounds.width, currentBounds.height);
492+
bounds1.maximized = shell.getMaximized();
493+
preferences.setTerminalWindow(bounds1);
480494
}
495+
else {
496+
Rectangle rect = shell.getBounds();
497+
preferences.setTerminalWindow(new Bounds(rect.x, rect.y, screenWidth, screenHeight));
498+
}
499+
500+
font.dispose();
481501
});
482502
}
483503

@@ -494,12 +514,12 @@ protected void createContents(Composite parent) {
494514

495515
createLineInputGroup(container);
496516

497-
Rectangle rect = preferences.getTerminalWindow();
517+
Bounds bounds = preferences.getTerminalWindow();
498518

499519
canvas = new Canvas(container, SWT.DOUBLE_BUFFERED | SWT.V_SCROLL);
500520
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
501-
gridData.widthHint = rect.width * characterWidth;
502-
gridData.heightHint = rect.height * characterHeight;
521+
gridData.widthHint = bounds.width * characterWidth;
522+
gridData.heightHint = bounds.height * characterHeight;
503523
canvas.setLayoutData(gridData);
504524

505525
createBottomControls(container);
@@ -508,7 +528,7 @@ protected void createContents(Composite parent) {
508528
background = colors[0];
509529
canvas.setBackground(background);
510530

511-
resizeScreen(rect.width, rect.height);
531+
resizeScreen(bounds.width, bounds.height);
512532

513533
canvas.addControlListener(new ControlAdapter() {
514534

@@ -536,11 +556,9 @@ public void controlResized(ControlEvent e) {
536556
int height = size.y / characterHeight;
537557
if (width != screenWidth || height != screenHeight) {
538558
resizeScreen(width, height);
539-
preferences.removePropertyChangeListener(preferencesChangeListener);
540-
try {
541-
preferences.setTerminalSize(width, height);
542-
} finally {
543-
preferences.addPropertyChangeListener(preferencesChangeListener);
559+
if (!shell.getMaximized()) {
560+
Bounds currentBounds = preferences.getTerminalWindow();
561+
preferences.setTerminalWindow(new Bounds(currentBounds.x, currentBounds.y, width, height));
544562
}
545563
sizeDisplayTimer = System.currentTimeMillis();
546564
display.timerExec(500, timerRunnable);

0 commit comments

Comments
 (0)