Files
client/app/renderer/js/appearance.js
T

187 lines
10 KiB
JavaScript

// Local appearance preferences. TS6 reloadWindowScale uses token overrides, not page zoom;
// detailed tree/chat are the official defaults (main.js appearance constructor @1299504).
import { S, emit } from './state.js';
import { tp } from './core.js';
import { h, icon, formatTime } from './dom.js';
import { t, onLanguageChange } from './i18n.js';
import { toast } from './toast.js';
import { errorText } from './text.js';
import { toggleRow } from './settings-ui.js';
import { decorateSelect } from './select-controls.js';
export const OPTIONS = Object.freeze({
displaySize: Object.freeze(['small', 'medium', 'large']),
treeStyle: Object.freeze(['compact', 'detailed']),
chatStyle: Object.freeze(['compact', 'detailed']),
});
const DEFAULTS = Object.freeze({ displaySize: 'medium', treeStyle: 'detailed', chatStyle: 'detailed' });
// Display-size previews use the official itemSizingForSelectedScale values (main.js@4808105:
// item/icon/radius/padding-v/padding-h, scaled by 0.7). Tree and chat previews render the product's
// own tree rows and chat messages with synthetic local data, styled per option like the official
// live server-tree / chat-view previews (render@4820089).
const SCALE = Object.freeze({ small: [24, 20, 3, 8, 16], medium: [26, 22, 4, 10, 20], large: [30, 24, 5, 12, 22] });
const ROW_WIDTHS = ['60%', '80%', '80%', '60%', '45%'];
const PREVIEW_TREE = Object.freeze([['channel', 'Lobby', 0], ['member', 'Alex', 1, true], ['member', 'Mira', 1], ['channel', 'Gaming', 0], ['member', 'Sam', 1], ['channel', 'AFK', 0]]);
const HOSTS = Object.freeze({ displaySize: 'appearance-display-size', treeStyle: 'appearance-tree-style', chatStyle: 'appearance-chat-style' });
const CAPTIONS = Object.freeze({
displaySize: ['appearance.displaySize', 'settings.display_size.info'],
treeStyle: ['appearance.treeStyle', 'settings.tree_style.info'],
chatStyle: ['settings.chat_style', 'settings.chat_style.info'],
});
let hosts = null;
let saving = false;
const TREE_CONTROLS = [
// Official order (render@4863822): Show User Badges, Show Country Flags, Highlight Contacts, …
['showBadges', 'badges', true],
['showCountryFlags', 'flags', false],
['highlightContacts', 'contact_highlight', true],
['showClientsBeforeChannels', 'clients_before_channels', true],
['showChannelBanners', 'channel.banner', true],
['showChannelBackground', 'channel.bg', true],
['channelCommanderAsLamp', 'channel.commander', true],
];
async function save(key, value) {
if (saving) return;
const focused = document.activeElement?.id;
saving = true;
render();
try {
const result = await tp.setPrefs({ [key]: value });
if (!result?.ok || !result.prefs) throw new Error(result?.error?.message ?? 'Preferences were not saved');
S.prefs = result.prefs;
} catch (error) { toast(errorText(error), 'error'); }
finally {
saving = false; render();
if (focused) document.getElementById(focused)?.focus({ preventScroll: true });
}
}
function renderControls() {
const iconHost = document.getElementById('appearance-icon-mode');
if (iconHost) {
// available_icon_sizes order (main.js@4818693): Small, Large, Large when collapsed
const select = h('select', { id: 'appearance-sidebar-icon-mode', class: 'multiselect medium', disabled: saving },
h('option', { value: 'small', text: t('appearance.small'), dataset: { icon: 'delay-low' } }),
h('option', { value: 'large', text: t('appearance.large'), dataset: { icon: 'delay-medium' } }),
h('option', { value: 'adaptive', text: t('settings.appearance.sidebar.icon_view_mode.large_collapsed'), dataset: { icon: 'delay-high' } }));
select.value = S.prefs.sidebarIconMode ?? 'adaptive';
select.addEventListener('change', () => { void save('sidebarIconMode', select.value); });
// .ts-card-setting-container > .tsv-flex > label, .tsv-flex-grow, Multiselect.medium (min-width 200px)
iconHost.replaceChildren(h('div', { class: 'ts-card-setting-container' }, h('div', { class: 'tsv-flex' },
h('label', { for: select.id },
h('span', { class: 'ts-setting-title', text: t('settings.appearance.sidebar.icon_view_mode') }),
h('p', { class: 'tsv-font-subtitle', text: t('settings.appearance.sidebar.icon_view_mode.info') })),
h('div', { class: 'tsv-flex-grow' }), select)));
decorateSelect(select);
}
const treeHost = document.getElementById('appearance-tree-options');
if (treeHost) {
const columns = [h('div', { class: 'appearance-server-column' }), h('div', { class: 'appearance-server-column' })];
for (const [key, suffix, initial] of TREE_CONTROLS) {
const value = S.prefs[key] ?? initial;
const title = `settings.appearance.server.show.${suffix}`;
const input = h('input', { id: `appearance-pref-${key}`, type: 'checkbox', class: 'toggle', role: 'switch', checked: value, disabled: saving, dataset: { preference: key } });
input.addEventListener('change', () => { void save(key, input.checked); });
const info = suffix === 'channel.commander' ? `${title}.info` : `${title}.${value ? 'enabled' : 'disabled'}`;
columns[suffix.startsWith('channel.') ? 1 : 0].append(toggleRow(t(title), t(info), input));
}
treeHost.replaceChildren(...columns);
}
}
export function applyAppearance(prefs = S.prefs) {
const values = {};
for (const [key, options] of Object.entries(OPTIONS)) {
values[key] = options.includes(prefs?.[key]) ? prefs[key] : DEFAULTS[key];
document.documentElement.dataset[key] = values[key];
}
for (const [key, , initial] of TREE_CONTROLS) document.documentElement.dataset[key] = String(prefs?.[key] ?? initial);
document.documentElement.dataset.sidebarIconMode = prefs?.sidebarIconMode ?? 'adaptive';
emit('appearance-changed', values);
return values;
}
function scalePreview(value) {
const [item, iconSize, radius, padV, padH] = SCALE[value];
const box = h('div', { class: 'tsv-settings-window-scale-preview' });
box.style.setProperty('--tsv-settings-window-scale-preview-item-size', `${item * 0.7}px`);
box.style.setProperty('--tsv-settings-window-scale-preview-icon-size', `${iconSize * 0.7}px`);
box.style.setProperty('--tsv-settings-window-scale-preview-border-radius', `${radius * 0.7}px`);
box.style.setProperty('--tsv-settings-window-scale-preview-padding-v', `${padV * 0.7}px`);
box.style.setProperty('--tsv-settings-window-scale-preview-padding-h', `${padH * 0.7}px`);
for (const width of ROW_WIDTHS) {
const row = h('div', { class: 'tsv-settings-window-scale-preview-item' });
row.style.maxWidth = width;
box.append(row);
}
return box;
}
function treePreview(value) {
const list = h('ul', { class: 'tree ts-style-preview', dataset: { preview: value } });
for (const [type, name, depth, talking] of PREVIEW_TREE) {
const row = type === 'channel'
? h('li', { class: 'tree-row channel-row' }, h('span', { class: 'row-btn' }, icon('channel'), h('span', { class: 'row-label', text: name })))
: h('li', { class: 'tree-row member-row', dataset: { talking: String(Boolean(talking)) } }, h('span', { class: 'row-btn' },
h('span', { class: 'avatar' }, icon('avatar', 'avatar-base'), icon('client-detailed-talking', 'avatar-talking')), h('span', { class: 'row-label', text: name })));
row.style.setProperty('--depth', String(depth));
list.append(row);
}
return list;
}
function chatPreview(value) {
const list = h('ul', { class: 'ts-style-preview ts-chat-preview', dataset: { preview: value } });
const time = new Date(2026, 0, 1, 13, 36);
for (const [author, key] of [['Alex', 'settings.preview.chat.1'], ['Mira', 'settings.preview.chat.2']]) {
const stamp = () => h('time', { class: 'msg-time', text: formatTime(time) });
const inline = stamp(); inline.classList.add('msg-inline-time');
list.append(h('li', { class: 'msg', dataset: { head: 'true', own: 'false' } },
h('div', { class: 'msg-head' }, h('span', { class: 'msg-author', text: author }), stamp()), inline,
h('div', { class: 'msg-body ts-parsed-text-content', text: t(key) })));
}
return list;
}
function preview(key, value) {
if (key === 'treeStyle') return treePreview(value);
if (key === 'chatStyle') return chatPreview(value);
return scalePreview(value);
}
function render() {
if (!hosts) return;
const selected = applyAppearance();
const focused = document.activeElement?.id?.startsWith('appearance-') ? document.activeElement.id : null;
for (const [key, options] of Object.entries(OPTIONS)) {
const [titleKey, infoKey] = CAPTIONS[key];
const captionId = `appearance-${key}-caption`;
const caption = h('div', { id: captionId, class: 'ts-card-caption' },
h('span', { class: 'ts-setting-title', text: t(titleKey) }), h('p', { class: 'tsv-font-subtitle', text: t(infoKey) }));
const choices = h('div', { class: `ts-collection-container${key === 'displaySize' ? ' ts-scale-picker' : ''}`, role: 'radiogroup', 'aria-labelledby': captionId });
for (const value of options) {
const checked = selected[key] === value;
const button = h('button', {
id: `appearance-${key}-${value}`, type: 'button', role: 'radio',
class: `ts-collection-item ts-magnify-container${key === 'displaySize' ? ' small' : ''}${checked ? ' selected' : ''}`,
'aria-checked': String(checked), disabled: saving,
}, h('div', { class: 'ts-tree-preview-title' }, h('span', { text: t(`appearance.${value}`) })),
h('div', { class: `ts-tree-preview-container${checked ? ' selected' : ''}` }, h('div', { class: 'preview', inert: true, 'aria-hidden': 'true' }, h('div', { class: 'preview-inner' }, preview(key, value)))));
button.addEventListener('click', () => { if (selected[key] !== value) void save(key, value); });
choices.append(button);
}
hosts[key].replaceChildren(caption, choices);
}
renderControls();
if (focused) document.getElementById(focused)?.focus();
}
export function initAppearance() {
hosts = Object.fromEntries(Object.entries(HOSTS).map(([key, id]) => [key, document.getElementById(id)]));
if (Object.values(hosts).some((node) => !node)) hosts = null;
applyAppearance();
render();
onLanguageChange(render);
}