aboutsummaryrefslogtreecommitdiff
path: root/build/js/ui/chrome.js
blob: 514d6016966d8bb113b0f0f236ba09ad1f1a8ebc (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Toolbox, tool-options bar, document tabs and status bar.
import { el, prettyShortcut } from '../core/util.js';
import { icon } from './icons.js';
import { fieldFor } from './widgets.js';
import { ColorPicker } from './colorpicker.js';
import { openDialog } from './dialogs.js';

export class Toolbox {
  constructor(app, container, toolOrder) {
    this.app = app;
    this.el = el('div', { class: 'toolbox' });
    container.appendChild(this.el);
    this.buttons = new Map();
    const grid = el('div', { class: 'tool-grid' });
    for (const id of toolOrder) {
      if (id === '-') { grid.appendChild(el('div', { class: 'tool-sep' })); continue; }
      const T = app.toolClasses.get(id);
      const b = el('button', {
        type: 'button', class: 'btn tool-btn', title: `${T.label}${T.shortcut ? ` (${prettyShortcut(T.shortcut)})` : ''}`,
        html: icon(T.icon, 20), onclick: () => app.setTool(id),
      });
      b.dataset.tool = id;
      this.buttons.set(id, b);
      grid.appendChild(b);
    }
    this.fg = el('button', { type: 'button', class: 'color-chip fg', title: 'Foreground color (click to edit)' });
    this.bg = el('button', { type: 'button', class: 'color-chip bg', title: 'Background color (click to edit)' });
    this.fg.addEventListener('click', () => this.editColor('fg'));
    this.bg.addEventListener('click', () => this.editColor('bg'));
    const swap = el('button', { type: 'button', class: 'btn icon-btn mini swap', title: 'Swap colors (X)', html: icon('swap', 12), onclick: () => app.run('color.swap') });
    const reset = el('button', { type: 'button', class: 'mini reset', title: 'Default colors (D)', onclick: () => app.run('color.reset') }, el('span'), el('span'));
    this.el.append(grid, el('div', { class: 'color-chips' }, this.bg, this.fg, swap, reset));
    app.on('tool', () => this.sync());
    app.on('colors', () => this.syncColors());
    this.sync();
    this.syncColors();
  }
  sync() {
    for (const [id, b] of this.buttons) b.classList.toggle('active', this.app.tool?.id === id);
  }
  syncColors() {
    this.fg.style.setProperty('--c', this.app.colors.fg.toCSS());
    this.bg.style.setProperty('--c', this.app.colors.bg.toCSS());
  }
  async editColor(which) {
    const orig = this.app.colors[which].clone();
    const picker = new ColorPicker({ color: orig, size: 260, onChange: (c) => this.app.setColor(which, c) });
    const ok = await openDialog({
      title: which === 'fg' ? 'Foreground color' : 'Background color', body: picker.el, width: 300,
      buttons: [{ label: 'Cancel', value: false }, { label: 'OK', value: true, primary: true }],
    });
    if (!ok) this.app.setColor(which, orig);
  }
}

export class ToolOptionsBar {
  constructor(app, container) {
    this.app = app;
    this.el = el('div', { class: 'options-bar' });
    container.appendChild(this.el);
    app.on('tool', () => this.refresh());
  }
  refresh() {
    const tool = this.app.tool;
    this.closeMore();
    this.el.innerHTML = '';
    if (!tool) return;
    this.el.appendChild(el('div', { class: 'options-tool' }, el('span', { html: icon(tool.constructor.icon, 18) }), el('span', {}, tool.label)));
    this.controls = [];
    this.readout = null;
    const more = tool.constructor.schema.filter((f) => f.more);
    for (const f of tool.constructor.schema) {
      if (f.type === 'hidden' || f.more) continue;
      if (f.type === 'action') {
        this.el.appendChild(el('button', { type: 'button', class: `btn${f.primary ? ' primary' : ''}`, onclick: () => tool.runAction(f.action) }, f.label));
        continue;
      }
      if (f.type === 'readout') {
        this.readout = el('span', { class: 'readout' }, tool[f.key] || '');
        this.readoutKey = f.key;
        this.el.appendChild(this.readout);
        continue;
      }
      const ctl = fieldFor(f, tool.options[f.key], (v) => {
        tool.setOption(f.key, v);
        if (this.controls.some((c) => c.f.when)) this.applyVisibility();
      }, { compact: true });
      ctl.classList?.add('opt');
      this.controls.push({ f, ctl });
      this.el.appendChild(ctl);
    }
    if (more.length) {
      const btn = el('button', { type: 'button', class: 'btn more-btn', title: 'More options', 'aria-haspopup': 'true' }, 'More ▾');
      btn.addEventListener('click', () => (this.popover ? this.closeMore() : this.openMore(btn, tool, more)));
      this.el.appendChild(btn);
    }
    this.applyVisibility();
  }

  /** Popover with the tool's less common options (schema fields marked `more`). */
  openMore(btn, tool, fields) {
    this.closeMore();
    const pop = el('div', { class: 'options-popover form' });
    for (const f of fields) pop.appendChild(fieldFor(f, tool.options[f.key], (v) => tool.setOption(f.key, v)));
    const r = btn.getBoundingClientRect();
    pop.style.left = `${Math.max(4, Math.min(window.innerWidth - 300, r.left))}px`;
    pop.style.top = `${r.bottom + 4}px`;
    document.body.appendChild(pop);
    btn.classList.add('active');
    const outside = (e) => { if (!pop.contains(e.target) && e.target !== btn) this.closeMore(); };
    const esc = (e) => { if (e.key === 'Escape') { e.stopPropagation(); this.closeMore(); } };
    document.addEventListener('pointerdown', outside, true);
    document.addEventListener('keydown', esc, true);
    this.popover = { pop, btn, cleanup: () => { document.removeEventListener('pointerdown', outside, true); document.removeEventListener('keydown', esc, true); } };
  }

  closeMore() {
    if (!this.popover) return;
    this.popover.cleanup();
    this.popover.pop.remove();
    this.popover.btn.classList.remove('active');
    this.popover = null;
  }
  applyVisibility() {
    const tool = this.app.tool;
    for (const { f, ctl } of this.controls) if (f.when) ctl.style.display = f.when(tool.options) ? '' : 'none';
  }
  refreshReadout() {
    if (this.readout) this.readout.textContent = this.app.tool?.[this.readoutKey] || '';
  }
}

export class DocTabs {
  constructor(app, container) {
    this.app = app;
    this.el = el('div', { class: 'doc-tabs' });
    container.appendChild(this.el);
    app.on('docs', () => this.render());
    app.on('doc', () => this.render());
    app.on('history', () => this.render());
  }
  render() {
    this.el.innerHTML = '';
    for (const doc of this.app.documents) {
      const tab = el('div', { class: `doc-tab${doc === this.app.doc ? ' active' : ''}`, title: `${doc.name} — ${doc.width}×${doc.height}` },
        el('span', { class: 'doc-tab-name' }, `${doc.name}${doc.isModified ? ' •' : ''}`),
        el('button', {
          type: 'button', class: 'btn icon-btn doc-tab-close', title: 'Close', html: icon('close', 12),
          onclick: (e) => { e.stopPropagation(); this.app.closeDocument(doc); },
        }));
      tab.addEventListener('pointerdown', (e) => { if (!e.target.closest('button')) this.app.setDocument(doc); });
      tab.addEventListener('auxclick', (e) => { if (e.button === 1) this.app.closeDocument(doc); });
      this.el.appendChild(tab);
    }
  }
}

export class StatusBar {
  constructor(app, container) {
    this.app = app;
    this.zoom = el('input', { type: 'text', class: 'zoom-input', title: 'Zoom (type a percentage)' });
    this.zoom.addEventListener('change', () => {
      const v = parseFloat(this.zoom.value);
      if (v > 0) app.renderer.setZoom(v / 100);
      this.zoom.blur();
    });
    this.zoom.addEventListener('keydown', (e) => e.stopPropagation());
    this.size = el('span', { class: 'status-item' });
    this.pos = el('span', { class: 'status-item' });
    this.sel = el('span', { class: 'status-item' });
    this.hint = el('span', { class: 'status-hint' });
    this.el = el('div', { class: 'statusbar' }, this.zoom, this.size, this.pos, this.sel, this.hint);
    container.appendChild(this.el);
    app.on('view', () => this.update());
    app.on('doc', () => this.update());
    app.on('docChange', () => this.update());
    app.on('tool', () => this.setHint(app.tool?.hint || ''));
    app.on('toolHint', () => this.setHint(app.tool?.hint || ''));
    app.on('pointerInfo', (i) => { this.pos.textContent = i ? `${i.x}, ${i.y} px` : ''; });
    this.update();
  }
  update() {
    const doc = this.app.doc;
    if (document.activeElement !== this.zoom) this.zoom.value = doc ? `${+(doc.view.zoom * 100).toFixed(1)}%` : '';
    this.size.textContent = doc ? `${doc.width} × ${doc.height} px` : '';
    const b = doc?.selection?.bounds;
    this.sel.textContent = b ? `Selection ${b.w} × ${b.h}` : '';
  }
  setHint(text) {
    this.hint.textContent = text;
  }
}