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.

16.2 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// Files tab (copy of windows/ui-files.js): Send files (drop zone + Choose…) and copy/paste files
// with shortcuts, then two direction cards — files and the other computer's folder coming here,
// files and this computer's folder going there — then transfers. Drags across the edge, Monitor 2
// drops, network folders in one switch each.
'use strict';

UI.tab({
  id: 'files',
  order: 50,
  title: I18n.t('File Sharing'),
  icon: 'globe',
  heading: I18n.t('File Sharing'),
  color: 'linear-gradient(#64d2ff, #0a84ff)',
  desc: I18n.t('Send files and folders between your devices, or share a folder.'),
  badge: (s) => (s.files.dropToMac || s.files.acceptFromMac ? 'on' : ''),
  build(body) {
    const { el, api } = UI;
    const sw = (label, set) => UI.switch(set, { label });
    const mount = sw(I18n.t('{Name}’s folder here'), (v) => api.setFiles({ mountMac: v }));
    const openBtn = UI.button(I18n.t('Open'), () => api.openMacDrive(), 'small');
    const macDrive = UI.row(I18n.t('Open {name}’s shared folder'), '', [openBtn, mount]);
    // Multi-device: every other device's folder connected here, each with its Open (smb view().drives).
    const drivesBox = el('div');
    let drivesKey = '';
    const drawDrives = (list) => {
      const key = JSON.stringify(list);
      if (key === drivesKey) return;
      drivesKey = key;
      drivesBox.replaceChildren(...list.map((d) => {
        const r = UI.row(I18n.t('Open {device}’s shared folder', { device: d.name }), d.drive || d.path || '', [UI.button(I18n.t('Open'), () => api.openDrive(d.id), 'small')], { literal: true });
        return r;
      }));
    };
    const share = sw(I18n.t('{me}’s folder on {name}'), (v) => api.setShareOn(v));
    const chooseBtn = UI.button(I18n.t('Choose…'), () => api.chooseShareFolder(), 'small');
    const sharePill = el('span', { class: 'pill' });
    const pcShare = UI.row(I18n.t('Share a folder with {name}'), '', [sharePill, chooseBtn, share]);
    const pair = UI.dirCards(
      { on: { get: (s) => s.files.acceptFromMac, set: (v) => api.setFiles({ acceptFromMac: v }) }, label: I18n.t('Take files from {name} on {me}'), rows: [
        macDrive,
        drivesBox,
      ] },
      { on: { get: (s) => s.files.dropToMac, set: (v) => api.setFiles({ dropToMac: v }) }, label: I18n.t('Send files from {me} to {name}'), rows: [
        pcShare,
      ] },
    );
    // Send files: drop zone + Choose…
    const zoneText = el('div', { class: 'big', text: I18n.t('Drop files or folders here') });
    const zoneHint = el('div', { class: 'hint', text: '' });
    const zone = el('div', { class: 'dropzone' }, [el('div', {}, [zoneText, zoneHint])]);
    const sendBtn = UI.button(I18n.t('Choose…'), () => api.chooseSendFiles(sendIds()), 'small');
    const cancelSendBtn = UI.button(I18n.t('Cancel'), () => api.cancelDrop(), 'small'); // the send on its way (as Recent's Cancel)
    let pausing = '';
    const pauseSendBtn = UI.button(I18n.t('Pause'), () => pausing && api.pauseSend(pausing), 'small'); // Send files: the rest waits under Waiting to send
    const sendRow = UI.row(I18n.t('Send files'), '', [pauseSendBtn, cancelSendBtn, sendBtn], { literal: true });
    // Waiting to send (shared/ui-outbox.js): sent while away, paused, cut off.
    const outbox = UI.outboxCard ? UI.outboxCard(api) : null;
    const sendBar = el('div', { class: 'bar' }, [el('span')]);
    sendBar.hidden = true;
    sendRow.querySelector('.text').append(sendBar);
    let canSend = false;
    zone.ondragover = (e) => { e.preventDefault(); if (canSend) zone.classList.add('over'); };
    zone.ondragleave = () => zone.classList.remove('over');
    zone.ondrop = (e) => {
      e.preventDefault();
      zone.classList.remove('over');
      // As an array: a FileList reaches the preload as a plain object, which can't be iterated.
      if (canSend) api.sendFiles(api.pathsOf([...e.dataTransfer.files]), sendIds());
    };
    // Dropping anywhere else in the window must not open the file.
    window.addEventListener('dragover', (e) => e.preventDefault());
    window.addEventListener('drop', (e) => e.preventDefault());

    // Copy and paste files with shortcuts
    const keysSw = sw(I18n.t('Copy and paste files'), (v) => api.setFiles({ shortcuts: v }));
    const copyRec = UI.keyRecorder((v) => api.setFiles({ copyKeys: v }));
    const pasteRec = UI.keyRecorder((v) => api.setFiles({ pasteKeys: v }));
    const keysRow = UI.row(I18n.t('Copy and paste files'), '', [keysSw], { literal: true });
    const copyRow = UI.row(I18n.t('Copy'), '', [copyRec]);
    const pasteRow = UI.row(I18n.t('Paste'), '', [pasteRec], { literal: true });

    const edgeDrag = sw(I18n.t('Drag files across the screen edge'), (v) => api.setFiles({ edgeDrag: v }));
    const edgeRow = UI.row(I18n.t('Drag files between computers'), '', [edgeDrag]);
    // Unencrypted file transfers (shared/fastfile.js fxPlain): the switch is on while they're encrypted.
    const encrypt = sw(I18n.t('Encrypt file transfers'), (v) => api.setFiles({ plain: !v }));
    const encryptRow = UI.row(I18n.t('Encrypt file transfers'), '', [encrypt]);
    const clipSw = sw(I18n.t('Copy received files to clipboard'), (v) => api.setFiles({ clipReceived: v }));
    const clipRow = UI.row(I18n.t('Copy received files to clipboard'), '', [clipSw]);
    const cancelBtn = UI.button(I18n.t('Cancel'), () => api.cancelDrop(), 'small');
    const outRow = UI.row('', '', [cancelBtn], { literal: true });
    const inRow = UI.row('', '', [], { literal: true });
    // TwinDesk Drive (shared/ui-drive.js): one-to-one and Multi-device alike; hidden where the state has none.
    const drive = UI.driveCard ? UI.driveCard(api, { tilde: (p) => String(p || '').replace(/^\/home\/[^/]+/, '~'), manager: '' }) : null;
    // Four tabs listed beside the page: File Sharing, TwinDesk Drive, Shared folders, Settings.
    // Multi-device (shared/ui-feed.js): who this computer takes files from, whose shared folders connect here,
    // and which devices Send files goes to (the selected one until picked). All devices by default.
    const kept = (k, d) => { try { const v = JSON.parse(localStorage.getItem(k) || 'null'); return v === 'all' || Array.isArray(v) ? v : d; } catch (_) { return d; } };
    const keepIt = (k, v) => { try { localStorage.setItem(k, JSON.stringify(v)); } catch (_) { /* this window only */ } };
    const meshDevs = (s) => (UI.meshDevices ? UI.meshDevices(s, 'files', { in: !!(s.files && s.files.acceptFromMac) }) : []);
    const recvPick = UI.pickRow ? UI.pickRow({
      label: I18n.t('Receive files from'), title: I18n.t('Receive files from'), devices: meshDevs,
      get: () => kept('td.pick.files.in', 'all'),
      save: (sel, s) => {
        keepIt('td.pick.files.in', sel);
        if (sel === 'all') api.setFiles({ acceptFromMac: true });
        for (const d of meshDevs(s)) { const want = sel === 'all' || sel.includes(d.id); if (d.in !== want) api.setConsent(d.id, 'files', 'in', want); }
      },
    }) : null;
    const foldersPick = UI.pickRow ? UI.pickRow({
      label: I18n.t('Show shared folders from'), title: I18n.t('Show shared folders from'), devices: meshDevs,
      get: (s) => ((s.files || {}).mountFrom) || 'all',
      save: (sel) => api.setFiles({ mountFrom: sel }),
    }) : null;
    let sendSel = kept('td.pick.files.send', null);
    const sendPick = UI.pickRow ? UI.pickRow({
      label: I18n.t('Send to'), title: I18n.t('Send files to'), devices: meshDevs,
      get: (s) => sendSel || [((s.peers || []).find((p) => p.selected) || {}).id].filter(Boolean),
      save: (sel) => { sendSel = sel; keepIt('td.pick.files.send', sel); },
    }) : null;
    // Multi-device: the devices Send files goes to (none: the selected one); one-to-one, nothing (the partner).
    function sendIds() {
      if (!UI.inMesh || !UI.inMesh() || !lastFiles) return undefined;
      if (sendSel === 'all') return meshDevs(lastFiles).filter((d) => d.connected).map((d) => d.id);
      return Array.isArray(sendSel) ? sendSel : undefined;
    }
    let lastFiles = null;
    const meshFolders = UI.card(I18n.t('Across devices'), [recvPick && recvPick.el, foldersPick && foldersPick.el].filter(Boolean), { cls: 'keep-on' });
    const netFolders = UI.titled(I18n.t('Network folders'), pair.el);
    const shelf = UI.shelfTab ? UI.shelfTab(api) : null; // File Sharing › File Shelf
    const sections = UI.tabbed('files', [ // tabs: a bar at the top, one part at a time
      { id: 'share', title: I18n.t('Send & receive'), cards: [
        UI.card(I18n.t('Send'), [...(sendPick ? [sendPick.el] : []), sendRow, zone], { cls: 'keep-on' }),
        netFolders,
        meshFolders,
        outbox ? outbox.card : null,
        UI.card(I18n.t('Recent'), [outRow, inRow], { actions: [
          (UI.withNew || ((b) => b))(UI.button(I18n.t('File Shelf'), () => api.showFileShelf(), 'small'), 'file-shelf'), // the last files received, to drag into another app (shared/file-shelf.js)
          UI.button(I18n.t('Clear'), () => api.clearFiles(), 'small'),
        ] }),
      ] },
      { id: 'shelf', title: I18n.t('File Shelf'), cards: [shelf ? shelf.card : null] }, // shared/ui-shelf.js
      { id: 'drive', title: I18n.t('TwinDesk Drive'), cards: [drive ? drive.card : null] },
      { id: 'folders', title: I18n.t('Shared folders'), cards: drive ? [drive.folders, drive.invites] : [] },
      { id: 'settings', title: I18n.t('Settings'), cards: [
        UI.card(I18n.t('Shortcuts'), [keysRow, copyRow, pasteRow], { cls: 'keep-on' }),
        UI.card(I18n.t('Transfers'), [edgeRow, encryptRow, clipRow]),
      ] },
    ]);
    body.append(sections);
    if (UI.shelfTabNew) UI.shelfTabNew(body, I18n.t('File Shelf')); // New until the tab is opened
    const mb = (n) => (n / 1048576).toFixed(1) + ' MB';
    return (s) => {
      const f = s.files;
      const m = f.smb;
      const p = UI.peer(s);
      pair.update(s);
      sections.spy();
      lastFiles = s;
      const inMesh = !!(UI.inMesh && UI.inMesh());
      netFolders.hidden = inMesh; // Multi-device: Across devices says it per device
      meshFolders.hidden = !inMesh;
      for (const r of [recvPick, foldersPick, sendPick]) if (r) r.render(s);
      if (drive) drive.update(s);
      if (shelf) shelf.update(s);
      if (outbox) outbox.update(f);
      clipSw.set(!!f.clipReceived);
      // Send files
      canSend = s.link.state === 'connected';
      // Not connected: they wait for the device last connected (shared/outbox.js), when that's on.
      const later = !canSend && f.outboxHours !== 0 && !!f.outboxPeer;
      zone.classList.toggle('off', !canSend && !later);
      zoneText.textContent = canSend ? I18n.t('Drop files or folders here') : later ? I18n.t('Drop files here: they go when {device} connects', { device: f.outboxPeer }) : I18n.t('Connect to send files');
      canSend = canSend || later;
      zoneHint.textContent = !canSend ? '' : p.phone ? I18n.t('They go to {device}’s Downloads', { device: p.the })
        : I18n.t('They go to {device}’s Downloads, or the folder chosen there for received files', { device: p.the });
      sendBtn.disabled = !canSend;
      const b = f.batch;
      sendBar.hidden = !b;
      cancelSendBtn.hidden = !b && !f.current;
      pausing = b && b.kind === 'send' && b.id ? b.id : '';
      pauseSendBtn.hidden = !pausing;
      if (b) sendBar.firstChild.style.width = Math.floor((100 * b.sent) / Math.max(1, b.bytes)) + '%';
      sendRow.setHint(b ? I18n.t(b.kind === 'paste' ? 'Pasting {file} to {device}{progress}' : 'Sending {file} to {device}{progress}', { file: b.name, device: p.the, progress: b.files > 1 ? I18n.t(' · {index} of {count}', { index: b.index + 1, count: b.files }) : '' }) : canSend ? I18n.t('To {device}, or right-click files › Send to in the file manager', { device: p.the }) : '');
      // Shortcuts
      const ck = f.clip;
      keysSw.set(f.shortcuts);
      copyRec.set(f.copyKeys);
      pasteRec.set(f.pasteKeys);
      keysRow.setHint(ck.error ? I18n.t(ck.error) : I18n.t('Select files, press {copy}, then {paste} on {device}', { copy: UI.keysLabel(f.copyKeys), paste: UI.keysLabel(f.pasteKeys), device: p.the }));
      keysRow.hint.classList.toggle('bad', !!ck.error);
      copyRow.hidden = pasteRow.hidden = !f.shortcuts;
      pasteRow.setHint(ck.pasting ? I18n.t('Pasting…')
        : ck.incoming ? I18n.t('{device} copied {items} · press {keys}', { device: p.them, items: ck.incoming.name || I18n.t('{count} items', { count: ck.incoming.items }), keys: UI.keysLabel(f.pasteKeys) })
          : I18n.t('Into the app in front (a chat, a mail…), or the file manager folder in front, else {folder}', { folder: UI.receivedName(s) }));
      edgeDrag.set(f.edgeDrag);
      encrypt.set(f.plain !== true);
      encryptRow.setHint(f.plain === true ? I18n.t('Off: files cross the network unencrypted, so others on it can read them (changes are still caught). Turn it off on both computers; a phone follows its computer.') : I18n.t('Files are encrypted on the way, like everything else'));
      encryptRow.hint.classList.toggle('bad', f.plain === true);
      edgeRow.setHint(f.edgeDrag && f.macDrags === false ? I18n.t('Turned off on {name}')
        : !f.edgeDrag ? '' : f.session === 'wayland' ? I18n.t('From {device}: drop on a folder or the desktop. From here: needs an Xorg session', { device: p.the })
          : I18n.t('Drag through the screen edge. Here, drop on a folder or the desktop'));
      mount.set(f.mountMac);
      drawDrives(api.openDrive && Array.isArray(m.drives) ? m.drives.filter((d) => (d.drive || d.path) && !d.selected) : []);
      openBtn.hidden = !m.drive && !m.wifi;
      macDrive.setHint(m.mountMessage ? I18n.t(m.mountMessage) : (!f.mountMac ? '' : m.drive ? I18n.t('In the file manager’s sidebar') : m.busy === 'mount' ? I18n.t('Connecting…')
        : m.wifi ? I18n.t('Over Bluetooth: Open connects it with Wi-Fi Direct') : I18n.t('Waiting for {name} to share one')));
      share.set(f.shareOn);
      const busy = m.busy === 'setup' ? I18n.t('Setting up Samba…') : m.busy === 'remove' ? I18n.t('Removing…') : '';
      sharePill.hidden = !f.shareOn && !busy;
      sharePill.textContent = busy || (m.peer && m.peer.mounted ? I18n.t('On {name}') : m.peer && m.peer.error ? I18n.t('Problem') : I18n.t('Shared'));
      sharePill.className = 'pill ' + (busy ? 'muted' : m.peer && m.peer.mounted ? 'good' : m.peer && m.peer.error ? 'bad' : 'muted');
      chooseBtn.disabled = !!m.busy;
      pcShare.setHint(m.message ? I18n.t(m.message) : m.peer && m.peer.error ? I18n.t(m.peer.error) : f.shareOn ? m.folder : I18n.t('{folder} · Shares it with Samba (asks for your password once)', { folder: m.folder }));

      cancelBtn.hidden = !f.current;
      if (f.current) {
        const c = f.current;
        outRow.label.textContent = I18n.t('Sending {file}{progress}', { file: c.name, progress: c.count > 1 ? I18n.t(' ({index} of {count})', { index: c.index + 1, count: c.count }) : '' });
        outRow.setHint(I18n.t('{sent} of {size}{waiting}', { sent: mb(c.sent), size: mb(c.size), waiting: f.queued ? I18n.t(' · {count} more waiting', { count: f.queued }) : '' }));
      } else {
        outRow.label.textContent = f.last ? I18n.t(f.last.ok ? 'Sent {file}' : 'Not sent: {file}', { file: f.last.name || '' }) : I18n.t('Nothing sent');
        outRow.setHint(f.last ? I18n.t(f.last.text, p) + ', ' + UI.ago(f.last.at) : '');
      }
      const inc = f.incoming;
      const dr = f.drag;
      if (dr.state) {
        inRow.label.textContent = I18n.t(dr.state === 'copying' ? 'Copying {file}' : 'Dragging {file}', { file: dr.name });
        inRow.setHint(dr.state === 'copying' ? '' : I18n.t('Esc cancels'));
        return;
      }
      if (dr.last && (!inc.last || dr.last.at > inc.last.at)) {
        inRow.label.textContent = dr.last.ok ? I18n.t('Received from {name}', { name: p.name }) : I18n.t('Not received');
        inRow.setHint(I18n.t(dr.last.text, p) + ', ' + UI.ago(dr.last.at));
        return;
      }
      inRow.label.textContent = inc.receiving ? I18n.t(inc.receiving === 1 ? 'Receiving {count} file from {name}' : 'Receiving {count} files from {name}', { count: inc.receiving, name: p.name })
        : inc.last ? I18n.t(inc.last.ok ? 'Received {file}' : 'Not received: {file}', { file: inc.last.name }) : I18n.t('Nothing received');
      inRow.setHint(inc.receiving ? '' : inc.last ? I18n.t(inc.last.text, p) + ', ' + UI.ago(inc.last.at) : '');
    };
  },
});