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.

5.7 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// Explorer's right-click › TwinDesk (Show more options on Windows 11): one menu with
//   Send to phone        files and folders: the Send files flow, as Send to's shortcut (sendto.js)
//   Import from phone    a folder, or a folder's background: Phone Files opens, and what's saved
//                        from the phone goes into that folder (received.js redirectTo)
//   Capture from phone   the same: the phone's camera opens (cam.capture), every photo taken comes
//                        into that folder until the camera is closed there
//   Share folder         a folder, or a folder's background: TwinDesk opens Share <folder> with…, which
//                        devices get it (shared/folders-host.js; --td share is main.js's, not this file's)
// On Windows 11 the first-level menu has the same through TwinDesk's own IExplorerCommand
// (windows-helper/shellmenu, shellmenu.js); this one stays under Show more options.
// The keys are the user's own (HKCU\Software\Classes), written at start from one .reg file, and the
// uninstaller takes them away (installer.nsh). The commands start TwinDesk with --td <what> <path>;
// a running TwinDesk gets them through second-instance (main.js).
'use strict';

const { app } = require('electron');
const { execFile } = require('child_process');
const { launcher } = require('./storepkg');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { redirectTo, redirected } = require('../shared/received.js');

const ARG = '--td';
const IMPORT_MS = 10 * 60000;
const CAPTURE_MS = 20 * 60000;

class Explorer {
  // opts: phoneOn() → a phone is connected; send(msg) to it; on(type, fn) from it; goTab(tab);
  //   popup(kind, text, sub); onChange()
  constructor(opts) {
    Object.assign(this, opts);
    this.on('cam.capture', (m) => {
      if (m && m.on === false && (redirected() || {}).why === 'capture') {
        redirectTo('', 0);
        this.onChange();
      }
    });
  }

  // The menu in the registry (again at every start: the app may have moved).
  install() {
    const exe = launcher(); // the Store's MSIX: its alias (storepkg.js); the icon is this exe's
    const pre = app.isPackaged ? '' : `"${path.resolve(process.argv[1] || app.getAppPath())}" `; // a dev copy: electron.exe <app folder>
    const esc = (t) => t.replace(/\\/g, '\\\\').replace(/"/g, '\\"'); // a .reg string's escapes
    const cmd = (args) => `"${esc(`"${exe}" ${pre}${args}`)}"`;
    const icon = `"${esc(process.execPath)},0"`;
    const key = (root, ...sub) => '[' + ['HKEY_CURRENT_USER', 'Software', 'Classes', root, 'shell', 'TwinDesk', ...sub].join('\\') + ']';
    const menu = (root, items) => [
      key(root), '"MUIVerb"="TwinDesk"', `"Icon"=${icon}`, '"SubCommands"=""', '',
      ...items.flatMap(([id, label, args, ico]) => [
        key(root, 'shell', id), `"MUIVerb"="${label}"`, `"Icon"="${esc(ico)}"`, '',
        key(root, 'shell', id, 'command'), `@=${cmd(args)}`, '',
      ]),
    ];
    const send = ['1send', 'Send to phone', '--send-files "%1"', 'shell32.dll,-16761'];
    const imp = (v) => ['2import', 'Import from phone', `${ARG} import "${v}"`, 'shell32.dll,-16752'];
    const cap = (v) => ['3capture', 'Capture from phone', `${ARG} capture "${v}"`, 'imageres.dll,-5359'];
    const share = (v) => ['4share', 'Share folder', `${ARG} share "${v}"`, 'imageres.dll,-1023'];
    const reg = ['Windows Registry Editor Version 5.00', '',
      ...menu('*', [send]),
      ...menu('Directory', [send, imp('%1'), cap('%1'), share('%1')]),
      ...menu('Directory\\Background', [imp('%V'), cap('%V'), share('%V')]),
    ].join('\r\n');
    const file = path.join(os.tmpdir(), 'twindesk-explorer.reg');
    try {
      fs.writeFileSync(file, '' + reg, 'utf16le');
    } catch (e) {
      return;
    }
    execFile('reg.exe', ['import', file], { windowsHide: true }, (err) => {
      fs.unlink(file, () => {});
      if (err) console.log('[explorer] menu: ' + err.message);
    });
  }

  // argv with --td <what> <folder>: true when it was ours. A running TwinDesk gets a second copy's argv
  // with the switches first, so <what> <folder> can come later than right after --td.
  handle(argv) {
    const list = argv || [];
    const i = list.indexOf(ARG);
    if (i < 0) return false;
    const j = list.findIndex((a, k) => k > i && (a === 'import' || a === 'capture' || a === 'share'));
    const what = j < 0 ? '' : list[j];
    if (what === 'share') return false; // Share folder: main.js (shared/foldermenu.js)
    const dir = String(j < 0 ? '' : list[j + 1] || '').replace(/"$/, '');
    if (!path.isAbsolute(dir)) return true;
    if (what === 'import') this.import(dir);
    else if (what === 'capture') this.capture(dir);
    return true;
  }

  import(dir) {
    redirectTo(dir, IMPORT_MS, 'import');
    this.goTab('pfiles');
    if (!this.phoneOn()) this.popup('phoneOff', 'Connect your phone', 'Then choose files in Phone Files');
    this.onChange();
  }

  capture(dir) {
    if (!this.phoneOn()) {
      this.goTab('connection');
      return this.popup('phoneOff', 'Connect your phone', 'Capture from phone needs it');
    }
    redirectTo(dir, CAPTURE_MS, 'capture');
    this.send({ t: 'cam.capture', on: true });
    this.popup('camera', 'Take photos on your phone', 'They come into ' + path.basename(dir));
    this.onChange();
  }

  cancel() {
    const r = redirected();
    if (r && r.why === 'capture' && this.phoneOn()) this.send({ t: 'cam.capture', on: false });
    redirectTo('', 0);
    this.onChange();
  }

  get view() {
    const r = redirected();
    return r ? { dir: r.dir, name: path.basename(r.dir) || r.dir, why: r.why, until: r.until } : null;
  }
}

module.exports = { Explorer, ARG };