Files
ts3java/ts3-client/swing/src/main/java/com/ts3client/ui/ServerTabPane.java
T
erikbrockoandClaude Opus 5.5 0ff0919db2 Keep the server tab strip in step with theme switches
The strip is out of the component tree while one server is open, so a
look-and-feel switch missed it and it came back in the old theme's colours.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 16:31:43 +00:00

199 lines
7.1 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 javax.swing.SwingUtilities;
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);
}
/**
* The tab strip is out of the component tree while a single server is open, so a
* look-and-feel switch would miss it and leave it in the old theme's colours.
*/
@Override
public void updateUI() {
super.updateUI();
if (tabbed != null && tabbed.getParent() == null) SwingUtilities.updateComponentTreeUI(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;
}
}