Connect a left-clicked bookmark on the current tab

Middle-click still opens the server in a new tab.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-20 06:55:29 +00:00
parent 50b71eb12e
commit b85d1e5076
3 changed files with 49 additions and 6 deletions

View File

@@ -202,7 +202,8 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener {
} }
@Override @Override
public void onConnectBookmark(Bookmark bookmark) { public void onConnectBookmark(Bookmark bookmark, boolean newTab) {
if (newTab) selectTab(newTab());
connectToBookmark(bookmark); connectToBookmark(bookmark);
} }
@@ -571,21 +572,31 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener {
*/ */
private void startConnection(String address, int port, String nickname, String password, String identityId, private void startConnection(String address, int port, String nickname, String password, String identityId,
String channel, String channelPassword) { String channel, String channelPassword) {
startConnection(address, port, nickname, password, identityId, channel, channelPassword, false);
}
/**
* @param replaceCurrent connect on the current tab, leaving the server it is on, instead of
* moving to a free tab
*/
private void startConnection(String address, int port, String nickname, String password, String identityId,
String channel, String channelPassword, boolean replaceCurrent) {
settings.lastAddress = address + ":" + port; settings.lastAddress = address + ":" + port;
settings.nickname = nickname; settings.nickname = nickname;
settings.serverPassword = password; settings.serverPassword = password;
settings.save(); settings.save();
ServerTab tab = tabForNewConnection(); ServerTab tab = (replaceCurrent && selected != null) ? selected : tabForNewConnection();
// The first connection to come up takes the microphone; later ones are muted // The first connection to come up takes the microphone; later ones are muted
// until the user activates them. // until the user activates them.
if (micTab == null) setMicTab(tab); if (micTab == null) setMicTab(tab);
tab.connect(address, port, nickname, password, identityId, channel, channelPassword); tab.switchTo(address, port, nickname, password, identityId, channel, channelPassword);
} }
/** Connects on the current tab, leaving whatever server it is on. */
private void connectToBookmark(Bookmark b) { private void connectToBookmark(Bookmark b) {
String nick = (b.nickname != null && !b.nickname.isBlank()) ? b.nickname : settings.nickname; String nick = (b.nickname != null && !b.nickname.isBlank()) ? b.nickname : settings.nickname;
startConnection(b.address, b.port, nick, b.password, b.identityId, b.channel, b.channelPassword); startConnection(b.address, b.port, nick, b.password, b.identityId, b.channel, b.channelPassword, true);
} }
private void addCurrentServerBookmark() { private void addCurrentServerBookmark() {

View File

@@ -8,6 +8,10 @@ import javax.swing.JMenu;
import javax.swing.JMenuBar; import javax.swing.JMenuBar;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
import javax.swing.KeyStroke; import javax.swing.KeyStroke;
import javax.swing.MenuSelectionManager;
import javax.swing.SwingUtilities;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/** /**
* The main window's menu bar: Connections, Bookmarks, Self, Tools and Help. * The main window's menu bar: Connections, Bookmarks, Self, Tools and Help.
@@ -45,7 +49,8 @@ final class MainMenuBar extends JMenuBar {
void onShowAbout(); void onShowAbout();
void onConnectBookmark(Bookmark bookmark); /** @param newTab open the server in a new tab instead of replacing the current one */
void onConnectBookmark(Bookmark bookmark, boolean newTab);
void onAddCurrentServerBookmark(); void onAddCurrentServerBookmark();
@@ -141,7 +146,16 @@ final class MainMenuBar extends JMenuBar {
bookmarksMenu.removeAll(); bookmarksMenu.removeAll();
for (Bookmark b : bookmarks.all()) { for (Bookmark b : bookmarks.all()) {
JMenuItem item = new JMenuItem(b.displayName(), Icons.of("SERVER_GREEN")); JMenuItem item = new JMenuItem(b.displayName(), Icons.of("SERVER_GREEN"));
item.addActionListener(e -> listener.onConnectBookmark(b)); item.setToolTipText("Connect here (middle-click to open a new tab)");
item.addActionListener(e -> listener.onConnectBookmark(b, false));
item.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (!SwingUtilities.isMiddleMouseButton(e)) return;
MenuSelectionManager.defaultManager().clearSelectedPath();
listener.onConnectBookmark(b, true);
}
});
bookmarksMenu.add(item); bookmarksMenu.add(item);
} }
if (!bookmarks.all().isEmpty()) bookmarksMenu.addSeparator(); if (!bookmarks.all().isEmpty()) bookmarksMenu.addSeparator();

View File

@@ -230,6 +230,24 @@ final class ServerTab implements ServerTabConnectionEvents.Listener {
}, "identity-resolve").start(); }, "identity-resolve").start();
} }
/**
* Replaces this tab's connection: leaves the server it is on, then connects to the
* new one in its place — what left-clicking a bookmark does.
*/
void switchTo(String address, int port, String nickname, String password, String identityId,
String channel, String channelPassword) {
if (!isBusy()) {
connect(address, port, nickname, password, identityId, channel, channelPassword);
return;
}
new Thread(() -> {
conn.disconnectBlocking("Switching servers");
// Queued behind the disconnect's own UI work, which clears the "connecting" flag.
SwingUtilities.invokeLater(() ->
connect(address, port, nickname, password, identityId, channel, channelPassword));
}, "ts3j-switch-server").start();
}
void disconnect() { void disconnect() {
if (conn.isConnected()) conn.disconnect(); if (conn.isConnected()) conn.disconnect();
} }