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.6 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith, Erdoğan Yeşil. GPL-3.0, see LICENSE.
// Multi-device tab with it on (docs/multi-device.md): what only several devices need, in one place.
// Turn off (TwinDesk restarts one to one), where the devices sit (the shared layout's grid, docs/mesh.md
// "Shared layout"), and Join every device automatically (docs/mesh.md "Joining everyone").
'use strict';

UI.tab({
  id: 'multi',
  order: 95,
  title: I18n.t('Multi-device'),
  heading: I18n.t('Multi-device'),
  color: 'linear-gradient(#5e5ce6, #3634a3)',
  desc: I18n.t('Several computers and phones connected at once'),
  bottom: true,
  build(body) {
    const { api } = UI;
    const turnOff = UI.button(I18n.t('Turn off'), () => {
      turnOff.disabled = true;
      turnOff.textContent = I18n.t('Restarting…');
      api.setMulti(false);
    }, 'small');

    // ---- the layout: the monitor map (shared/ui-monitor-map.js, docs/mesh.md "Shared layout") ----
    // Every device's monitors where they sit on the desk; drag one to move it. Each change goes through
    // moveOnMap, so every device shows the same map, and the pointer crosses only where monitors touch.
    let last = null; // the last state
    const layoutHint = UI.note('info', '');
    const reachNote = UI.note('warn', ''); // the devices the pointer can't get to from here, and why
    const map = MonitorMap.create({ select: (id) => { selected = id; if (last) drawLayout(last); }, move: (id, x, y, snap) => api.moveOnMap(id, x, y, snap) });
    const resetBtn = UI.button(I18n.t('Line up again'), () => api.resetMap(), 'small');
    const actions = UI.el('div', { class: 'mm-actions' }, [resetBtn]);
    // An offline computer in the way: the pointer goes on to what is past it, or stops (a red line).
    const skipSw = UI.switch((v) => api.setMapSkip(v), { label: I18n.t('Skip computers that are not connected') });
    const skipRow = UI.row(I18n.t('Skip computers that are not connected'), '', [skipSw]);
    // Use the whole edge (#376), for the device selected on the map (click or drag it): a small device next
    // to a big monitor takes that monitor's whole edge, positions scaled along both (off: only where the
    // monitors meet).
    const wholeSw = UI.switch((v) => { if (selected) api.setMapWhole(selected, v); }, { label: I18n.t('Uses the whole edge') });
    const wholeRow = UI.row('', I18n.t('The full side of the monitor it touches leads to it, scaled along the edge. Off: only where the monitors meet'), [wholeSw], { literal: true });
    const pickRow = UI.row(I18n.t('Whole edge'), I18n.t('Select a device on the map to let it use the whole edge of the monitor it touches'), []);
    const wholeBox = UI.el('div', { class: 'whole-edge' }, [wholeRow, pickRow]);
    let selected = null;
    function drawWhole(lay, people, selfId) {
      const ok = !!(selected && selected !== selfId && people.has(selected) && (lay.map.world || {})[selected]);
      wholeRow.hidden = !ok;
      pickRow.hidden = ok;
      if (!ok) return;
      const label = I18n.t('{name} uses the whole edge', { name: people.get(selected).name });
      wholeRow.label.textContent = label;
      wholeSw.set(new Set(lay.map.whole || []).has(selected));
    }

    function drawLayout(s) {
      const lay = s && s.layout;
      if (!lay || !lay.map || !s.device) return;
      const people = new Map([[s.device.id, { name: s.device.name || I18n.t('This computer'), os: 'linux', connected: true, self: true }]]);
      for (const d of s.peers || []) {
        if (d && d.id) people.set(d.id, { name: d.name || d.id.slice(0, 8), os: d.os, connected: !!d.connected, self: false, via: d.state === 'via' });
      }
      map.draw(lay.map, people, s.device.id);
      drawWhole(lay, people, s.device.id);
      resetBtn.hidden = !lay.map.arranged; // only once someone moved a device on the map
      const skip = lay.map.skip !== false;
      skipSw.set(skip);
      skipRow.setHint(skip ? I18n.t('On: the pointer goes past a computer that is not connected, to the device on its far side')
        : I18n.t('Off: the pointer stops at a computer that is not connected (a red line)'));
      const reach = lay.reach || {};
      const red = Object.keys(reach).filter((id) => reach[id] === 'blocked' && (people.get(id) || {}).via);
      const behind = Object.values(reach).includes('behind');
      const autoJoin = (s.mesh || {}).autoJoin === true;
      reachNote.set(red.length ? I18n.t(behind
        ? autoJoin ? '{devices} are not connected to this computer, so the pointer stops before them and does not get to the devices past them. Pair them with this computer.' : '{devices} are not connected to this computer, so the pointer stops before them and does not get to the devices past them. Pair them with this computer, or turn on Join every device automatically.'
        : autoJoin ? '{devices} are not connected to this computer, so the pointer stops before them. Pair them with this computer.' : '{devices} are not connected to this computer, so the pointer stops before them. Pair them with this computer, or turn on Join every device automatically.', { devices: red.map((id) => people.get(id).name).join(', ') }) : '');
      layoutHint.set(I18n.t('Drag a device to where it sits on your desk. The pointer crosses where the monitors touch, at the same place along the edge (the lit lines).'));
    }

    // ---- Mesh managers, at the bottom (state.mgr, shared/ui-meshmgr.js); who shares what is on each feature's page ----
    const mgrs = window.MeshManagers ? MeshManagers.create() : null;

    const joinSw = UI.switch((v) => api.setMesh({ autoJoin: v }), { label: I18n.t('Join every device automatically') });
    const joinRow = UI.row(I18n.t('Join every device automatically'), '', [joinSw]);
    // The other devices' logs, copied into this one's log folder (shared/peerlogs.js, docs/peer-logs.md).
    const shareLogSw = UI.switch((v) => api.setMesh({ shareLogs: v }), { label: I18n.t('Let the other devices read this computer’s log') });
    const collectLogSw = UI.switch((v) => api.setMesh({ collectLogs: v }), { label: I18n.t('Copy the other devices’ logs here') });
    const logsOpen = UI.button(I18n.t('Open log folder'), () => api.openLink('logs'), 'small');
    const logRows = [
      UI.row(I18n.t('Let the other devices read this computer’s log'), I18n.t('To find what went wrong from any computer. Devices you paired with only'), [shareLogSw]),
      UI.row(I18n.t('Copy the other devices’ logs here'), I18n.t('In the log folder, under peers, as they grow'), [collectLogSw, logsOpen]),
    ];

    body.append(
      UI.card(I18n.t('Mesh mode'), [
        UI.row(I18n.t('Use several devices at once'), I18n.t('On. TwinDesk restarts when you turn it on or off'), [turnOff]),
        UI.row(I18n.t('Computers'), I18n.t('Connect to this one over the same network only, not Bluetooth. Phones over Wi-Fi, a USB cable or Bluetooth, as before'), []),
      ]),
      UI.card(I18n.t('Layout'), [map.el, actions, reachNote, layoutHint, skipRow, wholeBox], { cls: 'keep-on' }),
      UI.card(I18n.t('Joining'), [joinRow]),
      UI.card(I18n.t('Logs'), logRows),
      ...(mgrs ? [mgrs.el] : []),
    );

    return (s) => {
      last = s;
      drawLayout(s);
      if (mgrs) mgrs.render(s.mgr || null);
      const jn = (s.mesh || {}).autoJoin === true;
      joinSw.set(jn);
      shareLogSw.set(!s.mesh || s.mesh.shareLogs !== false);
      collectLogSw.set(!s.mesh || s.mesh.collectLogs !== false);
      joinRow.setHint(jn ? I18n.t('Pair with one device and the rest connect to this one by themselves. Devices you pair with can introduce others, without a pairing code')
        : I18n.t('Off: this computer neither introduces devices to each other nor accepts an introduction. Pair each device yourself'));
    };
  },
});