Skip to content

All your computers and your phone/iPhone, working as one desk. Screens, apps, files, clipboard, calls, texts, notifications, mouse and keyboard, shared between Mac, Windows, Linux , Android, Web and ios / ipados . Free and open source. No account, no cloud, and no shared network needed.

30.3 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// Photos page (Mac, PC and Linux): the phone's photos and videos, newest first, a page at a
// time; small pictures only for the tiles on screen; click one to see it big; Save copies the
// files here (into Downloads, as Send does); Remove deletes them from the phone (asks once more
// here; without All files access the phone shows Android's own confirmation too). On USB, Wi-Fi or Wi-Fi Direct; over Bluetooth it says
// so and loads nothing (thumbnails there slowed everything else down).
// The settings button opens Photo backup (shared/photo-backup.js on the app's side): the albums
// picked, copied into a folder here, all off until it's switched on.
'use strict';

(() => {
  const tr = (text, vars) => window.I18n ? window.I18n.t(text, vars)
    : text.replace(/\{(\w+)\}/g, (m, k) => vars && Object.prototype.hasOwnProperty.call(vars, k) ? String(vars[k]) : m);

UI.tab({
  id: 'media',
  group: 'Phone',
  order: 7,
  title: tr('Photos'),
  icon: 'photo',
  color: 'linear-gradient(#ffd60a, #ff9f0a)',
  desc: tr('See your phone’s photos and videos and copy them here.'),
  for: ['android', 'ios'], // a phone's: Android, iPhone or iPad (shared/ui-kinds.js)
  wifiOnly: true, // pictures are too big for Bluetooth: Connect with Wi-Fi Direct on top, nothing loads
  master: { get: (s) => !!(s.hub && s.hub.media), set: (v) => UI.api.hub({ do: 'set', what: 'media', on: v }) },
  badge: (s) => (s.hub && s.hub.media ? 'on' : ''),
  build(body, page) {
    const { el } = UI;
    const winPc = /Win/.test(navigator.platform || navigator.userAgent);
    const top = UI.hubTop('media');
    let kind = 'all';
    const filter = UI.segmented([
      { value: 'all', label: tr('All') }, { value: 'photos', label: tr('Photos') }, { value: 'videos', label: tr('Videos') }, { value: 'screenshots', label: tr('Screenshots') },
    ], (v) => { kind = v; filter.set(v); reset(); });
    filter.set(kind);
    const selectBtn = UI.button(tr('Select'), () => { selecting = !selecting; anchor = ''; if (!selecting) chosen.clear(); paint(); }, 'small');
    const saveBtn = UI.button(tr('Save'), () => save([...chosen]), 'primary small');
    // Remove asks once more (its label turns into the question), as Files' Delete does.
    let confirmRemove = false;
    const removeBtn = UI.button(tr('Remove'), () => {
      if (!confirmRemove) { confirmRemove = true; return paint(); }
      remove([...chosen]);
    }, 'small');
    const canRemove = () => !!(UI.state && UI.state.hub && UI.state.hub.perm && UI.state.hub.perm.media && UI.state.hub.perm.media.remove);
    const status = el('span', { class: 'hint hub-media-status', attrs: { 'data-literal': '' } });
    const folderRow0 = UI.saveFolderRow(); // Download location, in the settings (the gear)
    // Photo backup's settings (this computer's side; phones that can say hub.state media.backup).
    const backup = backupView();
    const gearWrap = el('span', { class: 'gear-with-new' }, [UI.gearButton(() => backup.show(true))]);
    const gearNew = UI.newPill ? UI.newPill('photo-backup') : null;
    if (gearNew) gearWrap.append(gearNew);
    const bar = el('div', { class: 'hub-media-bar' }, [filter.el, el('span', { class: 'hub-grow' }), status, removeBtn, saveBtn, selectBtn, gearWrap]);
    const grid = el('div', { class: 'hub-grid' });
    const sentinel = el('div', { class: 'hub-sentinel' });
    // Albums (a phone that lists them: media.albums): All, then each with its cover and count. The
    // filters above work inside the one picked.
    let album = ''; // '' all; else the album's id
    let albums = []; // {id, name, n, cover}
    const covers = new Set(); // cover ids still to get
    const albumList = el('div', { class: 'hub-albums', hidden: true, attrs: { role: 'listbox', 'aria-label': tr('Albums') } });
    const main = el('div', { class: 'hub-media-main' }, [bar, grid, sentinel]);
    const wrap = el('div', { class: 'hub-media' }, [albumList, main]);
    function paintAlbums() {
      albumList.hidden = !albums.length;
      wrap.classList.toggle('with-albums', !!albums.length);
      if (!albums.length) return;
      const total = albums.reduce((a, x) => a + x.n, 0);
      const row = (id, name, n, cover) => {
        const pic = el('span', { class: 'hub-album-pic' + (cover ? '' : ' all') });
        if (cover && thumbs.get(cover)) pic.style.backgroundImage = `url("${thumbs.get(cover)}")`;
        if (cover) pic.dataset.cover = cover;
        const b = el('button', { class: 'hub-album' + (album === id ? ' on' : ''), attrs: { role: 'option', 'aria-selected': String(album === id), 'data-literal': '' } }, [
          pic, el('span', { class: 'hub-album-text' }, [el('span', { class: 'hub-album-name', text: name }), el('span', { class: 'hub-album-n', text: String(n) })]),
        ]);
        b.onclick = () => { if (album === id) return; album = id; paintAlbums(); reset(); };
        return b;
      };
      albumList.replaceChildren(row('', tr('All'), total, ''), ...albums.map((a) => row(a.id, a.name, a.n, a.cover)));
    }

    // The viewer: one big, with its name and Save.
    const bigImg = el('img', { class: 'hub-big', alt: '' });
    const bigName = el('div', { class: 'hub-big-name', attrs: { 'data-literal': '' } });
    const bigSub = el('div', { class: 'hint', attrs: { 'data-literal': '' } });
    const prev = UI.button('‹', () => step(-1));
    const next = UI.button('›', () => step(1));
    const bigSave = UI.button(tr('Save'), () => viewing && save([viewing.id]), 'primary');
    let confirmBig = false;
    const bigRemove = UI.button(tr('Remove'), () => {
      if (!viewing) return;
      if (!confirmBig) { confirmBig = true; bigRemove.say(tr('Remove from the phone?')); bigRemove.classList.add('danger'); return; }
      remove([viewing.id]);
    });
    // Copy and Share: the picture shown (the phone's large one, up to 1600 px), once it's here.
    let bigData = ''; // that picture, a data: URL ('' while it comes)
    const bigCopy = UI.button(tr('Copy'), () => {
      if (!viewing || !bigData) return;
      Promise.resolve(UI.api.hub({ do: 'copyImage', data: bigData })).then(() => {
        bigCopy.say(tr('Copied'));
        setTimeout(() => bigCopy.say(tr('Copy'), false), 1500);
      }).catch(() => {});
    });
    const bigShare = UI.button(tr('Share'), () => {
      if (!viewing || !bigData) return;
      UI.api.hub({ do: 'shareImage', data: bigData, name: viewing.name });
    });
    // Open: in this computer's own app for pictures; on Windows also Open with (its list of apps).
    const bigOpen = UI.button(tr('Open'), () => viewing && bigData && UI.api.hub({ do: 'openImage', data: bigData, name: viewing.name }));
    bigOpen.title = tr('Open it in this computer’s app for pictures');
    const bigOpenWith = UI.button(tr('Open with…'), () => viewing && bigData && UI.api.hub({ do: 'openImage', data: bigData, name: viewing.name, with: true }));
    const closeBtn = UI.button(tr('Close'), () => view(null));
    // The bar's buttons as icons (their names as tooltips); a button that says something (Copied, Remove from
    // the phone?, Save to …) shows its words beside the icon (.says).
    const ICON = {
      remove: 'M4 5.5h12 M8 5.5V4h4v1.5 M5.5 5.5l.8 11h7.4l.8-11 M8.5 8.5v5 M11.5 8.5v5',
      copy: 'M7 7h9v9H7z M4 13V4h9',
      open: 'M11 4h5v5 M16 4l-7 7 M14 11.5V16H4V6h4.5',
      openWith: 'M4 4h5v5H4z M11 4h5v5h-5z M4 11h5v5H4z M11 11h5v5h-5z',
      share: 'M10 3v10 M6.5 6.5L10 3l3.5 3.5 M5 10v6h10v-6',
      save: 'M10 3v9 M6.5 8.5L10 12l3.5-3.5 M4 15.5h12',
      close: 'M5 5l10 10 M15 5L5 15',
    };
    const iconize = (b, key, says) => {
      const name = b.textContent;
      const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
      svg.setAttribute('viewBox', '0 0 20 20');
      svg.setAttribute('aria-hidden', 'true');
      const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
      path.setAttribute('d', ICON[key]);
      svg.append(path);
      const label = el('span', { class: 'ibtn-label', text: name });
      b.textContent = '';
      b.append(svg, label);
      b.classList.add('ibtn');
      b.classList.toggle('says', !!says);
      b.title = b.title || name;
      b.setAttribute('aria-label', name);
      // Its words: b.say(text, shown) keeps the icon.
      b.say = (text, shown = true) => { label.textContent = text; b.setAttribute('aria-label', text); b.classList.toggle('says', shown); };
      return b;
    };
    iconize(bigRemove, 'remove'); iconize(bigCopy, 'copy'); iconize(bigOpen, 'open'); iconize(bigOpenWith, 'openWith');
    iconize(bigShare, 'share'); iconize(bigSave, 'save', true); iconize(closeBtn, 'close');
    const viewer = el('div', { class: 'hub-viewer', hidden: true }, [
      el('div', { class: 'hub-viewer-stage' }, [prev, bigImg, next]),
      el('div', { class: 'hub-viewer-bar' }, [el('div', { class: 'hub-grow' }, [bigName, bigSub]), bigRemove, bigCopy, bigOpen, bigOpenWith, bigShare, bigSave, closeBtn]),
    ]);
    const mainView = el('div', { class: 'gear-main' }, [top.el, wrap]);
    // The viewer beside .gear-main, not in it (#413): the page's entry animation (rise, fill both) stays
    // on each child of .page-body, and Chromium then holds a position: fixed descendant inside that
    // child, so the viewer covered only the scrolled grid with the photo below it, not the window.
    body.append(mainView, backup.el, viewer);

    // ---- Photo backup ----
    function backupView() {
      const backTo = UI.button(tr('Back'), () => show(false), 'small');
      const onSw = UI.switch((v) => UI.api.hub({ do: 'backup', a: 'set', on: v }), { label: tr('Photo backup') });
      const vidSw = UI.switch((v) => UI.api.hub({ do: 'backup', a: 'set', videos: v }), { label: tr('Videos too') });
      const delSw = UI.switch((v) => UI.api.hub({ do: 'backup', a: 'set', deletes: v }), { label: tr('Remove here what’s removed on the phone') });
      const note = el('div', { class: 'note warn', hidden: true, attrs: { 'data-literal': '' } });
      const onRow = UI.row(tr('Back up to {me}'), tr('Copies the albums you pick into a folder on {me}, a folder for each album. New photos follow whenever the phone is connected over USB or Wi-Fi.'), [onSw]);
      const vidRow = UI.row(tr('Videos too'), tr('Off: photos only.'), [vidSw]);
      const delRow = UI.row(tr('Remove here what’s removed on the phone'), winPc ? tr('Moves {me}’s copy to the Recycle Bin. Only copies this backup made and that weren’t changed here.')
        : tr('Moves {me}’s copy to the Trash. Only copies this backup made and that weren’t changed here.'), [delSw]);
      const showBtn = UI.button(tr('Show'), () => UI.api.hub({ do: 'backup', a: 'show' }), 'small');
      const changeBtn = UI.button(tr('Change…'), () => UI.api.hub({ do: 'backup', a: 'folder' }), 'small');
      const defBtn = UI.button(tr('Default'), () => UI.api.hub({ do: 'backup', a: 'default' }), 'small');
      const folderRow = UI.row(tr('Folder'), '', [showBtn, changeBtn, defBtn], { literalHint: true });
      const nowBtn = UI.button(tr('Back up now'), () => UI.api.hub({ do: 'backup', a: 'now' }), 'small');
      const stateRow = UI.row(tr('Status'), '', [nowBtn], { literalHint: true });
      const settingsCard = UI.card(tr('Photo backup'), [onRow, vidRow, delRow, folderRow, stateRow]);
      const albumBox = el('div');
      const albumsCard = UI.card(tr('Albums'), [albumBox]);
      const saveCard = UI.card(tr('Saving'), [folderRow0]);
      const view = el('div', { class: 'gear-settings', hidden: true }, [el('div', { class: 'feed-back' }, [backTo]), saveCard, note, settingsCard, albumsCard]);
      let open = false;
      let picked = {};
      let albumKey = '';
      function show(on) {
        open = on;
        view.hidden = !on;
        mainView.hidden = on;
        if (!on) return;
        if (UI.newSeen) UI.newSeen('photo-backup');
        if (gearNew) gearNew.remove();
        UI.api.hub({ do: 'backup', a: 'load' }); // its code loads now (nothing runs until it's on)
        if (!albums.length && UI.hubReady(UI.state, 'media') && !UI.onBle(UI.state)) UI.hubAsk({ t: 'media.albums' });
        render(UI.state);
      }
      function paintAlbums(b) {
        const on = !!(b && b.on);
        const key = JSON.stringify([albums.map((a) => [a.id, a.n]), picked, on]);
        if (key === albumKey) return;
        albumKey = key;
        if (!albums.length) {
          albumBox.replaceChildren(UI.row(tr('No albums yet'), tr('They show here once the phone is connected over USB or Wi-Fi.')));
          return;
        }
        albumBox.replaceChildren(...albums.map((a) => {
          const sw = UI.switch((v) => {
            const next = { ...picked };
            if (v) next[a.id] = a.name; else delete next[a.id];
            picked = next;
            UI.api.hub({ do: 'backup', a: 'set', albums: next });
          }, { label: a.name });
          sw.set(!!picked[a.id]);
          return UI.row(a.name, tr(a.n === 1 ? '1 item' : '{count} items', { count: a.n.toLocaleString() }), [sw], { literal: true, literalHint: true });
        }));
      }
      function render(s) {
        if (!open || !s) return;
        const h = s.hub || {};
        const b = h.backup && h.backup.loaded !== false ? h.backup : null;
        const phone = UI.phoneName(s);
        let why = '';
        if (!h.media) why = tr('Switch on Photos at the top of this page first.');
        else if (!h.connected) why = tr('Connect {phone} to set up Photo backup.', { phone });
        else if (UI.onBle(s)) why = tr('Photo backup works over USB or Wi-Fi, not Bluetooth.');
        else if (h.perm && h.perm.media && !h.perm.media.backup) why = tr('Update TwinDesk on {phone} to use Photo backup.', { phone });
        note.textContent = why;
        note.hidden = !why;
        if (!b) return;
        picked = b.albums || {};
        onSw.set(b.on); vidSw.set(b.videos); delSw.set(b.deletes);
        for (const x of [onSw, vidSw, delSw]) x.disable(!b.can && !b.on);
        folderRow.setHint(b.folder);
        defBtn.hidden = !b.custom;
        const n = Object.keys(picked).length;
        stateRow.setHint(!b.on ? tr('Off')
          : !n ? tr('Pick the albums to back up below.')
            : b.state === 'copying' ? tr('Copying, {count} to go', { count: b.left })
              : b.state === 'looking' ? tr('Looking for new photos…')
                : b.state === 'error' ? tr('The folder can’t be written. Choose another one.')
                  : !b.can ? tr('Waiting for the phone')
                    : tr(b.done === 1 ? '1 item backed up' : '{count} items backed up', { count: b.done.toLocaleString() }));
        nowBtn.disabled = !b.on || !n || !b.can || b.state === 'copying' || b.state === 'looking';
        paintAlbums(b);
      }
      return { el: view, show, render };
    }

    let items = []; // {id, video, at, w, h, size, name, dur, album}
    let nextKey = '';
    let more = false;
    let asking = false;
    let selecting = false;
    const chosen = new Set();
    let anchor = ''; // the id last chosen or let go: Shift-click does the same to every tile from it (#319)
    const thumbs = new Map(); // id -> data: URL ('' asked)
    const tiles = new Map(); // id -> tile element
    let viewing = null;

    function tile(it) {
      const img = el('img', { alt: '', draggable: false });
      if (thumbs.get(it.id)) img.src = thumbs.get(it.id);
      const t = el('button', { class: 'hub-tile', attrs: { 'data-literal': '' }, title: `${it.name}\n${new Date(it.at).toLocaleString()}` }, [img,
        it.video ? el('span', { class: 'hub-dur', text: it.dur ? UI.duration(it.dur / 1000) : tr('Video') }) : null,
        el('span', { class: 'hub-check' })]);
      t.dataset.id = it.id;
      t.onclick = (e) => {
        if (!selecting) return view(it);
        const on = !chosen.has(it.id);
        const a = e.shiftKey && anchor ? items.findIndex((x) => x.id === anchor) : -1;
        const b = items.indexOf(it);
        for (const x of a >= 0 && b >= 0 ? items.slice(Math.min(a, b), Math.max(a, b) + 1) : [it]) if (on) chosen.add(x.id); else chosen.delete(x.id);
        anchor = it.id;
        confirmRemove = false;
        paint();
      };
      tiles.set(it.id, t);
      seen.observe(t);
      return t;
    }

    function paint() {
      wrap.classList.toggle('selecting', selecting);
      if (!selecting || !chosen.size) confirmRemove = false;
      removeBtn.hidden = !selecting || !canRemove();
      removeBtn.disabled = !chosen.size;
      removeBtn.textContent = confirmRemove ? tr(chosen.size === 1 ? 'Remove 1 item from the phone?' : 'Remove {count} items from the phone?', { count: chosen.size }) : tr('Remove');
      removeBtn.classList.toggle('danger', confirmRemove);
      selectBtn.textContent = selecting ? tr('Done') : tr('Select');
      saveBtn.hidden = !selecting;
      saveBtn.disabled = !chosen.size;
      saveBtn.textContent = chosen.size ? tr('Save {count} to {me}', { count: chosen.size, me: UI.fill('{me}') }) : tr('Save');
      for (const [id, t] of tiles) t.classList.toggle('chosen', chosen.has(id));
      status.textContent = items.length ? tr(kind === 'videos' ? '{count} videos' : kind === 'photos' ? '{count} photos' : kind === 'screenshots' ? '{count} screenshots' : '{count} items', { count: `${items.length}${more ? '+' : ''}` }) : '';
    }

    // The tiles on screen get their pictures, those first (what scrolled past is dropped).
    const visible = new Set();
    let thumbTimer = 0;
    const seen = new IntersectionObserver((entries) => {
      for (const e of entries) {
        if (e.isIntersecting) visible.add(e.target.dataset.id); else visible.delete(e.target.dataset.id);
      }
      clearTimeout(thumbTimer);
      thumbTimer = setTimeout(() => {
        if (UI.onBle(UI.state)) return; // over Bluetooth: none
        const ids = [...visible].filter((id) => !thumbs.has(id));
        // A little ahead too: the next row or two below.
        const last = items.findIndex((x) => x.id === [...visible].pop());
        if (last >= 0) for (const x of items.slice(last + 1, last + 13)) if (!thumbs.has(x.id) && !ids.includes(x.id)) ids.push(x.id);
        for (const id of covers) if (!thumbs.get(id) && !ids.includes(id)) ids.push(id); // the albums' covers too, till they come
        for (const id of ids) if (!thumbs.has(id)) thumbs.set(id, '');
        if (ids.length) UI.hubAsk({ t: 'media.thumb', ids });
      }, 120);
    }, { root: document.getElementById('content'), rootMargin: '200px 0px' });
    const askCovers = () => {
      if (UI.onBle(UI.state) || !covers.size) return;
      const ids = [...visible].filter((id) => !thumbs.get(id)).concat([...covers].filter((id) => !thumbs.get(id)));
      if (ids.length) UI.hubAsk({ t: 'media.thumb', ids });
    };

    const end = new IntersectionObserver((entries) => {
      if (entries.some((e) => e.isIntersecting) && more && !asking) page1();
    }, { root: document.getElementById('content'), rootMargin: '600px 0px' });
    end.observe(sentinel);

    function page1() {
      if (UI.onBle(UI.state)) return;
      asking = true;
      UI.hubAsk({ t: 'media.list', kind, before: nextKey, n: 120, ...(album ? { album } : {}) });
    }

    function reset() {
      items = [];
      nextKey = '';
      more = false;
      chosen.clear();
      for (const t of tiles.values()) seen.unobserve(t);
      tiles.clear();
      visible.clear();
      grid.replaceChildren();
      paint();
      if (UI.hubReady(UI.state, 'media') && !UI.onBle(UI.state)) page1();
    }

    function save(ids) {
      if (!ids.length) return;
      UI.hubAsk({ t: 'media.save', ids });
      status.textContent = tr('Saving {count} to {me}’s {folder}…', { count: ids.length, me: UI.fill('{me}'), folder: UI.receivedName(UI.state) });
      if (selecting) { selecting = false; chosen.clear(); }
      paint();
    }

    // Off the phone: the ones gone leave the grid when it answers (media.delete).
    function remove(ids) {
      if (!ids.length || !canRemove()) return;
      UI.hubAsk({ t: 'media.delete', ids });
      confirmRemove = false;
      if (selecting) { selecting = false; chosen.clear(); }
      paint();
      status.textContent = tr('Removing {count}…', { count: ids.length });
    }

    function drop(ids) {
      const gone = new Set(ids);
      if (!gone.size) return;
      if (viewing && gone.has(viewing.id)) {
        const i = items.indexOf(viewing);
        const nextIt = items.slice(i + 1).find((x) => !gone.has(x.id)) || items.slice(0, i).reverse().find((x) => !gone.has(x.id));
        view(nextIt || null);
      }
      items = items.filter((x) => !gone.has(x.id));
      for (const id of gone) {
        const t = tiles.get(id);
        if (t) { seen.unobserve(t); t.remove(); }
        tiles.delete(id);
        thumbs.delete(id);
        visible.delete(id);
        chosen.delete(id);
      }
      if (!items.length && !more) grid.replaceChildren(el('div', { class: 'hub-empty', text: tr(kind === 'all' ? 'No photos or videos' : kind === 'videos' ? 'No videos' : kind === 'screenshots' ? 'No screenshots' : 'No photos') }));
      if (viewing) view(viewing); // its ‹ › again
    }

    function view(it) {
      viewing = it;
      viewer.hidden = !it;
      confirmBig = false;
      bigRemove.say(tr('Remove'), false);
      bigRemove.classList.remove('danger');
      zoomTo(1);
      bigRemove.hidden = !canRemove();
      bigData = '';
      paintBig();
      if (!it) return;
      if (thumbs.get(it.id)) bigImg.src = thumbs.get(it.id); else bigImg.removeAttribute('src');
      bigName.textContent = it.name;
      bigSub.textContent = [new Date(it.at).toLocaleString(), it.w && it.h ? `${it.w} × ${it.h}` : '', UI.bytes(it.size || 0), it.album].filter(Boolean).join(', ');
      if (!UI.onBle(UI.state)) UI.hubAsk({ t: 'media.view', id: it.id });
      const i = items.indexOf(it);
      prev.disabled = i <= 0;
      next.disabled = i < 0 || i >= items.length - 1;
    }

    // Copy and Share: photos only (a video's is a still), Share where this computer has one.
    function paintBig() {
      const photo = !!viewing && !viewing.video;
      bigCopy.hidden = bigOpen.hidden = !photo;
      bigOpenWith.hidden = !photo || !winPc;
      bigOpen.disabled = bigOpenWith.disabled = !bigData;
      bigShare.hidden = !photo || !(UI.state && UI.state.hub && UI.state.hub.shareSheet);
      bigCopy.disabled = bigShare.disabled = !bigData;
    }

    function step(d) {
      const i = items.indexOf(viewing);
      const it = items[i + d];
      if (it) view(it);
      if (i + d >= items.length - 6 && more && !asking) page1();
    }

    // Zoom and pan: Ctrl+scroll zooms around the pointer, a double click zooms in (and back), a drag pans
    // while zoomed. Scrolling without Ctrl goes to the next or previous one (pans while zoomed).
    const stage = bigImg.parentElement;
    let zoom = 1, tx = 0, ty = 0;
    const paintZoom = () => {
      bigImg.style.transform = zoom === 1 ? '' : `translate(${tx}px, ${ty}px) scale(${zoom})`;
      bigImg.classList.toggle('zoomed', zoom > 1);
    };
    // Zoom to z keeping the point (cx, cy) of the stage (default its centre) where it is.
    function zoomTo(z, cx, cy) {
      z = Math.min(8, Math.max(1, z));
      if (z === 1) { zoom = 1; tx = 0; ty = 0; return paintZoom(); }
      const r = bigImg.getBoundingClientRect();
      const ox = (cx == null ? r.left + r.width / 2 : cx) - (r.left + r.width / 2);
      const oy = (cy == null ? r.top + r.height / 2 : cy) - (r.top + r.height / 2);
      const k = z / zoom;
      tx -= ox * (k - 1);
      ty -= oy * (k - 1);
      zoom = z;
      paintZoom();
    }
    let wheelAt = 0;
    stage.addEventListener('wheel', (e) => {
      if (viewer.hidden) return;
      e.preventDefault();
      if (e.ctrlKey) return zoomTo(zoom * Math.pow(1.0018, -e.deltaY), e.clientX, e.clientY);
      if (zoom > 1) { tx -= e.deltaX; ty -= e.deltaY; return paintZoom(); }
      const now = Date.now();
      if (now - wheelAt < 280 || Math.abs(e.deltaY) + Math.abs(e.deltaX) < 4) return; // one photo per flick
      wheelAt = now;
      step((Math.abs(e.deltaY) >= Math.abs(e.deltaX) ? e.deltaY : e.deltaX) > 0 ? 1 : -1);
    }, { passive: false });
    bigImg.addEventListener('dblclick', (e) => zoomTo(zoom > 1 ? 1 : 2.5, e.clientX, e.clientY));
    bigImg.draggable = false;
    bigImg.addEventListener('pointerdown', (e) => {
      if (zoom === 1 || e.button !== 0) return;
      e.preventDefault();
      bigImg.setPointerCapture(e.pointerId);
      let x = e.clientX, y = e.clientY;
      const move = (ev) => { tx += ev.clientX - x; ty += ev.clientY - y; x = ev.clientX; y = ev.clientY; paintZoom(); };
      const up = () => { bigImg.removeEventListener('pointermove', move); bigImg.removeEventListener('pointerup', up); bigImg.removeEventListener('pointercancel', up); };
      bigImg.addEventListener('pointermove', move);
      bigImg.addEventListener('pointerup', up);
      bigImg.addEventListener('pointercancel', up);
    });
    // Right click: what the bar does, and zoom.
    let menu = null;
    const closeMenu = () => { if (menu) { menu.remove(); menu = null; } };
    stage.addEventListener('contextmenu', (e) => {
      if (viewer.hidden || !viewing) return;
      e.preventDefault();
      closeMenu();
      const items = [
        [zoom > 1 ? tr('Actual size') : tr('Zoom in'), () => zoomTo(zoom > 1 ? 1 : 2, e.clientX, e.clientY)],
        null,
        ...[[bigCopy, tr('Copy')], [bigOpen, tr('Open')], [bigOpenWith, tr('Open with…')], [bigShare, tr('Share')], [bigSave, bigSave.getAttribute('aria-label') || tr('Save')]]
          .filter(([b]) => !b.hidden).map(([b, label]) => [label, () => b.click(), b.disabled]),
        ...(bigRemove.hidden ? [] : [null, [tr('Remove from the phone'), () => { confirmBig = true; bigRemove.click(); }]]),
      ];
      menu = el('div', { class: 'hub-menu', attrs: { role: 'menu' } }, items.map((x) => {
        if (!x) return el('div', { class: 'hub-menu-sep' });
        const b = el('button', { class: 'hub-menu-item', text: x[0], attrs: { role: 'menuitem' } });
        b.disabled = !!x[2];
        b.onclick = () => { closeMenu(); x[1](); };
        return b;
      }));
      document.body.append(menu);
      const r = menu.getBoundingClientRect();
      menu.style.left = Math.min(e.clientX, window.innerWidth - r.width - 8) + 'px';
      menu.style.top = Math.min(e.clientY, window.innerHeight - r.height - 8) + 'px';
      const first = menu.querySelector('button:not([disabled])');
      if (first) first.focus();
    });
    document.addEventListener('mousedown', (e) => { if (menu && !menu.contains(e.target)) closeMenu(); }, true);
    window.addEventListener('blur', closeMenu);

    document.addEventListener('keydown', (e) => {
      if (viewer.hidden || page.hidden) return;
      if (menu && e.key === 'Escape') return closeMenu();
      if ((e.ctrlKey || e.metaKey) && (e.key === '=' || e.key === '+')) { e.preventDefault(); return zoomTo(zoom * 1.25); }
      if ((e.ctrlKey || e.metaKey) && e.key === '-') { e.preventDefault(); return zoomTo(zoom / 1.25); }
      if ((e.ctrlKey || e.metaKey) && e.key === '0') { e.preventDefault(); return zoomTo(1); }
      if (e.key === 'Escape') view(null);
      else if (e.key === 'ArrowLeft') step(-1);
      else if (e.key === 'ArrowRight') step(1);
    });

    UI.hubOn('media.list', (m) => {
      if (m.kind !== kind || (m.before || '') !== nextKey || (m.album || '') !== album) return; // an answer for an older filter or album
      asking = false;
      const fresh = (m.items || []).filter((x) => x && x.id && !tiles.has(x.id));
      items = items.concat(fresh);
      more = !!m.more;
      nextKey = m.next || nextKey;
      grid.append(...fresh.map(tile));
      if (!items.length) grid.replaceChildren(el('div', { class: 'hub-empty', text: tr(kind === 'all' ? 'No photos or videos' : kind === 'videos' ? 'No videos' : kind === 'screenshots' ? 'No screenshots' : 'No photos') }));
      paint();
    });
    UI.hubOn('media.thumb', (m) => {
      if (!m.data) return;
      thumbs.set(m.id, m.data);
      if (covers.delete(m.id)) for (const p of albumList.querySelectorAll('[data-cover]')) if (p.dataset.cover === m.id) p.style.backgroundImage = `url("${m.data}")`;
      const t = tiles.get(m.id);
      if (t) t.querySelector('img').src = m.data;
      if (viewing && viewing.id === m.id && !bigImg.getAttribute('src')) bigImg.src = m.data;
    });
    UI.hubOn('media.albums', (m) => {
      albums = (Array.isArray(m.items) ? m.items : []).filter((a) => a && typeof a.id === 'string' && typeof a.name === 'string')
        .map((a) => ({ id: a.id.slice(0, 80), name: a.name.slice(0, 100), n: Math.max(0, Number(a.n) || 0), cover: typeof a.cover === 'string' ? a.cover.slice(0, 40) : '' }));
      if (album && !albums.some((a) => a.id === album)) { album = ''; reset(); }
      for (const a of albums) if (a.cover && !thumbs.get(a.cover)) covers.add(a.cover);
      paintAlbums();
      backup.render(UI.state);
      askCovers();
    });
    UI.hubOn('media.delete', (m) => {
      drop(Array.isArray(m.ids) ? m.ids.filter((x) => typeof x === 'string') : []);
      paint();
      const notes = [];
      if (m.confirm > 0) notes.push(tr('Tap the notification on the phone to remove {count} more', { count: m.confirm }));
      if (m.failed > 0) notes.push(tr('{count} couldn’t be removed', { count: m.failed }));
      if (notes.length) status.textContent = notes.join(', ');
      if (albums.length) UI.hubAsk({ t: 'media.albums' }); // their counts
    });
    UI.hubOn('media.view', (m) => {
      if (!viewing || viewing.id !== m.id || !m.data) return;
      bigImg.src = m.data;
      bigData = m.data;
      paintBig();
    });

    let loaded = false;
    UI.onShown(page, (shown) => {
      if (!shown) view(null);
      else if (UI.hubReady(UI.state, 'media') && !UI.onBle(UI.state) && !loaded) { loaded = true; reset(); UI.hubAsk({ t: 'media.albums' }); }
    });
    paint();
    UI.onDeviceChange(() => { // another device: its photos, not the previous one's
      loaded = false;
      view(null);
      album = '';
      albums = [];
      covers.clear();
      paintAlbums();
      if (!page.hidden && UI.hubReady(UI.state, 'media') && !UI.onBle(UI.state)) { loaded = true; reset(); UI.hubAsk({ t: 'media.albums' }); }
    });

    return (s) => {
      top.render(s);
      backup.render(s);
      folderRow0.update(s);
      bigSave.say(tr('Save to {me}', { me: UI.fill('{me}') }));
      const ready = UI.hubReady(s, 'media') && !UI.onBle(s); // over Bluetooth: the Wi-Fi Direct note only
      wrap.hidden = !ready;
      if (!ready) { loaded = false; view(null); return; }
      if (!loaded) { loaded = true; reset(); UI.hubAsk({ t: 'media.albums' }); }
    };
  },
});

})();