Files
ts3java/ts3-client/swing/src/main/java/com/ts3client/ui/ServerTabPane.java
T
erikbrockoandClaude Opus 5.5 8f385b9343 Keep each server's session state in core
What a server tab knows beyond its views - the connection lifecycle,
title and status, the identity used, the microphone/speaker/away and
commander flags, chat sending and the channel lookups - moves from the
Swing ServerTab and ServerTabSelfState into core's ServerSession, so
another frontend gets the same behaviour without copying it.

ServerSession also takes over what the Swing event bridge decided on its
own: clearing the model on disconnect, naming the tab after the server,
showing senders under their contact names, and the chat's system lines.
The bridge now only updates views. SelfState moves along, while its
icons stay with the tray.

A server switch no longer needs to queue behind the UI thread: the
disconnect is reported synchronously, so the session is free again by
the time it reconnects.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 08:09:23 +00:00

188 lines
6.7 KiB
Java

package com.ts3client.ui;
import com.formdev.flatlaf.util.ScaledEmptyBorder;
import com.formdev.flatlaf.util.UIScale;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.util.ArrayList;
import java.util.List;
/**
* Container for the open server connections. With a single connection the
* server view fills the window as it always did; from the second connection on
* the views move into a {@link JTabbedPane} whose tab strip sits directly under
* the toolbar.
*/
final class ServerTabPane extends JPanel {
interface Listener {
void selectTab(ServerTab tab);
void closeTab(ServerTab tab);
}
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());
this.listener = listener;
tabbed.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
tabbed.setFocusable(false);
tabbed.addChangeListener(e -> {
if (updating) return;
int i = tabbed.getSelectedIndex();
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.session().title(), tab.component());
if (selected != null) tabbed.setSelectedIndex(tabs.indexOf(selected));
} finally {
updating = false;
}
refresh(lastMicTab);
}
/**
* Scrolling over the tab strip steps through the connections. The content
* area keeps its own scrolling, so only wheel events on the strip itself
* (and on the tab labels, which forward theirs) change tabs.
*/
private void onWheel(MouseWheelEvent e) {
if (!tabbedMode) return;
Component content = tabbed.getSelectedComponent();
if (content != null && content.getBounds().contains(e.getPoint())) return;
int steps = e.getWheelRotation();
if (steps == 0) return;
int target = Math.max(0, Math.min(tabs.size() - 1, tabbed.getSelectedIndex() + steps));
if (target != tabbed.getSelectedIndex()) listener.selectTab(tabs.get(target));
e.consume();
}
void addTab(ServerTab tab) {
tabs.add(tab);
relayout();
}
void removeTab(ServerTab tab) {
tabs.remove(tab);
relayout();
}
void setSelected(ServerTab tab) {
int i = tabs.indexOf(tab);
if (!tabbedMode || i < 0 || tabbed.getSelectedIndex() == i) return;
updating = true;
try {
tabbed.setSelectedIndex(i);
} finally {
updating = false;
}
}
/** 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);
tabbed.setTitleAt(i, tab.session().title());
tabbed.setToolTipTextAt(i, tab.session().status());
tabbed.setTabComponentAt(i, buildTabComponent(tab, tab == micTab));
}
}
/**
* Moves the views between the plain single-server layout and the tabbed one,
* keeping whatever is currently selected on screen.
*/
private void relayout() {
boolean wantTabs = tabs.size() > 1;
updating = true;
try {
removeAll();
tabbed.removeAll();
if (wantTabs) {
for (ServerTab tab : tabs) tabbed.addTab(tab.session().title(), tab.component());
add(tabbed, BorderLayout.CENTER);
} else if (!tabs.isEmpty()) {
add(tabs.get(0).component(), BorderLayout.CENTER);
}
tabbedMode = wantTabs;
} finally {
updating = false;
}
revalidate();
repaint();
}
/** A tab's label: title, microphone marker and a close button. */
private JComponent buildTabComponent(ServerTab tab, boolean hasMic) {
JPanel cell = new JPanel(new FlowLayout(FlowLayout.LEFT, UIScale.scale(4), 0));
cell.setOpaque(false);
cell.setBorder(new ScaledEmptyBorder(1, 0, 1, 0));
JLabel label = new JLabel(tab.session().title(), hasMic ? Icons.micActiveSmall() : null, JLabel.LEADING);
label.setFont(Theme.uiFont());
label.setToolTipText(hasMic ? "Speaking on this server" : tab.session().status());
// A label with a tooltip swallows mouse events, so select the tab explicitly.
label.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
listener.selectTab(tab);
}
});
cell.add(label);
dragReorder.attach(cell);
dragReorder.attach(label);
JButton close = new JButton("✕");
close.setFont(Theme.uiFont());
close.setFocusable(false);
close.setBorder(BorderFactory.createEmptyBorder());
close.setContentAreaFilled(false);
close.setMargin(new Insets(0, 0, 0, 0));
close.setForeground(Theme.chatSystem());
close.setPreferredSize(UIScale.scale(new Dimension(14, 14)));
close.setToolTipText("Close this connection");
close.addActionListener(e -> listener.closeTab(tab));
cell.add(close);
return cell;
}
}