Window size is stored and restored at startup; closes #275

This commit is contained in:
hneemann 2019-05-09 20:19:15 +02:00
parent 71144a260d
commit 4b60be0808
4 changed files with 81 additions and 6 deletions

View File

@ -153,7 +153,8 @@ public class FSMFrame extends JFrame implements ClosingWindowListener.ConfirmSav
setJMenuBar(bar);
pack();
new WindowSizeStorage("fsm").setDefaultSize(600, 600).restore(this);
setFSM(new FSM());
SwingUtilities.invokeLater(() -> {

View File

@ -276,8 +276,8 @@ public final class Main extends JFrame implements ClosingWindowListener.ConfirmS
enableClockShortcut();
setPreferredSize(Screen.getInstance().scale(new Dimension(1024, 768)));
pack();
new WindowSizeStorage(builder.mainFrame?"main":"sub").restore(this);
if (builder.parent != null) {
Point p = builder.parent.getLocation();
final float d = 20 * Screen.getInstance().getScaling();
@ -1798,7 +1798,7 @@ public final class Main extends JFrame implements ClosingWindowListener.ConfirmS
} else if (file != null && file.getName().endsWith(".tru")) {
TableDialog.openFile(file);
} else {
MainBuilder builder = new MainBuilder();
MainBuilder builder = new MainBuilder().setMainFrame();
if (file != null)
builder.setFileToOpen(file);
SwingUtilities.invokeLater(() -> {
@ -1826,6 +1826,7 @@ public final class Main extends JFrame implements ClosingWindowListener.ConfirmS
private boolean allowAllFileActions = true;
private File baseFileName;
private boolean keepPrefMainFile;
private boolean mainFrame = false;
/**
* @param fileToOpen the file to open
@ -1909,6 +1910,10 @@ public final class Main extends JFrame implements ClosingWindowListener.ConfirmS
SwingUtilities.invokeLater(() -> build().setVisible(true));
}
private MainBuilder setMainFrame() {
mainFrame = true;
return this;
}
}
private class ModelKeyListener extends KeyAdapter {

View File

@ -955,8 +955,8 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
AffineTransform newTrans = new AffineTransform();
if (gr.getMin() != null && getWidth() != 0 && getHeight() != 0) {
Vector delta = gr.getMax().sub(gr.getMin());
double sx = ((double) getWidth()) / (delta.x + Style.NORMAL.getThickness() * 2);
double sy = ((double) getHeight()) / (delta.y + Style.NORMAL.getThickness() * 2);
double sx = ((double) getWidth()) / (delta.x + Style.NORMAL.getThickness() * 4);
double sy = ((double) getHeight()) / (delta.y + Style.NORMAL.getThickness() * 6);
double s = Math.min(sx, sy);

View File

@ -0,0 +1,69 @@
/*
* Copyright (c) 2019 Helmut Neemann.
* Use of this source code is governed by the GPL v3 license
* that can be found in the LICENSE file.
*/
package de.neemann.gui;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.prefs.Preferences;
/**
* Used to store the window size
*/
public final class WindowSizeStorage {
private static final Preferences PREFS = Preferences.userRoot().node("dig").node("win");
private static final String WIDTH_KEY = "width";
private static final String HEIGHT_KEY = "height";
private final Preferences prefs;
private int defWidth = 1024;
private int defHeight = 768;
/**
* Creates a new instance.
*
* @param key the key used to store the size, must be unique
*/
public WindowSizeStorage(String key) {
prefs = PREFS.node(key);
}
/**
* Sets the default size. Used at the first startup
*
* @param width width
* @param height height
* @return this for chained calls
*/
public WindowSizeStorage setDefaultSize(int width, int height) {
this.defWidth = width;
this.defHeight = height;
return this;
}
/**
* Restore the last used size.
*
* @param component the component to use
*/
public void restore(Component component) {
int width = prefs.getInt(WIDTH_KEY, 0);
int height = prefs.getInt(HEIGHT_KEY, 0);
if (width < 100 || height < 80)
component.setSize(Screen.getInstance().scale(new Dimension(defWidth, defHeight)));
else
component.setSize(new Dimension(width, height));
component.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent componentEvent) {
final Dimension size = component.getSize();
prefs.putInt(WIDTH_KEY, size.width);
prefs.putInt(HEIGHT_KEY, size.height);
}
});
}
}