All files / src/desktop Overlay.tsx

97.14% Statements 68/70
93.87% Branches 46/49
100% Functions 20/20
100% Lines 58/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188                                                      1x 11x   1x 7x   1x 1x         29x 29x 29x 29x 29x 29x 29x       29x   29x 12x   12x 1x 1x       12x         9x 9x 9x 9x     3x 3x   12x 12x 12x 12x 12x 12x 12x       29x   29x               29x 29x           29x                   3x 2x 1x 1x       3x 2x 3x 3x     29x       3x 3x 3x                       1x 1x         7x 7x   6x   7x                                                               1x 1x  
import { invoke } from '@tauri-apps/api/core';
import { getCurrentWindow } from '@tauri-apps/api/window';
import {
  useEffect,
  useRef,
  useState,
  type PointerEvent as ReactPointerEvent,
} from 'react';
import { createRoot } from 'react-dom/client';
import type { Rect } from '../lib/Db';
import '../Styles.css';
 
interface PickableWindow {
  id: number;
  title: string;
  rect: Rect;
}
 
interface OverlayInfo {
  /** Monitor size in the units Rust expects back (points on macOS, pixels elsewhere). */
  width: number;
  height: number;
  windows: PickableWindow[];
}
 
type Point = { x: number; y: number };
 
const finish = (pick: { rect?: Rect; windowId?: number; error?: string }) =>
  invoke('finish_selection', pick);
 
const reportError = (error: unknown) =>
  finish({ error: error instanceof Error ? error.message : String(error) });
 
const inside = (r: Rect, p: Point) =>
  p.x >= r.x && p.y >= r.y && p.x < r.x + r.w && p.y < r.y + r.h;
 
/** Full-screen frozen frame of one monitor: drag a region, or click a window in window mode. */
export function Overlay() {
  const pickWindow =
    new URLSearchParams(location.search).get('kind') === 'window';
  const [src, setSrc] = useState<string>();
  const [info, setInfo] = useState<OverlayInfo>();
  const [start, setStart] = useState<Point | null>(null);
  const [end, setEnd] = useState<Point | null>(null);
  const [hover, setHover] = useState<PickableWindow>();
  const readyTimer = useRef<ReturnType<typeof setTimeout> | undefined>(
    undefined,
  );
  // Monitor units per CSS pixel.
  const k = info ? info.width / innerWidth : 1;
 
  useEffect(() => {
    let active = true;
    let url: string | undefined;
    readyTimer.current = setTimeout(() => {
      active = false;
      void reportError(
        'The selection overlay did not become ready. Please try again.',
      );
    }, 15_000);
    Promise.all([
      invoke<ArrayBuffer>('overlay_frame'),
      invoke<OverlayInfo>('overlay_info'),
    ])
      .then(([png, next]) => {
        Iif (!active) return;
        url = URL.createObjectURL(new Blob([png], { type: 'image/png' }));
        setSrc(url);
        setInfo(next);
      })
      .catch((error: unknown) => {
        clearTimeout(readyTimer.current);
        Eif (active) void reportError(error);
      });
    const onKey = (e: KeyboardEvent) => e.key === 'Escape' && void finish({});
    addEventListener('keydown', onKey);
    return () => {
      active = false;
      clearTimeout(readyTimer.current);
      if (url) URL.revokeObjectURL(url);
      removeEventListener('keydown', onKey);
    };
  }, []);
 
  const point = (e: ReactPointerEvent) => ({ x: e.clientX, y: e.clientY });
  const box =
    start && end
      ? {
          x: Math.min(start.x, end.x),
          y: Math.min(start.y, end.y),
          w: Math.abs(end.x - start.x),
          h: Math.abs(end.y - start.y),
        }
      : null;
  const dragging = !!box && (box.w > 3 || box.h > 3);
  const scaled = (r: Rect) => ({
    x: r.x * k,
    y: r.y * k,
    w: r.w * k,
    h: r.h * k,
  });
  const highlight = dragging
    ? box
    : hover && {
        x: hover.rect.x / k,
        y: hover.rect.y / k,
        w: hover.rect.w / k,
        h: hover.rect.h / k,
      };
 
  function onMove(e: ReactPointerEvent) {
    if (start) return setEnd(point(e));
    if (!pickWindow || !info) return;
    const p = { x: e.clientX * k, y: e.clientY * k };
    setHover(info.windows.find((w) => inside(w.rect, p)));
  }
 
  function onUp() {
    if (dragging && box) void finish({ rect: scaled(box) });
    else if (hover) void finish({ windowId: hover.id });
    setStart(null);
    setEnd(null);
  }
 
  return (
    <div
      className='overlay'
      onPointerDown={(e) => {
        e.currentTarget.setPointerCapture(e.pointerId);
        setStart(point(e));
        setEnd(point(e));
      }}
      onPointerMove={onMove}
      onPointerUp={onUp}
    >
      {src && (
        <img
          className='overlay-frame'
          src={src}
          alt=''
          draggable={false}
          onError={() => {
            clearTimeout(readyTimer.current);
            void reportError(
              'Could not load the screenshot. Please try again.',
            );
          }}
          onLoad={() => {
            const win = getCurrentWindow();
            void win
              .show()
              .then(() => win.setFocus())
              .catch(reportError)
              .finally(() => clearTimeout(readyTimer.current));
          }}
        />
      )}
      {highlight ? (
        <div
          className='overlay-box'
          style={{
            left: highlight.x,
            top: highlight.y,
            width: highlight.w,
            height: highlight.h,
          }}
        >
          <span className='overlay-size'>
            {dragging
              ? `${Math.round(highlight.w * k)} × ${Math.round(highlight.h * k)}`
              : hover?.title}
          </span>
        </div>
      ) : (
        <div className='overlay-shade' />
      )}
      <p className='overlay-hint' role='status'>
        {pickWindow
          ? 'Click a window or drag an area · Esc to cancel'
          : 'Drag to select an area · Esc to cancel'}
      </p>
    </div>
  );
}
 
const root = document.getElementById('root');
Iif (root) createRoot(root).render(<Overlay />);