ui.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /*
  2. The interface around the canvas: menu bar, ribbon (clipboard, image,
  3. options of the current tool, colours), tool rail, status bar and the
  4. keyboard shortcuts.
  5. */
  6. import { App } from './app.js';
  7. import { View } from './view.js';
  8. import { Sel, Clipboard } from './selection.js';
  9. import { Ops } from './imageops.js';
  10. import { FileIO } from './fileio.js';
  11. import { resizeDialog, canvasSizeDialog, isDialogOpen, dialog } from './dialogs.js';
  12. import { availableFonts, fontCss } from './texttool.js';
  13. import { SHAPES } from './shapes.js';
  14. import { icon } from './icons.js';
  15. import { T, $, el, clamp, shortcutLabel, toast } from './util.js';
  16. /* ------------------------------------------------------------ actions */
  17. const has = () => Sel.has();
  18. const ACTIONS = {
  19. new: { icon: 'newfile', label: () => T('new', 'New'), run: () => FileIO.newDialog() },
  20. open: { icon: 'open', label: () => T('open', 'Open...'), key: 'Ctrl+O', run: () => FileIO.openDialog() },
  21. save: { icon: 'save', label: () => T('save', 'Save'), key: 'Ctrl+S', run: () => FileIO.save() },
  22. saveas: { icon: 'saveas', label: () => T('save-as', 'Save as...'), key: 'Ctrl+Shift+S', run: () => FileIO.saveAs(), visible: () => App.backend },
  23. download: { icon: 'download', label: () => T('download', 'Download a copy'), run: () => FileIO.download() },
  24. close: { icon: 'close', label: () => T('close', 'Close'), run: () => FileIO.requestClose(), visible: () => FileIO.canClose() },
  25. undo: { icon: 'undo', label: () => T('undo', 'Undo'), key: 'Ctrl+Z', run: () => App.history.undo(), enabled: () => App.history.canUndo() },
  26. redo: { icon: 'redo', label: () => T('redo', 'Redo'), key: 'Ctrl+Y', run: () => App.history.redo(), enabled: () => App.history.canRedo() },
  27. cut: { icon: 'cut', label: () => T('cut', 'Cut'), key: 'Ctrl+X', run: () => Clipboard.copy(true), enabled: has },
  28. copy: { icon: 'copy', label: () => T('copy', 'Copy'), key: 'Ctrl+C', run: () => Clipboard.copy(false) },
  29. paste: { icon: 'paste', label: () => T('paste', 'Paste'), key: 'Ctrl+V', run: () => Clipboard.pasteButton() },
  30. pastefile: { icon: 'pastefile', label: () => T('paste-from', 'Paste from file...'), run: () => FileIO.pasteFromFile() },
  31. selectall: { icon: 'selectall', label: () => T('select-all', 'Select all'), key: 'Ctrl+A', run: () => Sel.selectAll() },
  32. deselect: { icon: 'deselect', label: () => T('deselect', 'Deselect'), key: 'Esc', run: () => App.finalize(), enabled: has },
  33. delete: { icon: 'trash', label: () => T('delete-sel', 'Delete selection'), key: 'Del', run: () => Sel.remove(), enabled: has },
  34. crop: { icon: 'crop', label: () => T('crop', 'Crop to selection'), key: 'Ctrl+Shift+X', run: () => Ops.crop(), enabled: has },
  35. resize: { icon: 'resize', label: () => T('resize-image-menu', 'Resize image...'), run: () => { App.finalize(); resizeDialog((w, h, s) => Ops.resize(w, h, s)); } },
  36. canvassize: { icon: 'canvassize', label: () => T('canvas-size-menu', 'Canvas size...'), run: () => { App.finalize(); canvasSizeDialog((w, h, a) => Ops.canvasSize(w, h, a)); } },
  37. rotcw: { icon: 'rotcw', label: () => T('rotate-right', 'Rotate right 90°'), run: () => Ops.rotate(90) },
  38. rotccw: { icon: 'rotccw', label: () => T('rotate-left', 'Rotate left 90°'), run: () => Ops.rotate(-90) },
  39. rot180: { icon: 'rot180', label: () => T('rotate-180', 'Rotate 180°'), run: () => Ops.rotate(180) },
  40. fliph: { icon: 'fliph', label: () => T('flip-h', 'Flip horizontal'), run: () => Ops.flip('h') },
  41. flipv: { icon: 'flipv', label: () => T('flip-v', 'Flip vertical'), run: () => Ops.flip('v') },
  42. invert: { icon: 'invert', label: () => T('invert', 'Invert colours'), run: () => Ops.invert() },
  43. grayscale: { icon: 'grayscale', label: () => T('grayscale', 'Black and white'), run: () => Ops.grayscale() },
  44. clear: { icon: 'trash', label: () => T('clear', 'Clear image'), run: () => Ops.clear() },
  45. zoomin: { icon: 'zoomin', label: () => T('zoom-in', 'Zoom in'), key: 'Ctrl+=', run: () => View.zoomStep(1) },
  46. zoomout: { icon: 'zoomout', label: () => T('zoom-out', 'Zoom out'), key: 'Ctrl+-', run: () => View.zoomStep(-1) },
  47. actual: { icon: 'actual', label: () => T('actual-size', 'Actual size (100%)'), run: () => View.setZoom(1) },
  48. fit: { icon: 'fit', label: () => T('fit', 'Fit to window'), key: 'Ctrl+0', run: () => View.fit() },
  49. grid: {
  50. icon: 'grid', label: () => T('pixel-grid', 'Pixel grid'), key: 'Ctrl+G',
  51. checked: () => App.state.grid,
  52. run: () => {
  53. App.state.grid = !App.state.grid;
  54. App.savePrefs();
  55. View.layout();
  56. if (App.state.grid && View.zoom < 4) {
  57. toast(T('grid-hint', 'The pixel grid shows at 400% zoom and more'));
  58. }
  59. }
  60. },
  61. theme: { icon: 'moon', label: () => T('dark-mode', 'Dark mode'), checked: () => document.body.classList.contains('dark'), run: () => toggleTheme() },
  62. shortcuts: { icon: 'keyboard', label: () => T('shortcuts', 'Keyboard shortcuts'), run: () => showShortcuts() }
  63. };
  64. function runAction(id) {
  65. const a = ACTIONS[id];
  66. if (!a) {
  67. return;
  68. }
  69. if (a.enabled && !a.enabled()) {
  70. return;
  71. }
  72. a.run();
  73. }
  74. const MENUS = {
  75. file: { label: () => T('menu-file', 'File'), items: ['new', 'open', 'save', 'saveas', 'download', '-', 'close'] },
  76. edit: { label: () => T('menu-edit', 'Edit'), items: ['undo', 'redo', '-', 'cut', 'copy', 'paste', 'pastefile', '-', 'selectall', 'deselect', 'delete'] },
  77. image: { label: () => T('menu-image', 'Image'), items: ['crop', 'resize', 'canvassize', '-', 'rotcw', 'rotccw', 'rot180', 'fliph', 'flipv', '-', 'invert', 'grayscale', '-', 'clear'] },
  78. view: { label: () => T('menu-view', 'View'), items: ['zoomin', 'zoomout', 'actual', 'fit', '-', 'grid', 'theme', '-', 'shortcuts'] }
  79. };
  80. /* ------------------------------------------------------------ popover menus */
  81. let openMenuEl = null;
  82. let openMenuAnchor = null;
  83. function closeMenu() {
  84. if (openMenuEl) {
  85. openMenuEl.remove();
  86. openMenuEl = null;
  87. }
  88. if (openMenuAnchor) {
  89. openMenuAnchor.classList.remove('open');
  90. openMenuAnchor.setAttribute('aria-expanded', 'false');
  91. openMenuAnchor = null;
  92. }
  93. }
  94. function placePopover(pop, anchor, alignRight) {
  95. document.body.appendChild(pop);
  96. const r = anchor.getBoundingClientRect();
  97. const pw = pop.offsetWidth, ph = pop.offsetHeight;
  98. let x = alignRight ? r.right - pw : r.left;
  99. let y = r.bottom + 4;
  100. if (y + ph > window.innerHeight - 4) {
  101. y = Math.max(4, r.top - ph - 4);
  102. }
  103. x = clamp(x, 4, Math.max(4, window.innerWidth - pw - 4));
  104. pop.style.left = x + 'px';
  105. pop.style.top = y + 'px';
  106. }
  107. // items: action ids, '-' separators, or { label, icon, run, checked }
  108. function openMenu(anchor, items, alignRight) {
  109. closeMenu();
  110. const m = el('div', { class: 'menu', role: 'menu' });
  111. let lastSep = true;
  112. items.forEach((it) => {
  113. if (it === '-') {
  114. if (!lastSep) {
  115. m.appendChild(el('div', { class: 'menu-sep', role: 'separator' }));
  116. lastSep = true;
  117. }
  118. return;
  119. }
  120. const a = typeof it === 'string' ? ACTIONS[it] : it;
  121. if (!a || (a.visible && !a.visible())) {
  122. return;
  123. }
  124. lastSep = false;
  125. const disabled = a.enabled ? !a.enabled() : false;
  126. const checked = a.checked ? a.checked() : null;
  127. const b = el('button', { type: 'button', class: 'menu-item', role: checked === null ? 'menuitem' : 'menuitemcheckbox', disabled: disabled, 'aria-checked': checked === null ? null : String(checked) });
  128. b.innerHTML = '<span class="mi-icon">' + (checked === true ? icon('check') : a.icon ? icon(a.icon) : '') + '</span>';
  129. b.appendChild(el('span', { class: 'mi-label' }, typeof a.label === 'function' ? a.label() : a.label));
  130. b.appendChild(el('span', { class: 'mi-key' }, shortcutLabel(a.key)));
  131. b.addEventListener('click', () => {
  132. closeMenu();
  133. a.run();
  134. });
  135. m.appendChild(b);
  136. });
  137. if (m.lastChild && m.lastChild.classList.contains('menu-sep')) {
  138. m.lastChild.remove();
  139. }
  140. placePopover(m, anchor, alignRight);
  141. openMenuEl = m;
  142. openMenuAnchor = anchor;
  143. anchor.classList.add('open');
  144. anchor.setAttribute('aria-expanded', 'true');
  145. const first = m.querySelector('.menu-item:not([disabled])');
  146. if (first) {
  147. first.focus({ preventScroll: true });
  148. }
  149. m.addEventListener('keydown', (e) => {
  150. const list = [...m.querySelectorAll('.menu-item:not([disabled])')];
  151. const i = list.indexOf(document.activeElement);
  152. if (e.key === 'ArrowDown') {
  153. e.preventDefault();
  154. list[(i + 1) % list.length].focus();
  155. } else if (e.key === 'ArrowUp') {
  156. e.preventDefault();
  157. list[(i - 1 + list.length) % list.length].focus();
  158. } else if (e.key === 'Escape') {
  159. e.preventDefault();
  160. e.stopPropagation();
  161. const a2 = openMenuAnchor;
  162. closeMenu();
  163. if (a2) {
  164. a2.focus();
  165. }
  166. } else if (e.key === 'ArrowRight' || e.key === 'ArrowLeft') {
  167. const bar = anchor.closest('#menus');
  168. if (bar) {
  169. const btns = [...bar.querySelectorAll('.menu-btn')];
  170. const j = btns.indexOf(anchor);
  171. const next = btns[(j + (e.key === 'ArrowRight' ? 1 : -1) + btns.length) % btns.length];
  172. next.click();
  173. }
  174. }
  175. });
  176. }
  177. // A popover holding arbitrary content (shape gallery, zoom presets)
  178. function openPopover(anchor, content, alignRight) {
  179. closeMenu();
  180. const p = el('div', { class: 'menu popover' }, content);
  181. placePopover(p, anchor, alignRight);
  182. openMenuEl = p;
  183. openMenuAnchor = anchor;
  184. anchor.classList.add('open');
  185. }
  186. document.addEventListener('pointerdown', (e) => {
  187. if (openMenuEl && !openMenuEl.contains(e.target) && !(openMenuAnchor && openMenuAnchor.contains(e.target))) {
  188. closeMenu();
  189. }
  190. }, true);
  191. window.addEventListener('blur', closeMenu);
  192. window.addEventListener('resize', closeMenu);
  193. /* ------------------------------------------------------------ small controls */
  194. function iconBtn(name, title, onClick, extra) {
  195. const attrs = Object.assign({ type: 'button', title: title, 'aria-label': title }, extra || {});
  196. attrs.class = 'ib' + (extra && extra.class ? ' ' + extra.class : '');
  197. const b = el('button', attrs);
  198. b.innerHTML = icon(name);
  199. if (onClick) {
  200. b.addEventListener('click', onClick);
  201. }
  202. return b;
  203. }
  204. function actionBtn(id) {
  205. const a = ACTIONS[id];
  206. const title = a.label() + (a.key ? ' (' + shortcutLabel(a.key) + ')' : '');
  207. const b = iconBtn(a.icon, title, () => runAction(id));
  208. b.dataset.action = id;
  209. return b;
  210. }
  211. function sep() {
  212. return el('div', { class: 'rb-sep' });
  213. }
  214. // Slider + number box for a size in px
  215. function sizeControl(key, min, max, label) {
  216. const s = App.state.sizes;
  217. const range = el('input', { type: 'range', min: String(min), max: String(max), value: String(s[key]), class: 'size-range', 'aria-label': label || T('size', 'Size') });
  218. const num = el('input', { type: 'number', min: String(min), max: String(max), value: String(s[key]), class: 'size-num', 'aria-label': label || T('size', 'Size') });
  219. const set = (v, from) => {
  220. v = clamp(Math.round(v) || min, min, max);
  221. s[key] = v;
  222. if (from !== range) {
  223. range.value = String(v);
  224. }
  225. if (from !== num) {
  226. num.value = String(v);
  227. }
  228. App.savePrefs();
  229. App.emit('options');
  230. View.queueOverlay();
  231. };
  232. range.addEventListener('input', () => set(parseFloat(range.value), range));
  233. num.addEventListener('change', () => set(parseFloat(num.value), num));
  234. const wrap = el('div', { class: 'opt size-opt', 'data-size': key }, el('span', { class: 'opt-label' }, label || T('size', 'Size')), range, el('span', { class: 'num-wrap' }, num, el('span', { class: 'unit' }, 'px')));
  235. wrap.refresh = () => {
  236. range.value = String(s[key]);
  237. num.value = String(s[key]);
  238. };
  239. return wrap;
  240. }
  241. function checkOpt(label, title, get, set) {
  242. const input = el('input', { type: 'checkbox' });
  243. input.checked = get();
  244. input.addEventListener('change', () => {
  245. set(input.checked);
  246. App.savePrefs();
  247. App.emit('options');
  248. });
  249. return el('label', { class: 'opt check-opt', title: title || label }, input, el('span', null, label));
  250. }
  251. function toggleBtn(name, title, get, set, text) {
  252. const b = el('button', { type: 'button', class: 'ib toggle' + (text ? ' text' : ''), title: title, 'aria-label': title, 'aria-pressed': String(!!get()) });
  253. if (text) {
  254. b.innerHTML = text;
  255. } else {
  256. b.innerHTML = icon(name);
  257. }
  258. b.classList.toggle('on', !!get());
  259. // Keep the focus in the text box while it is open
  260. b.addEventListener('mousedown', (e) => e.preventDefault());
  261. b.addEventListener('click', () => {
  262. set(!get());
  263. b.classList.toggle('on', !!get());
  264. b.setAttribute('aria-pressed', String(!!get()));
  265. App.savePrefs();
  266. App.emit('options');
  267. });
  268. return b;
  269. }
  270. function segIcons(options, get, set, label) {
  271. const wrap = el('div', { class: 'seg icons', role: 'radiogroup', 'aria-label': label || '' });
  272. const refresh = () => {
  273. wrap.querySelectorAll('button').forEach((b) => {
  274. const on = b.dataset.value === get();
  275. b.classList.toggle('on', on);
  276. b.setAttribute('aria-checked', String(on));
  277. });
  278. };
  279. options.forEach((o) => {
  280. const b = el('button', { type: 'button', role: 'radio', class: 'ib', 'data-value': o.value, title: o.title, 'aria-label': o.title });
  281. b.innerHTML = icon(o.icon);
  282. b.addEventListener('mousedown', (e) => e.preventDefault());
  283. b.addEventListener('click', () => {
  284. set(o.value);
  285. refresh();
  286. App.savePrefs();
  287. App.emit('options');
  288. });
  289. wrap.appendChild(b);
  290. });
  291. refresh();
  292. return wrap;
  293. }
  294. /* ------------------------------------------------------------ tools */
  295. const TOOLS = [
  296. { id: 'select', icon: 'select', key: 'S', label: () => T('tool-select', 'Select') },
  297. { id: 'lasso', icon: 'lasso', key: 'L', label: () => T('tool-lasso', 'Free-form select') },
  298. { id: 'pencil', icon: 'pencil', key: 'P', label: () => T('tool-pencil', 'Pencil') },
  299. { id: 'brush', icon: 'brush', key: 'B', label: () => T('tool-brush', 'Brush') },
  300. { id: 'eraser', icon: 'eraser', key: 'E', label: () => T('tool-eraser', 'Eraser') },
  301. { id: 'fill', icon: 'fill', key: 'F', label: () => T('tool-fill', 'Fill') },
  302. { id: 'text', icon: 'text', key: 'T', label: () => T('tool-text', 'Text') },
  303. { id: 'picker', icon: 'picker', key: 'I', label: () => T('tool-picker', 'Colour picker') },
  304. { id: 'shapes', icon: 'shapes', key: 'U', label: () => T('tool-shapes', 'Shapes') }
  305. ];
  306. function buildToolRail() {
  307. const rail = $('toolrail');
  308. rail.innerHTML = '';
  309. TOOLS.forEach((t, i) => {
  310. if (i === 2 || i === 6 || i === 8) {
  311. rail.appendChild(el('div', { class: 'rail-sep' }));
  312. }
  313. const title = t.label() + ' (' + t.key + ')';
  314. const b = iconBtn(t.icon, title, () => App.setTool(t.id), { 'data-tool': t.id, role: 'radio' });
  315. rail.appendChild(b);
  316. });
  317. const sync = () => {
  318. rail.querySelectorAll('[data-tool]').forEach((b) => {
  319. const on = b.dataset.tool === App.state.tool;
  320. b.classList.toggle('on', on);
  321. b.setAttribute('aria-checked', String(on));
  322. });
  323. };
  324. App.on('tool', sync);
  325. sync();
  326. }
  327. /* ------------------------------------------------------------ tool options */
  328. const SHAPE_LABELS = {
  329. line: ['shape-line', 'Line'], arrow: ['shape-arrow', 'Arrow'], rect: ['shape-rect', 'Rectangle'],
  330. roundrect: ['shape-roundrect', 'Rounded rectangle'], ellipse: ['shape-ellipse', 'Ellipse'],
  331. triangle: ['shape-triangle', 'Triangle'], righttriangle: ['shape-righttriangle', 'Right triangle'],
  332. diamond: ['shape-diamond', 'Diamond'], pentagon: ['shape-pentagon', 'Pentagon'], hexagon: ['shape-hexagon', 'Hexagon'],
  333. star: ['shape-star', 'Star'], heart: ['shape-heart', 'Heart'], arrowright: ['shape-arrowright', 'Right arrow'],
  334. arrowleft: ['shape-arrowleft', 'Left arrow'], arrowup: ['shape-arrowup', 'Up arrow'], arrowdown: ['shape-arrowdown', 'Down arrow'],
  335. callout: ['shape-callout', 'Speech bubble']
  336. };
  337. function shapeLabel(id) {
  338. const l = SHAPE_LABELS[id] || [id, id];
  339. return T(l[0], l[1]);
  340. }
  341. function transparentOpt() {
  342. return checkOpt(T('transparent-bg', 'Transparent background'),
  343. T('transparent-bg-tip', 'Erasing and moving a selection leave transparent pixels instead of the secondary colour'),
  344. () => App.state.transparentBg, (v) => { App.state.transparentBg = v; });
  345. }
  346. function hint(text) {
  347. return el('div', { class: 'opt hint' }, text);
  348. }
  349. const OPTIONS = {
  350. select() {
  351. return [actionBtn('selectall'), actionBtn('crop'), actionBtn('delete'), sep(),
  352. actionBtn('rotccw'), actionBtn('rotcw'), actionBtn('fliph'), actionBtn('flipv'), sep(), transparentOpt()];
  353. },
  354. lasso() {
  355. return OPTIONS.select();
  356. },
  357. pencil() {
  358. return [sizeControl('pencil', 1, 64)];
  359. },
  360. brush() {
  361. return [segIcons([
  362. { value: 'round', icon: 'roundbrush', title: T('brush-round', 'Round brush') },
  363. { value: 'calligraphy', icon: 'calligraphy', title: T('brush-calligraphy', 'Calligraphy pen') },
  364. { value: 'highlighter', icon: 'highlighter', title: T('brush-highlighter', 'Highlighter') },
  365. { value: 'spray', icon: 'spray', title: T('brush-spray', 'Airbrush') }
  366. ], () => App.state.brushType, (v) => { App.state.brushType = v; }, T('brush-type', 'Brush')), sep(), sizeControl('brush', 1, 200)];
  367. },
  368. eraser() {
  369. return [sizeControl('eraser', 1, 200), sep(), transparentOpt()];
  370. },
  371. fill() {
  372. const s = App.state;
  373. const range = el('input', { type: 'range', min: '0', max: '100', value: String(s.tolerance), class: 'size-range', 'aria-label': T('tolerance', 'Tolerance') });
  374. const val = el('span', { class: 'opt-value' }, s.tolerance + '%');
  375. range.addEventListener('input', () => {
  376. s.tolerance = parseInt(range.value, 10);
  377. val.textContent = s.tolerance + '%';
  378. App.savePrefs();
  379. });
  380. return [el('div', { class: 'opt', title: T('tolerance-tip', 'How different a colour may be and still be filled') }, el('span', { class: 'opt-label' }, T('tolerance', 'Tolerance')), range, val)];
  381. },
  382. text() {
  383. const s = App.state;
  384. const fonts = availableFonts();
  385. if (fonts.indexOf(s.font) < 0) {
  386. s.font = fonts.indexOf('Arial') >= 0 ? 'Arial' : fonts[0];
  387. }
  388. const font = el('select', { class: 'font-select', 'aria-label': T('font', 'Font'), title: T('font', 'Font') });
  389. fonts.forEach((f) => {
  390. font.appendChild(el('option', { value: f, style: { fontFamily: fontCss(f) } }, f));
  391. });
  392. font.value = s.font;
  393. font.style.fontFamily = fontCss(s.font);
  394. font.addEventListener('change', () => {
  395. s.font = font.value;
  396. font.style.fontFamily = fontCss(s.font);
  397. App.savePrefs();
  398. App.emit('options');
  399. App.tools.text.refocus();
  400. });
  401. const sizes = el('datalist', { id: 'font-sizes' });
  402. [8, 10, 12, 14, 16, 18, 20, 24, 28, 32, 36, 48, 64, 72, 96, 128].forEach((n) => sizes.appendChild(el('option', { value: String(n) })));
  403. const size = el('input', { type: 'number', min: '4', max: '999', list: 'font-sizes', value: String(s.fontSize), class: 'size-num', 'aria-label': T('font-size', 'Font size'), title: T('font-size', 'Font size') });
  404. const applySize = () => {
  405. s.fontSize = clamp(Math.round(parseFloat(size.value)) || 28, 4, 999);
  406. App.savePrefs();
  407. App.emit('options');
  408. };
  409. size.addEventListener('input', applySize);
  410. size.addEventListener('change', () => {
  411. applySize();
  412. size.value = String(s.fontSize);
  413. App.tools.text.refocus();
  414. });
  415. return [
  416. el('div', { class: 'opt' }, font),
  417. el('div', { class: 'opt' }, el('span', { class: 'num-wrap' }, size, el('span', { class: 'unit' }, 'px')), sizes),
  418. sep(),
  419. toggleBtn(null, T('bold', 'Bold'), () => s.bold, (v) => { s.bold = v; }, '<b>B</b>'),
  420. toggleBtn(null, T('italic', 'Italic'), () => s.italic, (v) => { s.italic = v; }, '<i>I</i>'),
  421. toggleBtn(null, T('underline', 'Underline'), () => s.underline, (v) => { s.underline = v; }, '<u>U</u>'),
  422. sep(),
  423. segIcons([
  424. { value: 'left', icon: 'alignleft', title: T('align-left', 'Align left') },
  425. { value: 'center', icon: 'aligncenter', title: T('align-center', 'Centre') },
  426. { value: 'right', icon: 'alignright', title: T('align-right', 'Align right') }
  427. ], () => s.align, (v) => { s.align = v; }, T('align', 'Alignment')),
  428. sep(),
  429. checkOpt(T('text-bg', 'Background'), T('text-bg-tip', 'Fill the text box with the secondary colour'), () => s.textBg, (v) => { s.textBg = v; })
  430. ];
  431. },
  432. picker() {
  433. return [hint(T('picker-hint', 'Click to pick the primary colour, right-click for the secondary. Tip: hold Alt with any painting tool.'))];
  434. },
  435. shapes() {
  436. const s = App.state;
  437. const gallery = el('button', { type: 'button', class: 'ib wide shape-btn', title: T('shape', 'Shape'), 'aria-haspopup': 'true' });
  438. const showShape = () => {
  439. gallery.innerHTML = icon(s.shape) + icon('chevron', 'chev');
  440. gallery.title = T('shape', 'Shape') + ': ' + shapeLabel(s.shape);
  441. };
  442. showShape();
  443. gallery.addEventListener('click', () => {
  444. if (openMenuAnchor === gallery) {
  445. closeMenu();
  446. return;
  447. }
  448. const grid = el('div', { class: 'shape-grid' });
  449. SHAPES.forEach((sh) => {
  450. const b = el('button', { type: 'button', class: 'ib' + (sh.id === s.shape ? ' on' : ''), title: shapeLabel(sh.id), 'aria-label': shapeLabel(sh.id) });
  451. b.innerHTML = icon(sh.id);
  452. b.addEventListener('click', () => {
  453. s.shape = sh.id;
  454. App.savePrefs();
  455. showShape();
  456. closeMenu();
  457. App.tools.shapes.setShape(sh.id);
  458. });
  459. grid.appendChild(b);
  460. });
  461. openPopover(gallery, grid);
  462. });
  463. const dash = el('select', { 'aria-label': T('line-style', 'Line style'), title: T('line-style', 'Line style') });
  464. [['solid', T('line-solid', 'Solid')], ['dash', T('line-dash', 'Dashed')], ['dot', T('line-dot', 'Dotted')]].forEach((o) => dash.appendChild(el('option', { value: o[0] }, o[1])));
  465. dash.value = s.dash;
  466. dash.addEventListener('change', () => {
  467. s.dash = dash.value;
  468. App.savePrefs();
  469. App.emit('options');
  470. });
  471. return [
  472. el('div', { class: 'opt' }, gallery),
  473. sep(),
  474. toggleBtn('outline', T('outline-tip', 'Outline (primary colour)'), () => s.outline, (v) => { s.outline = v; if (!v && !s.filled) { s.filled = true; refreshOptions(); } }),
  475. toggleBtn('filled', T('fill-tip', 'Fill (secondary colour)'), () => s.filled, (v) => { s.filled = v; if (!v && !s.outline) { s.outline = true; refreshOptions(); } }),
  476. el('div', { class: 'opt' }, dash),
  477. sep(),
  478. sizeControl('shape', 1, 64, T('width-label', 'Width'))
  479. ];
  480. }
  481. };
  482. function refreshOptions() {
  483. const box = $('tool-options');
  484. box.innerHTML = '';
  485. const t = TOOLS.find((x) => x.id === App.state.tool);
  486. if (t) {
  487. box.appendChild(el('div', { class: 'opt-title' }, t.label()));
  488. }
  489. const build = OPTIONS[App.state.tool];
  490. if (build) {
  491. build().forEach((c) => box.appendChild(c));
  492. }
  493. box.scrollLeft = 0;
  494. markOverflow();
  495. syncActionButtons();
  496. }
  497. // Fade the right edge of the tool options while more of them are hidden
  498. function markOverflow() {
  499. const box = $('tool-options');
  500. box.classList.toggle('more', box.scrollWidth - box.scrollLeft > box.clientWidth + 2);
  501. }
  502. function bindOptionsScroll() {
  503. const box = $('tool-options');
  504. box.addEventListener('wheel', (e) => {
  505. if (box.scrollWidth > box.clientWidth && Math.abs(e.deltaY) > Math.abs(e.deltaX) && !e.ctrlKey) {
  506. box.scrollLeft += e.deltaY;
  507. e.preventDefault();
  508. }
  509. }, { passive: false });
  510. box.addEventListener('scroll', markOverflow);
  511. window.addEventListener('resize', markOverflow);
  512. }
  513. /* ------------------------------------------------------------ colours */
  514. const PALETTE = [
  515. '#000000', '#4b5563', '#dc2626', '#f97316', '#facc15', '#16a34a', '#06b6d4', '#2563eb', '#7c3aed', '#db2777',
  516. '#ffffff', '#9ca3af', '#92400e', '#fdba74', '#fef08a', '#86efac', '#a5f3fc', '#93c5fd', '#c4b5fd', '#f9a8d4'
  517. ];
  518. function addCustomColor(hex) {
  519. const list = App.state.customColors.filter((c) => c !== hex && PALETTE.indexOf(c) < 0);
  520. if (PALETTE.indexOf(hex) < 0) {
  521. list.unshift(hex);
  522. }
  523. App.state.customColors = list.slice(0, 10);
  524. App.savePrefs();
  525. renderCustomColors();
  526. }
  527. let colorInput = null;
  528. let colorTarget = 'primary';
  529. function editColor(which, anchor) {
  530. colorTarget = which;
  531. colorInput.value = App.state[which];
  532. const r = anchor.getBoundingClientRect();
  533. const host = colorInput.parentElement.getBoundingClientRect();
  534. colorInput.style.left = r.left - host.left + 'px';
  535. colorInput.style.top = r.bottom - host.top + 'px';
  536. try {
  537. if (colorInput.showPicker) {
  538. colorInput.showPicker();
  539. } else {
  540. colorInput.click();
  541. }
  542. } catch (e) {
  543. colorInput.click();
  544. }
  545. }
  546. function swatch(hex) {
  547. const b = el('button', { type: 'button', class: 'swatch', title: hex, 'aria-label': hex, style: { background: hex } });
  548. b.addEventListener('click', () => App.setColor('primary', hex));
  549. b.addEventListener('contextmenu', (e) => {
  550. e.preventDefault();
  551. App.setColor('secondary', hex);
  552. });
  553. return b;
  554. }
  555. function renderCustomColors() {
  556. const row = $('custom-colors');
  557. if (!row) {
  558. return;
  559. }
  560. row.innerHTML = '';
  561. const list = App.state.customColors;
  562. for (let i = 0; i < 10; i++) {
  563. if (list[i]) {
  564. row.appendChild(swatch(list[i]));
  565. } else {
  566. row.appendChild(el('span', { class: 'swatch empty', 'aria-hidden': 'true' }));
  567. }
  568. }
  569. }
  570. function buildColors() {
  571. const box = $('colors');
  572. box.innerHTML = '';
  573. const primary = el('button', { type: 'button', class: 'well primary', title: T('primary-tip', 'Primary colour: left mouse button. Click to change.') });
  574. const secondary = el('button', { type: 'button', class: 'well secondary', title: T('secondary-tip', 'Secondary colour: right mouse button, eraser and shape fill. Click to change.') });
  575. const swapB = iconBtn('swap', T('swap-colors', 'Swap colours (X)'), () => App.swapColors(), { class: 'mini swap' });
  576. const resetB = iconBtn('reset', T('reset-colors', 'Default colours (D)'), () => resetColors(), { class: 'mini reset' });
  577. colorInput = el('input', { type: 'color', class: 'color-input', tabindex: '-1', 'aria-hidden': 'true' });
  578. colorInput.addEventListener('input', () => App.setColor(colorTarget, colorInput.value));
  579. colorInput.addEventListener('change', () => {
  580. App.setColor(colorTarget, colorInput.value);
  581. addCustomColor(colorInput.value);
  582. });
  583. primary.addEventListener('click', () => editColor('primary', primary));
  584. secondary.addEventListener('click', () => editColor('secondary', secondary));
  585. const wells = el('div', { class: 'wells' }, secondary, primary, swapB, resetB, colorInput);
  586. const pal = el('div', { class: 'palette' });
  587. PALETTE.forEach((c) => pal.appendChild(swatch(c)));
  588. const custom = el('div', { class: 'palette custom', id: 'custom-colors', title: T('custom-colors', 'Your colours') });
  589. const edit = iconBtn('palette', T('edit-colors', 'More colours...'), () => editColor('primary', edit));
  590. box.appendChild(wells);
  591. box.appendChild(el('div', { class: 'palettes' }, pal, custom));
  592. box.appendChild(edit);
  593. renderCustomColors();
  594. const sync = () => {
  595. primary.style.setProperty('--c', App.state.primary);
  596. secondary.style.setProperty('--c', App.state.secondary);
  597. };
  598. App.on('colors', sync);
  599. sync();
  600. }
  601. function resetColors() {
  602. App.setColor('primary', '#000000');
  603. App.setColor('secondary', '#ffffff');
  604. }
  605. /* ------------------------------------------------------------ menu bar and ribbon */
  606. function buildMenuBar() {
  607. const menus = $('menus');
  608. menus.innerHTML = '';
  609. Object.keys(MENUS).forEach((id) => {
  610. const m = MENUS[id];
  611. const b = el('button', { type: 'button', class: 'menu-btn', 'aria-haspopup': 'true', 'aria-expanded': 'false' }, m.label());
  612. b.addEventListener('click', () => {
  613. if (openMenuAnchor === b) {
  614. closeMenu();
  615. return;
  616. }
  617. openMenu(b, m.items);
  618. });
  619. b.addEventListener('pointerenter', () => {
  620. if (openMenuEl && openMenuAnchor && openMenuAnchor !== b && openMenuAnchor.classList.contains('menu-btn')) {
  621. openMenu(b, m.items);
  622. }
  623. });
  624. menus.appendChild(b);
  625. });
  626. const quick = $('quick');
  627. quick.innerHTML = '';
  628. quick.appendChild(actionBtn('save'));
  629. quick.appendChild(actionBtn('undo'));
  630. quick.appendChild(actionBtn('redo'));
  631. }
  632. function buildRibbon() {
  633. const clip = $('g-clipboard');
  634. clip.innerHTML = '';
  635. const paste = el('button', { type: 'button', class: 'big-btn', title: T('paste', 'Paste') + ' (' + shortcutLabel('Ctrl+V') + ')' });
  636. paste.innerHTML = icon('paste') + '<span>' + T('paste', 'Paste') + '</span>';
  637. paste.addEventListener('click', () => runAction('paste'));
  638. clip.appendChild(paste);
  639. clip.appendChild(el('div', { class: 'stack' }, actionBtn('cut'), actionBtn('copy')));
  640. const img = $('g-image');
  641. img.innerHTML = '';
  642. const rotate = iconBtn('rotcw', T('rotate-flip', 'Rotate and flip'), null, { 'aria-haspopup': 'true' });
  643. rotate.addEventListener('click', () => {
  644. if (openMenuAnchor === rotate) {
  645. closeMenu();
  646. return;
  647. }
  648. openMenu(rotate, ['rotcw', 'rotccw', 'rot180', '-', 'fliph', 'flipv']);
  649. });
  650. img.appendChild(el('div', { class: 'stack' }, actionBtn('crop'), actionBtn('resize')));
  651. img.appendChild(el('div', { class: 'stack' }, rotate, actionBtn('canvassize')));
  652. }
  653. function syncActionButtons() {
  654. document.querySelectorAll('[data-action]').forEach((b) => {
  655. const a = ACTIONS[b.dataset.action];
  656. if (a && a.enabled) {
  657. b.disabled = !a.enabled();
  658. }
  659. });
  660. }
  661. /* ------------------------------------------------------------ status bar */
  662. function selectionSize() {
  663. if (Sel.draft && !Array.isArray(Sel.draft)) {
  664. return Sel.draft;
  665. }
  666. if (Sel.sel) {
  667. return Sel.sel;
  668. }
  669. const sh = App.tools.shapes && App.tools.shapes.sh;
  670. if (sh) {
  671. return { w: Math.abs(sh.x1 - sh.x0), h: Math.abs(sh.y1 - sh.y0) };
  672. }
  673. return null;
  674. }
  675. function buildStatus() {
  676. const pos = $('st-pos');
  677. const selEl = $('st-sel');
  678. const size = $('st-size');
  679. const pick = $('st-pick');
  680. const zoomLabel = $('zoom-label');
  681. const slider = $('zoom-slider');
  682. $('st-pos-icon').innerHTML = icon('pointer');
  683. document.querySelector('#statusbar .sel-icon').innerHTML = icon('select');
  684. $('st-size-icon').innerHTML = icon('image');
  685. $('zoom-out').innerHTML = icon('minus');
  686. $('zoom-in').innerHTML = icon('plus');
  687. $('zoom-out').title = ACTIONS.zoomout.label() + ' (' + shortcutLabel('Ctrl+-') + ')';
  688. $('zoom-in').title = ACTIONS.zoomin.label() + ' (' + shortcutLabel('Ctrl+=') + ')';
  689. $('zoom-out').addEventListener('click', () => View.zoomStep(-1));
  690. $('zoom-in').addEventListener('click', () => View.zoomStep(1));
  691. slider.addEventListener('input', () => View.setZoom(Math.pow(2, parseFloat(slider.value) / 100)));
  692. zoomLabel.addEventListener('click', () => {
  693. if (openMenuAnchor === zoomLabel) {
  694. closeMenu();
  695. return;
  696. }
  697. const items = [3200, 1600, 800, 400, 200, 100, 50, 25, 12.5].map((p) => ({
  698. label: p + '%', run: () => View.setZoom(p / 100), checked: () => Math.abs(View.zoom * 100 - p) < 0.5
  699. }));
  700. openMenu(zoomLabel, items.concat(['-', 'fit']), true);
  701. });
  702. App.on('pointer', (p) => {
  703. const x = p ? Math.floor(p.x) : -1, y = p ? Math.floor(p.y) : -1;
  704. const inside = x >= 0 && y >= 0 && x < App.W && y < App.H;
  705. pos.textContent = inside ? x + ', ' + y + ' px' : '';
  706. pos.parentElement.style.display = inside ? '' : 'none';
  707. });
  708. const showSel = () => {
  709. const r = selectionSize();
  710. selEl.textContent = r ? Math.round(r.w) + ' × ' + Math.round(r.h) + ' px' : '';
  711. selEl.parentElement.style.display = r ? '' : 'none';
  712. syncActionButtons();
  713. };
  714. App.on('selection', showSel);
  715. showSel();
  716. const showSize = (g) => {
  717. size.textContent = (g ? g.w : App.W) + ' × ' + (g ? g.h : App.H) + ' px';
  718. };
  719. App.on('docsize', () => showSize());
  720. App.on('gripsize', (g) => showSize(g));
  721. App.on('pickhover', (hex) => {
  722. pick.style.display = hex ? '' : 'none';
  723. if (hex) {
  724. pick.querySelector('.chip').style.background = hex;
  725. pick.querySelector('.hex').textContent = hex.toUpperCase();
  726. }
  727. });
  728. App.on('zoom', () => {
  729. const pct = View.zoom * 100;
  730. zoomLabel.textContent = (pct >= 10 ? Math.round(pct) : Math.round(pct * 10) / 10) + '%';
  731. slider.value = String(Math.round(Math.log2(View.zoom) * 100));
  732. });
  733. showSize();
  734. }
  735. /* ------------------------------------------------------------ title, busy */
  736. function buildTitle() {
  737. const t = $('doc-title');
  738. const update = () => {
  739. t.textContent = '';
  740. t.appendChild(el('span', { class: 'doc-name' }, App.file.name));
  741. if (App.dirty) {
  742. t.appendChild(el('span', { class: 'dirty-dot', title: T('unsaved', 'Unsaved changes') }));
  743. }
  744. FileIO.updateTitle();
  745. };
  746. App.on('file', update);
  747. App.on('dirty', update);
  748. update();
  749. App.on('busy', (b) => document.body.classList.toggle('busy', !!b));
  750. }
  751. /* ------------------------------------------------------------ theme */
  752. let systemDark = false;
  753. function applyTheme(dark) {
  754. systemDark = dark;
  755. document.body.classList.toggle('dark', dark);
  756. try {
  757. ao_module_setWindowTheme(dark ? 'dark' : 'white');
  758. } catch (e) {
  759. //not in a float window
  760. }
  761. const b = $('theme-btn');
  762. if (b) {
  763. b.innerHTML = icon(dark ? 'sun' : 'moon');
  764. b.title = dark ? T('light-mode', 'Light mode') : T('dark-mode', 'Dark mode');
  765. }
  766. }
  767. export function initTheme() {
  768. if (App.backend && typeof ao_module_getSystemThemeColor === 'function') {
  769. applyTheme(false);
  770. try {
  771. ao_module_getSystemThemeColor((pref) => applyTheme(pref === 'darkTheme' || pref === 'dark'));
  772. } catch (e) {
  773. //no preference service
  774. }
  775. } else {
  776. let saved = null;
  777. try {
  778. saved = localStorage.getItem('arozos.paint.theme');
  779. } catch (e) {
  780. saved = null;
  781. }
  782. applyTheme(saved ? saved === 'dark' : !!(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches));
  783. }
  784. try {
  785. ao_module_onThemeChanged((theme) => applyTheme(theme === 'dark' || theme === 'darkTheme'));
  786. } catch (e) {
  787. //older ao_module
  788. }
  789. }
  790. function toggleTheme() {
  791. if (App.backend && typeof ao_module_toggleSystemTheme === 'function') {
  792. ao_module_toggleSystemTheme();
  793. return;
  794. }
  795. const dark = !systemDark;
  796. try {
  797. localStorage.setItem('arozos.paint.theme', dark ? 'dark' : 'light');
  798. } catch (e) {
  799. //no storage
  800. }
  801. applyTheme(dark);
  802. }
  803. /* ------------------------------------------------------------ shortcuts */
  804. // Only shortcuts the browser lets a page take over
  805. const KEYMAP = {
  806. 'Ctrl+o': 'open', 'Ctrl+s': 'save', 'Ctrl+Shift+s': 'saveas',
  807. 'Ctrl+z': 'undo', 'Ctrl+y': 'redo', 'Ctrl+Shift+z': 'redo',
  808. 'Ctrl+x': 'cut', 'Ctrl+c': 'copy', 'Ctrl+a': 'selectall', 'Ctrl+Shift+x': 'crop',
  809. 'Ctrl+=': 'zoomin', 'Ctrl+Shift++': 'zoomin', 'Ctrl+Shift+=': 'zoomin', 'Ctrl++': 'zoomin',
  810. 'Ctrl+-': 'zoomout', 'Ctrl+Shift+_': 'zoomout', 'Ctrl+0': 'fit', 'Ctrl+g': 'grid'
  811. };
  812. const SIZE_KEY = { pencil: 'pencil', brush: 'brush', eraser: 'eraser', shapes: 'shape' };
  813. function changeSize(dir) {
  814. const key = SIZE_KEY[App.state.tool];
  815. if (!key) {
  816. return;
  817. }
  818. const s = App.state.sizes;
  819. const v = s[key];
  820. const step = v < 10 ? 1 : v < 40 ? 2 : 5;
  821. s[key] = clamp(v + dir * step, 1, key === 'pencil' || key === 'shape' ? 64 : 200);
  822. App.savePrefs();
  823. App.emit('options');
  824. const ctl = document.querySelector('[data-size="' + key + '"]');
  825. if (ctl && ctl.refresh) {
  826. ctl.refresh();
  827. }
  828. View.queueOverlay();
  829. }
  830. function onKey(e) {
  831. if (isDialogOpen()) {
  832. return;
  833. }
  834. const mod = e.ctrlKey || e.metaKey;
  835. const typing = View.isTyping(e);
  836. const combo = (mod ? 'Ctrl+' : '') + (e.shiftKey && mod ? 'Shift+' : '') + (e.key.length === 1 ? e.key.toLowerCase() : e.key);
  837. if (typing) {
  838. // Saving and opening work while typing; everything else is the text box's
  839. if (mod && (combo === 'Ctrl+s' || combo === 'Ctrl+Shift+s' || combo === 'Ctrl+o')) {
  840. e.preventDefault();
  841. runAction(KEYMAP[combo]);
  842. }
  843. return;
  844. }
  845. if (openMenuEl) {
  846. return;
  847. }
  848. if (App.tool && App.tool.key && !mod && App.tool.key(e)) {
  849. e.preventDefault();
  850. return;
  851. }
  852. if (mod) {
  853. const id = KEYMAP[combo];
  854. if (id) {
  855. e.preventDefault();
  856. runAction(id);
  857. }
  858. return;
  859. }
  860. if (e.altKey) {
  861. return;
  862. }
  863. const k = e.key.toLowerCase();
  864. const tool = TOOLS.find((t) => t.key.toLowerCase() === k);
  865. if (tool) {
  866. e.preventDefault();
  867. App.setTool(tool.id);
  868. return;
  869. }
  870. if (k === 'x') {
  871. App.swapColors();
  872. } else if (k === 'd') {
  873. resetColors();
  874. } else if (k === '[') {
  875. changeSize(-1);
  876. } else if (k === ']') {
  877. changeSize(1);
  878. } else if (e.key === 'Escape') {
  879. App.finalize();
  880. } else if (e.key === 'Delete' && Sel.has()) {
  881. Sel.remove();
  882. } else {
  883. return;
  884. }
  885. e.preventDefault();
  886. }
  887. function showShortcuts() {
  888. const rows = [
  889. [T('tools', 'Tools'), 'S, L, P, B, E, F, T, I, U'],
  890. [T('swap-colors-short', 'Swap colours'), 'X'],
  891. [T('reset-colors-short', 'Default colours'), 'D'],
  892. [T('size-smaller-bigger', 'Smaller / bigger brush'), '[ ]'],
  893. [T('pick-color', 'Pick a colour while painting'), 'Alt + ' + T('click', 'click')],
  894. [T('pan', 'Move around'), T('space-drag', 'Space + drag, or middle mouse button')],
  895. [T('zoom', 'Zoom'), shortcutLabel('Ctrl+') + T('wheel', 'mouse wheel')],
  896. [T('constrain', 'Square, circle, 45° line'), 'Shift + ' + T('drag', 'drag')],
  897. [T('duplicate', 'Move a copy of the selection'), shortcutLabel('Ctrl+') + T('drag', 'drag')],
  898. [T('nudge', 'Nudge selection or shape'), T('arrow-keys', 'Arrow keys')],
  899. [T('finish', 'Finish shape or text'), 'Enter / Esc'],
  900. [T('paste', 'Paste'), shortcutLabel('Ctrl+V')]
  901. ];
  902. const table = el('table', { class: 'keys' });
  903. rows.forEach((r) => table.appendChild(el('tr', null, el('td', null, r[0]), el('td', null, el('kbd', null, r[1])))));
  904. dialog({ title: T('shortcuts', 'Keyboard shortcuts'), body: table, buttons: [{ label: T('ok', 'OK'), primary: true }] });
  905. }
  906. /* ------------------------------------------------------------ init */
  907. export function buildUI() {
  908. buildMenuBar();
  909. buildRibbon();
  910. buildToolRail();
  911. buildColors();
  912. buildStatus();
  913. buildTitle();
  914. bindOptionsScroll();
  915. refreshOptions();
  916. App.on('tool', () => {
  917. closeMenu();
  918. refreshOptions();
  919. });
  920. App.on('history', syncActionButtons);
  921. document.querySelectorAll('.grip').forEach((g) => {
  922. g.title = T('grip-tip', 'Drag to change the canvas size');
  923. });
  924. window.addEventListener('keydown', onKey);
  925. document.addEventListener('paste', (e) => Clipboard.onPaste(e));
  926. window.ao_module_close = () => FileIO.requestClose();
  927. window.addEventListener('beforeunload', (e) => {
  928. if (App.dirty && !FileIO.canClose()) {
  929. e.preventDefault();
  930. e.returnValue = '';
  931. }
  932. });
  933. syncActionButtons();
  934. }