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.

7.4 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// Terminal in Multi-device (every build), three sections that open when clicked:
//   SSH from this computer into: tick a computer to open terminals on it (it's asked, and allows it on its own
//     Terminal page); Allow SSH into this computer from: tick a computer to let it open one here (only ever
//     turned on here); Nicknames: the same on every computer, for `ssh twindesk-<nickname>`. A computer asking for a shell here waits at the top for Allow or Decline
// (shared/pairsync-mesh.js GUARD). state.termMesh (shared/term-mesh.js).
//   const tm = TermMesh.create(); body.append(tm.el); tm.render(state.termMesh)
'use strict';

(function () {
  const t = (k, v) => I18n.t(k, v);

  function create() {
    const { api } = UI;
    const el = UI.el('div', { class: 'term-mesh' });
    const asksCard = UI.card(t('Asking for a terminal here'), []);
    const meCard = UI.card(t('This computer'), []);
    const intoCard = UI.card(t('SSH from this computer into'), []);
    const fromCard = UI.card(t('Allow SSH into this computer from'), []);
    const nickCard = UI.card(t('Nicknames'), []);
    el.append(asksCard, meCard, intoCard, fromCard, nickCard);
    const OPEN = 'td.termmesh.open';
    let open = new Set();
    try { open = new Set(JSON.parse(localStorage.getItem(OPEN) || '[]')); } catch (_) { /* all closed */ }
    const toggle = (k) => {
      if (open.has(k)) open.delete(k); else open.add(k);
      try { localStorage.setItem(OPEN, JSON.stringify([...open])); } catch (_) { /* this window only */ }
      lastKey = '';
      render(last);
    };
    // A section's summary row; its list shows once opened.
    const summary = (k, text) => {
      const b = UI.button(open.has(k) ? t('Hide') : t('Show'), () => toggle(k), 'small');
      b.setAttribute('aria-expanded', String(open.has(k)));
      const r = UI.row(text, '', [b], { literal: true });
      r.classList.add('tm-summary');
      r.onclick = (e) => { if (!e.target.closest('button')) toggle(k); }; // the whole row opens it
      return r;
    };
    const tick = (on, change, label, disabled) => {
      const c = UI.el('input', { type: 'checkbox', attrs: { 'aria-label': label } });
      c.checked = !!on;
      c.disabled = !!disabled;
      c.onchange = () => change(c.checked);
      return c;
    };
    const devRow = (box, name, state) => UI.el('label', { class: 'sf-dev' }, [box, UI.el('span', { class: 'sf-dev-name', text: name }), UI.el('span', { class: 'sf-dev-os', text: state })]);
    const body = (card) => card.querySelector('.card') || card;
    let lastKey = '';
    let editing = ''; // the computer whose nickname is being typed
    let err = { id: '', text: '' };

    // A nickname: typed in place of the row's buttons, Save or Enter keeps it, Escape leaves it.
    function nickEditor(id, current, placeholder) {
      const input = UI.el('input', { type: 'text', value: current, placeholder, maxLength: 30, spellcheck: false, autocomplete: 'off', attrs: { 'aria-label': t('Nickname') } });
      const save = async () => {
        const r = await api.termMesh({ nick: { id, name: input.value } });
        if (r && r.ok === false) { err = { id, text: r.why === 'taken' ? t('Another computer already goes by that name') : t('Use letters, digits, spaces and dashes') }; lastKey = ''; return; }
        editing = '';
        err = { id: '', text: '' };
        lastKey = '';
      };
      input.onkeydown = (e) => {
        if (e.key === 'Enter') save();
        if (e.key === 'Escape') { editing = ''; err = { id: '', text: '' }; lastKey = ''; render(last); }
      };
      setTimeout(() => input.focus(), 0);
      return [input, UI.button(t('Save'), save, 'small primary')];
    }

    let last = null;
    function render(m) {
      last = m;
      if (!m || !Array.isArray(m.devices)) { el.hidden = true; return; }
      el.hidden = false;
      const key = JSON.stringify([m, editing, err]);
      if (key === lastKey) return;
      lastKey = key;

      // Waiting here: Allow or Decline.
      const asks = (m.asks || []).map((a) => UI.row(t('{computer} asks to open a terminal on this computer', { computer: a.name }),
        t('Allow it only if you use {computer} yourself: it can run anything on this computer as you', { computer: a.name }), [
          UI.button(t('Allow'), () => api.termMesh({ answer: { id: a.id, yes: true } }), 'small primary'),
          UI.button(t('Decline'), () => api.termMesh({ answer: { id: a.id, yes: false } }), 'small'),
        ], { literal: true }));
      asksCard.hidden = !asks.length;
      body(asksCard).replaceChildren(...asks);

      // This computer's own nickname: what the others type to reach it.
      const me = m.me || {};
      const meRow = UI.row(me.nick || me.name, editing === me.id ? (err.id === me.id ? err.text : t('Letters, digits, spaces and dashes')) : t('The others reach this computer with {command}', { command: 'ssh ' + me.alias }),
        editing === me.id ? nickEditor(me.id, me.nick || '', me.name) : [UI.button(me.nick ? t('Rename') : t('Set a nickname'), () => { editing = me.id; lastKey = ''; render(last); }, 'small')], { literal: true });
      body(meCard).replaceChildren(meRow);

      const devs = m.devices;
      const nameOf = (d) => (d.nick ? d.nick + ' (' + d.name + ')' : d.name);
      const empty = () => UI.el('div', { class: 'sl-empty', text: t('No other computer yet') });

      // SSH from this computer into …
      const into = devs.filter((d) => d.there).length;
      const intoRows = [summary('into', into ? t('{count} of {total} computers', { count: into, total: devs.length }) : t('None yet'))];
      if (open.has('into')) {
        const list = UI.el('div', { class: 'sl-list' }, devs.length ? devs.map((d) => devRow(
          tick(d.there, (v) => api.setConsent(d.id, 'terminal', 'in', v), nameOf(d)), nameOf(d),
          !d.there ? '' : !d.connected ? t('Not connected') : d.ready ? 'ssh ' + d.alias : t('Waiting for it to allow'),
        )) : [empty()]);
        intoRows.push(list);
      }
      body(intoCard).replaceChildren(...intoRows);

      // Allow SSH into this computer from …
      const from = devs.filter((d) => d.here).length;
      const fromRows = [summary('from', from ? t('{count} of {total} computers', { count: from, total: devs.length }) : t('None: a computer can ask, and you answer here'))];
      if (open.has('from')) {
        const list = UI.el('div', { class: 'sl-list' }, devs.length ? devs.map((d) => devRow(
          tick(d.here, (v) => api.setConsent(d.id, 'terminal', 'out', v), nameOf(d)), nameOf(d),
          d.asked ? t('Asking now') : !d.connected ? t('Not connected') : '',
        )) : [empty()]);
        fromRows.push(list);
      }
      body(fromCard).replaceChildren(...fromRows);

      // Nicknames: `ssh twindesk-<nickname>` on every computer.
      const nickRows = [summary('nick', t('What the computers are called in ssh'))];
      if (open.has('nick')) {
        for (const d of devs) {
          nickRows.push(UI.row(nameOf(d), editing === d.id ? (err.id === d.id ? err.text : t('Letters, digits, spaces and dashes')) : 'ssh ' + d.alias,
            editing === d.id ? nickEditor(d.id, d.nick || '', d.name) : [UI.button(d.nick ? t('Rename') : t('Set a nickname'), () => { editing = d.id; lastKey = ''; render(last); }, 'small')], { literal: true }));
        }
        if (!devs.length) nickRows.push(empty());
      }
      body(nickCard).replaceChildren(...nickRows);
    }

    return { el, render };
  }

  window.TermMesh = { create };
})();