Find in chat with Ctrl+F
A find bar above the conversations highlights matches and steps through them, or filters the selected tab down to matching messages. New lines no longer pull the view away while it is open. Ctrl+F opens it from anywhere the tree doesn't claim the shortcut. Note tabs are now found by their scroll pane like other tabs, so sending checks for a target instead of assuming every tab has one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,8 +3,11 @@ package com.ts3client.ui;
|
||||
import com.formdev.flatlaf.util.ScaledEmptyBorder;
|
||||
import com.formdev.flatlaf.util.UIScale;
|
||||
import com.ts3client.text.BBCode;
|
||||
import com.ts3client.text.HtmlText;
|
||||
import com.ts3client.text.TextQuery;
|
||||
import com.ts3client.text.TsLink;
|
||||
|
||||
import javax.swing.AbstractAction;
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.Icon;
|
||||
import javax.swing.JEditorPane;
|
||||
@@ -13,6 +16,7 @@ import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTabbedPane;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.KeyStroke;
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.event.HyperlinkEvent;
|
||||
import javax.swing.text.BadLocationException;
|
||||
@@ -24,15 +28,18 @@ import java.awt.Dimension;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.Insets;
|
||||
import java.awt.Point;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseWheelEvent;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Chat area with one tab per conversation (server, current channel and one per
|
||||
@@ -76,6 +83,20 @@ public final class ChatPanel extends JPanel {
|
||||
|
||||
private final TabDragReorder dragReorder = new TabDragReorder(tabs, this::moveTab);
|
||||
|
||||
private final ChatSearch search = new ChatSearch(new ChatSearch.Host() {
|
||||
@Override
|
||||
public JEditorPane selectedLog() {
|
||||
Tab t = selectedTab();
|
||||
return t == null ? null : t.log;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showMatching(TextQuery filter) {
|
||||
Tab t = selectedTab();
|
||||
if (t != null) t.showMatching(filter);
|
||||
}
|
||||
});
|
||||
|
||||
private SendHandler sendHandler;
|
||||
private LinkHandler linkHandler;
|
||||
|
||||
@@ -93,10 +114,24 @@ public final class ChatPanel extends JPanel {
|
||||
tabs.addChangeListener(e -> {
|
||||
Tab t = selectedTab();
|
||||
if (t != null) t.setUnread(false);
|
||||
// A search filters only the tab it is in; the one left behind shows everything again.
|
||||
for (Tab other : allTabs()) {
|
||||
if (other != t) other.showMatching(null);
|
||||
}
|
||||
search.retarget();
|
||||
});
|
||||
tabs.addMouseWheelListener(this::onWheel);
|
||||
dragReorder.attach(tabs);
|
||||
add(tabs, BorderLayout.CENTER);
|
||||
add(search.bar(), BorderLayout.NORTH);
|
||||
String find = "ts3-chat-find";
|
||||
getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("control F"), find);
|
||||
getActionMap().put(find, new AbstractAction() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
openFind();
|
||||
}
|
||||
});
|
||||
|
||||
JPanel bottom = new JPanel(new BorderLayout(UIScale.scale(4), 0));
|
||||
bottom.setBorder(new ScaledEmptyBorder(4, 0, 0, 0));
|
||||
@@ -116,6 +151,11 @@ public final class ChatPanel extends JPanel {
|
||||
this.linkHandler = h;
|
||||
}
|
||||
|
||||
/** Opens the find bar on the selected conversation. */
|
||||
public void openFind() {
|
||||
edt(search::open);
|
||||
}
|
||||
|
||||
/** Appends text to the input line, e.g. a client link dropped onto the chat. */
|
||||
public void appendToInput(String text) {
|
||||
edt(() -> {
|
||||
@@ -211,7 +251,7 @@ public final class ChatPanel extends JPanel {
|
||||
String text = input.getText().trim();
|
||||
if (text.isEmpty() || sendHandler == null) return;
|
||||
Tab tab = selectedTab();
|
||||
if (tab == null) return;
|
||||
if (tab == null || tab.target == null) return;
|
||||
sendHandler.send(tab.target, tab.clientId, text);
|
||||
input.setText("");
|
||||
}
|
||||
@@ -332,9 +372,21 @@ public final class ChatPanel extends JPanel {
|
||||
for (Tab t : privateTabs.values()) {
|
||||
if (t.scroll == c) return t;
|
||||
}
|
||||
for (Tab t : noteTabs.values()) {
|
||||
if (t.scroll == c) return t;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private List<Tab> allTabs() {
|
||||
List<Tab> all = new ArrayList<>();
|
||||
all.add(serverTab);
|
||||
all.add(channelTab);
|
||||
all.addAll(privateTabs.values());
|
||||
all.addAll(noteTabs.values());
|
||||
return all;
|
||||
}
|
||||
|
||||
private void edt(Runnable r) {
|
||||
if (SwingUtilities.isEventDispatchThread()) r.run();
|
||||
else SwingUtilities.invokeLater(r);
|
||||
@@ -355,6 +407,10 @@ public final class ChatPanel extends JPanel {
|
||||
final Runnable onClose;
|
||||
private final Icon icon;
|
||||
private JLabel titleLabel;
|
||||
/** Everything this conversation has shown, whether or not a search currently hides it. */
|
||||
private final List<String> entries = new ArrayList<>();
|
||||
/** Only messages matching this are on screen; {@code null} shows all. */
|
||||
private TextQuery filter;
|
||||
|
||||
Tab(Target target, int clientId, String title, Icon icon) {
|
||||
this(target, clientId, "", title, icon, null, null);
|
||||
@@ -474,18 +530,37 @@ public final class ChatPanel extends JPanel {
|
||||
}
|
||||
|
||||
void setHistory(List<String> htmlEntries) {
|
||||
StringBuilder html = new StringBuilder();
|
||||
entries.clear();
|
||||
if (htmlEntries != null) {
|
||||
for (String entry : htmlEntries) html.append(normalizeTs3LogEntry(entry));
|
||||
for (String entry : htmlEntries) entries.add(normalizeTs3LogEntry(entry));
|
||||
}
|
||||
log.setText("<html><body><div id=\"chatlog\">" + html + "</div></body></html>");
|
||||
log.setCaretPosition(log.getDocument().getLength());
|
||||
render();
|
||||
}
|
||||
|
||||
/** Replaces the whole log with fixed content; used by static-content tabs. */
|
||||
void setStaticContent(String html) {
|
||||
entries.clear();
|
||||
entries.add(html);
|
||||
render();
|
||||
}
|
||||
|
||||
void showMatching(TextQuery filter) {
|
||||
if (Objects.equals(this.filter, filter)) return;
|
||||
this.filter = filter;
|
||||
render();
|
||||
}
|
||||
|
||||
private boolean shows(String entry) {
|
||||
return filter == null || filter.matches(HtmlText.plain(entry));
|
||||
}
|
||||
|
||||
private void render() {
|
||||
StringBuilder html = new StringBuilder();
|
||||
for (String entry : entries) {
|
||||
if (shows(entry)) html.append(entry);
|
||||
}
|
||||
log.setText("<html><body><div id=\"chatlog\">" + html + "</div></body></html>");
|
||||
log.setCaretPosition(0);
|
||||
log.setCaretPosition(noteKey != null ? 0 : log.getDocument().getLength());
|
||||
}
|
||||
|
||||
void appendLine(String html) {
|
||||
@@ -493,11 +568,16 @@ public final class ChatPanel extends JPanel {
|
||||
}
|
||||
|
||||
void appendRawHtml(String html) {
|
||||
try {
|
||||
HTMLDocument doc = (HTMLDocument) log.getDocument();
|
||||
doc.insertBeforeEnd(doc.getElement("chatlog"), normalizeTs3LogEntry(html));
|
||||
log.setCaretPosition(doc.getLength());
|
||||
} catch (BadLocationException | IOException ignored) {
|
||||
String entry = normalizeTs3LogEntry(html);
|
||||
entries.add(entry);
|
||||
if (shows(entry)) {
|
||||
try {
|
||||
HTMLDocument doc = (HTMLDocument) log.getDocument();
|
||||
doc.insertBeforeEnd(doc.getElement("chatlog"), entry);
|
||||
// Following new lines would pull the view away from the match being read.
|
||||
if (!search.isOpen()) log.setCaretPosition(doc.getLength());
|
||||
} catch (BadLocationException | IOException ignored) {
|
||||
}
|
||||
}
|
||||
if (tabs.getSelectedComponent() != scroll) setUnread(true);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user