diff --git a/ts3-client/core/src/main/java/eu/lixko/ts3j/client/config/Settings.java b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/config/Settings.java index a88a523..e401495 100644 --- a/ts3-client/core/src/main/java/eu/lixko/ts3j/client/config/Settings.java +++ b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/config/Settings.java @@ -198,6 +198,11 @@ public final class Settings { public boolean showMasterVolumeSlider = true; /** Share of the tree/info column the tree takes, i.e. where their divider sits. */ public double infoSplitRatio = 0.68; + /** Share of the window's width the tree/info column takes, beside the chat. */ + public double chatSplitRatio = 0.45; + /** The main window's bounds when not maximised; a width of 0 until it has been placed. */ + public int windowX, windowY, windowWidth, windowHeight; + public boolean windowMaximized; /** Whether the ban list shows the bans we created ourselves in bold. */ public boolean banHighlightOwn = true; /** The ban list columns the user shows, comma-separated column names; empty for the default set. */ @@ -323,6 +328,12 @@ public final class Settings { showStatusBar = parseB(props.getProperty("showStatusBar"), showStatusBar); showMasterVolumeSlider = parseB(props.getProperty("showMasterVolumeSlider"), showMasterVolumeSlider); infoSplitRatio = parseD(props.getProperty("infoSplitRatio"), infoSplitRatio); + chatSplitRatio = parseD(props.getProperty("chatSplitRatio"), chatSplitRatio); + windowX = parseI(props.getProperty("windowX"), windowX); + windowY = parseI(props.getProperty("windowY"), windowY); + windowWidth = parseI(props.getProperty("windowWidth"), windowWidth); + windowHeight = parseI(props.getProperty("windowHeight"), windowHeight); + windowMaximized = parseB(props.getProperty("windowMaximized"), windowMaximized); banHighlightOwn = parseB(props.getProperty("banHighlightOwn"), banHighlightOwn); banListColumns = props.getProperty("banListColumns", banListColumns); notifications.load(props); @@ -385,6 +396,12 @@ public final class Settings { props.setProperty("showStatusBar", Boolean.toString(showStatusBar)); props.setProperty("showMasterVolumeSlider", Boolean.toString(showMasterVolumeSlider)); props.setProperty("infoSplitRatio", Double.toString(infoSplitRatio)); + props.setProperty("chatSplitRatio", Double.toString(chatSplitRatio)); + props.setProperty("windowX", Integer.toString(windowX)); + props.setProperty("windowY", Integer.toString(windowY)); + props.setProperty("windowWidth", Integer.toString(windowWidth)); + props.setProperty("windowHeight", Integer.toString(windowHeight)); + props.setProperty("windowMaximized", Boolean.toString(windowMaximized)); props.setProperty("banHighlightOwn", Boolean.toString(banHighlightOwn)); props.setProperty("banListColumns", banListColumns); notifications.store(props); diff --git a/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/MainFrame.java b/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/MainFrame.java index 4a874c2..697bf98 100644 --- a/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/MainFrame.java +++ b/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/MainFrame.java @@ -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 startup = bookmarks.startupBookmarks(); diff --git a/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/ServerTab.java b/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/ServerTab.java index c3b96bb..ffaf1d5 100644 --- a/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/ServerTab.java +++ b/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/ServerTab.java @@ -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; diff --git a/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/SplitRatio.java b/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/SplitRatio.java new file mode 100644 index 0000000..3fd6b4c --- /dev/null +++ b/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/SplitRatio.java @@ -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(); + } +} diff --git a/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/WindowGeometry.java b/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/WindowGeometry.java new file mode 100644 index 0000000..df7f373 --- /dev/null +++ b/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/WindowGeometry.java @@ -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; + } +}