package com.ts3client.ui; import com.formdev.flatlaf.util.UIScale; import com.ts3client.text.BBCode; import javax.swing.JEditorPane; import javax.swing.JViewport; import javax.swing.SwingUtilities; import javax.swing.text.html.HTMLEditorKit; import javax.swing.text.AttributeSet; import javax.swing.text.html.StyleSheet; import java.awt.Color; import java.awt.Font; /** * The HTML panes showing TeamSpeak text — chat logs, client and channel information — * and the theme-dependent stylesheet they share. * *

Text that is already on screen keeps the colours its stylesheet was built with, so * a look-and-feel change re-installs the editor kit and parses the pane's content again * instead of only recolouring what is written afterwards. */ final class HtmlStyles { private static final String BODY_STYLE = "HtmlStyles.bodyStyle"; private HtmlStyles() { } /** * @param bodyStyle extra CSS for {@code body}, e.g. font and margins; the text colour * is added by the theme * @return a read-only HTML pane whose stylesheet follows the current theme */ static JEditorPane pane(String bodyStyle) { JEditorPane pane = new JEditorPane() { @Override public void updateUI() { super.updateUI(); restyle(this); } }; pane.putClientProperty(BODY_STYLE, bodyStyle); pane.setEditable(false); installKit(pane, null, false); return pane; } /** * Paints the scroll pane {@code pane} sits in with the pane's own background: text * only reaches as far as it goes, and the empty space below it would otherwise show * the window's colour instead of the log's. */ static void fillViewport(JEditorPane pane) { if (pane.getParent() instanceof JViewport viewport) { viewport.setBackground(pane.getBackground()); } } /** Re-themes {@code pane}, keeping the text it shows. */ private static void restyle(JEditorPane pane) { // Also runs from JEditorPane's constructor, before the pane is one of ours. if (pane.getClientProperty(BODY_STYLE) == null) return; int length = pane.getDocument().getLength(); String html = length == 0 ? null : pane.getText(); // A chat log sits at its newest line; keep it there rather than jumping to the top. boolean atEnd = pane.getCaretPosition() >= length; // Not during the look-and-feel update that got us here: the pane is rebuilding its // views, and replacing the document underneath it would race with that. SwingUtilities.invokeLater(() -> installKit(pane, html, atEnd)); } private static void installKit(JEditorPane pane, String html, boolean atEnd) { HTMLEditorKit kit = new HTMLEditorKit(); StyleSheet css = new ScaledStyleSheet(); css.addStyleSheet(kit.getStyleSheet()); Object bodyStyle = pane.getClientProperty(BODY_STYLE); css.addRule("body { " + (bodyStyle == null ? "" : bodyStyle + " ") + "color:" + hex(Theme.chatText()) + "; }"); // Secondary text (timestamps, labels, placeholders), an author's name and the lines // about what happened on the server. css.addRule(".muted { color:" + hex(Theme.chatSystem()) + "; }"); css.addRule(".name { color:" + hex(Theme.chatName()) + "; font-weight:bold; }"); css.addRule(".event { color:" + hex(Theme.chatEvent()) + "; }"); css.addRule("a { color:" + hex(Theme.chatName()) + "; text-decoration:none; }"); // Client references look exactly like a message author's name. css.addRule("a." + BBCode.IDENTITY_LINK_CLASS + " { color:" + hex(Theme.chatName()) + "; font-weight:bold; text-decoration:none; }"); // Channel (and other TeamSpeak protocol) references stand out of a line the same // way a client's name does. css.addRule("a." + BBCode.TS_LINK_CLASS + " { color:" + hex(Theme.chatName()) + "; font-weight:bold; text-decoration:none; }"); // External links are underlined so they can't be mistaken for an identity. css.addRule("a." + BBCode.EXTERNAL_LINK_CLASS + " { color:" + hex(Theme.link()) + "; text-decoration:underline; }"); kit.setStyleSheet(css); pane.setEditorKit(kit); fillViewport(pane); if (html != null) { pane.setText(html); pane.setCaretPosition(atEnd ? pane.getDocument().getLength() : 0); } } /** A CSS length of {@code px} at the interface scale. */ static String px(int px) { return UIScale.scale(px) + "px"; } private static String hex(Color c) { return String.format("#%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue()); } /** * Scales every font size, the way a browser scales CSS pixels: Swing takes a * {@code font-size} in pixels literally, and the text is written in unscaled ones. */ private static final class ScaledStyleSheet extends StyleSheet { @Override public Font getFont(AttributeSet a) { Font font = super.getFont(a); return UIScale.getUserScaleFactor() == 1f ? font : font.deriveFont(UIScale.scale(font.getSize2D())); } } }