// 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; } }