Files
ts3java/ts3-client/swing/src/main/java/com/ts3client/ui/DesignPanel.java
T
erikbrockoandClaude Opus 5.5 59176ea017 Colour client rows in the channel tree: for everyone, while talking, while muted or away
Design settings on desktop and Android offer a few see-through presets
that suit light and dark themes alike, or a custom colour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 21:47:51 +00:00

283 lines
12 KiB
Java

package com.ts3client.ui;
import com.formdev.flatlaf.util.ScaledEmptyBorder;
import com.formdev.flatlaf.util.UIScale;
import com.ts3client.config.Settings;
import com.ts3client.gfx.IconPack;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.File;
import java.util.Arrays;
/**
* Options page for how the client looks: the light or dark look-and-feel, the interface
* scale, the channel tree's flags and row colours, and the icon pack the user interface is drawn with — where to look for more of
* them, and a viewer showing everything the active pack contains.
*
* <p>Theme and pack apply right away so the change can be seen in the window behind the
* dialog; cancelling puts the previous ones back. The scale is only read at startup.
*/
final class DesignPanel extends JPanel {
/** Size of the tiles in the icon viewer. */
private static final int PREVIEW_SIZE = 32;
private static final int TILE = 44;
/** Stands for "no pack": the icons the client draws itself. */
private static final String BUILT_IN = "Plain (drawn by the client)";
/** The scale factors offered besides following the desktop (0). */
private static final double[] SCALES = {0, 1, 1.25, 1.5, 1.75, 2, 2.5, 3};
private final Settings settings;
/** What {@link #revert()} goes back to: as the dialog opened, or as last applied. */
private String originalPackId;
private String originalPackDir;
private Settings.Appearance originalAppearance;
private final JComboBox<Settings.Appearance> appearanceCombo =
new JComboBox<>(Settings.Appearance.values());
private final JComboBox<Double> scaleCombo = new JComboBox<>();
private final JComboBox<Object> packCombo = new JComboBox<>();
private final JLabel packInfo = new JLabel();
private final JTextField packDirField;
private final JCheckBox countryFlags = new JCheckBox("Show country flags in the channel tree");
private final RowColorCombo anyColor;
private final RowColorCombo talkingColor;
private final RowColorCombo mutedAwayColor;
private final DefaultListModel<String> previewModel = new DefaultListModel<>();
private final JList<String> preview = new JList<>(previewModel);
DesignPanel(Settings settings) {
super(new BorderLayout(0, UIScale.scale(8)));
this.settings = settings;
this.originalPackId = settings.iconPack;
this.originalPackDir = settings.iconPackDir;
this.originalAppearance = settings.appearance;
this.packDirField = new JTextField(settings.iconPackDir, 18);
this.anyColor = new RowColorCombo(settings.clientRowColors.any);
this.talkingColor = new RowColorCombo(settings.clientRowColors.talking);
this.mutedAwayColor = new RowColorCombo(settings.clientRowColors.mutedAway);
setBorder(new ScaledEmptyBorder(12, 12, 12, 12));
add(buildHeader(), BorderLayout.NORTH);
add(buildPreview(), BorderLayout.CENTER);
reloadPacks();
}
/** Copies the chosen appearance and pack into the settings; the caller saves them. */
void apply() {
settings.appearance = selectedAppearance();
if (scaleCombo.getSelectedItem() instanceof Double scale) settings.uiScale = scale;
settings.iconPackDir = packDirField.getText().trim();
Object selected = packCombo.getSelectedItem();
settings.iconPack = selected instanceof IconPack pack ? pack.id() : "";
settings.countryFlags = countryFlags.isSelected();
settings.clientRowColors.any = anyColor.value();
settings.clientRowColors.talking = talkingColor.value();
settings.clientRowColors.mutedAway = mutedAwayColor.value();
originalPackId = settings.iconPack;
originalPackDir = settings.iconPackDir;
originalAppearance = settings.appearance;
}
/** Puts back the appearance and pack last applied, for a cancelled edit. */
void revert() {
settings.iconPack = originalPackId;
settings.iconPackDir = originalPackDir;
IconTheme.get().reload(settings);
if (settings.appearance != originalAppearance) {
settings.appearance = originalAppearance;
LookAndFeelManager.switchTo(originalAppearance);
}
}
// ---- layout ----
private JComponent buildHeader() {
JPanel p = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = UIScale.scale(new Insets(4, 4, 4, 4));
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.HORIZONTAL;
packCombo.setToolTipText("Icon packs installed here or in a TeamSpeak 3 client");
packCombo.addActionListener(e -> onPackSelected());
packCombo.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index,
boolean selected, boolean focus) {
super.getListCellRendererComponent(list, value, index, selected, focus);
setText(value instanceof IconPack pack ? pack.name() : String.valueOf(value));
return this;
}
});
appearanceCombo.setToolTipText("Light or dark window colours");
appearanceCombo.setSelectedItem(settings.appearance);
appearanceCombo.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index,
boolean selected, boolean focus) {
super.getListCellRendererComponent(list, value, index, selected, focus);
setText(value == Settings.Appearance.DARK ? "Dark" : "Light");
return this;
}
});
appearanceCombo.addActionListener(e -> onAppearanceSelected());
JPanel dirRow = new JPanel(new BorderLayout(UIScale.scale(6), 0));
JButton browse = new JButton("Browse…");
browse.addActionListener(e -> browseForPackFolder());
dirRow.add(packDirField, BorderLayout.CENTER);
dirRow.add(browse, BorderLayout.EAST);
packDirField.setToolTipText("Extra folder to look for icon packs in");
packInfo.setEnabled(false);
countryFlags.setSelected(settings.countryFlags);
int row = 0;
FormPanel.addSection(p, c, row++, "Appearance");
FormPanel.addRow(p, c, row++, new JLabel("Theme:"), appearanceCombo);
if (DisplayScale.isManaged()) {
buildScaleCombo();
FormPanel.addRow(p, c, row++, new JLabel("Interface scale:"), scaleCombo);
}
FormPanel.addSection(p, c, row++, "Channel Tree");
FormPanel.addWide(p, c, row++, countryFlags);
FormPanel.addRow(p, c, row++, new JLabel("Client background:"), anyColor);
FormPanel.addRow(p, c, row++, new JLabel("While talking:"), talkingColor);
FormPanel.addRow(p, c, row++, new JLabel("Muted / away:"), mutedAwayColor);
FormPanel.addSection(p, c, row++, "Icon Pack");
FormPanel.addRow(p, c, row++, new JLabel("Icon pack:"), packCombo);
c.gridx = 1;
c.gridy = row++;
p.add(packInfo, c);
FormPanel.addRow(p, c, row, new JLabel("Extra pack folder:"), dirRow);
return p;
}
private void buildScaleCombo() {
for (double scale : SCALES) scaleCombo.addItem(scale);
if (settings.uiScale > 0 && Arrays.stream(SCALES).noneMatch(s -> s == settings.uiScale)) {
scaleCombo.addItem(settings.uiScale);
}
scaleCombo.setSelectedItem(Math.max(0, settings.uiScale));
scaleCombo.setToolTipText("Size of text, icons and controls; takes effect after a restart");
scaleCombo.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index,
boolean selected, boolean focus) {
super.getListCellRendererComponent(list, value, index, selected, focus);
double scale = value instanceof Double d ? d : 0;
setText(scale > 0 ? percent(scale)
: "Automatic (" + percent(DisplayScale.detected()) + ")");
return this;
}
});
}
private static String percent(double scale) {
return Math.round(scale * 100) + "%";
}
private JComponent buildPreview() {
preview.setLayoutOrientation(JList.HORIZONTAL_WRAP);
preview.setVisibleRowCount(-1);
preview.setFixedCellWidth(UIScale.scale(TILE));
preview.setFixedCellHeight(UIScale.scale(TILE));
preview.setCellRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index,
boolean selected, boolean focus) {
super.getListCellRendererComponent(list, value, index, selected, focus);
String key = String.valueOf(value);
ImageIcon icon = Icons.of(key, UIScale.scale(PREVIEW_SIZE));
setText("");
setIcon(icon);
setToolTipText(key);
setHorizontalAlignment(SwingConstants.CENTER);
return this;
}
});
JScrollPane scroll = new JScrollPane(preview);
scroll.setPreferredSize(UIScale.scale(new Dimension(360, 200)));
return scroll;
}
// ---- behaviour ----
private void reloadPacks() {
IconTheme theme = IconTheme.get();
theme.reload(settings);
// Filling the combo box changes its selection, and with it the active pack,
// so remember the one the settings asked for before touching it.
IconPack selected = theme.activePack();
packCombo.removeAllItems();
packCombo.addItem(BUILT_IN);
for (IconPack pack : theme.packs()) packCombo.addItem(pack);
packCombo.setSelectedItem(selected == null ? BUILT_IN : selected);
onPackSelected();
}
private Settings.Appearance selectedAppearance() {
Object selected = appearanceCombo.getSelectedItem();
return selected instanceof Settings.Appearance a ? a : Settings.Appearance.LIGHT;
}
private void onAppearanceSelected() {
Settings.Appearance chosen = selectedAppearance();
if (chosen == settings.appearance) return;
settings.appearance = chosen;
LookAndFeelManager.switchTo(chosen);
}
private void onPackSelected() {
Object selected = packCombo.getSelectedItem();
IconPack pack = selected instanceof IconPack p ? p : null;
IconTheme.get().setPack(pack);
previewModel.clear();
if (pack == null) {
packInfo.setText("Drawn by the client itself");
} else {
for (String key : pack.keys()) previewModel.addElement(key);
packInfo.setText(pack.keys().size() + " icons"
+ (pack.fallsBack() ? ", missing ones from the default pack" : ""));
}
}
private void browseForPackFolder() {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setDialogTitle("Folder containing icon packs");
String current = packDirField.getText().trim();
if (!current.isEmpty()) chooser.setCurrentDirectory(new File(current));
if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) return;
packDirField.setText(chooser.getSelectedFile().getAbsolutePath());
settings.iconPackDir = packDirField.getText();
reloadPacks();
}
}