All files / src/desktop CapturePage.tsx

100% Statements 26/26
100% Branches 32/32
100% Functions 16/16
100% Lines 22/22

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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231                        3x                                             3x 3x   3x 8x                                                               182x 182x 182x 117x     182x 117x       182x 723x               182x                                     728x       2x                           546x                             11x                                               3x                                                             8x     2x                             3x                            
import { useEffect, useId, useMemo } from 'react';
import type { Shot } from '../lib/Db';
import type { Action } from '../platform/Desktop';
import { Icon, MODE_LABEL, type IconName } from '../Ui';
import { formatShortcut, type Shortcuts } from './Shortcuts';
 
const MODES: {
  action: Action;
  label: string;
  description: string;
  waylandDescription: string;
  icon: IconName;
}[] = [
  {
    action: 'selection',
    label: 'Select area',
    description: 'Drag across any part of the screen.',
    waylandDescription: 'Desktop screenshot picker.',
    icon: 'selection',
  },
  {
    action: 'window',
    label: 'Window',
    description: 'Click a window to capture just that window.',
    waylandDescription: 'Not available in Wayland sessions.',
    icon: 'window',
  },
  {
    action: 'screen',
    label: 'Screen',
    description: 'The whole display under the pointer.',
    waylandDescription: 'The whole desktop.',
    icon: 'visible',
  },
];
const DELAYS = [0, 3, 5, 10];
const RECENT = 8;
 
const when = (ts: number) =>
  new Date(ts).toLocaleString(undefined, {
    month: 'short',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
  });
 
export function CapturePage({
  shortcuts,
  mac,
  wayland,
  delay,
  onDelay,
  onCapture,
  shots,
  onOpen,
  onCopy,
  onViewAll,
  previewMode,
}: {
  shortcuts: Shortcuts;
  mac: boolean;
  wayland: boolean;
  delay: number;
  onDelay: (seconds: number) => void;
  onCapture: (action: Action, delay: number) => void;
  shots: Shot[];
  onOpen: (id: string) => void;
  onCopy: (shot: Shot) => void;
  onViewAll: () => void;
  previewMode?: Action;
}) {
  const id = useId();
  const recent = useMemo(() => shots.slice(0, RECENT), [shots]);
  const thumbs = useMemo(
    () => new Map(recent.map((s) => [s.id, URL.createObjectURL(s.thumb)])),
    [recent],
  );
  useEffect(
    () => () => thumbs.forEach((url) => URL.revokeObjectURL(url)),
    [thumbs],
  );
 
  const kbd = (action: Action) =>
    !wayland && shortcuts[action] ? (
      <kbd>{formatShortcut(shortcuts[action], mac)}</kbd>
    ) : (
      <span className='meta'>
        {wayland ? 'Desktop settings' : 'No shortcut'}
      </span>
    );
 
  return (
    <div className='page'>
      <header className='page-head' data-tauri-drag-region>
        <div className='page-head-title'>
          <h1>Capture</h1>
          <p>Screenshots open in the editor and are kept in History.</p>
        </div>
        <div className='delay-field'>
          <span className='field-label' id={`${id}-delay-label`}>
            <Icon name='timer' size={15} />
            Delay
          </span>
          <div
            className='segmented'
            role='radiogroup'
            aria-labelledby={`${id}-delay-label`}
            aria-describedby={`${id}-delay-hint`}
          >
            {DELAYS.map((d) => (
              <button
                key={d}
                role='radio'
                aria-checked={delay === d}
                onClick={() => onDelay(d)}
              >
                {d ? `${d}s` : 'Off'}
              </button>
            ))}
          </div>
          <p className='field-hint' id={`${id}-delay-hint`}>
            For these buttons, so you can open menus first.
          </p>
        </div>
      </header>
 
      <div className='mode-grid' data-tour='capture-controls'>
        {MODES.map((m) => (
          <button
            key={m.action}
            type='button'
            className='mode-card'
            data-tour={`capture-${m.action}`}
            aria-pressed={previewMode ? previewMode === m.action : undefined}
            disabled={wayland && m.action === 'window'}
            aria-disabled={
              wayland && m.action === 'window' ? 'true' : undefined
            }
            title={
              wayland && m.action === 'window'
                ? 'Window picking requires X11.'
                : undefined
            }
            onClick={() => onCapture(m.action, delay)}
          >
            <span className='mode-icon'>
              <Icon name={m.icon} size={22} />
            </span>
            <span className='mode-label'>{m.label}</span>
            <span className='mode-desc'>
              {wayland ? m.waylandDescription : m.description}
            </span>
            <span className='mode-key'>
              {wayland && m.action === 'window'
                ? 'Requires X11'
                : kbd(m.action)}
            </span>
          </button>
        ))}
      </div>
 
      <button
        className='quick-action'
        data-tour='capture-copy-selection'
        aria-pressed={
          previewMode ? previewMode === 'copy-selection' : undefined
        }
        onClick={() => onCapture('copy-selection', delay)}
      >
        <span className='mode-icon'>
          <Icon name='copy' size={20} />
        </span>
        <span className='quick-text'>
          <span className='mode-label'>Copy selected area</span>
          <span className='mode-desc'>
            Straight to the clipboard, without the editor. Also kept in History.
          </span>
        </span>
        {kbd('copy-selection')}
      </button>
 
      <section aria-labelledby={`${id}-recent-title`}>
        <div className='section-head'>
          <h2 id={`${id}-recent-title`}>Recent</h2>
          {shots.length > 0 && (
            <button className='link' onClick={onViewAll}>
              View all {shots.length}
            </button>
          )}
        </div>
        {recent.length === 0 ? (
          <div className='recent-empty'>
            <Icon name='image' size={22} />
            <p>Your screenshots will appear here.</p>
          </div>
        ) : (
          <ul className='recent-shots'>
            {recent.map((s) => (
              <li key={s.id} className='shot'>
                <button
                  className='shot-open'
                  onClick={() => onOpen(s.id)}
                  aria-label={`Edit ${s.title}`}
                >
                  <img src={thumbs.get(s.id)} alt='' />
                </button>
                <div className='shot-meta'>
                  <span className='shot-title truncate' title={s.title}>
                    {s.title}
                  </span>
                  <span className='meta'>
                    {MODE_LABEL[s.mode]} ยท {when(s.createdAt)}
                  </span>
                </div>
                <button
                  className='icon-btn sm shot-copy'
                  onClick={() => onCopy(s)}
                  aria-label={`Copy ${s.title}`}
                  title='Copy image'
                >
                  <Icon name='copy' size={15} />
                </button>
              </li>
            ))}
          </ul>
        )}
      </section>
    </div>
  );
}