1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
// Layer Style dialog: effect list on the left, settings of the selected effect on
// the right. Edits preview live on the layer; OK records one undo step.
import { el } from '../core/util.js';
import { EFFECTS, normalizeEffects, defaultEffect } from '../render/effects.js';
import { fieldFor } from './widgets.js';
import { openDialog } from './dialogs.js';
export async function layerStyleDialog(app, focus = null) {
const doc = app.doc;
const layer = doc?.activeLayer;
if (!layer) return;
const original = layer.effects;
const fx = normalizeEffects(original);
let current = focus || EFFECTS.find((e) => fx[e.id].enabled)?.id || EFFECTS[0].id;
const preview = () => {
layer.effects = anyEnabled(fx) ? structuredClone(fx) : null;
doc.markDirty();
};
const list = el('div', { class: 'fx-list' });
const pane = el('div', { class: 'fx-pane' });
const rows = new Map();
for (const e of EFFECTS) {
const cb = el('input', { type: 'checkbox', checked: fx[e.id].enabled, title: `Enable ${e.label}` });
cb.addEventListener('change', () => {
fx[e.id].enabled = cb.checked;
select(e.id);
preview();
});
const row = el('div', { class: 'fx-row' }, cb, el('span', {}, e.label));
row.addEventListener('click', (ev) => { if (ev.target !== cb) select(e.id); });
rows.set(e.id, { row, cb });
list.appendChild(row);
}
function select(id) {
current = id;
for (const [k, { row }] of rows) row.classList.toggle('active', k === id);
const spec = EFFECTS.find((e) => e.id === id);
pane.innerHTML = '';
pane.appendChild(el('div', { class: 'fx-pane-title' }, spec.label));
const form = el('div', { class: 'form' });
for (const p of spec.params) {
form.appendChild(fieldFor(p, fx[id][p.key], (v) => {
fx[id][p.key] = v;
// Adjusting an effect implies you want to see it.
if (!fx[id].enabled) { fx[id].enabled = true; rows.get(id).cb.checked = true; }
preview();
}));
}
const reset = el('button', {
type: 'button', class: 'btn',
onclick: () => {
fx[id] = { ...defaultEffect(id), enabled: fx[id].enabled };
select(id);
preview();
},
}, 'Reset to defaults');
form.appendChild(el('div', { class: 'form-row' }, el('span'), reset));
pane.appendChild(form);
}
select(current);
const ok = await openDialog({
title: `Layer Style — ${layer.name}`,
body: el('div', { class: 'fx-dialog' }, list, pane),
width: 560,
buttons: [{ label: 'Cancel', value: false }, { label: 'OK', value: true, primary: true }],
});
layer.effects = original;
if (ok && doc.layers.includes(layer)) {
const next = anyEnabled(fx) ? structuredClone(fx) : null;
if (JSON.stringify(next) !== JSON.stringify(original)) doc.change('Layer style', () => { layer.effects = next; });
else doc.markDirty();
} else {
doc.markDirty();
}
}
function anyEnabled(fx) {
return EFFECTS.some((e) => fx[e.id]?.enabled);
}
|