Add browser-style drag-to-reorder for chat and server tabs
Dragging a tab shows animated snapshots on the glass pane and swaps with whichever neighbour it reaches the midpoint of; the real tab model is only touched once, on release, so an in-flight mouse grab never gets pulled out from under a rebuilt tab component. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -34,11 +34,14 @@ final class ServerTabPane extends JPanel {
|
||||
private final Listener listener;
|
||||
private final JTabbedPane tabbed = new JTabbedPane();
|
||||
private final List<ServerTab> tabs = new ArrayList<>();
|
||||
private final TabDragReorder dragReorder = new TabDragReorder(tabbed, this::moveTab);
|
||||
|
||||
/** True while the tab strip is in use, i.e. more than one connection is open. */
|
||||
private boolean tabbedMode;
|
||||
/** Suppresses selection callbacks while we rearrange the pane ourselves. */
|
||||
private boolean updating;
|
||||
/** Remembered so a drag-reorder can rebuild the tab labels without the caller's help. */
|
||||
private ServerTab lastMicTab;
|
||||
|
||||
ServerTabPane(Listener listener) {
|
||||
super(new BorderLayout());
|
||||
@@ -51,6 +54,24 @@ final class ServerTabPane extends JPanel {
|
||||
if (i >= 0 && i < tabs.size()) listener.selectTab(tabs.get(i));
|
||||
});
|
||||
tabbed.addMouseWheelListener(this::onWheel);
|
||||
dragReorder.attach(tabbed);
|
||||
}
|
||||
|
||||
/** Drags a tab from one position to another, keeping the current selection on screen. */
|
||||
private void moveTab(int from, int to) {
|
||||
if (from < 0 || to < 0 || from >= tabs.size() || to >= tabs.size() || from == to) return;
|
||||
ServerTab selected = tabbed.getSelectedIndex() >= 0 && tabbed.getSelectedIndex() < tabs.size()
|
||||
? tabs.get(tabbed.getSelectedIndex()) : null;
|
||||
tabs.add(to, tabs.remove(from));
|
||||
updating = true;
|
||||
try {
|
||||
tabbed.removeAll();
|
||||
for (ServerTab tab : tabs) tabbed.addTab(tab.title(), tab.component());
|
||||
if (selected != null) tabbed.setSelectedIndex(tabs.indexOf(selected));
|
||||
} finally {
|
||||
updating = false;
|
||||
}
|
||||
refresh(lastMicTab);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,6 +114,7 @@ final class ServerTabPane extends JPanel {
|
||||
|
||||
/** Refreshes the tab labels; {@code micTab} is marked as owning the microphone. */
|
||||
void refresh(ServerTab micTab) {
|
||||
lastMicTab = micTab;
|
||||
if (!tabbedMode) return;
|
||||
for (int i = 0; i < tabs.size(); i++) {
|
||||
ServerTab tab = tabs.get(i);
|
||||
@@ -143,6 +165,8 @@ final class ServerTabPane extends JPanel {
|
||||
}
|
||||
});
|
||||
cell.add(label);
|
||||
dragReorder.attach(cell);
|
||||
dragReorder.attach(label);
|
||||
|
||||
JButton close = new JButton("✕");
|
||||
close.setFont(Theme.UI_FONT);
|
||||
|
||||
Reference in New Issue
Block a user