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.

2.6 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// Multi-device › Share screen, the receiving side: the sharer's map may say which of this computer's
// monitors a shared screen fills (screen.state place: the id of one of our Electron displays, the ids
// the layout publishes). Every viewer window given a monitor is laid out here: one alone fills that
// monitor's work area; two or more share it in a grid (two side by side, four in 2 × 2 …). A window
// whose place goes away keeps where it is. An id this computer doesn't have (unplugged): nothing moves.
// Main process, Mac, Windows and Linux.
//   const P = require('../shared/screen-place.js'); P.set(key, win, placeId); P.drop(key)
'use strict';

const byKey = new Map(); // key -> { win, place }

// n windows in a work area: the grid with the fewest empty cells, wider than tall first.
function grid(area, n) {
  const cols = Math.ceil(Math.sqrt(n));
  const rows = Math.ceil(n / cols);
  const w = Math.floor(area.width / cols), h = Math.floor(area.height / rows);
  const out = [];
  for (let i = 0; i < n; i++) out.push({ x: area.x + (i % cols) * w, y: area.y + Math.floor(i / cols) * h, width: w, height: h });
  return out;
}

function screenApi() {
  try { return require('electron').screen; } catch (_) { return null; }
}

function layout(place, screen = screenApi()) {
  if (!screen || !place) return;
  const d = screen.getAllDisplays().find((x) => String(x.id) === String(place));
  if (!d) return;
  const here = [...byKey.values()].filter((e) => e.place === place && e.win && !e.win.isDestroyed());
  const cells = grid(d.workArea, here.length);
  here.forEach((e, i) => {
    try {
      if (e.win.isFullScreen()) e.win.setFullScreen(false);
      if (e.win.isMaximized()) e.win.unmaximize();
      e.win.setAspectRatio(0); // the cell's shape, not the picture's (the picture letterboxes)
      e.win.setBounds(cells[i]);
      e.placed = true;
    } catch (_) { /* closing */ }
  });
}

// key: one viewer (a device id); place: a display id or '' (none: the window stays where it is).
function set(key, win, place) {
  const p = place ? String(place).slice(0, 40) : '';
  const was = byKey.get(key);
  if (!p) {
    if (was) { byKey.delete(key); layout(was.place); }
    return;
  }
  if (was && was.place === p && was.win === win) return;
  byKey.set(key, { win, place: p });
  if (was && was.place !== p) layout(was.place);
  layout(p);
}

function drop(key) {
  set(key, null, '');
}

// The place given to this viewer now, so a window opened later goes there too.
function of(key) {
  const e = byKey.get(key);
  return e ? e.place : '';
}

module.exports = { set, drop, of, grid, layout };