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.

3.5 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// Multi-device: Jump to a device (Mouse › Screen, and Keyboard). The mesh's monitor map (shared/ui-monitor-map.js,
// pick mode); a click on a device sets its shortcut, 2 to 4 keys, that takes the pointer straight there
// (shared/jumpkeys.js, kvm.jumpTo). This computer's own shortcut brings everything home.
// settings.kvm.jumps = { deviceId | 'self': accelerator }
//   const j = UI.jumpCard(); body.append(j.el); j.update(state)
'use strict';

(function () {
  const t = (k, v) => I18n.t(k, v);
  const parts = (acc) => (acc ? acc.split('+').length : 0);

  UI.jumpCard = () => {
    let last = null;
    let picked = '';
    let selfId = '';
    const jumpsOf = (s) => ((s && s.kvm && s.kvm.jumps) || {});
    const keyOf = (id) => (id === selfId ? 'self' : id);
    const map = window.MonitorMap ? MonitorMap.create({
      pick: (id) => { picked = id; paint(); },
      badge: (id) => { const acc = last && jumpsOf(last)[keyOf(id)]; return acc ? UI.keysLabel(acc) : ''; },
    }) : null;
    const save = (acc) => {
      if (!last || !picked) return;
      const next = { ...jumpsOf(last) };
      if (acc) {
        for (const [k, v] of Object.entries(next)) if (v === acc) delete next[k]; // one shortcut, one device
        next[keyOf(picked)] = acc;
      } else delete next[keyOf(picked)];
      UI.api.setKvm({ jumps: next });
    };
    const rec = UI.keyRecorder((acc) => save(acc), { valid: (acc) => parts(acc) >= 2 && parts(acc) <= 4 });
    const clear = UI.button(t('Clear'), () => save(''), 'small');
    const editRow = UI.row('', '', [rec.el, clear], { literal: true });
    const hint = UI.el('div', { class: 'hint jump-hint' });
    const card = UI.card(t('Jump to a device'), [map ? map.el : null, hint, editRow].filter(Boolean), { cls: 'keep-on' });

    function people(s) {
      const me = s.device || { id: s.macId, name: s.macName };
      selfId = me.id || '';
      const os = s.device ? (s.device.os || (navigator.platform.startsWith('Win') ? 'win' : 'linux')) : 'mac';
      const m = new Map([[selfId, { name: me.name || t('This computer'), os, connected: true, self: true }]]);
      for (const d of s.peers || []) if (d && d.id) m.set(d.id, { name: d.name || d.id.slice(0, 8), os: d.os, connected: !!d.connected, self: false, via: d.state === 'via' });
      return m;
    }

    function paint() {
      const s = last;
      if (!s) return;
      const ppl = people(s);
      const p = picked && ppl.get(picked);
      editRow.hidden = !p;
      if (p) {
        editRow.label.textContent = picked === selfId ? t('Shortcut to bring everything back to {device}', { device: p.name }) : t('Shortcut to jump to {device}', { device: p.name });
        rec.set(jumpsOf(s)[keyOf(picked)] || '');
        clear.disabled = !jumpsOf(s)[keyOf(picked)];
      }
      const errs = (s.kvmState && s.kvmState.jumpErrors) || {};
      const bad = Object.keys(errs).map((k) => (ppl.get(k === 'self' ? selfId : k) || {}).name).filter(Boolean);
      hint.textContent = bad.length ? t('Another app uses the shortcut for {devices}. Pick other keys.', { devices: bad.join(', ') })
        : t('Click a device to give it a shortcut of 2 to 4 keys. Press it anywhere, on any device, to move the pointer there.');
      if (map && s.layout && s.layout.map) map.draw(s.layout.map, ppl, selfId);
    }

    return {
      el: card,
      update(s) {
        last = s;
        card.hidden = !(UI.inMesh && UI.inMesh()) || !(s.layout && s.layout.map);
        if (!card.hidden) paint();
      },
    };
  };
})();