Remember the window's place and the tree/chat divider
The main window comes back at its last size and position, maximised if it was; a place no longer on any screen falls back to the default. The tree/chat divider is saved like the tree/info one, and both keep their share of the window as it is resized. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -196,8 +196,7 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener {
|
||||
|
||||
statusTimer = new javax.swing.Timer(3000, e -> updateConnectionStatus());
|
||||
statusTimer.start();
|
||||
setSize(UIScale.scale(new Dimension(880, 560)));
|
||||
setLocationRelativeTo(null);
|
||||
WindowGeometry.restore(this, settings, new Dimension(880, 560));
|
||||
|
||||
// Deferred so the window is on screen before we start connecting.
|
||||
List<Bookmark> startup = bookmarks.startupBookmarks();
|
||||
|
||||
@@ -22,8 +22,6 @@ import javax.swing.JSplitPane;
|
||||
import javax.swing.SwingUtilities;
|
||||
import java.awt.Component;
|
||||
import java.awt.Window;
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
|
||||
/**
|
||||
* One server connection and the views bound to it (tree, info and chat). The
|
||||
@@ -52,8 +50,6 @@ final class ServerTab {
|
||||
private JSplitPane leftColumn;
|
||||
private int normalDividerSize;
|
||||
private int savedDividerLocation = -1;
|
||||
/** Whether the saved divider share has been applied; before that, layout is not the user's doing. */
|
||||
private boolean splitRestored;
|
||||
|
||||
/** The one open ban list window for this server, if any. */
|
||||
private BanListDialog banList;
|
||||
@@ -157,25 +153,16 @@ final class ServerTab {
|
||||
}
|
||||
});
|
||||
|
||||
// Both dividers stay where the user put them, in this tab and the next session.
|
||||
leftColumn = new JSplitPane(JSplitPane.VERTICAL_SPLIT, treePanel, infoPanel);
|
||||
leftColumn.setResizeWeight(0.68);
|
||||
leftColumn.setContinuousLayout(true);
|
||||
normalDividerSize = leftColumn.getDividerSize();
|
||||
// The info panel keeps the height the user gave it, in this tab and the next session.
|
||||
leftColumn.addComponentListener(new ComponentAdapter() {
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e) {
|
||||
if (splitRestored) return;
|
||||
splitRestored = true;
|
||||
leftColumn.setDividerLocation(settings.infoSplitRatio);
|
||||
}
|
||||
});
|
||||
leftColumn.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, e -> rememberSplit());
|
||||
SplitRatio.keep(leftColumn, () -> settings.infoSplitRatio, r -> settings.infoSplitRatio = r,
|
||||
infoPanel::isVisible);
|
||||
|
||||
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftColumn, chatPanel);
|
||||
split.setResizeWeight(0.55);
|
||||
split.setDividerLocation(400);
|
||||
split.setContinuousLayout(true);
|
||||
SplitRatio.keep(split, () -> settings.chatSplitRatio, r -> settings.chatSplitRatio = r, () -> true);
|
||||
this.component = split;
|
||||
}
|
||||
|
||||
@@ -341,19 +328,6 @@ final class ServerTab {
|
||||
treeActions.browseFiles(channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Records where the user put the divider, as a share of the column. Collapsed and
|
||||
* pre-layout positions are not the user's choice, so they are left out.
|
||||
*/
|
||||
private void rememberSplit() {
|
||||
if (!splitRestored || !infoPanel.isVisible()) return;
|
||||
int span = leftColumn.getHeight() - leftColumn.getDividerSize();
|
||||
if (span <= 0) return;
|
||||
double ratio = leftColumn.getDividerLocation() / (double) span;
|
||||
if (ratio <= 0.05 || ratio >= 0.98) return;
|
||||
settings.infoSplitRatio = ratio;
|
||||
}
|
||||
|
||||
/** Collapses the info panel to nothing while its content lives in the chat tab, or restores it. */
|
||||
private void setInfoPanelHidden(boolean hidden) {
|
||||
if (hidden == !infoPanel.isVisible()) return;
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package eu.lixko.ts3j.client.ui;
|
||||
|
||||
import javax.swing.JSplitPane;
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
import java.util.function.BooleanSupplier;
|
||||
import java.util.function.DoubleConsumer;
|
||||
import java.util.function.DoubleSupplier;
|
||||
|
||||
/**
|
||||
* Keeps a split pane's divider at the share of its span the user gave it: restores the saved
|
||||
* share at the first layout, holds it as the pane grows or shrinks, and saves it when the
|
||||
* user drags the divider.
|
||||
*/
|
||||
final class SplitRatio {
|
||||
|
||||
private final JSplitPane split;
|
||||
private final DoubleSupplier saved;
|
||||
private final DoubleConsumer save;
|
||||
private final BooleanSupplier tracking;
|
||||
/** The span at the last divider move; another span means a layout moved it, not the user. */
|
||||
private int lastSpan;
|
||||
private boolean restored;
|
||||
|
||||
/**
|
||||
* @param tracking whether the divider is the user's to place right now; while it is not
|
||||
* (a side collapsed, say), moves are neither saved nor corrected
|
||||
*/
|
||||
static void keep(JSplitPane split, DoubleSupplier saved, DoubleConsumer save, BooleanSupplier tracking) {
|
||||
new SplitRatio(split, saved, save, tracking);
|
||||
}
|
||||
|
||||
private SplitRatio(JSplitPane split, DoubleSupplier saved, DoubleConsumer save, BooleanSupplier tracking) {
|
||||
this.split = split;
|
||||
this.saved = saved;
|
||||
this.save = save;
|
||||
this.tracking = tracking;
|
||||
split.setResizeWeight(saved.getAsDouble());
|
||||
split.addComponentListener(new ComponentAdapter() {
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e) {
|
||||
if (restored || span() <= 0) return;
|
||||
restored = true;
|
||||
lastSpan = span();
|
||||
split.setDividerLocation(saved.getAsDouble());
|
||||
}
|
||||
});
|
||||
split.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, e -> moved());
|
||||
}
|
||||
|
||||
private void moved() {
|
||||
int span = span();
|
||||
if (!restored || span <= 0 || !tracking.getAsBoolean()) return;
|
||||
if (span != lastSpan) {
|
||||
lastSpan = span;
|
||||
return;
|
||||
}
|
||||
double ratio = split.getDividerLocation() / (double) span;
|
||||
if (ratio <= 0.05 || ratio >= 0.98) return;
|
||||
save.accept(ratio);
|
||||
// Space the pane gains or loses is shared out in the same proportion.
|
||||
split.setResizeWeight(ratio);
|
||||
}
|
||||
|
||||
private int span() {
|
||||
int size = split.getOrientation() == JSplitPane.HORIZONTAL_SPLIT ? split.getWidth() : split.getHeight();
|
||||
return size - split.getDividerSize();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package eu.lixko.ts3j.client.ui;
|
||||
|
||||
import com.formdev.flatlaf.util.UIScale;
|
||||
import eu.lixko.ts3j.client.config.Settings;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.Timer;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Frame;
|
||||
import java.awt.GraphicsEnvironment;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
|
||||
/**
|
||||
* Puts the main window where the user left it, and keeps track of where that is: its bounds
|
||||
* while not maximised, and whether it is maximised.
|
||||
*/
|
||||
final class WindowGeometry {
|
||||
|
||||
/** How much of a saved window must still be on some screen for its place to count. */
|
||||
private static final int MIN_VISIBLE = 64;
|
||||
/**
|
||||
* A window manager may report the new size of a window it maximises before the new state,
|
||||
* so bounds are read once things have settled.
|
||||
*/
|
||||
private static final int SETTLE_MS = 300;
|
||||
|
||||
private WindowGeometry() {
|
||||
}
|
||||
|
||||
static void restore(JFrame frame, Settings settings, Dimension defaultSize) {
|
||||
Rectangle saved = new Rectangle(settings.windowX, settings.windowY, settings.windowWidth, settings.windowHeight);
|
||||
if (saved.width > 0 && saved.height > 0 && onScreen(saved)) {
|
||||
Dimension min = frame.getMinimumSize();
|
||||
saved.width = Math.max(saved.width, min.width);
|
||||
saved.height = Math.max(saved.height, min.height);
|
||||
frame.setBounds(saved);
|
||||
} else {
|
||||
frame.setSize(UIScale.scale(defaultSize));
|
||||
frame.setLocationRelativeTo(null);
|
||||
}
|
||||
if (settings.windowMaximized) frame.setExtendedState(Frame.MAXIMIZED_BOTH);
|
||||
track(frame, settings);
|
||||
}
|
||||
|
||||
private static void track(JFrame frame, Settings settings) {
|
||||
Timer settle = new Timer(SETTLE_MS, e -> {
|
||||
int state = frame.getExtendedState();
|
||||
if ((state & Frame.ICONIFIED) != 0 || !frame.isShowing()) return;
|
||||
settings.windowMaximized = (state & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH;
|
||||
if (state != Frame.NORMAL) return;
|
||||
Rectangle r = frame.getBounds();
|
||||
settings.windowX = r.x;
|
||||
settings.windowY = r.y;
|
||||
settings.windowWidth = r.width;
|
||||
settings.windowHeight = r.height;
|
||||
});
|
||||
settle.setRepeats(false);
|
||||
frame.addComponentListener(new ComponentAdapter() {
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e) {
|
||||
settle.restart();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void componentMoved(ComponentEvent e) {
|
||||
settle.restart();
|
||||
}
|
||||
});
|
||||
frame.addWindowStateListener(e -> settle.restart());
|
||||
}
|
||||
|
||||
private static boolean onScreen(Rectangle r) {
|
||||
for (var device : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
|
||||
Rectangle visible = device.getDefaultConfiguration().getBounds().intersection(r);
|
||||
if (visible.width >= MIN_VISIBLE && visible.height >= MIN_VISIBLE) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user