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.

10.8 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith, Erdoğan Yeşil. GPL-3.0, see LICENSE.
// Cursor tab: directions, edge resistance, speed, scrolling. Which device each edge leads to is the
// layout in the Multi-device tab (ui-multi.js, docs/mesh.md "Shared layout").
'use strict';

UI.tab({
  id: 'cursor',
  group: 'Input',
  order: 20,
  for: ['computer', 'android', 'tv'], // iOS takes no mouse or keyboard from an app (shared/ui-kinds.js)
  title: I18n.t('Mouse'),
  desc: I18n.t('Move the pointer off the edge of the screen to go to the other computer.'),
  color: 'linear-gradient(#ffb340, #ff9500)',
  // One switch: cursor sharing, the same one the hotkey's double-tap and the other
  // computer flip (kvm.link keeps both in step). Through Bluetooth it is Over Bluetooth (kvm.setShared).
  master: { get: (s) => !!(s.kvm && s.kvm.mouse && s.kvm.linked !== false && !(s.input && s.input.paused)), set: (v) => UI.api.setKvm(v ? { mouse: true, shared: true } : { shared: false }) },
  badge: (s) => (s.kvm && s.kvm.mouse && s.kvm.linked !== false && !(s.input && s.input.paused) ? 'on' : ''),
  build(body) {
    const { el, api } = UI;
    const status = UI.note('info', '', { literal: true });
    const lock = UI.cursorLock();

    // A row whose label follows the other computer's name (UI.names fills it on render).
    const peerRow = (label, hint, controls) => {
      const r = UI.row(label, hint, controls);
      r.label.dataset.peer = label;
      return r;
    };

    const drive = UI.switch((v) => api.setKvm({ driveOther: v }), { label: I18n.t('Use here, control there') });
    const driven = UI.switch((v) => api.setKvm({ beDriven: v }), { label: I18n.t('Let it control this computer') });
    const resistance = UI.slider({ min: 0, max: 400, step: 10, format: (v) => `${v} px` }, (v) => api.setKvm({ resistancePx: v }));
    // Hold a key to cross (#230; in Multi-device, this Mac's own mouse and trackpad at its edges) and
    // the emergency return (#178).
    const portals = typeof CursorPortalsUI !== "undefined" ? CursorPortalsUI.create() : null; // Cursor portals (module cursor)
    const crossingModifier = UI.select([
      { value: '', label: I18n.t('None') },
      { value: 'Command', label: '⌘ Command' },
      { value: 'Control', label: '⌃ Control' },
      { value: 'Alt', label: '⌥ Option' },
      { value: 'Shift', label: '⇧ Shift' },
    ], (v) => api.setKvm({ crossingModifier: v }));
    crossingModifier.el.setAttribute('aria-label', I18n.t('Hold key to switch'));
    const stayFull = UI.switch((v) => api.setKvm({ stayFullscreen: v }), { label: I18n.t('Stay while an app is fullscreen') }); // #373
    const emergencyKey = UI.keyRecorder((v) => api.setKeys({ emergencyKeys: v }), {
      bare: true, allowEscape: true, valid: (v) => {
        const f = /(?:^|\+)F(\d+)$/.exec(v);
        return !f || (Number(f[1]) >= 1 && Number(f[1]) <= 20);
      },
    });
    const speed = UI.slider({ min: 0.25, max: 4, step: 0.25, format: (v) => `${v.toFixed(2)}×` }, (v) => api.setKvm({ speed: v }));
    const reverse = UI.switch((v) => api.setKvm({ reverseScroll: v }), { label: I18n.t('Reverse scrolling') });
    const scrollSpeed = UI.slider({ min: 0.25, max: 4, step: 0.25, format: (v) => `${v.toFixed(2)}×` }, (v) => api.setKvm({ scrollSpeed: v }));
    const gestures = UI.switch((v) => api.setKvm({ gestures: v }), { label: I18n.t('Trackpad gestures') });
    const gestureRow = UI.row(I18n.t('Trackpad gestures'), I18n.t('Pinch to zoom, swipe between desktops, Mission Control / Task View, back and forward — done the native way on {name}. The same on both'), [gestures]);
    const syncPads = UI.switch((v) => api.setKvm({ syncPads: v }), { label: I18n.t('Sync touchpads') });
    const syncRow = UI.row(I18n.t('Sync touchpads'), I18n.t('On: every computer’s mouse and trackpad move the one pointer, wherever it is. Off: using a computer’s own trackpad takes its pointer back. The same on both'), [syncPads]);
    const shake = UI.switch((v) => api.setKvm({ shakeHome: v }), { label: I18n.t('Shake to bring the pointer back') });
    const shakeRow = UI.row(I18n.t('Shake to bring the pointer back'), I18n.t('While you use another computer, shake this computer’s mouse or trackpad left and right: the pointer comes back here'), [shake]);
    const ble = UI.switch((v) => api.setKvm({ ble: v }), { label: I18n.t('Over Bluetooth') });
    const bleRow = UI.row(I18n.t('Over Bluetooth'), I18n.t('When {name} isn’t on this network and you’re connected through Bluetooth (slower). Off: nothing crosses until you’re on Wi-Fi again. The same on both'), [ble]);
    // #158: the pointer and keys over Bluetooth while the rest stays on Wi-Fi (shared/bleinput.js), for
    // crowded Wi-Fi, with every computer that has it on too. Off by default; turning it on asks first.
    let bleInputOn = false;
    const bleInput = UI.switch((v) => {
      if (!v) return api.setNet({ bleInput: false });
      bleInput.set(bleInputOn);
      window.MeshIntro.ask({
        title: I18n.t('Mouse and keyboard over Bluetooth?'),
        lines: [I18n.t('The pointer and keys go over Bluetooth; screens, files and the clipboard stay on Wi-Fi. For crowded Wi-Fi, where the pointer stutters or lags in bursts.'),
          I18n.t('Bluetooth then stays on beside the Wi-Fi for the whole session. On many computers the two share one radio and can interfere, slowing the Wi-Fi (on 2.4 GHz especially), and the pointer moves at Bluetooth’s pace.')],
        hint: I18n.t('Turn it on on both computers, with Bluetooth with computers on (General). The session connects again through Bluetooth to start it.'),
        ok: I18n.t('Turn on'),
      }, () => api.setNet({ bleInput: true }));
    }, { label: I18n.t('Mouse and keyboard over Bluetooth') });
    const bleInputRow = UI.row(I18n.t('Mouse and keyboard over Bluetooth'), '', [bleInput]);
    bleInputRow.hidden = !(window.MeshIntro && window.MeshIntro.ask);

    // A phone as the peer: how the pointer and keys reach it (settings.phone.input, the phone's own toggle too).
    const via = UI.segmented([{ value: 'adb', label: 'ADB' }, { value: 'phone', label: I18n.t('Accessibility') }], (v) => api.setPhone({ input: v, inputPicked: true }));
    const viaRow = UI.row(I18n.t('Through'), I18n.t('ADB: full control. Accessibility: TwinDesk on the phone taps and types itself, over Wi-Fi, Wi-Fi Direct or Bluetooth, no developer options'), [via.el]);
    let driven2 = null;
    let crossingModifierRow = null;
    // Hotkey (double-tap: cursor sharing on or off), here from the Keyboard page, in every edition.
    const hotkey = UI.select(Object.entries(UI.HOTKEYS).map(([value, label]) => ({ value, label })), (v) => UI.api.setKeys({ hotkey: v }));
    const hotkeyCard = UI.card(I18n.t('Hotkey'), [UI.row(I18n.t('Hotkey'), I18n.t('Double-tap to turn cursor sharing on or off'), [hotkey])]);
    const jump = UI.jumpCard ? UI.jumpCard() : null; // Multi-device: Jump to a device (shared/ui-jump.js)
    body.append(
      // Groups, as General's list; one shows only while it has something.
      UI.groupedSections([
        { id: 'sharing', title: I18n.t('Sharing'), cards: [
          status,
          UI.pair(
        UI.dir('m2w', [peerRow(I18n.t('Use {me}’s mouse and keyboard on {name}'), I18n.t('Push the pointer past the edge'), [drive]), viaRow]),
        UI.dir('w2m', [driven2 = peerRow(I18n.t('Use {name}’s mouse and keyboard on {me}'), '', [driven])]),
      ),
          hotkeyCard,
        ] },
        { id: 'screen', title: I18n.t('Screen'), cards: [
          jump && jump.el,
          UI.note('info', I18n.t('Which device each edge of the screen leads to: Multi-device › Layout.')),
          portals && portals.el,
        ] },
        { id: 'feel', title: I18n.t('Look and feel'), cards: [
          lock.el,
          UI.card(I18n.t('Emergency return'), [UI.row(I18n.t('Return shortcut'), I18n.t('Stops input sharing and returns the pointer if the input helper responds. Set separately on each Mac.'), [emergencyKey.el])]),
          UI.card(I18n.t('Feel'), [
        UI.row(I18n.t('Edge resistance'), I18n.t('Push a little further to cross'), [resistance]),
        crossingModifierRow = UI.row(I18n.t('Hold key to switch'), I18n.t('Hold this key when this Mac’s own mouse or trackpad leaves it at an edge.'), [crossingModifier.el]),
        UI.row(I18n.t('Stay while an app is fullscreen'), I18n.t('The pointer doesn’t leave at an edge while a game, video or presentation fills the screen. The hotkey still switches.'), [stayFull.el]),
        UI.row(I18n.t('Pointer speed here'), I18n.t('Another computer’s mouse on this Mac, and this Mac’s on a phone'), [speed]),
        peerRow(I18n.t('Reverse scrolling from {name}'), '', [reverse]),
        UI.row(I18n.t('Scrolling speed here'), I18n.t('Another computer’s mouse wheel on this Mac'), [scrollSpeed]),
        gestureRow,
        syncRow,
        shakeRow,
      ]),
        ] },
        { id: 'more', title: I18n.t('More'), cards: [
          UI.card(I18n.t('Not on the same network'), [bleRow]),
          UI.card(I18n.t('Crowded Wi-Fi'), [bleInputRow]),
        ] },
      ]),
    );

    return (s) => {
      hotkey.set((s.keys && s.keys.hotkey) || 'AltRight');
      if (jump) jump.update(s);
      if (s.net) {
        bleInputOn = s.net.bleInput === true;
        bleInput.set(bleInputOn);
        bleInputRow.setHint(bleInputOn ? I18n.t('On: the pointer and keys go over Bluetooth whenever the session has it, the rest over Wi-Fi. Needs it on both computers')
          : I18n.t('For crowded Wi-Fi where the pointer stutters. Off: everything goes over Wi-Fi'));
      }
      const k = s.kvm;
      if (!k) return;
      const [kind, text] = UI.kvmStatus(s);
      status.className = `note ${kind}`;
      status.set(text);
      lock.update(s);
      driven2.setHint(I18n.t('Double-tap the hotkey to take everything back'));
      drive.set(k.driveOther);
      driven.set(k.beDriven);
      resistance.set(k.resistancePx);
      crossingModifier.set(k.crossingModifier || '');
      stayFull.set(!!k.stayFullscreen);
      // Portals have their own keys: Hold key to switch steps aside while they're on.
      crossingModifier.el.disabled = !!k.portalsOn;
      crossingModifierRow.setHint(k.portalsOn ? I18n.t('Off while cursor portals are on: each portal has its own key to hold.') : I18n.t('Hold this key when this Mac’s own mouse or trackpad leaves it at an edge.'));
      if (portals) portals.update(s);
      emergencyKey.set((s.keys && s.keys.emergencyKeys) || 'Control+Alt+Command+Escape');
      speed.set(k.speed);
      reverse.set(k.reverseScroll);
      scrollSpeed.set(k.scrollSpeed || 1);
      gestures.set(k.gestures !== false);
      syncPads.set(k.syncPads === true);
      shake.set(k.shakeHome !== false);
      ble.set(k.ble !== false);
      viaRow.hidden = !UI.peer(s).phone;
      via.set(((s.phone || {}).bridge || {}).input || (((s.phone || {}).settings || {}).input === 'phone' ? 'phone' : 'adb'));
    };
  },
});