diff --git a/.gitignore b/.gitignore index 147f683..29ac437 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,9 @@ src-tauri/gen/ artifacts/ FileFlipper/ .DS_Store +scripts/windows-debug/ +/codesign* +*.dSYM/ +.env +.env.* +!.env.example diff --git a/README.md b/README.md index 25a4371..43805cb 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ Built on Tauri 2, Rust, React and TypeScript. Native capture and drag behaviour - [Compression, naming, cancellation](#compression-naming-cancellation) - [Capture & annotation](#capture--annotation) - [OCR](#ocr) +- [Send to a device](#send-to-a-device) - [Shortcuts](#shortcuts) - [Tray menu](#tray-menu) - [Output locations](#output-locations) @@ -36,9 +37,9 @@ Built on Tauri 2, Rust, React and TypeScript. Native capture and drag behaviour ## What it does -**The radial menu is the whole interface.** Hold `Shift` and drag a file in Finder (or `Ctrl + Shift` on Windows) and a ring opens under the cursor. Release over a format to convert, or add `Option`/`Alt` to switch to the tool ring. The main window accepts drops too, and its `Convert files` / `File tools` buttons open the same rings for files you pick yourself. +**The radial menu is the whole interface.** Hold `Shift` and drag a file in Finder (or `Shift + Alt` on Windows) and a ring opens under the cursor. Release over a format to convert, or add `Option`/`Alt` to switch to the tool ring. The main window accepts drops too, and its `Convert files` / `File tools` buttons open the same rings for files you pick yourself. -**Everything is type-aware.** The ring only offers what the selected file can actually become — an image offers PNG/JPG/WEBP/HEIC/TIFF/BMP/GIF/PDF, a PPTX offers PDF and Markdown, and a `.key` or `.pages` file offers no conversion wedges at all instead of failing later. The centre shows a glass preview with the file's dimensions and size; a multi-file selection renders a stacked icon deck. +**Everything is type-aware.** The ring only offers what the selected file can actually become, and never repeats the source format — an image can target PNG/JPG/WEBP/HEIC/TIFF/BMP/GIF/PDF except its current format, a PPTX offers PDF and Markdown, and a `.key` or `.pages` file offers no conversion wedges at all instead of failing later. The centre shows a glass preview with the file's dimensions and size; a multi-file selection renders a stacked icon deck. **Batch by default.** Select ten files, drag, and one gesture produces ten outputs. Results land next to each source with a descriptive suffix (`report (compressed).pdf`, `photo (rotated).png`) and never overwrite — a numeric suffix is appended on collision. Settings can collect every result into one folder instead. @@ -46,6 +47,8 @@ Built on Tauri 2, Rust, React and TypeScript. Native capture and drag behaviour **OCR runs on-device.** English and Simplified Chinese, via a bundled Tesseract WASM engine with the traineddata shipped inside the app. No network access at any point in the OCR path. +**Files can hop to another machine.** Every running Shift announces itself on the local network, so the recent list can hand a file to another computer the way AirDrop does — tap a device name, watch the progress bar, and the file lands in Shift's output folder on the other side. + ## Quick start Shift lives in the menu bar (macOS) and the notification area (Windows), and it also has a normal window. There is no required window to keep open — the rings work from anywhere. @@ -62,9 +65,9 @@ Shift lives in the menu bar (macOS) and the notification area (Windows), and it > 💡 Changed your mind? Release back near the centre (inside the 92 px inner radius) and nothing happens. > 💡 Near a screen edge the ring slides back on screen; the whole 760 px ring is clamped inside the monitor under the pointer. -**Use a tool (`Option + Shift`, or `Ctrl + Shift` on Windows).** Same gesture, and the wedges become tools for that file type instead of formats. While the ring is open, keep `Shift` held and press or release `Option`/`Ctrl` to flip between the format ring and the tool ring. +**Use a tool (`Option + Shift`, or `Alt + Shift` on Windows).** Same gesture, and the wedges become tools for that file type instead of formats. While the ring is open, keep `Shift` held and press or release `Option`/`Alt` to flip between the format ring and the tool ring. -**Prefer not to drag?** In the main window, **Convert files** and **File tools** open a picker and then show the same ring for what you chose; **OCR** picks an image and reads it. Dropping files straight onto the main window opens the action ring directly. +**Prefer not to drag?** In the main window, **Convert files** and **File tools** open a picker and then show the same ring for what you chose; **OCR** picks an image and reads it. Dropping files straight onto the main window opens the action ring directly. Drop them **onto a button** instead and that button lights up and runs its own action: **Convert files** opens the format ring, **File tools** the tool ring, **OCR** reads the image. **Several files at once.** Multi-select with `⌘`/`Ctrl`-click, then drag. Every file is processed. With 2+ images or 2+ PDFs selected, a **Merge** wedge appears that combines them into a single PDF. @@ -76,7 +79,7 @@ Recognition is by extension, against four tables in `src-tauri/src/files.rs`. Th | Kind | Extensions | | --- | --- | -| `image` | **`png` `jpg` `jpeg` `webp` `tiff` `tif` `bmp` `gif`** `heic` `heif` `avif` | +| `image` | **`png` `jpg` `jpeg` `webp` `tiff` `tif` `bmp` `gif` `heic` `heif` `avif`** | | `video` | **`mp4` `mov` `m4v` `avi` `mkv` `webm`** | | `audio` | **`mp3` `wav` `m4a` `aiff` `aif` `caf` `flac` `ogg`** | | `pdf` | **`pdf`** | @@ -87,37 +90,40 @@ Recognition is by extension, against four tables in `src-tauri/src/files.rs`. Th Decoding and encoding are not symmetric, and the app does not pretend otherwise: -- **Image decode** is native (Rust `image`) for PNG, JPEG, WebP, TIFF, BMP, GIF, and every tool-ring operation uses that decoder. HEIC/HEIF/AVIF are not in it, so those formats convert through the system path — `sips` on macOS, ImageMagick elsewhere — but rotate, flip, B&W, 50% and compress will not read them until they are converted first. -- **Image encode** for PNG, JPG, WebP, TIFF, BMP, GIF is native. HEIC export goes through `sips` on macOS and requires ImageMagick on Windows/Linux. AVIF reads fine and can be written by the same system path, but the ring does not offer it yet. +- **Image decode** is native (Rust `image`) for PNG, JPEG, WebP, TIFF, BMP and GIF. HEIC/HEIF/AVIF first decode through the system codec — `sips` on macOS, ImageMagick elsewhere — and can then convert to every offered image target, including WEBP, or to PDF. The pixel tools still need a natively readable input, so convert those formats first before rotate, flip, B&W, 50% or compress. +- **Image encode** for PNG, JPG, WebP, TIFF, BMP and GIF is native. HEIC export uses `sips` on macOS and requires ImageMagick on Windows/Linux. AVIF is accepted as an input but is not offered as an export target. - **Media** decode and encode are whatever your FFmpeg build supports; the menu offers MP4, MOV, GIF and M4A for video, and M4A, WAV and MP3 for audio. - **Office** files are parsed in pure Rust (`calamine` for spreadsheets, `zip` + `quick-xml` for PPTX), so no Office installation is needed. - **Recognised but not converted:** `.doc`, `.pages`, `.ppt`, `.key` and `.numbers` get their proper icon plus Edit, Open and Reveal, but no format wedges. Export them to DOCX, PPTX or XLSX first. ## Conversions -The format ring for a given file type. Every arrow below is wired in `src-tauri/src/files.rs`. +The format ring for a given file type. Every target below is wired in `src-tauri/src/files.rs`; the source's own format is filtered out at runtime. -| Source | Ring offers | Engine | Notes | +| Source | Ring targets (source format excluded) | Engine | Notes | | --- | --- | --- | --- | | Image | `PNG` `JPG` `WEBP` `TIFF` `BMP` `GIF` | Rust `image` | Lossless path when the codec allows it | | Image | `HEIC` | `sips` / ImageMagick | macOS native; other OS need `magick` | | Image | `PDF` | `printpdf` | One page per image, sized at 300 dpi | | Video | `MP4` `MOV` | FFmpeg | `libx264`, falling back to `h264_videotoolbox` then `mpeg4` | -| Video | `GIF` | FFmpeg | First 15 s only, 12 fps, 480 px wide — capped so a long recording cannot produce a 100 MB GIF | +| Video | `GIF` | FFmpeg | First 15 s only, 12 fps, 480 px wide, 8 MB ceiling — capped so a long recording cannot produce a 100 MB GIF | | Video | `M4A` | FFmpeg | Audio track only, 192k AAC | | Audio | `M4A` `WAV` `MP3` | FFmpeg | MP3 falls back to `lame` if FFmpeg lacks `libmp3lame` | | PDF | `TXT` `MD` | `lopdf` | Text-layer extraction; errors clearly when a PDF is scanned | -| PDF | `DOCX` `RTF` | `lopdf` + Pandoc | Text layer becomes the document body; needs Pandoc | +| PDF | `DOCX` | Page rendering + native DOCX packaging | PDFKit on macOS, Poppler on Windows/Linux; preserves images, diagrams and page layout without Microsoft Word | +| PDF | `RTF` `ODT` | `lopdf` + Pandoc | Text layer becomes the document body; needs Pandoc | | PDF | `HEIC` | PDFKit / Poppler + `sips` | Pages render as PNG then transcode; needs a system HEIC codec | | PDF | `PNG` `JPG` `TIFF` | PDFKit / Poppler | One file per page into a `… pages` folder | | Document `docx` `rtf` `odt` `txt` `md` `html` | `PDF` | AppKit renderer (macOS) · Pandoc elsewhere | macOS path needs no LaTeX or wkhtmltopdf | -| Document `docx` `rtf` `odt` `txt` `md` `html` | `DOCX` `RTF` `HTML` `TXT` `MD` `ODT` | Pandoc | Markdown writes as GFM | +| Document `docx` `rtf` `odt` `txt` `md` `html` | `DOCX` `RTF` `HTML` `TXT` `MD` `ODT` | Pandoc | The source format is omitted; Markdown writes as GFM, HTML and RTF as standalone documents | | Presentation `.pptx` | `MD` `PDF` | Pure Rust + Pandoc | Per-slide headings, bullet text and **tables as real Markdown tables**; PDF is a text summary, not a layout render | | Spreadsheet | `MD` `PDF` | `calamine` / Rust | One Markdown table per sheet; `csv`/`tsv` parsed directly | Anything not in this table is not offered — the ring is built from this catalogue at runtime, so a wedge you can click is a wedge that works. If a dependency is missing, the wedge names it instead of failing silently. -> Note: unlike some radial converters, Shift does **not** hide the file's own format. Dragging a PNG still shows PNG, which makes it a quick way to re-encode a file in place. +**Page progress follows one rule.** Conversions that can enumerate real pages report the same `Converting page N/M…` event. Image-only DOCX→PDF and image-bearing PDF→DOCX use that path. One-shot Word→PDF layout and text-only PDF→DOCX cannot expose a reliable page count, so they show `Processing…` rather than a fabricated percentage. + +> Note: the conversion ring never repeats the source format. For example, a DOCX offers no DOCX target, avoiding a needless full rewrite that can lose layout. > Scanned/image-only PDFs have no text layer, so `TXT`/`MD` return a clear error — use **OCR** on those, which renders each page first. ## Tools @@ -199,6 +205,20 @@ Tesseract.js with the LSTM core, running as WASM inside the app. `eng` and `chi_ - **Scanned PDFs** — pages are rendered to images first, then OCR'd sequentially with per-page progress, and the results are written to a ` (OCR)` text file with `--- Page N ---` separators. - **Text PDFs** — use Extract Text instead; it reads the embedded layer directly and is far faster. +## Send to a device + +Shift keeps a live list of the other Shift copies on your network and uses it for an AirDrop-style transfer. Hover any row in **Recent** and press the send button, or open a file's **Tools** ring and release over **Send** — either way a picker opens with one card per device, and clicking a card starts the transfer. The Tools ring offers Send for every file type, and selecting several files sends them as one batch. + +Nothing lands without consent: the receiving machine shows a small card naming the sender and the file, with **Accept** and **Decline** and a countdown. Declining, closing the card, or letting it expire stops the transfer before a single file byte moves. An accepted batch is asked once, then the remaining files of that batch follow on their own. Accepted files are written to Shift's output folder, appear in that machine's Recent list, and a note names the sender. Names that already exist get ` 2`, ` 3` appended, and a partial transfer never leaves a half-written file behind. + +How it finds peers, with no server and no configuration: + +- Every copy sends a small UDP beacon every two seconds to multicast group `239.255.77.88:47888`, plus a legacy broadcast copy for networks that drop multicast. Beacons carry the machine name, OS, and the ephemeral TCP port that will receive files. +- A beacon that has not been repeated for nine seconds drops the device from the list, so switching networks clears stale entries by itself. +- File bytes move over a direct TCP connection: a `SHSF` handshake, a JSON header, the payload, then a one-byte acknowledgement, so a sender only reports success once the receiver has the whole file on disk. + +Only the local network is involved — nothing reaches the internet. The receiver listens on all interfaces, so a macOS firewall prompt appears the first time Shift shares; allowing it enables both directions. + ## Shortcuts | Action | macOS | Windows / Linux | @@ -207,7 +227,7 @@ Tesseract.js with the LSTM core, running as WASM inside the app. `eng` and `chi_ | Record area | `⌥⇧3` | `Alt+Shift+3` | | Capture full screen | `⌥⇧4` | `Alt+Shift+4` | | Format ring while dragging | hold `⇧` | hold `Shift` | -| Tool ring while dragging | `⌥⇧` | `Ctrl+Shift` | +| Tool ring while dragging | `⌥⇧` | `Alt+Shift` | The three capture shortcuts are registered globally. The drag rings are modifier-driven: begin dragging with the modifier already held and the ring appears under the cursor without Shift needing focus. @@ -224,7 +244,7 @@ The menu bar (macOS) and notification area (Windows) icon exposes the actions yo | OCR an image… | — | Pick an image and read its text locally | | Quit Shift | `Cmd/Ctrl+Q` | Exit; an in-flight recording is finalised first | -Tray labels are English only for now; everything inside the windows follows the app language. +Tray labels follow the selected app language; native macOS permission prompts remain in English. Settings → **Behaviour** holds two switches: **Drag rings** (master enable for the Shift-drag gesture) and **Launch at login**. @@ -246,11 +266,17 @@ Core conversion, radial menu, capture, and OCR work with nothing installed. Thes | --- | --- | --- | --- | --- | | FFmpeg | media convert/compress, 720p, mute, frame, mono; recording off-macOS | optional | **required** for recording | **required** | | Pandoc | document ↔ DOCX/RTF/ODT/HTML/TXT/MD | optional | needed | needed | -| Poppler `pdftoppm` | PDF → images, scanned-PDF OCR | not needed (PDFKit) | needed | needed | -| ImageMagick `magick` | HEIC/AVIF encode | not needed (`sips`) | needed | needed | +| Typst | Pandoc document-to-PDF engine | not needed (AppKit) | needed | needed | +| Poppler `pdftoppm` | PDF → images, PDF → Word, scanned-PDF OCR | not needed (PDFKit) | needed | needed | +| ImageMagick `magick` | HEIC/HEIF/AVIF input plus HEIC output | not needed (`sips`) | needed | needed | | LAME | MP3 encode when FFmpeg lacks `libmp3lame` | rare | rare | rare | -Tools are found on `PATH` at call time and results are cached, so a missing dependency degrades one wedge rather than the app. +Settings -> **Runtime components** shows the tools found on the current machine, +their versions and paths. Missing Pandoc, Typst, FFmpeg, Poppler and ImageMagick can be +installed from the same panel through Homebrew on macOS or WinGet on Windows, +then detected again automatically. Tools are also found on `PATH` and their +standard install locations at call time, so a missing dependency degrades one +wedge rather than the app. ## Platform status @@ -265,6 +291,7 @@ Tools are found on `PATH` at call time and results are cached, so a missing depe | HEIC / AVIF encode | `sips` | ImageMagick | ImageMagick | | PDF text, merge, split, rotate, compress | Yes | Yes | Yes | | PDF → images | PDFKit sidecar (+ `sips` for HEIC) | Poppler (+ ImageMagick for HEIC) | Poppler (+ ImageMagick for HEIC) | +| PDF → Word | Built-in page renderer | Poppler required | Poppler required | | Scanned-PDF OCR | Yes | Poppler required | Poppler required | | Document → PDF | bundled AppKit renderer | Pandoc + a PDF engine | Pandoc + a PDF engine | | PPTX / XLSX / ODS → Markdown | Yes (pure Rust) | Yes | Yes | @@ -284,6 +311,7 @@ pnpm tauri dev ```bash pnpm check # TypeScript, no emit +pnpm test # conversion matrix and drag-target tests pnpm build # tsc + vite cargo test --lib # Rust unit tests, from src-tauri/ pnpm tauri build # release bundle @@ -294,17 +322,19 @@ Universal macOS build: ```bash rustup target add aarch64-apple-darwin x86_64-apple-darwin pnpm mac:drag-bridge -RUSTUP_TOOLCHAIN=stable pnpm tauri build --target universal-apple-darwin --bundles app +PATH="$HOME/.rustup/toolchains/stable-aarch64-apple-darwin/bin:$PATH" \ + RUSTUP_TOOLCHAIN=stable \ + pnpm tauri build --target universal-apple-darwin --bundles app,dmg ``` -The result is one universal Mach-O including universal drag, PDF-render and doc-render sidecars. Windows build steps are in `docs/BUILD_WINDOWS.md`. +The result is one universal Mach-O including universal drag, PDF-render and doc-render sidecars. To produce the Windows installer and portable ZIP from macOS, run `scripts/build-windows-release.sh`; full details are in `docs/BUILD_WINDOWS.md`. **Permissions (macOS).** Screen Recording for capture, Files & Folders for the output folders you choose, Microphone when recording with audio. Shift checks Screen Recording before opening the overlay, so a denial surfaces an actionable error rather than a black screenshot. Dev builds signed with a local certificate keep those grants stable across rebuilds; distribution needs Developer ID plus notarization. ## Architecture ```text -React UI one webview, five views +React UI one webview, six views ├─ main drop target, shortcuts, recent outputs, language, settings ├─ radial transparent always-on-top ring, hit-tested by polar sector ├─ capture frozen still + area selection @@ -320,6 +350,7 @@ Tauri command layer src-tauri/src/ ├─ recording.rs screencapture / FFmpeg, with interrupt-based finalization ├─ drag_bridge.rs global drag detection, ring mode, radial positioning ├─ settings.rs output directories, write checks, privacy deep links + ├─ share.rs LAN beacons, peer list, AirDrop-style file transfer └─ editor.rs editor and pin window state Platform adapters @@ -336,10 +367,11 @@ The radial hit-test is pure geometry — one polar calculation shared by native ```text src/ React UI, i18n (en/zh), Tauri API bindings +src/lib/ pure helpers and Node tests, including the conversion matrix src-tauri/src/ Rust command layer and platform adapters native/macos/ Swift sidecars: drag bridge, PDF renderer, doc renderer, compiled into src-tauri/binaries/ by pnpm mac:drag-bridge -scripts/ OCR asset staging, macOS sidecar build +scripts/ OCR staging, macOS sidecars, Windows installer and release builds public/tesseract/ WASM core + traineddata, generated, not committed assets/ app icon source src-tauri/icons/ generated app icons per platform @@ -350,7 +382,9 @@ FileFlipper/ separate companion project, its own repository ## Privacy -Shift makes no network requests. Files are read, converted, and written locally; OCR runs in-process against bundled models; recent outputs are a local list. Nothing is uploaded, and nothing is collected. +Shift makes no internet requests. Files are read, converted, and written locally; OCR runs in-process against bundled models; recent outputs are a local list. Nothing is uploaded, and nothing is collected. + +The one exception is opt-in by usage: **Send to a device** talks to your local network only — UDP beacons on `239.255.77.88:47888` plus a direct TCP connection between the two machines, carrying the machine name so each side can label the other. It is inert until you open that picker or another Shift sends you a file. ## Known limitations @@ -360,6 +394,8 @@ Shift makes no network requests. Files are read, converted, and written locally; - HEIC/HEIF/AVIF convert through the system decoder, but the pixel tools need a PNG/JPEG/WebP/TIFF/BMP/GIF input; AVIF is not yet an export target. - PDF compression resamples embedded images to at most 1440 px as q60 JPEG. It deliberately skips anything it cannot prove it can round-trip — masks, indexed or CMYK colour, JPX/JBIG2/CCITT — so exotic scans may still resist shrinking. - Windows recording and media conversion require `ffmpeg` on `PATH`. +- LAN sharing needs a network that allows peer traffic. Multicast is skipped by some guest and corporate Wi-Fi; those networks still work over the broadcast and unicast fallbacks, but two copies cannot meet across a router that isolates clients. +- On Windows only one Shift copy per machine can hold the discovery port, so extra instances do not appear as peers. - Windows paths build and pass CI, but gesture-level QA needs real Windows hardware. - Signing identity changes on macOS may require re-approving Screen Recording and Accessibility. @@ -368,7 +404,7 @@ Roadmap: `docs/ROADMAP.md`. ## FAQ **❓ I hold Shift while dragging and no ring appears.** -Start the drag first — mouse button held, file moving past the drag threshold — and hold `Shift` during it. Pressing `Shift` with a file merely selected does nothing. On Windows the ring needs `Shift` held at the moment the drag crosses the system threshold; add `Ctrl` at that moment for tools. +Start the drag first — mouse button held, file moving past the drag threshold — and hold `Shift` during it. Pressing `Shift` with a file merely selected does nothing. On Windows the ring needs `Shift` held at the moment the drag crosses the system threshold; add `Alt` at that moment for tools. **❓ It says this file type has no formats.** Unrecognised extensions get Open and Reveal only, and legacy Apple/Office formats (`.doc`, `.pages`, `.ppt`, `.key`, `.numbers`) intentionally offer no conversion. Export them to DOCX, PPTX or XLSX first. @@ -376,7 +412,7 @@ Unrecognised extensions get Open and Reveal only, and legacy Apple/Office format **❓ Can I make it run at login, or toggle it off?** Yes, both, in Settings → **Behaviour**. **Drag rings** is the master switch for the Shift-drag gesture: off means no ring ever appears, while the tray, window and shortcuts keep working. **Launch at login** registers a LaunchAgent on macOS, a `HKCU\...\Run` entry on Windows, or an autostart `.desktop` file on Linux, and is reversible from the same toggle. -The login item is registered for the current user only and points at the app bundle, so macOS keeps the same Screen Recording identity across launches. Move the app and re-toggle it once. +The login item is registered for the current user only and points at the executable inside the app bundle, so macOS keeps the same Screen Recording identity across launches. Move the app and re-toggle it once. **❓ Are my files uploaded?** No. There is no upload path in the code and no network call in the conversion or OCR paths; the Tesseract models ship inside the app. Nothing is collected, and there is no account. @@ -385,7 +421,7 @@ No. There is no upload path in the code and no network call in the conversion or Next to the source file by default, with a suffix — check the recent list in the main window first, since clicking an entry opens the result. Settings lists every resolved output path and can reveal it. **❓ Which languages are supported?** -English and Simplified Chinese, switchable in the main window's Settings. On first launch it follows the system language. The tray menu and native macOS permission prompts stay in English. +English and Simplified Chinese, switchable in the main window's Settings. On first launch it follows the system language. The tray menu follows the selected language; native macOS permission prompts stay in English. **❓ Can I edit a screenshot without saving it?** Yes — annotation works on a canvas copy and only writes on **Save copy** or **Copy**. The source file is never modified. Use **Pin** to keep it floating on screen as a reference instead. @@ -395,7 +431,7 @@ Quit from the tray and delete the app. Nothing is written outside your output fo ## Contributing -Adding a format is one entry in `formatSetFor` in `src/components/RadialMenu.tsx` plus its handling in `perform_action` in `src-tauri/src/files.rs`; adding a tool is one entry in `toolWedges` plus one match arm. CI runs `pnpm check`, `pnpm build`, `cargo test --lib`, and a debug bundle on macOS and Windows. +Adding a format is one entry in `conversionFormats` in `src/lib/conversionFormats.ts`, its matrix test, and its handling in `perform_action` in `src-tauri/src/files.rs`; adding a tool is one entry in `toolWedges` plus one match arm. Run `pnpm check`, `pnpm test`, `pnpm build` and `cargo test --lib` before pushing. CI builds debug bundles on macOS and Windows. **Forks and derivative works are welcome.** You may use, modify, redistribute, and build on this code commercially, provided you keep the copyright notice and the LICENSE file. diff --git a/README.zh-CN.md b/README.zh-CN.md index 923c9a1..90efa2f 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -18,6 +18,7 @@ Shift 把文件格式转换、截图、录屏、标注、中英双语 OCR 全部 - [压缩、命名与取消](#压缩命名与取消) - [截图与标注](#截图与标注) - [OCR](#ocr) +- [发送到设备](#发送到设备) - [快捷键](#快捷键) - [菜单栏菜单](#菜单栏菜单) - [输出位置](#输出位置) @@ -36,9 +37,9 @@ Shift 把文件格式转换、截图、录屏、标注、中英双语 OCR 全部 ## 能力概览 -**环形菜单就是全部交互。** 在访达中按住 `Shift` 拖动文件(Windows 为 `Ctrl + Shift`),圆环会出现在光标下方。松开在某个格式上即完成转换;再加上 `Option`/`Alt` 可切换到工具环。主窗口同样接受拖放,其「文件转换」与「文件工具」按钮会先让你挑选文件,再弹出同一个圆环。 +**环形菜单就是全部交互。** 在访达中按住 `Shift` 拖动文件(Windows 为 `Shift + Alt`),圆环会出现在光标下方。松开在某个格式上即完成转换;再加上 `Option`/`Alt` 可切换到工具环。主窗口同样接受拖放,其「文件转换」与「文件工具」按钮会先让你挑选文件,再弹出同一个圆环。 -**一切按类型驱动。** 圆环只列出所选文件真正能变成的格式——图片会给出 PNG/JPG/WEBP/HEIC/TIFF/BMP/GIF/PDF,PPTX 给出 PDF 与 Markdown,而 `.key`、`.pages` 文件不会给出任何转换扇区,而不是等你点击后才失败。中心是毛玻璃预览,显示尺寸与体积;多选文件会渲染成一叠层叠图标。 +**一切按类型驱动。** 圆环只列出所选文件真正能变成的格式,并且不会重复显示文件自身的格式——图片可在 PNG/JPG/WEBP/HEIC/TIFF/BMP/GIF/PDF 之间转换,但当前格式不会出现目标;PPTX 给出 PDF 与 Markdown,而 `.key`、`.pages` 文件不会给出任何转换扇区,而不是等你点击后才失败。中心是毛玻璃预览,显示尺寸与体积;多选文件会渲染成一叠层叠图标。 **默认批量处理。** 选中十个文件、一次拖拽,就产出十个结果。结果保存在源文件旁边并附带描述性后缀(`report (compressed).pdf`、`photo (rotated).png`),永不覆盖——重名时自动追加序号。也可在设置中改为统一收集到一个文件夹。 @@ -46,6 +47,8 @@ Shift 把文件格式转换、截图、录屏、标注、中英双语 OCR 全部 **OCR 在本机运行。** 英文与简体中文,使用随应用打包的 Tesseract WASM 引擎与语言模型。OCR 全流程不发生任何网络请求。 +**文件可以送到另一台电脑。** 每个运行中的 Shift 都会在局域网里广播自己的存在,因此「最近使用」列表可以像 AirDrop 那样把文件交给另一台电脑——点一下设备名,看着进度条走完,文件就出现在对方电脑的 Shift 输出文件夹里。 + ## 快速上手 Shift 常驻菜单栏(macOS)与通知区域(Windows),同时也有普通窗口。无需一直开着某个窗口——圆环在任何地方都能唤起。 @@ -62,9 +65,9 @@ Shift 常驻菜单栏(macOS)与通知区域(Windows),同时也有普 > 💡 改变主意了?把鼠标移回中心(内圈 92 px 之内)再松开,什么都不会发生。 > 💡 靠近屏幕边缘时圆环会自动移回屏幕内;整个 760 px 的圆环会被约束在光标所在显示器范围内。 -**使用工具(`Option + Shift`,Windows 为 `Ctrl + Shift`)** 操作相同,扇区会从格式换成该类型文件可用的工具。圆环打开时,保持按住 `Shift`,再按下或松开 `Option`/`Ctrl`,即可在格式环与工具环之间来回切换。 +**使用工具(`Option + Shift`,Windows 为 `Alt + Shift`)** 操作相同,扇区会从格式换成该类型文件可用的工具。圆环打开时,保持按住 `Shift`,再按下或松开 `Option`/`Alt`,即可在格式环与工具环之间来回切换。 -**不想拖动?** 主窗口中的「文件转换」与「文件工具」会先让你挑选文件,然后对选中的文件弹出同一个圆环;「文字识别」则选取图片并识别。直接把文件拖到主窗口上,会直接打开操作环。 +**不想拖动?** 主窗口中的「文件转换」与「文件工具」会先让你挑选文件,然后对选中的文件弹出同一个圆环;「文字识别」则选取图片并识别。直接把文件拖到主窗口上,会直接打开操作环。**拖到按钮上**则按钮会高亮并只执行它自己的操作:「文件转换」打开格式环,「文件工具」打开工具环,「文字识别」直接识别图片。 **一次处理多个文件。** 用 `⌘`/`Ctrl` 点选多个文件后一起拖动,每个文件都会被处理。选中 2 张以上图片或 2 个以上 PDF 时,会出现「合并」扇区,把它们合成一个 PDF。 @@ -76,7 +79,7 @@ Shift 常驻菜单栏(macOS)与通知区域(Windows),同时也有普 | 类型 | 扩展名 | | --- | --- | -| `image` | **`png` `jpg` `jpeg` `webp` `tiff` `tif` `bmp` `gif`** `heic` `heif` `avif` | +| `image` | **`png` `jpg` `jpeg` `webp` `tiff` `tif` `bmp` `gif` `heic` `heif` `avif`** | | `video` | **`mp4` `mov` `m4v` `avi` `mkv` `webm`** | | `audio` | **`mp3` `wav` `m4a` `aiff` `aif` `caf` `flac` `ogg`** | | `pdf` | **`pdf`** | @@ -87,37 +90,40 @@ Shift 常驻菜单栏(macOS)与通知区域(Windows),同时也有普 解码与编码能力并不对称,应用也不会假装对称: -- **图片解码**由 Rust `image` 原生完成,支持 PNG、JPEG、WebP、TIFF、BMP、GIF,工具环的每个操作都走这个解码器。HEIC/HEIF/AVIF 不在其中,它们的转换走系统通道——macOS 用 `sips`,其他系统用 ImageMagick——因此旋转、翻转、黑白、50%、压缩在处理这些格式前需要先转一次。 -- **图片编码**中的 PNG、JPG、WebP、TIFF、BMP、GIF 为原生实现。HEIC 导出在 macOS 走 `sips`,在 Windows/Linux 需要 ImageMagick。AVIF 可以正常读取,同一套系统通道也能写出,但圆环暂未提供该选项。 +- **图片解码**由 Rust `image` 原生完成,支持 PNG、JPEG、WebP、TIFF、BMP 与 GIF。HEIC/HEIF/AVIF 先走系统解码器——macOS 用 `sips`,其他系统用 ImageMagick——随后可转换为圆环提供的任意图片格式(包括 WEBP),也可生成 PDF。像素类工具仍需要可原生读取的输入,因此旋转、翻转、黑白、50%、压缩前请先转一次。 +- **图片编码**中的 PNG、JPG、WebP、TIFF、BMP、GIF 为原生实现。HEIC 导出在 macOS 走 `sips`,在 Windows/Linux 需要 ImageMagick。AVIF 可作为输入,但暂未提供为导出目标。 - **音视频**的解码与编码范围取决于你的 FFmpeg 构建;菜单为视频提供 MP4、MOV、GIF、M4A,为音频提供 M4A、WAV、MP3。 - **Office 文件**以纯 Rust 解析(表格用 `calamine`,PPTX 用 `zip` + `quick-xml`),因此无需安装 Office。 - **可识别但不转换:** `.doc`、`.pages`、`.ppt`、`.key`、`.numbers` 会显示各自类型的图标,并提供编辑、打开、在文件夹中显示,但没有格式扇区。请先导出为 DOCX、PPTX 或 XLSX。 ## 格式转换 -以下是各文件类型在格式环中的选项。每一条转换路径都实现在 `src-tauri/src/files.rs`。 +以下是各文件类型在格式环中的选项。每一条转换路径都实现在 `src-tauri/src/files.rs`;文件自身的格式会在运行时自动排除。 -| 源类型 | 圆环提供 | 引擎 | 说明 | +| 源类型 | 圆环目标(不含源格式) | 引擎 | 说明 | | --- | --- | --- | --- | | 图片 | `PNG` `JPG` `WEBP` `TIFF` `BMP` `GIF` | Rust `image` | 编码器允许时走无损路径 | | 图片 | `HEIC` | `sips` / ImageMagick | macOS 原生;其他系统需要 `magick` | | 图片 | `PDF` | `printpdf` | 每张图一页,按 300 dpi 定尺寸 | | 视频 | `MP4` `MOV` | FFmpeg | 优先 `libx264`,依次回退 `h264_videotoolbox`、`mpeg4` | -| 视频 | `GIF` | FFmpeg | 仅取前 15 秒、12 fps、宽 480 px——设有上限,避免长时间录屏产出上百 MB 的 GIF | +| 视频 | `GIF` | FFmpeg | 仅取前 15 秒、12 fps、宽 480 px、8 MB——设有上限,避免长时间录屏产出上百 MB 的 GIF | | 视频 | `M4A` | FFmpeg | 仅保留音频轨,192k AAC | | 音频 | `M4A` `WAV` `MP3` | FFmpeg | 若 FFmpeg 缺少 `libmp3lame`,MP3 回退到 `lame` | | PDF | `TXT` `MD` | `lopdf` | 抽取文字层;扫描件会明确报错并引导使用文字识别 | -| PDF | `DOCX` `RTF` | `lopdf` + Pandoc | 以文字层作为正文;需要 Pandoc | +| PDF | `DOCX` | 页面渲染 + 原生 DOCX 封装 | macOS 用 PDFKit,Windows/Linux 用 Poppler;保留图片、图表和页面版式,不依赖 Microsoft Word | +| PDF | `RTF` `ODT` | `lopdf` + Pandoc | 以文字层作为正文;需要 Pandoc | | PDF | `HEIC` | PDFKit / Poppler + `sips` | 先渲染为 PNG 再转码;需要系统 HEIC 编码器 | | PDF | `PNG` `JPG` `TIFF` | PDFKit / Poppler | 每页一个文件,输出到 `… pages` 文件夹 | | 文档 `docx` `rtf` `odt` `txt` `md` `html` | `PDF` | AppKit 渲染器(macOS)· 其他系统 Pandoc | macOS 路径不需要 LaTeX 或 wkhtmltopdf | -| 文档 `docx` `rtf` `odt` `txt` `md` `html` | `DOCX` `RTF` `HTML` `TXT` `MD` `ODT` | Pandoc | Markdown 以 GFM 写出 | +| 文档 `docx` `rtf` `odt` `txt` `md` `html` | `DOCX` `RTF` `HTML` `TXT` `MD` `ODT` | Pandoc | 自动省略源格式;Markdown 以 GFM 写出,HTML 与 RTF 写出完整独立文档 | | 演示文稿 `.pptx` | `MD` `PDF` | 纯 Rust + Pandoc | 按页生成标题、要点文字,**表格会输出为真正的 Markdown 表格**;PDF 仍是文字摘要版,非版面还原 | | 电子表格 | `MD` `PDF` | `calamine` / 纯 Rust | 每个工作表一张 Markdown 表;`csv`/`tsv` 直接解析 | 表中没有的转换不会出现在界面上——圆环在运行时按这份目录生成,因此看得见的扇区就是能用的扇区。若缺少依赖,扇区会直接告诉你需要安装什么,而不是静默失败。 -> 说明:与部分环形转换工具不同,Shift **不会**隐藏文件自身的格式。拖动 PNG 时仍会出现 PNG,这也是一键重编码、统一画质的好办法。 +**页码进度遵循统一规则。** 能真实逐页处理的转换统一显示「正在转换第 N/M 页…」;当前包括图片型 DOCX→PDF 与带图 PDF→DOCX。一次性排版的文本 DOCX→PDF、以及纯文字 PDF→DOCX 无法可靠获得页数,因此显示「处理中…」,不会伪造页码。 + +> 说明:转换环不会重复列出文件自身的格式。例如 DOCX 不会出现 DOCX,避免无意义的整篇重写和版式损失。 > 扫描件/纯图片 PDF 没有文字层,选 `TXT`/`MD` 会返回明确报错——请改用「文字识别」,它会先把每页渲染成图片。 ## 工具 @@ -201,6 +207,20 @@ PDF 转图片与 PDF 拆分会新建 `名称 pages` 文件夹,内含 `Page 001 - **扫描版 PDF**——先把各页渲染成图片,再逐页识别并显示每页进度,结果写入 ` (OCR)` 文本文件,页与页之间用 `--- Page N ---` 分隔。 - **含文字层的 PDF**——请改用「提取文字」,它直接读取内嵌文字层,速度远快于 OCR。 +## 发送到设备 + +Shift 会持续维护局域网内其他 Shift 实例的列表,并据此提供 AirDrop 式传输。把鼠标移到「最近使用」的任意一行点击发送按钮,或打开文件的「文件工具」环并松开在「发送」扇区上——两种入口都会弹出每台设备一张卡片的设备选择面板,点击卡片即刻开始传输。工具环对每一种文件类型都提供「发送」,选中多个文件时按一批一起发送。 + +没有对方同意,文件不会落地:接收方会弹出一张小卡片,写明发送者与文件名,并提供「接收」「拒绝」两个按钮以及倒计时。点击拒绝、关掉卡片或任其超时,都会在文件字节开始传输之前就中止本次分享。多文件批次只会询问一次,同一批的其余文件随后自动接收。接收后的文件写入对方 Shift 的输出文件夹,出现在对方的「最近使用」列表里,同时弹出提示告知是谁发来的。同名文件自动追加 ` 2`、` 3`,传输中断不会留下写了一半的文件。 + +无需服务器、无需配置,发现机制如下: + +- 每个实例每两秒向组播组 `239.255.77.88:47888` 发送一次小型 UDP 广播,并额外发送一份传统广播,以兼容丢弃组播的网络。广播内容包含机器名、操作系统,以及用于接收文件的动态 TCP 端口。 +- 九秒内没有再次收到广播,设备就会从列表移除,因此切换网络后过期条目会自动消失。 +- 文件字节通过直连 TCP 传输:`SHSF` 握手、JSON 头、文件内容,最后一字节确认;只有接收方已完整落盘,发送方才会报告成功。 + +整个过程只涉及本地网络——不会有任何数据走到互联网。接收端监听全部网卡,因此 macOS 首次分享时会弹出防火墙授权提示,允许后双向均可用。 + ## 快捷键 | 操作 | macOS | Windows / Linux | @@ -209,7 +229,7 @@ PDF 转图片与 PDF 拆分会新建 `名称 pages` 文件夹,内含 `Page 001 | 区域录屏 | `⌥⇧3` | `Alt+Shift+3` | | 全屏截图 | `⌥⇧4` | `Alt+Shift+4` | | 拖拽时显示格式环 | 按住 `⇧` | 按住 `Shift` | -| 拖拽时显示工具环 | `⌥⇧` | `Ctrl+Shift` | +| 拖拽时显示工具环 | `⌥⇧` | `Alt+Shift` | 三个截图快捷键注册为全局快捷键。拖拽圆环由修饰键驱动:在开始拖动之前就按住修饰键,圆环会直接出现在光标处,无需先让 Shift 获得焦点。 @@ -226,7 +246,7 @@ PDF 转图片与 PDF 拆分会新建 `名称 pages` 文件夹,内含 `Page 001 | OCR an image… | — | 选择图片并在本机识别文字 | | Quit Shift | `Cmd/Ctrl+Q` | 退出;若有正在进行的录屏会先收尾保存 | -菜单项文案目前仅有英文;窗口内的界面会跟随应用语言。 +菜单项文案会跟随应用语言;macOS 系统权限弹窗仍为英文。 设置面板的「行为」区有两个开关:**拖拽圆环**(Shift 拖拽手势的总开关)与**开机时启动**。 @@ -248,11 +268,12 @@ PDF 转图片与 PDF 拆分会新建 `名称 pages` 文件夹,内含 `Page 001 | --- | --- | --- | --- | --- | | FFmpeg | 音视频转换/压缩、720p、静音、截帧、单声道;非 macOS 的录屏 | 可选 | 录屏**必需** | **必需** | | Pandoc | 文档 ↔ DOCX/RTF/ODT/HTML/TXT/MD | 可选 | 需要 | 需要 | -| Poppler `pdftoppm` | PDF 转图片、扫描件 OCR | 不需要(PDFKit) | 需要 | 需要 | -| ImageMagick `magick` | HEIC/AVIF 编码 | 不需要(`sips`) | 需要 | 需要 | +| Typst | Pandoc 文档转 PDF 引擎 | 不需要(AppKit) | 需要 | 需要 | +| Poppler `pdftoppm` | PDF 转图片、PDF 转 Word、扫描件 OCR | 不需要(PDFKit) | 需要 | 需要 | +| ImageMagick `magick` | HEIC/HEIF/AVIF 输入与 HEIC 输出 | 不需要(`sips`) | 需要 | 需要 | | LAME | 当 FFmpeg 缺少 `libmp3lame` 时的 MP3 编码 | 少见 | 少见 | 少见 | -这些工具在调用时从 `PATH` 查找并缓存结果,因此缺失某个依赖只会影响对应的扇区,不会影响整个应用。 +Shift 会在设置 →「运行组件」中显示当前机器实际找到的工具、版本和路径,并可一键安装缺失的 Pandoc、Typst、FFmpeg、Poppler 与 ImageMagick。macOS 使用 Homebrew,Windows 使用 WinGet;安装后在界面中自动重新检测,无需重启。工具在调用时也会从 `PATH` 及系统常见安装目录查找,因此缺失某个依赖只会影响对应的扇区,不会影响整个应用。 ## 平台支持情况 @@ -267,6 +288,7 @@ PDF 转图片与 PDF 拆分会新建 `名称 pages` 文件夹,内含 `Page 001 | HEIC / AVIF 编码 | `sips` | ImageMagick | ImageMagick | | PDF 文字、合并、拆分、旋转、压缩 | 支持 | 支持 | 支持 | | PDF 转图片 | PDFKit 伴生进程(HEIC 需 `sips`) | Poppler(HEIC 需 ImageMagick) | Poppler(HEIC 需 ImageMagick) | +| PDF 转 Word | 内置页面渲染器 | 需 Poppler | 需 Poppler | | 扫描件文字识别 | 支持 | 需 Poppler | 需 Poppler | | 文档转 PDF | 内置 AppKit 渲染器 | Pandoc + PDF 引擎 | Pandoc + PDF 引擎 | | PPTX / XLSX / ODS 转 Markdown | 支持(纯 Rust) | 支持 | 支持 | @@ -286,6 +308,7 @@ pnpm tauri dev ```bash pnpm check # TypeScript 类型检查,不产出文件 +pnpm test # 转换矩阵与拖拽命中测试 pnpm build # tsc + vite cargo test --lib # Rust 单元测试,在 src-tauri/ 目录下执行 pnpm tauri build # 发布构建 @@ -296,17 +319,19 @@ macOS 通用(Universal)构建: ```bash rustup target add aarch64-apple-darwin x86_64-apple-darwin pnpm mac:drag-bridge -RUSTUP_TOOLCHAIN=stable pnpm tauri build --target universal-apple-darwin --bundles app +PATH="$HOME/.rustup/toolchains/stable-aarch64-apple-darwin/bin:$PATH" \ + RUSTUP_TOOLCHAIN=stable \ + pnpm tauri build --target universal-apple-darwin --bundles app,dmg ``` -产物是单个通用 Mach-O,内含通用版的拖拽、PDF 渲染与文档渲染伴生进程。Windows 构建步骤见 `docs/BUILD_WINDOWS.md`。 +产物是单个通用 Mach-O,内含通用版的拖拽、PDF 渲染与文档渲染伴生进程。若要在 macOS 上同时生成 Windows 安装版与便携版,运行 `scripts/build-windows-release.sh`;完整步骤见 `docs/BUILD_WINDOWS.md`。 **权限(macOS)。** 截图与录屏需要「屏幕录制」,输出目录需要「文件与文件夹」授权,带声音录屏需要「麦克风」。Shift 会在打开截图遮罩前先检查屏幕录制权限,因此被拒绝时给出的是可操作的提示,而不是一张全黑的截图。使用本地证书签名的开发构建,可以在反复重编译后保持这些授权稳定;对外分发则需要 Developer ID 证书与公证。 ## 架构 ```text -React UI 单一 webview,五种视图 +React UI 单一 webview,六种视图 ├─ main 拖放目标、快捷键、最近输出、语言、设置 ├─ radial 透明置顶圆环,按极角扇区命中判定 ├─ capture 冻结画面 + 区域框选 @@ -322,6 +347,7 @@ Tauri 命令层 src-tauri/src/ ├─ recording.rs screencapture / FFmpeg,以中断方式收尾 ├─ drag_bridge.rs 全局拖拽检测、圆环模式、环形菜单定位 ├─ settings.rs 输出目录、可写检测、隐私面板深链 + ├─ share.rs 局域网广播、设备列表、AirDrop 式文件传输 └─ editor.rs 编辑器与钉图窗口状态 平台适配器 @@ -338,10 +364,11 @@ OCR:Tesseract.js WASM + 内置 eng/chi_sim,进程内运行,完全离线 ```text src/ React 界面、多语言(en/zh)、Tauri API 绑定 +src/lib/ 纯函数与 Node 测试,包括格式转换矩阵 src-tauri/src/ Rust 命令层与平台适配器 native/macos/ Swift 伴生进程:拖拽桥、PDF 渲染器、文档渲染器 由 pnpm mac:drag-bridge 编译进 src-tauri/binaries/ -scripts/ OCR 资源准备、macOS 伴生进程构建 +scripts/ OCR 资源准备、macOS 伴生进程、Windows 安装器与发布构建 public/tesseract/ WASM 内核与语言模型,构建生成,不提交 assets/ 应用图标源文件 src-tauri/icons/ 各平台生成的应用图标 @@ -352,7 +379,9 @@ FileFlipper/ 独立姊妹项目,拥有自己的仓库 ## 隐私 -Shift 不发起任何网络请求。文件在本地读取、转换、写回;OCR 在进程内基于内置模型运行;最近输出只是本地列表。既不上传,也不收集。 +Shift 不发起任何互联网请求。文件在本地读取、转换、写回;OCR 在进程内基于内置模型运行;最近输出只是本地列表。既不上传,也不收集。 + +唯一的例外取决于你是否使用:**发送到设备** 只与本地网络通信——`239.255.77.88:47888` 上的 UDP 广播,加上两台电脑之间的直连 TCP,其中携带机器名称以便双方互相标注。在你打开该面板、或有另一台 Shift 向你发送文件之前,它不会有任何动作。 ## 已知限制 @@ -362,6 +391,8 @@ Shift 不发起任何网络请求。文件在本地读取、转换、写回;OC - HEIC/HEIF/AVIF 通过系统解码器转换,但像素类工具需要 PNG/JPEG/WebP/TIFF/BMP/GIF 输入;AVIF 暂未提供为导出目标。 - PDF 压缩会把内嵌图片降采样到最长边 1440 px、q60 JPEG。凡是不能确认可安全往返的内容都会原样保留——蒙版、索引色/CMYK、JPX/JBIG2/CCITT——因此部分特殊扫描件仍难以压缩。 - Windows 的录屏与音视频转换要求 `PATH` 中存在 `ffmpeg`。 +- 局域网分享依赖允许点对点流量的网络。部分访客网络与企业 Wi-Fi 会丢弃组播;这些网络仍可经由广播与单播回退路径工作,但若路由器隔离了客户端,两个实例便无法相遇。 +- Windows 上每台电脑只有一个 Shift 实例能占用发现端口,因此额外实例不会出现在设备列表中。 - Windows 代码路径可编译并通过 CI,但手势层面的验证仍需真实 Windows 设备。 - macOS 上签名身份变化后,可能需要重新授予「屏幕录制」与「辅助功能」权限。 @@ -370,7 +401,7 @@ Shift 不发起任何网络请求。文件在本地读取、转换、写回;OC ## 常见问题 **❓ 按住 Shift 拖文件,圆环没出现。** -一定要先开始拖动——按住鼠标并把文件拖过拖动阈值——在拖动过程中按住 `Shift`。只选中文件再按 `Shift` 是没有反应的。Windows 需要在拖拽越过系统阈值的那一刻按住 `Shift`;同一时刻再按 `Ctrl` 则进入工具环。 +一定要先开始拖动——按住鼠标并把文件拖过拖动阈值——在拖动过程中按住 `Shift`。只选中文件再按 `Shift` 是没有反应的。Windows 需要在拖拽越过系统阈值的那一刻按住 `Shift`;同一时刻再按 `Alt` 则进入工具环。 **❓ 提示这类文件没有可用格式。** 未识别的扩展名仅提供打开与在文件夹中显示;旧版 Apple/Office 格式(`.doc`、`.pages`、`.ppt`、`.key`、`.numbers`)刻意不提供转换。请先导出为 DOCX、PPTX 或 XLSX。 @@ -387,7 +418,7 @@ Shift 不发起任何网络请求。文件在本地读取、转换、写回;OC 默认在源文件旁边,带描述性后缀。可先看主窗口的最近列表,点击条目即可打开结果;设置面板会列出全部实际输出路径并可在文件管理器中显示。 **❓ 支持哪些语言?** -英文与简体中文,可在主窗口的设置中切换;首次启动时跟随系统语言。托盘菜单与 macOS 系统权限弹窗仍为英文。 +英文与简体中文,可在主窗口的设置中切换;首次启动时跟随系统语言。托盘菜单会跟随所选语言;macOS 系统权限弹窗仍为英文。 **❓ 能先标注、不保存原图吗?** 可以。标注工作在画布副本上,只有「保存副本」或「复制」才会写盘,源文件永不被修改。也可以「钉在屏幕上」,把它作为浮动参考图。 @@ -397,7 +428,7 @@ Shift 不发起任何网络请求。文件在本地读取、转换、写回;OC ## 参与贡献 -新增一种格式 = 在 `src/components/RadialMenu.tsx` 的 `formatSetFor` 中加一项,并在 `src-tauri/src/files.rs` 的 `perform_action` 中加对应处理;新增一个工具 = 在 `toolWedges` 加一项加一个 match 分支。CI 会执行 `pnpm check`、`pnpm build`、`cargo test --lib`,并在 macOS 与 Windows 上产出 debug 包。 +新增一种格式 = 在 `src/lib/conversionFormats.ts` 的 `conversionFormats` 中加一项、补充对应矩阵测试,并在 `src-tauri/src/files.rs` 的 `perform_action` 中加对应处理;新增一个工具 = 在 `toolWedges` 加一项加一个 match 分支。推送前请运行 `pnpm check`、`pnpm test`、`pnpm build` 与 `cargo test --lib`。CI 会在 macOS 与 Windows 上产出 debug 包。 **欢迎二次开发。** 你可以免费使用、修改、再发布本代码,也可用于商业项目,只需保留原版权声明与 LICENSE 文件。 diff --git a/docs/BUILD_WINDOWS.md b/docs/BUILD_WINDOWS.md index f290a1d..5bca649 100644 --- a/docs/BUILD_WINDOWS.md +++ b/docs/BUILD_WINDOWS.md @@ -8,10 +8,17 @@ - Node.js 20+ and pnpm 8+ - Rust stable through rustup - FFmpeg on `PATH` for screen recording and media conversion -- Pandoc on `PATH` for document conversion -- Poppler (`pdftoppm`) on `PATH` for PDF-to-image rendering +- Pandoc and a Pandoc PDF engine (Typst is recommended) for document conversion +- Poppler (`pdftoppm`) on `PATH` for PDF-to-image and PDF-to-Word rendering - ImageMagick (`magick`) if HEIC/AVIF export is required +These tools are optional at install time. Settings -> **Runtime components** +detects them on the current machine and shows their version and resolved path. +Missing Pandoc, Typst, FFmpeg, Poppler and ImageMagick components have an +**Install** button that runs WinGet without blocking the UI, then checks again +automatically. This is the same flow as the Homebrew-backed runtime installer +on macOS. + ## Build ```powershell @@ -26,12 +33,31 @@ pnpm tauri build The installer is written below `src-tauri\target\release\bundle`. +## Release packages from macOS + +`scripts/build-windows-release.sh` cross-compiles the EXE and produces both +Windows artefacts without a Windows host: + +```bash +brew install makensis +scripts/build-windows-release.sh # add --skip-build to reuse the EXE +``` + +It writes `artifacts/Shift-Setup--win-x64.exe` (NSIS, per-user, no +administrator rights), `artifacts/Shift-Portable--win-x64.zip` and +refreshes `artifacts/SHA256SUMS.txt`. `scripts/windows-installer.nsi` installs +into `%LOCALAPPDATA%\Programs\Shift`, and its uninstaller only removes what the +installer wrote. + ## Runtime behavior - Hold `Shift` while starting a file drag, then move past the Windows drag threshold. Shift places a transparent radial window under the pointer and receives the native OLE drop. -- Hold `Ctrl + Shift` for the tool ring. +- Hold `Alt + Shift` for the tool ring. - Global shortcuts use `Alt + Shift + 2`, `Alt + Shift + 3`, and `Alt + Shift + 4`. - The first screen capture uses the same local-only processing paths as macOS; no cloud service is involved. +- Revealing a result uses `SHOpenFolderAndSelectItems`, so an Explorer window + already showing that folder is reused. `explorer /select,…` opens a fresh + window for every conversion and is not used. ## Cross-compile check from macOS @@ -39,19 +65,69 @@ The Windows-specific Rust code can be type-checked from macOS: ```bash rustup target add x86_64-pc-windows-msvc -PATH="/opt/homebrew/opt/llvm/bin:$PATH" \ +PATH="$HOME/.rustup/toolchains/stable-aarch64-apple-darwin/bin:/opt/homebrew/opt/llvm/bin:$PATH" \ + RUSTUP_TOOLCHAIN=stable-aarch64-apple-darwin \ cargo check --target x86_64-pc-windows-msvc ``` -This proves `cfg(windows)` code compiles. Interactive drag/drop and FFmpeg recording still need a real Windows host for runtime QA. +Prepending the rustup toolchain is required: a Homebrew `cargo` earlier on +`PATH` ignores `RUSTUP_TOOLCHAIN` and cannot find the Windows standard library. +This proves `cfg(windows)` code compiles. Interactive drag/drop and FFmpeg +recording still need a real Windows host for runtime QA. ## Cross-build a Windows EXE from macOS ```bash cargo install cargo-xwin --locked cd src-tauri -cargo +stable xwin build --target x86_64-pc-windows-msvc --release +PATH="$HOME/.rustup/toolchains/stable-aarch64-apple-darwin/bin:$PATH" \ + RUSTUP_TOOLCHAIN=stable-aarch64-apple-darwin \ + cargo xwin build --target x86_64-pc-windows-msvc --release --features tauri/custom-protocol ``` +`--features tauri/custom-protocol` is not optional. Tauri derives `cfg(dev)` from the +absence of that feature, and a dev binary points every window at `build.devUrl` +(`http://localhost:1420`) instead of the bundled assets, so it opens a blank window on +any machine that is not running the Vite dev server. The release script verifies that +the current `dist/assets/*` files and UI strings are embedded before it creates the +installer. + The executable is written to `src-tauri/target/x86_64-pc-windows-msvc/release/shift.exe`. -It needs the Microsoft WebView2 Runtime on the target machine. FFmpeg, Poppler and Pandoc remain optional external tools for recording, PDF rendering and document conversion. +It needs the Microsoft WebView2 Runtime on the target machine. FFmpeg, Poppler and Pandoc remain optional external tools for recording, PDF rendering, PDF-to-Word and document conversion. + +## Runtime components + +The Windows build uses these WinGet package IDs: + +| Component | Package | +| --- | --- | +| Pandoc | `JohnMacFarlane.Pandoc` | +| FFmpeg | `Gyan.FFmpeg` | +| Poppler `pdftoppm` | `oschwartz10612.Poppler` | +| ImageMagick `magick` | `ImageMagick.ImageMagick` | +| Typst PDF engine | `Typst.Typst` | + +WinGet runs with: + +```text +install --id --exact --silent --accept-package-agreements --accept-source-agreements +``` + +Some packages may show a Windows UAC prompt. If WinGet is missing, the +component panel links to Microsoft's official setup guide instead of downloading +an executable from a third-party source. + +Shift checks the normal `PATH`, WinGet Links and Packages directories, the +standard Pandoc/FFmpeg/ImageMagick install locations, Chocolatey and Scoop +locations. This lets tools installed while Shift is running become available +after a refresh without restarting the app. + +Build the frontend for the right engine first, otherwise Vite emits Safari 13 output: + +```bash +TAURI_ENV_PLATFORM=windows pnpm build +``` + +Interactive drag/drop, installer shortcuts and recording still need a manual pass on +a real Windows machine. The release script itself can run unattended; it does not +replace that runtime QA. diff --git a/native/macos/doc-render.swift b/native/macos/doc-render.swift index 144ac8c..6e4696a 100644 --- a/native/macos/doc-render.swift +++ b/native/macos/doc-render.swift @@ -1,17 +1,147 @@ import AppKit import Foundation +import PDFKit +import Vision func fail(_ message: String) -> Never { FileHandle.standardError.write((message + "\n").data(using: .utf8)!) exit(1) } -guard CommandLine.arguments.count == 3 else { - fail("usage: doc-render ") +func hasTextLayer(_ page: PDFPage) -> Bool { + let characters = (page.string ?? "").filter { !$0.isWhitespace } + return characters.count >= 20 +} + +/// Renders a PDF page on a white background, matching FileFlipper's PDFKit path. +func renderPage(_ page: PDFPage, scale: CGFloat) -> CGImage? { + let box = page.bounds(for: .mediaBox) + let sideways = page.rotation % 180 != 0 + let width = Int(((sideways ? box.height : box.width) * scale).rounded()) + let height = Int(((sideways ? box.width : box.height) * scale).rounded()) + guard width > 0, height > 0, + let context = CGContext( + data: nil, + width: width, + height: height, + bitsPerComponent: 8, + bytesPerRow: 0, + space: CGColorSpace(name: CGColorSpace.sRGB)!, + bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue + ) else { + return nil + } + context.setFillColor(CGColor(red: 1, green: 1, blue: 1, alpha: 1)) + context.fill(CGRect(x: 0, y: 0, width: width, height: height)) + context.interpolationQuality = .high + context.scaleBy(x: scale, y: scale) + page.draw(with: .mediaBox, to: context) + return context.makeImage() +} + +/// Uses Vision, the system's on-device Live Text engine, for scanned pages. +func recognizeText(_ page: PDFPage) -> String { + guard let image = renderPage(page, scale: 3) else { return "" } + let request = VNRecognizeTextRequest() + request.recognitionLevel = .accurate + request.usesLanguageCorrection = true + request.automaticallyDetectsLanguage = true + let preferred = [ + "zh-Hans", "zh-Hant", "en-US", "ja-JP", "ko-KR", + "fr-FR", "de-DE", "es-ES", "it-IT", "pt-BR", "ru-RU", + ] + let supported = (try? request.supportedRecognitionLanguages()) ?? [] + let languages = preferred.filter { supported.contains($0) } + if !languages.isEmpty { + request.recognitionLanguages = languages + } + do { + try VNImageRequestHandler(cgImage: image).perform([request]) + } catch { + return "" + } + let pieces = (request.results ?? []).compactMap { observation -> (CGRect, String)? in + guard let text = observation.topCandidates(1).first?.string else { return nil } + return (observation.boundingBox, text) + } + let sorted = pieces.sorted { $0.0.midY > $1.0.midY } + var rows: [[(CGRect, String)]] = [] + for piece in sorted { + if let last = rows.last?.first, + abs(last.0.midY - piece.0.midY) < min(last.0.height, piece.0.height) / 2 { + rows[rows.count - 1].append(piece) + } else { + rows.append([piece]) + } + } + return rows.map { row in + row.sorted { $0.0.minX < $1.0.minX }.map { $0.1 }.joined(separator: " ") + }.joined(separator: "\n") +} + +/// PDF -> editable DOCX through PDFKit and Cocoa's native OOXML writer. +func writeWordDocument(from pdf: URL, to output: URL) throws { + guard let document = PDFDocument(url: pdf) else { + throw NSError(domain: "Shift", code: 1, userInfo: [ + NSLocalizedDescriptionKey: "Could not open PDF: \(pdf.path)", + ]) + } + if document.isLocked { + throw NSError(domain: "Shift", code: 2, userInfo: [ + NSLocalizedDescriptionKey: "This PDF is password protected.", + ]) + } + let combined = NSMutableAttributedString() + for index in 0.. 0 else { continue } + if combined.length > 0 { + combined.append(NSAttributedString(string: "\n\u{0C}")) + } + combined.append(pageText) + } + guard combined.length > 0 else { + throw NSError(domain: "Shift", code: 3, userInfo: [ + NSLocalizedDescriptionKey: "No text could be read from this PDF.", + ]) + } + let data = try combined.data( + from: NSRange(location: 0, length: combined.length), + documentAttributes: [.documentType: NSAttributedString.DocumentType.officeOpenXML] + ) + try data.write(to: output, options: .atomic) +} + +guard CommandLine.arguments.count == 3 || CommandLine.arguments.count == 4 else { + fail("usage: doc-render [html-base-dir]") } let input = URL(fileURLWithPath: CommandLine.arguments[1]) let output = URL(fileURLWithPath: CommandLine.arguments[2]) +let app = NSApplication.shared +app.setActivationPolicy(.prohibited) + +if output.pathExtension.lowercased() == "docx" { + guard input.pathExtension.lowercased() == "pdf" else { + fail("PDF-to-Word input must be a PDF") + } + do { + try writeWordDocument(from: input, to: output) + exit(0) + } catch { + fail(error.localizedDescription) + } +} + let extensionName = input.pathExtension.lowercased() var documentType: NSAttributedString.DocumentType? @@ -39,7 +169,10 @@ if let documentType { options[.documentType] = documentType } if documentType == .html { - options[.baseURL] = input.deletingLastPathComponent() + let baseDirectory = CommandLine.arguments.count == 4 + ? URL(fileURLWithPath: CommandLine.arguments[3], isDirectory: true) + : input.deletingLastPathComponent() + options[.baseURL] = baseDirectory } let data: Data @@ -56,9 +189,6 @@ do { fail("could not parse document: \(error.localizedDescription)") } -let app = NSApplication.shared -app.setActivationPolicy(.prohibited) - let printInfo = NSPrintInfo.shared.copy() as! NSPrintInfo printInfo.paperSize = NSSize(width: 595.2, height: 841.8) printInfo.topMargin = 42 diff --git a/native/macos/drag-bridge.swift b/native/macos/drag-bridge.swift index b581a09..e32ada2 100644 --- a/native/macos/drag-bridge.swift +++ b/native/macos/drag-bridge.swift @@ -27,7 +27,9 @@ func cursorLocation() -> NSPoint { let dragPasteboard = NSPasteboard(name: .drag) var mouseWasDown = false var pickerShown = false +var cancelled = false var toolsMode = false +var missingDragFiles = 0 var changeCountAtMouseDown = dragPasteboard.changeCount var lastX = Double.nan var lastY = Double.nan @@ -46,9 +48,10 @@ func tick() { } let mouseDown = NSEvent.pressedMouseButtons & 1 != 0 let point = cursorLocation() - if !mouseDown { changeCountAtMouseDown = dragPasteboard.changeCount + cancelled = false + missingDragFiles = 0 } if !mouseDown && mouseWasDown && pickerShown { @@ -64,6 +67,7 @@ func tick() { let option = flags.contains(.option) if !pickerShown { + guard !cancelled else { return } guard shift, dragPasteboard.changeCount != changeCountAtMouseDown else { return } let files = draggedFileURLs() guard !files.isEmpty else { return } @@ -80,6 +84,19 @@ func tick() { emit(Payload(type: "mode", x: point.x, y: point.y, files: nil, tools: option)) } + // Some Finder versions also tear down the pasteboard during cancellation. + // Keep this as a fallback for drag sessions that disappear without a key event. + if draggedFileURLs().isEmpty { + missingDragFiles += 1 + if missingDragFiles >= 6 { + pickerShown = false + cancelled = true + emit(Payload(type: "cancel", x: point.x, y: point.y, files: nil, tools: nil)) + } + return + } + missingDragFiles = 0 + if abs(point.x - lastX) > 0.5 || abs(point.y - lastY) > 0.5 { lastX = point.x lastY = point.y diff --git a/native/macos/pdf-render.swift b/native/macos/pdf-render.swift index 23b3a83..d7fa7ac 100644 --- a/native/macos/pdf-render.swift +++ b/native/macos/pdf-render.swift @@ -30,22 +30,33 @@ guard let document = PDFDocument(url: input) else { for index in 0.."$CROSS_LOG" 2>&1 + ); then + sed -n '1,240p' "$CROSS_LOG" + exit 1 + fi + grep -Ev "^warning:|^\s+\||^\s+=|LNK4099|failed to load|^$|^\s+-->|^note:" \ + "$CROSS_LOG" || true +fi + +test -f "$EXE" + +echo "== verify the frontend is embedded ==" +python3 - "$EXE" <<'PY' +import pathlib, sys +exe = pathlib.Path(sys.argv[1]).read_bytes() +missing = [p.name for p in pathlib.Path("dist/assets").iterdir() if p.name.encode() not in exe] +for extra in ("index.html", "view=radial"): + if extra.encode() not in exe: + missing.append(extra) +# The runtime hint is rendered by the webview, so it must be present in the +# asset bundle even though the PE itself does not keep readable UTF-8 strings. +asset_text = "".join( + path.read_text(encoding="utf-8") for path in pathlib.Path("dist/assets").iterdir() +) +required = [ + "以下是本机安装组件的情况,未安装的组件请点击安装。", + "正在转换第 {done}/{total} 页…", + "正在生成文档…", +] +missing.extend(text for text in required if text not in asset_text) +old = "以下结果来自当前这台机器" +if old in asset_text: + sys.exit(f"stale frontend copy is still embedded: {old}") +if missing: + sys.exit(f"frontend not embedded, missing: {missing}") +print(f"embedded ok: {len(exe):,} byte exe carries the current dist/assets") +PY + +echo "== windows notes ==" +STAGE=$(mktemp -d /tmp/shift-windows-release.XXXXXX) +mkdir -p "$STAGE/portable/Shift-$VERSION-Windows-x64" +python3 - "$STAGE" "$VERSION" <<'PY' +import pathlib, sys +stage, version = pathlib.Path(sys.argv[1]), sys.argv[2] +text = f"""Shift {version} - Windows x64 + +系统要求 +-------- +- Windows 10 或 Windows 11(64 位)。 +- Microsoft Edge WebView2 Runtime。当前 Windows 10/11 通常已内置;如果 Shift + 启动后窗口空白或无法打开,请从微软官方页面安装: + https://developer.microsoft.com/microsoft-edge/webview2/ +- 安装与运行都不需要管理员权限。只有在“运行组件”里安装外部工具时, + Windows 才可能弹出 UAC 确认。 + +安装 +---- +1. 运行 Shift-Setup-{version}-win-x64.exe,默认安装到 + %LOCALAPPDATA%\\Programs\\Shift,并创建开始菜单与桌面快捷方式。 +2. 这是未签名构建。若 SmartScreen 显示“Windows 已保护你的电脑”,请选择 + “更多信息” > “仍要运行”。 +3. 卸载请在“设置 > 应用”中移除 Shift,无需手动删除文件。 + +便携版 +------ +不想安装时,解压 Shift-Portable-{version}-win-x64.zip,直接运行其中的 +shift.exe。请先完整解压,不要在压缩包的预览窗口里运行。 + +运行组件 +-------- +打开 Shift 主窗口,进入“设置” > “运行组件”: +- 首次进入该页会自动检测,之后只在你点击“重新检测”时重新扫描; + 扫描范围包括 PATH、WinGet、Chocolatey、Scoop 与常见安装目录。 +- 页面显示本机实际使用的组件、版本、路径与来源。 +- 缺失组件旁有“安装”,通过 Microsoft WinGet 安装,界面不会阻塞, + 完成后自动重新检测。 + +WinGet 包映射: +- Pandoc: JohnMacFarlane.Pandoc +- Typst: Typst.Typst +- FFmpeg: Gyan.FFmpeg +- Poppler: oschwartz10612.Poppler +- ImageMagick: ImageMagick.ImageMagick + +如果没有 WinGet,界面会给出微软官方安装说明入口。Shift 不会从第三方站点 +下载运行库,也不会卸载或覆盖你已有的工具。 + +校验 +---- +PowerShell: + Get-FileHash .\\Shift-Setup-{version}-win-x64.exe -Algorithm SHA256 +""" +(stage / "README-Windows.txt").write_bytes(text.replace("\n", "\r\n").encode("utf-8")) +PY + +echo "== NSIS installer ==" +mkdir -p artifacts +SIZE_KB=$(( ($(stat -f%z "$EXE" 2>/dev/null || stat -c%s "$EXE") + 1023) / 1024 )) +makensis -V2 \ + -DSHIFT_EXE="$PWD/$EXE" \ + -DLICENSE_FILE="$PWD/LICENSE" \ + -DNOTICES_FILE="$PWD/THIRD_PARTY_NOTICES.md" \ + -DREADME_FILE="$STAGE/README-Windows.txt" \ + -DICONS_DIR="$PWD/src-tauri/icons" \ + -DOUT_FILE="$PWD/$INSTALLER" \ + -DVERSION="$VERSION" \ + -DEST_SIZE_KB="$SIZE_KB" \ + scripts/windows-installer.nsi + +echo "== portable ZIP ==" +cp "$EXE" "$STAGE/portable/Shift-$VERSION-Windows-x64/shift.exe" +cp LICENSE "$STAGE/portable/Shift-$VERSION-Windows-x64/LICENSE.txt" +cp THIRD_PARTY_NOTICES.md "$STAGE/portable/Shift-$VERSION-Windows-x64/THIRD_PARTY_NOTICES.txt" +cp "$STAGE/README-Windows.txt" "$STAGE/portable/Shift-$VERSION-Windows-x64/README-Windows.txt" +(cd "$STAGE/portable" && zip -q -r -X -9 "$ROOT/$PORTABLE" "Shift-$VERSION-Windows-x64") +mv "$STAGE/README-Windows.txt" "artifacts/README-Windows.txt" + +echo "== checksums ==" +python3 - "$INSTALLER" "$PORTABLE" <<'PY' +import hashlib, pathlib, sys +artifacts = pathlib.Path("artifacts") +names = ["Shift-macOS-universal.dmg", "Shift-macOS-universal.zip"] +names += [pathlib.Path(arg).name for arg in sys.argv[1:]] +names.append("README-Windows.txt") +lines = [] +for name in names: + path = artifacts / name + lines.append(f"{hashlib.sha256(path.read_bytes()).hexdigest()} {name}") +(artifacts / "SHA256SUMS.txt").write_text("".join(line + "\n" for line in lines), encoding="utf-8") +print("\n".join(lines)) +PY + +echo "== result ==" +ls -lh "$INSTALLER" "$PORTABLE" +printf 'installer sha256: %s\n' "$(shasum -a 256 "$INSTALLER" | cut -d' ' -f1)" +printf 'exe sha256: %s\n' "$(shasum -a 256 "$EXE" | cut -d' ' -f1)" diff --git a/scripts/windows-installer.nsi b/scripts/windows-installer.nsi new file mode 100644 index 0000000..e983b64 --- /dev/null +++ b/scripts/windows-installer.nsi @@ -0,0 +1,189 @@ +; Shift - Windows installer (NSIS 3, per-user, no administrator rights). +; +; makensis -DSHIFT_EXE= -DLICENSE_FILE= -DNOTICES_FILE= \ +; -DREADME_FILE= -DICONS_DIR= -DOUT_FILE= \ +; -DVERSION=0.1.0 -DEST_SIZE_KB= scripts/windows-installer.nsi +; +; Install scope is deliberately per-user: Shift writes only to HKCU and its own +; folder, which is also what the portable build already does. + +!include "MUI2.nsh" +!include "LogicLib.nsh" +!include "WinVer.nsh" +!include "x64.nsh" + +!ifndef VERSION + !error "VERSION is required (for example -DVERSION=0.1.0)" +!endif +!ifndef SHIFT_EXE + !error "SHIFT_EXE is required" +!endif +!ifndef OUT_FILE + !define OUT_FILE "Shift-Setup.exe" +!endif +!ifndef EST_SIZE_KB + !define EST_SIZE_KB "40000" +!endif + +!define APP_NAME "Shift" +!define APP_ID "app.nex.shift" +!define APP_VERSION "${VERSION}" +!define PUBLISHER "Shift Contributors" +!define UNINSTALL_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APP_ID}" +!define WEBVIEW_KEY "SOFTWARE\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}" + +Unicode true +Name "${APP_NAME}" +OutFile "${OUT_FILE}" +InstallDir "$LOCALAPPDATA\Programs\${APP_NAME}" +InstallDirRegKey HKCU "Software\${APP_NAME}" "InstallDirectory" +RequestExecutionLevel user +SetCompressor /SOLID lzma + +!define /date BUILD_YEAR "%Y" +VIProductVersion "${APP_VERSION}.0" +VIAddVersionKey ProductName "${APP_NAME}" +VIAddVersionKey ProductVersion "${APP_VERSION}" +VIAddVersionKey CompanyName "${PUBLISHER}" +VIAddVersionKey LegalCopyright "Copyright ${BUILD_YEAR} ${PUBLISHER}" +VIAddVersionKey FileDescription "${APP_NAME} installer" +VIAddVersionKey FileVersion "${APP_VERSION}.0" +VIAddVersionKey OriginalFilename "Shift-Setup.exe" + +!define MUI_ICON "${ICONS_DIR}\icon.ico" +!define MUI_UNICON "${ICONS_DIR}\icon.ico" +!define MUI_ABORTWARNING +!define MUI_FINISHPAGE_RUN "$INSTDIR\shift.exe" +!define MUI_FINISHPAGE_RUN_TEXT "$(run_shift)" +!define MUI_FINISHPAGE_SHOWREADME +!define MUI_FINISHPAGE_SHOWREADME_TEXT "$(read_me)" +!define MUI_FINISHPAGE_SHOWREADME_NOTCHECKED +!define MUI_FINISHPAGE_SHOWREADME_FUNCTION show_readme + +!insertmacro MUI_PAGE_WELCOME +!insertmacro MUI_PAGE_DIRECTORY +!insertmacro MUI_PAGE_INSTFILES +!insertmacro MUI_PAGE_FINISH +!insertmacro MUI_UNPAGE_CONFIRM +!insertmacro MUI_UNPAGE_INSTFILES + +!insertmacro MUI_LANGUAGE "English" +!insertmacro MUI_LANGUAGE "SimpChinese" + +LangString run_shift 1033 "Launch Shift when the installer closes." +LangString run_shift 2052 "安装完成后启动 Shift。" +LangString read_me 1033 "Open the Windows notes." +LangString read_me 2052 "打开 Windows 使用说明。" +LangString close_shift 1033 "${APP_NAME} is running and has to close before it can be updated.$\r$\n$\r$\nClick OK to close it now, or Cancel to leave things as they are." +LangString close_shift 2052 "${APP_NAME} 正在运行,需要先退出才能更新。$\r$\n$\r$\n点击“确定”立即退出,点击“取消”保持现状。" +LangString webview_missing 1033 "${APP_NAME} draws its windows with Microsoft Edge WebView2, which this installer did not find.$\r$\n$\r$\nIf Shift opens a blank window or does not start, install the WebView2 Evergreen Runtime from Microsoft and launch Shift again.$\r$\n$\r$\nhttps://developer.microsoft.com/microsoft-edge/webview2/" +LangString webview_missing 2052 "${APP_NAME} 的界面依赖 Microsoft Edge WebView2,安装程序没有检测到它。$\r$\n$\r$\n如果 Shift 启动后窗口空白或无法打开,请从微软官网安装 WebView2 Evergreen Runtime,然后重新启动 Shift。$\r$\n$\r$\nhttps://developer.microsoft.com/microsoft-edge/webview2/" + +Var ReadmePath + +Function .onInit + ${IfNot} ${AtLeastWin10} + MessageBox MB_OK|MB_ICONSTOP "${APP_NAME} needs Windows 10 or later." + Abort + ${EndIf} + + ${IfNot} ${IsNativeAMD64} + MessageBox MB_OK|MB_ICONSTOP "This build is for 64-bit Windows (x64)." + Abort + ${EndIf} + + ; Shift keeps a tray process alive and Windows refuses to overwrite a mapped + ; EXE, so ask before closing a conversion the user may be waiting on. + ${If} ${FileExists} "$INSTDIR\shift.exe" + FindWindow $0 "" "${APP_NAME}" + ${If} $0 != 0 + MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION "$(close_shift)" IDOK +2 + Abort + ${EndIf} + ${EndIf} + + nsExec::Exec 'taskkill /IM shift.exe /F' + Pop $0 +FunctionEnd + +Function un.onInit + nsExec::Exec 'taskkill /IM shift.exe /F' + Pop $0 +FunctionEnd + +Section "Install" + SetOutPath "$INSTDIR" + File "${SHIFT_EXE}" +!ifdef LICENSE_FILE + File /oname=LICENSE.txt "${LICENSE_FILE}" +!endif +!ifdef NOTICES_FILE + File /oname=THIRD_PARTY_NOTICES.txt "${NOTICES_FILE}" +!endif +!ifdef README_FILE + File /oname=README-Windows.txt "${README_FILE}" +!endif + + WriteUninstaller "$INSTDIR\Uninstall.exe" + StrCpy $ReadmePath "$INSTDIR\README-Windows.txt" + + WriteRegStr HKCU "Software\${APP_NAME}" "InstallDirectory" "$INSTDIR" + + CreateDirectory "$SMPROGRAMS\${APP_NAME}" + CreateShortCut "$SMPROGRAMS\${APP_NAME}\${APP_NAME}.lnk" "$INSTDIR\shift.exe" + CreateShortCut "$SMPROGRAMS\${APP_NAME}\Uninstall.lnk" "$INSTDIR\Uninstall.exe" + CreateShortCut "$DESKTOP\${APP_NAME}.lnk" "$INSTDIR\shift.exe" + + WriteRegStr HKCU "${UNINSTALL_KEY}" "DisplayName" "${APP_NAME}" + WriteRegStr HKCU "${UNINSTALL_KEY}" "DisplayVersion" "${APP_VERSION}" + WriteRegStr HKCU "${UNINSTALL_KEY}" "Publisher" "${PUBLISHER}" + WriteRegStr HKCU "${UNINSTALL_KEY}" "DisplayIcon" "$INSTDIR\shift.exe" + WriteRegStr HKCU "${UNINSTALL_KEY}" "InstallLocation" "$INSTDIR" + WriteRegStr HKCU "${UNINSTALL_KEY}" "UninstallString" '"$INSTDIR\Uninstall.exe"' + WriteRegStr HKCU "${UNINSTALL_KEY}" "QuietUninstallString" '"$INSTDIR\Uninstall.exe" /S' + WriteRegDWORD HKCU "${UNINSTALL_KEY}" "NoModify" 1 + WriteRegDWORD HKCU "${UNINSTALL_KEY}" "NoRepair" 1 + WriteRegDWORD HKCU "${UNINSTALL_KEY}" "EstimatedSize" ${EST_SIZE_KB} +SectionEnd + +Section "PostInstall" + ; The runtime is machine-wide and normally already present on Windows 10/11, + ; so a missing one is a warning rather than a reason to refuse the install. + ClearErrors + ReadRegStr $0 HKLM "${WEBVIEW_KEY}" "pv" + ${If} $0 == "" + ReadRegStr $0 HKCU "${WEBVIEW_KEY}" "pv" + ${EndIf} + ${If} $0 == "" + MessageBox MB_OK|MB_ICONINFORMATION "$(webview_missing)" + ${EndIf} +SectionEnd + +Function show_readme + ${If} ${FileExists} "$ReadmePath" + Exec 'notepad.exe "$ReadmePath"' + ${EndIf} +FunctionEnd + +Section "Uninstall" + nsExec::Exec 'taskkill /IM shift.exe /F' + Pop $0 + + Delete "$INSTDIR\shift.exe" + Delete "$INSTDIR\LICENSE.txt" + Delete "$INSTDIR\THIRD_PARTY_NOTICES.txt" + Delete "$INSTDIR\README-Windows.txt" + Delete "$INSTDIR\Uninstall.exe" + RMDir "$INSTDIR" + + Delete "$SMPROGRAMS\${APP_NAME}\${APP_NAME}.lnk" + Delete "$SMPROGRAMS\${APP_NAME}\Uninstall.lnk" + RMDir "$SMPROGRAMS\${APP_NAME}" + Delete "$DESKTOP\${APP_NAME}.lnk" + + ; Only the keys the installer owns. User preferences and the login item are + ; managed by Shift itself and stay untouched. + DeleteRegKey HKCU "${UNINSTALL_KEY}" + DeleteRegValue HKCU "Software\${APP_NAME}" "InstallDirectory" + DeleteRegKey /ifempty HKCU "Software\${APP_NAME}" +SectionEnd diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 221ceaa..aa42230 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -3820,6 +3820,7 @@ dependencies = [ "base64 0.22.1", "calamine", "chrono", + "flate2", "image", "libc", "lopdf 0.45.0", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 0e17ab2..1f6727f 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -34,15 +34,30 @@ which = "7" [target.'cfg(windows)'.dependencies] windows-sys = { version = "0.59", features = [ "Win32_Foundation", + "Win32_Graphics_Gdi", "Win32_System_Console", + "Win32_System_Com", + "Win32_System_LibraryLoader", + "Win32_System_Registry", + "Win32_System_SystemInformation", "Win32_System_Threading", + "Win32_System_WindowsProgramming", + "Win32_Globalization", + "Win32_Security", + "Win32_Storage_FileSystem", + "Win32_UI_HiDpi", "Win32_UI_Input_KeyboardAndMouse", "Win32_UI_WindowsAndMessaging", + "Win32_UI_Shell", + "Win32_UI_Shell_Common", ] } [target.'cfg(unix)'.dependencies] libc = "0.2" +[dev-dependencies] +flate2 = "1" + [profile.dev] debug = 1 diff --git a/src-tauri/binaries/doc-render-aarch64-apple-darwin b/src-tauri/binaries/doc-render-aarch64-apple-darwin index 23ddd7c..df7c8dc 100755 Binary files a/src-tauri/binaries/doc-render-aarch64-apple-darwin and b/src-tauri/binaries/doc-render-aarch64-apple-darwin differ diff --git a/src-tauri/binaries/doc-render-universal-apple-darwin b/src-tauri/binaries/doc-render-universal-apple-darwin index df2dd2b..4512981 100755 Binary files a/src-tauri/binaries/doc-render-universal-apple-darwin and b/src-tauri/binaries/doc-render-universal-apple-darwin differ diff --git a/src-tauri/binaries/doc-render-x86_64-apple-darwin b/src-tauri/binaries/doc-render-x86_64-apple-darwin index 396e591..56588a5 100755 Binary files a/src-tauri/binaries/doc-render-x86_64-apple-darwin and b/src-tauri/binaries/doc-render-x86_64-apple-darwin differ diff --git a/src-tauri/binaries/drag-bridge-aarch64-apple-darwin b/src-tauri/binaries/drag-bridge-aarch64-apple-darwin index 6918b29..3c5f885 100755 Binary files a/src-tauri/binaries/drag-bridge-aarch64-apple-darwin and b/src-tauri/binaries/drag-bridge-aarch64-apple-darwin differ diff --git a/src-tauri/binaries/drag-bridge-universal-apple-darwin b/src-tauri/binaries/drag-bridge-universal-apple-darwin index a2cd7f8..0e82861 100755 Binary files a/src-tauri/binaries/drag-bridge-universal-apple-darwin and b/src-tauri/binaries/drag-bridge-universal-apple-darwin differ diff --git a/src-tauri/binaries/drag-bridge-x86_64-apple-darwin b/src-tauri/binaries/drag-bridge-x86_64-apple-darwin index 5a2fc9f..3536b50 100755 Binary files a/src-tauri/binaries/drag-bridge-x86_64-apple-darwin and b/src-tauri/binaries/drag-bridge-x86_64-apple-darwin differ diff --git a/src-tauri/binaries/pdf-render-aarch64-apple-darwin b/src-tauri/binaries/pdf-render-aarch64-apple-darwin index afaa790..ac8005f 100755 Binary files a/src-tauri/binaries/pdf-render-aarch64-apple-darwin and b/src-tauri/binaries/pdf-render-aarch64-apple-darwin differ diff --git a/src-tauri/binaries/pdf-render-universal-apple-darwin b/src-tauri/binaries/pdf-render-universal-apple-darwin index 7a1fa7c..c896df4 100755 Binary files a/src-tauri/binaries/pdf-render-universal-apple-darwin and b/src-tauri/binaries/pdf-render-universal-apple-darwin differ diff --git a/src-tauri/binaries/pdf-render-x86_64-apple-darwin b/src-tauri/binaries/pdf-render-x86_64-apple-darwin index 94b687a..532974c 100755 Binary files a/src-tauri/binaries/pdf-render-x86_64-apple-darwin and b/src-tauri/binaries/pdf-render-x86_64-apple-darwin differ diff --git a/src-tauri/build.rs b/src-tauri/build.rs index d860e1e..6190642 100644 --- a/src-tauri/build.rs +++ b/src-tauri/build.rs @@ -1,3 +1,4 @@ fn main() { + println!("cargo:rerun-if-changed=../dist"); tauri_build::build() } diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.json index 3afa47a..5dd6454 100644 --- a/src-tauri/capabilities/default.json +++ b/src-tauri/capabilities/default.json @@ -2,7 +2,7 @@ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "default", "description": "Default permissions for Shift windows.", - "windows": ["main", "radial", "capture", "recorder", "editor", "pin-*"], + "windows": ["main", "radial", "capture", "recorder", "editor", "pin"], "permissions": [ "core:default", "core:window:allow-show", diff --git a/src-tauri/src/autostart.rs b/src-tauri/src/autostart.rs index 9c7f591..108e2d7 100644 --- a/src-tauri/src/autostart.rs +++ b/src-tauri/src/autostart.rs @@ -10,11 +10,7 @@ use std::path::PathBuf; // Each helper is only pulled in where it is actually used, so no platform // carries dead imports. -#[cfg(target_os = "macos")] -use std::{fs, process::Command}; -#[cfg(target_os = "windows")] -use std::process::Command; -#[cfg(all(unix, not(target_os = "macos")))] +#[cfg(any(target_os = "macos", all(unix, not(target_os = "macos"))))] use std::fs; /// Registers or unregisters Shift as a login item. @@ -38,7 +34,20 @@ fn platform_set(enabled: bool) -> Result<(), String> { #[cfg(target_os = "macos")] fn platform_is_enabled() -> bool { - agent_plist().is_some_and(|path| path.exists()) + let Some(path) = agent_plist() else { + return false; + }; + if !path.exists() { + return false; + } + // A plist that still points at an old location is a leftover from a moved + // app, so the toggle has to be used again to register the new path. + let Some(current) = executable_path() else { + return false; + }; + fs::read_to_string(&path) + .map(|plist| plist.contains(¤t.to_string_lossy().to_string())) + .unwrap_or(false) } #[cfg(target_os = "windows")] @@ -75,17 +84,13 @@ fn platform_is_enabled() -> bool { false } -/// The app to launch. Inside a `.app` bundle this is the bundle itself, so the -/// Dock name, icon and macOS TCC identity match what the user approved. +/// The app to launch. On macOS this is the executable inside the `.app` +/// bundle: a LaunchAgent cannot run a bundle, it can only run a program. +/// Pointing it at the bundle makes launchd fail with `EX_CONFIG`, so the +/// executable is used instead. It keeps the same bundle identity, icon and +/// macOS TCC permission the user approved. fn executable_path() -> Option { let current = std::env::current_exe().ok()?; - let mut cursor = current.as_path(); - while let Some(parent) = cursor.parent() { - if parent.extension().and_then(|value| value.to_str()) == Some("app") { - return Some(parent.to_path_buf()); - } - cursor = parent; - } Some(current) } @@ -135,11 +140,11 @@ fn install_macos_agent() -> Result<(), String> { let domain = format!("gui/{}", unsafe { libc::getuid() }); // Modern launchctl first, legacy load as a fallback. Either one succeeding // is enough, and the plist also takes effect at the next login regardless. - let modern = Command::new("launchctl") + let modern = crate::process::command("launchctl") .args(["bootstrap", domain.as_str(), &path.to_string_lossy()]) .status(); if !matches!(modern, Ok(status) if status.success()) { - let _ = Command::new("launchctl") + let _ = crate::process::command("launchctl") .args(["load", &path.to_string_lossy()]) .status(); } @@ -155,10 +160,10 @@ fn remove_macos_agent() -> Result<(), String> { return Ok(()); } let domain = format!("gui/{}", unsafe { libc::getuid() }); - let _ = Command::new("launchctl") + let _ = crate::process::command("launchctl") .args(["bootout", domain.as_str(), &path.to_string_lossy()]) .status(); - let _ = Command::new("launchctl") + let _ = crate::process::command("launchctl") .args(["unload", &path.to_string_lossy()]) .status(); fs::remove_file(&path).map_err(|error| error.to_string()) @@ -167,34 +172,122 @@ fn remove_macos_agent() -> Result<(), String> { // -------------------------------------------------------------- Windows #[cfg(target_os = "windows")] -const RUN_KEY: &str = r"HKCU\Software\Microsoft\Windows\CurrentVersion\Run"; +const RUN_KEY: &str = r"Software\Microsoft\Windows\CurrentVersion\Run"; #[cfg(target_os = "windows")] -fn registry_command(arguments: &[&str]) -> Result { - Command::new("reg") - .args(arguments) - .status() - .map_err(|error| format!("Could not run reg.exe: {error}")) +fn wide(value: &str) -> Vec { + value.encode_utf16().chain(std::iter::once(0)).collect() } +/// Access the Run key in process. Spawning `reg.exe` for every settings +/// snapshot made a GUI app flash a console window and added tens of +/// milliseconds to each settings action. #[cfg(target_os = "windows")] fn query_windows_run_key() -> bool { - matches!( - registry_command(&["query", RUN_KEY, "/v", "Shift"]), - Ok(status) if status.success() - ) + use windows_sys::Win32::{ + Foundation::{ERROR_FILE_NOT_FOUND, ERROR_SUCCESS}, + System::Registry::{ + RegCloseKey, RegOpenKeyExW, RegQueryValueExW, HKEY, HKEY_CURRENT_USER, KEY_QUERY_VALUE, + REG_SZ, + }, + }; + + let subkey = wide(RUN_KEY); + let name = wide("Shift"); + let mut key: HKEY = std::ptr::null_mut(); + let opened = unsafe { + RegOpenKeyExW( + HKEY_CURRENT_USER, + subkey.as_ptr(), + 0, + KEY_QUERY_VALUE, + &mut key, + ) + }; + if opened == ERROR_FILE_NOT_FOUND { + return false; + } + if opened != ERROR_SUCCESS || key.is_null() { + return false; + } + let mut kind = 0_u32; + let mut size = 0_u32; + let query = unsafe { + RegQueryValueExW( + key, + name.as_ptr(), + std::ptr::null(), + &mut kind, + std::ptr::null_mut(), + &mut size, + ) + }; + let exists = query == ERROR_SUCCESS && kind == REG_SZ && size > 0; + unsafe { RegCloseKey(key) }; + exists } #[cfg(target_os = "windows")] fn set_windows_run_key(enabled: bool) -> Result<(), String> { - if enabled { + use windows_sys::Win32::{ + Foundation::ERROR_SUCCESS, + System::Registry::{ + RegCloseKey, RegCreateKeyExW, RegDeleteValueW, RegSetValueExW, HKEY, HKEY_CURRENT_USER, + KEY_SET_VALUE, REG_OPTION_NON_VOLATILE, REG_SZ, + }, + }; + + let subkey = wide(RUN_KEY); + let name = wide("Shift"); + let mut key: HKEY = std::ptr::null_mut(); + let opened = unsafe { + RegCreateKeyExW( + HKEY_CURRENT_USER, + subkey.as_ptr(), + 0, + std::ptr::null(), + REG_OPTION_NON_VOLATILE, + KEY_SET_VALUE, + std::ptr::null(), + &mut key, + std::ptr::null_mut(), + ) + }; + if opened != ERROR_SUCCESS || key.is_null() { + return Err(format!( + "Could not open the Windows startup registry key ({opened})." + )); + } + + let result = if enabled { let target = executable_path().ok_or_else(|| "Could not locate Shift.".to_string())?; - // Quote the path: it usually contains "Program Files" or a user name - // with spaces, and the Run key performs no quoting of its own. - let value = format!("\"{}\"", target.display()); - registry_command(&["add", RUN_KEY, "/v", "Shift", "/t", "REG_SZ", "/d", &value, "/f"])?; + // The Run key executes the string as written; quote paths containing spaces. + let value = wide(&format!("\"{}\"", target.display())); + let bytes = unsafe { + std::slice::from_raw_parts( + value.as_ptr().cast::(), + value.len() * std::mem::size_of::(), + ) + }; + unsafe { + RegSetValueExW( + key, + name.as_ptr(), + 0, + REG_SZ, + bytes.as_ptr(), + bytes.len() as u32, + ) + } } else { - registry_command(&["delete", RUN_KEY, "/v", "Shift", "/f"])?; + unsafe { RegDeleteValueW(key, name.as_ptr()) } + }; + unsafe { RegCloseKey(key) }; + if result != ERROR_SUCCESS { + return Err(format!( + "Could not {} the Windows startup entry ({result}).", + if enabled { "create" } else { "remove" } + )); } Ok(()) } @@ -236,3 +329,22 @@ fn remove_linux_autostart() -> Result<(), String> { } Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + + #[cfg(target_os = "macos")] + #[test] + fn macos_agent_points_at_an_executable_not_a_bundle() { + let path = agent_plist().unwrap(); + // A LaunchAgent cannot run an `.app` bundle, so the plist must name a + // program inside it. + let target = executable_path().unwrap(); + assert!( + target.extension().and_then(|value| value.to_str()) != Some("app"), + "the agent must not point at a bundle: {target:?}" + ); + assert!(path.ends_with(format!("{LABEL}.plist"))); + } +} diff --git a/src-tauri/src/capture.rs b/src-tauri/src/capture.rs index bbe45c2..f1200a9 100644 --- a/src-tauri/src/capture.rs +++ b/src-tauri/src/capture.rs @@ -1,7 +1,6 @@ use std::{ fs, path::{Path, PathBuf}, - process::Command, }; use chrono::Local; @@ -89,7 +88,7 @@ fn capture_screen(app: &AppHandle, monitor: &tauri::Monitor) -> Result Result Result<(), String> { + use windows_sys::Win32::Graphics::Gdi::{ + BitBlt, CreateCompatibleBitmap, CreateCompatibleDC, DeleteDC, DeleteObject, GetDC, + GetDIBits, ReleaseDC, SelectObject, BITMAPINFO, BITMAPINFOHEADER, BI_RGB, CAPTUREBLT, + DIB_RGB_COLORS, HGDIOBJ, SRCCOPY, + }; + use windows_sys::Win32::UI::HiDpi::{ + SetThreadDpiAwarenessContext, DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2, + }; + // Capture only the monitor under the cursor. Grabbing the virtual desktop + // would make a mixed-DPI or multi-monitor setup produce an image whose + // origin no longer matches the overlay window. + let x = monitor.position().x; + let y = monitor.position().y; + let width = monitor.size().width as i32; + let height = monitor.size().height as i32; + unsafe { + // Tauri's monitor bounds are physical pixels. Pin this worker thread + // to per-monitor-v2 while calling GDI; otherwise GetDC(NULL) may use a + // thread-scaled virtual desktop and the capture no longer aligns with + // the overlay. + let previous_awareness = + SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); + let restore_awareness = || { + if !previous_awareness.is_null() { + SetThreadDpiAwarenessContext(previous_awareness); + } + }; + if width <= 0 || height <= 0 { + restore_awareness(); + return Err("Windows did not report a usable screen size.".into()); + } + let screen = GetDC(std::ptr::null_mut()); + if screen.is_null() { + restore_awareness(); + return Err("Windows could not open the desktop surface.".into()); + } + let memory = CreateCompatibleDC(screen); + let bitmap = CreateCompatibleBitmap(screen, width, height); + if memory.is_null() || bitmap.is_null() { + if !bitmap.is_null() { + DeleteObject(bitmap as HGDIOBJ); + } + if !memory.is_null() { + DeleteDC(memory); + } + ReleaseDC(std::ptr::null_mut(), screen); + restore_awareness(); + return Err("Windows could not allocate a screenshot surface.".into()); + } + let previous = SelectObject(memory, bitmap as HGDIOBJ); + let copied = BitBlt( + memory, + 0, + 0, + width, + height, + screen, + x, + y, + SRCCOPY | CAPTUREBLT, + ); + let mut info = BITMAPINFO { + bmiHeader: BITMAPINFOHEADER { + biSize: std::mem::size_of::() as u32, + biWidth: width, + // Negative height asks GDI for top-down rows, matching PNG. + biHeight: -height, + biPlanes: 1, + biBitCount: 32, + biCompression: BI_RGB, + biSizeImage: 0, + biXPelsPerMeter: 0, + biYPelsPerMeter: 0, + biClrUsed: 0, + biClrImportant: 0, + }, + bmiColors: [windows_sys::Win32::Graphics::Gdi::RGBQUAD { + rgbBlue: 0, + rgbGreen: 0, + rgbRed: 0, + rgbReserved: 0, + }], + }; + let mut pixels = vec![0_u8; width as usize * height as usize * 4]; + let rows = GetDIBits( + memory, + bitmap, + 0, + height as u32, + pixels.as_mut_ptr().cast(), + &mut info, + DIB_RGB_COLORS, + ); + SelectObject(memory, previous); + DeleteObject(bitmap as HGDIOBJ); + DeleteDC(memory); + ReleaseDC(std::ptr::null_mut(), screen); + + if copied == 0 || rows == 0 { + restore_awareness(); + return Err("Windows could not copy the screen surface.".into()); + } + for pixel in pixels.chunks_exact_mut(4) { + pixel.swap(0, 2); + pixel[3] = 255; + } + let Some(image) = image::RgbaImage::from_raw(width as u32, height as u32, pixels) else { + restore_awareness(); + return Err("Windows returned an invalid screenshot buffer.".into()); + }; + let result = image + .save_with_format(output, image::ImageFormat::Png) + .map_err(|error| format!("Could not save the screenshot: {error}")); + restore_awareness(); + result + } +} + fn screenshot_directory(app: &AppHandle) -> Result { settings::screenshot_dir(app) } @@ -284,9 +383,13 @@ pub fn get_capture_session( .clone()) } +/// Capture work runs off the main thread: freezing a screen, decoding the grab +/// and writing the crop all take longer than a frame budget. #[tauri::command] -pub fn start_capture(app: AppHandle, mode: String) -> Result { - begin_capture(&app, &mode) +pub async fn start_capture(app: AppHandle, mode: String) -> Result { + tauri::async_runtime::spawn_blocking(move || begin_capture(&app, &mode)) + .await + .map_err(|error| error.to_string())? } #[tauri::command] @@ -300,12 +403,13 @@ pub fn cancel_capture(app: AppHandle) -> Result<(), String> { } #[tauri::command] -pub fn capture_crop( - app: AppHandle, - state: State<'_, AppState>, - rect: NormalizedRect, -) -> Result { - finish_capture(&app, &state, &rect) +pub async fn capture_crop(app: AppHandle, rect: NormalizedRect) -> Result { + tauri::async_runtime::spawn_blocking(move || { + let state = app.state::(); + finish_capture(&app, &state, &rect) + }) + .await + .map_err(|error| error.to_string())? } fn finish_capture( @@ -359,29 +463,45 @@ pub fn capture_fullscreen_core(app: &AppHandle, state: &AppState) -> Result) -> Result { - let result = capture_fullscreen_core(&app, &state); - if result.is_err() { - if let Some(window) = app.get_webview_window("main") { - let _ = window.show(); - let _ = window.set_focus(); +pub async fn capture_fullscreen(app: AppHandle) -> Result { + let result = tauri::async_runtime::spawn_blocking(move || { + let state = app.state::(); + let result = capture_fullscreen_core(&app, &state); + if result.is_err() { + if let Some(window) = app.get_webview_window("main") { + let _ = window.show(); + let _ = window.set_focus(); + } } - } + result + }) + .await + .map_err(|error| error.to_string())?; result } #[tauri::command] -pub fn copy_file_to(source: String, destination: String) -> Result<(), String> { - fs::copy(Path::new(&source), Path::new(&destination)) - .map(|_| ()) - .map_err(|error| error.to_string()) +pub async fn copy_file_to(source: String, destination: String) -> Result<(), String> { + tauri::async_runtime::spawn_blocking(move || { + fs::copy(Path::new(&source), Path::new(&destination)) + .map(|_| ()) + .map_err(|error| error.to_string()) + }) + .await + .map_err(|error| error.to_string())? } #[tauri::command] -pub fn open_screen_recording_settings() -> Result<(), String> { +pub async fn open_screen_recording_settings() -> Result<(), String> { + tauri::async_runtime::spawn_blocking(open_screen_recording_settings_sync) + .await + .map_err(|error| error.to_string())? +} + +fn open_screen_recording_settings_sync() -> Result<(), String> { #[cfg(target_os = "macos")] { - let status = Command::new("/usr/bin/open") + let status = crate::process::command("/usr/bin/open") .arg("x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture") .status() .map_err(|error| error.to_string())?; diff --git a/src-tauri/src/drag_bridge.rs b/src-tauri/src/drag_bridge.rs index c45a346..d3e883c 100644 --- a/src-tauri/src/drag_bridge.rs +++ b/src-tauri/src/drag_bridge.rs @@ -3,7 +3,7 @@ use std::thread; #[cfg(target_os = "macos")] use std::{ io::{BufRead, BufReader}, - process::{Command, Stdio}, + process::Stdio, }; use serde::Serialize; @@ -93,7 +93,7 @@ pub fn spawn(app: &AppHandle) { eprintln!("Shift drag bridge was not found. Shift-drag will be unavailable."); return; }; - let mut child = match Command::new(path) + let mut child = match crate::process::command(path) .stdout(Stdio::piped()) .stderr(Stdio::null()) .spawn() @@ -129,7 +129,7 @@ fn spawn_windows_monitor(app: &AppHandle) { Foundation::POINT, UI::{ Input::KeyboardAndMouse::{ - GetAsyncKeyState, VK_CONTROL, VK_LBUTTON, VK_RBUTTON, VK_SHIFT, + GetAsyncKeyState, VK_LBUTTON, VK_MENU, VK_RBUTTON, VK_SHIFT, }, WindowsAndMessaging::{ GetCursorPos, GetSystemMetrics, SM_CXDRAG, SM_CYDRAG, SM_SWAPBUTTON, @@ -144,6 +144,9 @@ fn spawn_windows_monitor(app: &AppHandle) { let mut tools_mode = false; let mut down = POINT { x: 0, y: 0 }; let mut ignore_press = false; + // Gesture tracing is edge-triggered so the log stays readable on a 33ms poll. + let mut reported_disabled = false; + let mut reported_foreground = false; loop { let swapped = unsafe { GetSystemMetrics(SM_SWAPBUTTON) } != 0; @@ -155,16 +158,18 @@ fn spawn_windows_monitor(app: &AppHandle) { } if !mouse_down { + // Release ends the gesture. macOS gets an explicit `cancel` from its + // bridge, but on Windows the ring only closes when a WebView2 OLE drop + // or leave arrives, so a drag that Explorer never turned into an OLE + // session (text, a shortcut, a drag started outside a shell folder) + // would leave the ring on screen for good. Let the webview decide: it + // knows whether OLE ever handed it files. if armed { - armed = false; - let window = app.get_webview_window("radial"); - if let Some(window) = window { - thread::spawn(move || { - thread::sleep(std::time::Duration::from_millis(360)); - let _ = window.hide(); - }); + if let Some(window) = app.get_webview_window("radial") { + let _ = window.emit("radial://dragend", ()); } } + armed = false; mouse_was_down = false; thread::sleep(std::time::Duration::from_millis(33)); continue; @@ -176,20 +181,31 @@ fn spawn_windows_monitor(app: &AppHandle) { ignore_press = foreground_is_self(); } if ignore_press { + if !reported_foreground { + reported_foreground = true; + eprintln!("shift: press started inside Shift itself, ignoring this gesture"); + } thread::sleep(std::time::Duration::from_millis(33)); continue; } + reported_foreground = false; let shift = unsafe { GetAsyncKeyState(VK_SHIFT as i32) } as u16 & 0x8000 != 0; - let control = unsafe { GetAsyncKeyState(VK_CONTROL as i32) } as u16 & 0x8000 != 0; + // Keep the drag gesture consistent with the Alt+Shift global shortcuts. + let alt = unsafe { GetAsyncKeyState(VK_MENU as i32) } as u16 & 0x8000 != 0; let threshold_x = unsafe { GetSystemMetrics(SM_CXDRAG) }.max(4); let threshold_y = unsafe { GetSystemMetrics(SM_CYDRAG) }.max(4); if !armed { if !drag_enabled(&app) { + if !reported_disabled { + reported_disabled = true; + eprintln!("shift: drag ring is disabled in settings, ignoring the gesture"); + } thread::sleep(std::time::Duration::from_millis(100)); continue; } + reported_disabled = false; if !shift { thread::sleep(std::time::Duration::from_millis(33)); continue; @@ -201,12 +217,18 @@ fn spawn_windows_monitor(app: &AppHandle) { continue; } armed = true; - tools_mode = control; + tools_mode = alt; let (x, y) = physical_to_logical(&app, point); let mode = if tools_mode { "tools" } else { "formats" }; - let _ = show_radial(&app, Vec::new(), mode, Some((x, y)), true); - } else if shift && control != tools_mode { - tools_mode = control; + match show_radial(&app, Vec::new(), mode, Some((x, y)), true) { + Ok(_) => eprintln!( + "shift: armed {mode} ring at logical ({x:.0}, {y:.0}) from physical ({}, {}) with threshold {threshold_x}x{threshold_y}", + point.x, point.y + ), + Err(error) => eprintln!("shift: could not open the {mode} ring: {error}"), + } + } else if shift && alt != tools_mode { + tools_mode = alt; let mode = if tools_mode { "tools" } else { "formats" }; let state = app.state::(); if let Ok(mut current) = state.radial_mode.lock() { @@ -249,7 +271,34 @@ fn physical_to_logical( && point.y <= y + monitor.size().height as i32 }); let scale = monitor.map(|monitor| monitor.scale_factor()).unwrap_or(1.0); - (point.x as f64 / scale, point.y as f64 / scale) + let logical = (point.x as f64 / scale, point.y as f64 / scale); + // set_position(LogicalPosition) is scaled back up by tao using the *window's* + // scale factor, not the cursor monitor's (tao window.rs set_outer_position). On a + // mixed-DPI desktop those differ, which shifts the ring by the ratio between them, + // so record both plus the match that produced the divisor. + let window_scale = app + .get_webview_window("radial") + .and_then(|window| window.scale_factor().ok()) + .unwrap_or(1.0); + let matched = match monitor { + Some(monitor) => format!( + "monitor {}x{} at {},{} scale {}", + monitor.size().width, + monitor.size().height, + monitor.position().x, + monitor.position().y, + monitor.scale_factor() + ), + None => format!( + "no monitor contained the point ({} monitors)", + monitors.len() + ), + }; + eprintln!( + "shift: ring coords physical ({}, {}) -> logical ({:.0}, {:.0}); {matched}; window scale {window_scale}", + point.x, point.y, logical.0, logical.1 + ); + logical } /// Whether the Shift-drag gesture may open a ring. Read by both the macOS @@ -257,7 +306,11 @@ fn physical_to_logical( #[allow(dead_code)] // unused on Linux, which has no drag monitor yet fn drag_enabled(app: &AppHandle) -> bool { app.try_state::() - .map(|state| state.drag_enabled.load(std::sync::atomic::Ordering::Relaxed)) + .map(|state| { + state + .drag_enabled + .load(std::sync::atomic::Ordering::Relaxed) + }) .unwrap_or(true) } @@ -303,14 +356,6 @@ fn handle_bridge_event(app: &AppHandle, event: BridgeEvent) { "radial://move" }; let _ = window.emit(event_name, payload); - - if event.event_type == "end" { - let window = window.clone(); - thread::spawn(move || { - thread::sleep(std::time::Duration::from_millis(500)); - let _ = window.hide(); - }); - } } "mode" => { let mode = if event.tools.unwrap_or(false) { @@ -326,6 +371,7 @@ fn handle_bridge_event(app: &AppHandle, event: BridgeEvent) { let _ = window.emit("radial://mode", mode); } } + "cancel" => hide(app), _ => {} } } @@ -405,6 +451,38 @@ pub fn show_radial( .set_position(LogicalPosition::new(origin_x, origin_y)) .map_err(|error| error.to_string())?; window.show().map_err(|error| error.to_string())?; + #[cfg(target_os = "windows")] + { + let title = window + .title() + .map(|value| format!("{value:?}")) + .unwrap_or_else(|error| format!("error({error})")); + let visible = window + .is_visible() + .map(|value| value.to_string()) + .unwrap_or_else(|error| format!("error({error})")); + let position = window + .outer_position() + .map(|value| format!("{},{}", value.x, value.y)) + .unwrap_or_else(|error| format!("error({error})")); + let size = window + .outer_size() + .map(|value| format!("{}x{}", value.width, value.height)) + .unwrap_or_else(|error| format!("error({error})")); + let scale = window + .scale_factor() + .map(|value| value.to_string()) + .unwrap_or_else(|error| format!("error({error})")); + eprintln!( + "shift: radial window after show: title={title} visible={visible} position={position} size={size} scale={scale}" + ); + } + // A drop has already finished when the main panel opens a ring. Focusing + // that ring enables normal hover and keyboard handling. During a live + // Finder drag the non-activating window must stay unfocused. + if !dragging { + let _ = window.set_focus(); + } window .emit("radial://show", context.clone()) .map_err(|error| error.to_string())?; @@ -422,10 +500,17 @@ pub fn hide(app: &AppHandle) { } } +/// The ring's own commands decode image previews, so they run off the main thread +/// and the ring never waits on a batch of large photos. #[tauri::command] -pub fn get_radial_context( - state: tauri::State<'_, AppState>, -) -> Result, String> { +pub async fn get_radial_context(app: AppHandle) -> Result, String> { + tauri::async_runtime::spawn_blocking(move || radial_context(&app)) + .await + .map_err(|error| error.to_string())? +} + +fn radial_context(app: &AppHandle) -> Result, String> { + let state = app.state::(); let files = state .radial_files .lock() @@ -439,14 +524,15 @@ pub fn get_radial_context( if files.is_empty() { return Ok(None); } + let dragging = *state + .radial_dragging + .lock() + .map_err(|error| error.to_string())?; Ok(Some(RadialContext { file_info: crate::files::inspect_paths(&files)?, files, mode, - dragging: *state - .radial_dragging - .lock() - .map_err(|error| error.to_string())?, + dragging, })) } @@ -457,7 +543,7 @@ pub fn hide_radial(app: AppHandle) -> Result<(), String> { } #[tauri::command] -pub fn show_files_in_radial( +pub async fn show_files_in_radial( app: AppHandle, files: Vec, mode: Option, @@ -465,12 +551,15 @@ pub fn show_files_in_radial( if files.is_empty() { return Err("No files selected.".into()); } - show_radial( - &app, - files, - mode.as_deref().unwrap_or("actions"), - None, - false, - ) + tauri::async_runtime::spawn_blocking(move || { + show_radial( + &app, + files, + mode.as_deref().unwrap_or("actions"), + None, + false, + ) + }) + .await + .map_err(|error| error.to_string())? } - diff --git a/src-tauri/src/editor.rs b/src-tauri/src/editor.rs index 8d94b90..a41eaa8 100644 --- a/src-tauri/src/editor.rs +++ b/src-tauri/src/editor.rs @@ -1,5 +1,8 @@ use std::path::Path; +use std::sync::atomic::{AtomicU64, Ordering}; +use base64::{engine::general_purpose::STANDARD, Engine as _}; +use image::{GenericImageView, Rgba, RgbaImage}; use tauri::{AppHandle, Emitter, Manager}; use crate::{ @@ -63,50 +66,216 @@ pub fn open_in_editor(app: AppHandle, path: String) -> Result String { - value - .as_bytes() - .iter() - .map(|byte| format!("{byte:02x}")) - .collect() +const PIN_LABEL: &str = "pin"; +const PIN_PREVIEW_EDGE: u32 = 1040; +static PIN_SEQUENCE: AtomicU64 = AtomicU64::new(0); +/// Decoding a 100 MP image needs several hundred MiB before it is reduced to +/// the 1040 px preview. Keep the decode bounded so a large photo cannot exhaust +/// memory and stall Windows while the user is pinning it. +const PIN_MAX_PIXELS: u64 = 40_000_000; + +#[derive(Clone, serde::Serialize)] +#[serde(rename_all = "camelCase")] +pub struct PinnedPreview { + pub id: u64, + pub path: String, + pub data_url: String, + pub width: u32, + pub height: u32, +} + +fn pin_preview(path: &Path) -> Result { + let (width, height) = + image::image_dimensions(path).map_err(|error| format!("Could not read image: {error}"))?; + if u64::from(width) * u64::from(height) > PIN_MAX_PIXELS { + return Err("This image is too large to pin safely.".into()); + } + let image = image::open(path).map_err(|error| format!("Could not read image: {error}"))?; + let preview = flatten_for_pin(image.thumbnail(PIN_PREVIEW_EDGE, PIN_PREVIEW_EDGE)); + let (preview_width, preview_height) = preview.dimensions(); + let mut jpeg = Vec::new(); + image::codecs::jpeg::JpegEncoder::new_with_quality(&mut jpeg, 82) + .encode_image(&preview) + .map_err(|error| format!("Could not prepare the pinned image: {error}"))?; + Ok(PinnedPreview { + id: 0, + path: path.to_string_lossy().to_string(), + data_url: format!("data:image/jpeg;base64,{}", STANDARD.encode(jpeg)), + width: preview_width, + height: preview_height, + }) +} + +/// JPEG has no alpha channel. Compose transparent pixels onto white first so +/// screenshots with transparent backgrounds cannot turn black or show a blank +/// surface after the preview is encoded. +fn flatten_for_pin(image: image::DynamicImage) -> image::DynamicImage { + if !image.color().has_alpha() { + return image; + } + let rgba = image.to_rgba8(); + let mut flattened = RgbaImage::new(rgba.width(), rgba.height()); + for (x, y, pixel) in rgba.enumerate_pixels() { + let alpha = f32::from(pixel[3]) / 255.0; + let blend = |channel: u8| -> u8 { + (f32::from(channel) * alpha + 255.0 * (1.0 - alpha)) + .round() + .clamp(0.0, 255.0) as u8 + }; + flattened.put_pixel( + x, + y, + Rgba([blend(pixel[0]), blend(pixel[1]), blend(pixel[2]), 255]), + ); + } + image::DynamicImage::ImageRgba8(flattened) +} + +fn show_on_main(app: &AppHandle) { + let app_for_main = app.clone(); + let _ = app.run_on_main_thread(move || { + let app = app_for_main; + if let Some(window) = app.get_webview_window(PIN_LABEL) { + let _ = window.show(); + let _ = window.set_focus(); + } + }); +} + +fn is_current_pin(requested: Option, current: Option) -> bool { + matches!((requested, current), (Some(requested), Some(current)) if requested == current) } #[tauri::command] -pub fn pin_image(app: AppHandle, path: String) -> Result<(), String> { - let source = Path::new(&path); - if !source.is_file() { - return Err("The image to pin was not found.".into()); - } - let (width, height) = image::image_dimensions(source) - .map_err(|error| format!("Could not read image: {error}"))?; - let max_edge = 520.0_f64; - let scale = (max_edge / width.max(height) as f64).min(1.0); - let window_width = (width as f64 * scale).max(160.0); - let window_height = (height as f64 * scale).max(100.0); - let label = format!("pin-{}", uuid::Uuid::new_v4()); - let url = format!("index.html?view=pin&path={}", hex_encode(&path)); - let mut builder = - tauri::WebviewWindowBuilder::new(&app, &label, tauri::WebviewUrl::App(url.into())) - .title("Shift Pin") - .inner_size(window_width, window_height) - .min_inner_size(120.0, 80.0) - .decorations(false) - .transparent(true) - .always_on_top(true) - .resizable(true) - .skip_taskbar(true) - .shadow(true); - if let Ok(cursor) = app.cursor_position() { - if let Ok(Some(monitor)) = app.primary_monitor() { - let monitor_scale = monitor.scale_factor(); - builder = builder.position( - cursor.x / monitor_scale - window_width / 2.0, - cursor.y / monitor_scale - window_height / 2.0, - ); +pub async fn pin_image(app: AppHandle, path: String) -> Result<(), String> { + let source = Path::new(&path).to_path_buf(); + let mut preview = tauri::async_runtime::spawn_blocking(move || { + if !source.is_file() { + return Err("The image to pin was not found.".to_string()); } + pin_preview(&source) + }) + .await + .map_err(|error| error.to_string())??; + preview.id = PIN_SEQUENCE.fetch_add(1, Ordering::Relaxed) + 1; + let max_edge = 520.0_f64; + let scale = (max_edge / preview.width.max(preview.height) as f64).min(1.0); + let window_width = (preview.width as f64 * scale).max(160.0); + let window_height = (preview.height as f64 * scale).max(100.0); + let window = app + .get_webview_window(PIN_LABEL) + .ok_or_else(|| "Pin window is unavailable.".to_string())?; + window + .set_size(tauri::LogicalSize::new(window_width, window_height)) + .map_err(|error| error.to_string())?; + if let Some((cursor, monitor_scale)) = app.cursor_position().ok().and_then(|cursor| { + window + .monitor_from_point(cursor.x, cursor.y) + .ok() + .flatten() + .map(|monitor| (cursor, monitor.scale_factor())) + }) { + let physical_width = window_width * monitor_scale; + let physical_height = window_height * monitor_scale; + window + .set_position(tauri::PhysicalPosition::new( + (cursor.x - physical_width / 2.0).round() as i32, + (cursor.y - physical_height / 2.0).round() as i32, + )) + .map_err(|error| error.to_string())?; + } + let state = app.state::(); + *state + .pinned_image + .lock() + .map_err(|error| error.to_string())? = Some(preview.clone()); + // The page starts dark and pulls the preview when this window becomes + // visible. Waiting for renderer-side decoding before showing the window + // deadlocks on both WKWebView and WebView2 because hidden webviews throttle + // images, animation frames and events. + window.show().map_err(|error| error.to_string())?; + window.set_focus().map_err(|error| error.to_string())?; + window + .emit("pin://show", preview) + .map_err(|error| error.to_string())?; + Ok(()) +} + +/// Called by the renderer after its bitmap has decoded or failed. A command is +/// used instead of a one-shot event so it cannot arrive before Rust listens. +/// The sequence id keeps a late reply from an older pin from revealing the +/// newer one before it has painted. +#[tauri::command] +pub fn pin_ready(app: AppHandle, id: Option) -> Result<(), String> { + let state = app.state::(); + let current = state + .pinned_image + .lock() + .map_err(|error| error.to_string())? + .as_ref() + .map(|preview| preview.id); + if !is_current_pin(id, current) { + return Ok(()); + } + show_on_main(&app); + Ok(()) +} + +#[tauri::command] +pub fn get_pinned_image( + state: tauri::State<'_, AppState>, +) -> Result, String> { + Ok(state + .pinned_image + .lock() + .map_err(|error| error.to_string())? + .clone()) +} + +#[tauri::command] +pub async fn close_pin(app: AppHandle) -> Result<(), String> { + if let Some(window) = app.get_webview_window(PIN_LABEL) { + window.hide().map_err(|error| error.to_string())?; + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::{flatten_for_pin, is_current_pin}; + use image::{DynamicImage, Rgba, RgbaImage}; + + #[test] + fn only_the_newest_pin_can_reveal_the_window() { + assert!(is_current_pin(Some(4), Some(4))); + assert!(!is_current_pin(None, None)); + assert!(!is_current_pin(None, Some(4))); + assert!(!is_current_pin(Some(3), Some(4))); + assert!(!is_current_pin(Some(4), None)); + } + + #[test] + fn the_pin_window_is_created_before_the_first_pin() { + let config: serde_json::Value = + serde_json::from_str(include_str!("../tauri.conf.json")).unwrap(); + let pin = config["app"]["windows"] + .as_array() + .unwrap() + .iter() + .find(|window| window["label"] == "pin") + .expect("the pin window must exist when the app starts"); + assert_eq!(pin["url"], "index.html?view=pin"); + assert_eq!(pin["visible"], false); + assert_eq!(pin["transparent"], false); + assert_eq!(pin["alwaysOnTop"], true); + } + + #[test] + fn transparent_pixels_are_flattened_onto_white() { + let mut image = RgbaImage::from_pixel(4, 4, Rgba([0, 0, 0, 0])); + image.put_pixel(1, 1, Rgba([10, 20, 30, 128])); + let flattened = flatten_for_pin(DynamicImage::ImageRgba8(image)).to_rgba8(); + assert_eq!(flattened.get_pixel(0, 0).0, [255, 255, 255, 255]); + assert_eq!(flattened.get_pixel(1, 1).0, [132, 137, 142, 255]); } - builder - .build() - .map(|_| ()) - .map_err(|error| error.to_string()) } diff --git a/src-tauri/src/files.rs b/src-tauri/src/files.rs index 6db15d2..46fdf4a 100644 --- a/src-tauri/src/files.rs +++ b/src-tauri/src/files.rs @@ -1,6 +1,7 @@ use std::{ + ffi::OsStr, fs::{self, File}, - io::Cursor, + io::{BufReader, Cursor, Write}, path::{Path, PathBuf}, process::Command, sync::OnceLock, @@ -8,11 +9,15 @@ use std::{ }; use base64::{engine::general_purpose::STANDARD, Engine as _}; -use image::{codecs::jpeg::JpegEncoder, DynamicImage, GenericImageView, ImageFormat, RgbImage}; +use image::{ + codecs::{gif::GifDecoder, jpeg::JpegEncoder}, + AnimationDecoder, Delay, DynamicImage, Frame, GenericImageView, ImageDecoder, ImageFormat, + RgbImage, RgbaImage, +}; use tauri::{AppHandle, Emitter, Manager}; use crate::{ - models::{ActionOutcome, FileInfo}, + models::{ActionOutcome, ActionResultEvent, FileActionProgress, FileInfo}, office, pdf, settings, }; @@ -34,6 +39,20 @@ fn extension_of(path: &Path) -> String { .to_ascii_lowercase() } +fn image_mime(ext: &str) -> &'static str { + match ext { + "png" => "image/png", + "jpg" | "jpeg" => "image/jpeg", + "webp" => "image/webp", + "tiff" | "tif" => "image/tiff", + "bmp" => "image/bmp", + "gif" => "image/gif", + "heic" | "heif" => "image/heic", + "avif" => "image/avif", + _ => "application/octet-stream", + } +} + fn kind_of(ext: &str) -> &'static str { if IMAGE_EXTENSIONS.contains(&ext) { "image" @@ -225,7 +244,7 @@ fn macos_convert(input: &Path, output: &Path, target: &str) -> Result<(), String "pdf" => "pdf", other => other, }; - let status = Command::new("/usr/bin/sips") + let status = crate::process::command("/usr/bin/sips") .args([ "-s", "format", @@ -240,7 +259,7 @@ fn macos_convert(input: &Path, output: &Path, target: &str) -> Result<(), String Ok(()) } else { Err(format!( - "This Mac cannot write {} files. Try PNG or WEBP.", + "This Mac cannot write {} files. Try PNG or JPG.", target.to_ascii_uppercase() )) } @@ -256,7 +275,7 @@ pub(crate) fn system_convert(input: &Path, output: &Path, target: &str) -> Resul let Some(magick) = resolve_tool("magick") else { return Err("HEIC/AVIF export on Windows requires ImageMagick (magick).".into()); }; - let status = Command::new(magick) + let status = crate::process::command(magick) .arg(input) .arg(output) .status() @@ -276,7 +295,7 @@ pub(crate) fn system_convert(input: &Path, output: &Path, target: &str) -> Resul let Some(magick) = resolve_tool("magick") else { return Err("HEIC/AVIF export on Linux requires ImageMagick (magick).".into()); }; - let status = Command::new(magick) + let status = crate::process::command(magick) .arg(input) .arg(output) .status() @@ -298,9 +317,30 @@ fn convert_file(input: &Path, target: &str, output_dir: Option<&Path>) -> Result if direct_formats.contains(&ext) { if let Ok(image) = image::open(input) { - save_image(&image, &output, format, 92)?; + if ext == "gif" { + save_gif_capped(input, &output, GIF_MAX_BYTES)?; + } else { + save_image(&image, &output, format, 92)?; + } return Ok(output); } + if ext == "webp" { + // HEIC/HEIF/AVIF are readable only through the system codec, while + // macOS sips cannot encode WEBP. Decode to PNG, then use the native + // WEBP encoder. + let intermediate = + std::env::temp_dir().join(format!("shift-image-{}.png", uuid::Uuid::new_v4())); + if system_convert(input, &intermediate, "png").is_ok() { + let converted = image::open(&intermediate).ok(); + let _ = fs::remove_file(intermediate); + if let Some(image) = converted { + save_image(&image, &output, format, 92)?; + return Ok(output); + } + } else { + let _ = fs::remove_file(intermediate); + } + } } system_convert(input, &output, ext)?; Ok(output) @@ -313,26 +353,239 @@ pub(crate) fn resolve_tool(name: &str) -> Option { } } #[cfg(target_os = "macos")] - let directories = [ - "/opt/homebrew/bin", - "/usr/local/bin", - "/opt/local/bin", - "/usr/bin", - "/bin", - "/usr/sbin", - "/sbin", - ]; - #[cfg(not(target_os = "macos"))] - let directories = ["/usr/local/bin", "/usr/bin", "/bin", "/usr/sbin", "/sbin"]; + { + let directories = [ + "/opt/homebrew/bin", + "/usr/local/bin", + "/opt/local/bin", + "/usr/bin", + "/bin", + "/usr/sbin", + "/sbin", + ]; + directories + .into_iter() + .map(|directory| Path::new(directory).join(name)) + .find(|candidate| candidate.is_file()) + } + #[cfg(target_os = "windows")] + { + find_windows_tool(name) + } + #[cfg(target_os = "linux")] + { + let directories = ["/usr/local/bin", "/usr/bin", "/bin", "/usr/sbin", "/sbin"]; + directories + .into_iter() + .map(|directory| Path::new(directory).join(name)) + .find(|candidate| candidate.is_file()) + } +} + +pub(crate) fn document_pdf_engine() -> Option { + [ + "xelatex", + "lualatex", + "pdflatex", + "tectonic", + "typst", + "weasyprint", + "wkhtmltopdf", + ] + .into_iter() + .find_map(resolve_tool) +} + +#[cfg(target_os = "windows")] +fn windows_tool_file_names(name: &str) -> Vec { + let mut names = vec![name.to_string()]; + if Path::new(name).extension().is_none() { + names.push(format!("{name}.exe")); + } + names +} + +#[cfg(target_os = "windows")] +fn push_windows_environment_directory( + directories: &mut Vec, + variable: &str, + suffix: &str, +) { + if let Some(root) = std::env::var_os(variable) { + directories.push(PathBuf::from(root).join(suffix)); + } +} + +#[cfg(target_os = "windows")] +fn windows_tool_directories() -> Vec { + let mut directories = Vec::new(); + for (variable, suffix) in [ + ("LOCALAPPDATA", r"Microsoft\WinGet\Links"), + ("LOCALAPPDATA", r"Microsoft\WindowsApps"), + ("LOCALAPPDATA", "Pandoc"), + ("LOCALAPPDATA", r"Programs\Pandoc"), + ("ProgramFiles", "Pandoc"), + ("ProgramW6432", "Pandoc"), + ("ProgramFiles", r"ffmpeg\bin"), + ("ProgramW6432", r"ffmpeg\bin"), + ("ProgramFiles", r"Gyan\FFmpeg\bin"), + ("ProgramW6432", r"Gyan\FFmpeg\bin"), + ("ProgramFiles", r"FFmpeg\bin"), + ("ProgramW6432", r"FFmpeg\bin"), + ("ProgramData", r"chocolatey\bin"), + ("USERPROFILE", r"scoop\shims"), + ] { + push_windows_environment_directory(&mut directories, variable, suffix); + } + + // ImageMagick installs into a versioned folder under Program Files. + for variable in ["ProgramFiles", "ProgramW6432"] { + let Some(root) = std::env::var_os(variable) else { + continue; + }; + let Ok(entries) = fs::read_dir(root) else { + continue; + }; + for entry in entries.flatten() { + let path = entry.path(); + if path.is_dir() + && entry + .file_name() + .to_string_lossy() + .to_ascii_lowercase() + .starts_with("imagemagick-") + { + directories.push(path); + } + } + } directories - .into_iter() - .map(|directory| Path::new(directory).join(name)) - .find(|candidate| candidate.is_file()) +} + +#[cfg(target_os = "windows")] +fn windows_winget_prefixes(name: &str) -> &'static [&'static str] { + match name { + "pandoc" => &["johnmacfarlane.pandoc", "chocolatey.pandoc"], + "ffmpeg" => &["gyan.ffmpeg"], + "pdftoppm" | "pdfimages" | "pdfinfo" => &["oschwartz10612.poppler"], + "magick" => &["imagemagick.imagemagick"], + "typst" => &["typst.typst"], + "winget" => &[], + _ => &[], + } +} + +#[cfg(target_os = "windows")] +fn find_file_below( + directory: &Path, + file_names: &[String], + remaining_depth: usize, +) -> Option { + if remaining_depth == 0 { + return None; + } + let entries = fs::read_dir(directory).ok()?; + let mut directories = Vec::new(); + for entry in entries.flatten() { + let path = entry.path(); + if path.is_dir() { + directories.push(path); + continue; + } + let file_name = entry.file_name().to_string_lossy().into_owned(); + if file_names + .iter() + .any(|candidate| file_name.eq_ignore_ascii_case(candidate)) + { + return Some(path); + } + } + directories.into_iter().find_map(|directory| { + find_file_below(&directory, file_names, remaining_depth.saturating_sub(1)) + }) +} + +#[cfg(target_os = "windows")] +fn find_winget_package_tool(name: &str, file_names: &[String]) -> Option { + let prefixes = windows_winget_prefixes(name); + if prefixes.is_empty() { + return None; + } + let packages = + PathBuf::from(std::env::var_os("LOCALAPPDATA")?).join(r"Microsoft\WinGet\Packages"); + let entries = fs::read_dir(packages).ok()?; + for entry in entries.flatten() { + let package_name = entry.file_name().to_string_lossy().to_ascii_lowercase(); + if !entry.path().is_dir() + || !prefixes + .iter() + .any(|prefix| package_name.starts_with(prefix)) + { + continue; + } + if let Some(found) = find_file_below(&entry.path(), file_names, 6) { + return Some(found); + } + } + None +} + +#[cfg(target_os = "windows")] +fn find_windows_tool(name: &str) -> Option { + static FOUND: OnceLock>> = + OnceLock::new(); + let cache = FOUND.get_or_init(|| std::sync::Mutex::new(std::collections::HashMap::new())); + if let Some(path) = cache.lock().ok().and_then(|tools| tools.get(name).cloned()) { + return Some(path); + } + let found = find_windows_tool_uncached(name); + if let Some(path) = found.as_ref() { + if let Ok(mut tools) = cache.lock() { + tools.insert(name.to_string(), path.clone()); + } + } + found +} + +#[cfg(target_os = "windows")] +fn find_windows_tool_uncached(name: &str) -> Option { + let file_names = windows_tool_file_names(name); + for directory in windows_tool_directories() { + for file_name in &file_names { + let candidate = directory.join(file_name); + if candidate.is_file() { + return Some(candidate); + } + } + } + find_winget_package_tool(name, &file_names) +} + +/// Runs long media and document work below the UI's scheduling priority on +/// macOS, so a CPU-heavy encoder cannot starve the always-on-top radial window. +pub(crate) fn background_command(program: impl AsRef) -> Command { + #[cfg(target_os = "macos")] + { + let mut command = crate::process::command("/usr/bin/nice"); + command.args(["-n", "10"]).arg(program); + command + } + #[cfg(target_os = "windows")] + { + let mut command = crate::process::command(program); + crate::process::background(&mut command); + command + } + #[cfg(not(any(target_os = "macos", target_os = "windows")))] + { + crate::process::command(program) + } } fn ffmpeg_path() -> Result { resolve_tool("ffmpeg").ok_or_else(|| { - "This conversion needs ffmpeg. Install it with Homebrew or add it to your PATH.".to_string() + "This conversion needs ffmpeg. Install it from Settings > Runtime Components or add it to your PATH." + .to_string() }) } @@ -340,10 +593,7 @@ fn ffmpeg_has_encoder(name: &str) -> bool { let Ok(ffmpeg) = ffmpeg_path() else { return false; }; - let Ok(output) = Command::new(ffmpeg) - .args(["-hide_banner", "-encoders"]) - .output() - else { + let Ok(output) = crate::process::output(ffmpeg, &["-hide_banner", "-encoders"]) else { return false; }; String::from_utf8_lossy(&output.stdout).contains(name) @@ -362,6 +612,10 @@ fn video_encoder() -> &'static str { }) } +pub(crate) fn video_encoder_name() -> &'static str { + video_encoder() +} + fn video_encode_args(crf: &'static str, bitrate: &'static str) -> Vec<&'static str> { match video_encoder() { "libx264" => vec![ @@ -382,6 +636,138 @@ fn video_encode_args(crf: &'static str, bitrate: &'static str) -> Vec<&'static s /// Video -> GIF stays small and predictable: first 15 seconds, 12 fps, 480 px wide. const GIF_MAX_SECONDS: &str = "15"; const GIF_FILTER: &str = "fps=12,scale=480:-1:flags=lanczos"; +/// Hard ceiling on the byte size of any GIF Shift writes. +const GIF_MAX_BYTES: u64 = 8 * 1024 * 1024; +/// The same ceiling for the ffmpeg path, passed as `-fs`. +const GIF_MAX_BYTES_FFMPEG: &str = "8388608"; +/// Longest animation kept when re-encoding an existing animated GIF. +const GIF_MAX_MILLIS: u32 = 15_000; +/// Most frames kept when re-encoding an existing animated GIF. +const GIF_MAX_FRAMES: usize = 180; +/// Widest dimension kept when re-encoding an existing animated GIF. +const GIF_MAX_SIDE: u32 = 480; + +/// Writes a GIF that respects the size ceiling. An animated source keeps its +/// frames; the running duration, the frame count and the resulting byte size +/// are all capped so a long recording cannot become a huge file. +fn save_gif_capped(input: &Path, output: &Path, max_bytes: u64) -> Result<(), String> { + let (width, height, frames) = match gif_frames(input) { + Ok(frames) => frames, + // Not an animated GIF: a still image becomes a single-frame GIF. + Err(_) => { + let image = + image::open(input).map_err(|error| format!("Could not read image: {error}"))?; + let rgba = image.to_rgba8(); + let (width, height) = scaled_size(rgba.width(), rgba.height()); + ( + width, + height, + vec![Frame::from_parts( + rgba, + 0, + 0, + Delay::from_numer_denom_ms(0, 1), + )], + ) + } + }; + let buffer = encode_gif_frames(&frames, width, height, max_bytes)?; + let mut file = File::create(output).map_err(|error| describe_write_error(output, error))?; + file.write_all(&buffer) + .map_err(|error| describe_write_error(output, error))?; + Ok(()) +} + +/// Encodes as many frames as fit into the byte ceiling. Frame count is found +/// by bisection because a frame's compressed size is only known once written. +fn encode_gif_frames( + frames: &[Frame], + width: u32, + height: u32, + max_bytes: u64, +) -> Result, String> { + let encode = |count: usize| -> Result, String> { + let mut buffer: Vec = Vec::new(); + { + let mut encoder = image::codecs::gif::GifEncoder::new(&mut buffer); + for frame in frames.iter().take(count) { + let resized = resize_rgba(frame.buffer(), width, height); + encoder + .encode_frame(Frame::from_parts(resized, 0, 0, frame.delay())) + .map_err(|error| format!("Could not write the GIF: {error}"))?; + } + } + Ok(buffer) + }; + + let full = encode(frames.len())?; + if full.len() as u64 <= max_bytes { + return Ok(full); + } + let (mut low, mut high) = (1_usize, frames.len() - 1); + let mut best = encode(1)?; + while low <= high { + let middle = (low + high) / 2; + let candidate = encode(middle)?; + if candidate.len() as u64 <= max_bytes { + best = candidate; + low = middle + 1; + } else { + high = middle.saturating_sub(1); + } + } + Ok(best) +} + +/// Decodes a GIF into frames, capped by duration and frame count. The size of +/// the source decides the output size: still images keep their dimensions, +/// animations are scaled down to the GIF width ceiling. +fn gif_frames(input: &Path) -> Result<(u32, u32, Vec), String> { + let file = File::open(input).map_err(|error| format!("Could not read image: {error}"))?; + let decoder = GifDecoder::new(BufReader::new(file)) + .map_err(|error| format!("Could not read image: {error}"))?; + let (width, height) = decoder.dimensions(); + let mut frames = Vec::new(); + let mut elapsed = 0_u32; + for frame in decoder.into_frames() { + let frame = frame.map_err(|error| format!("Could not read image: {error}"))?; + let delay = frame.delay().numer_denom_ms().0.min(GIF_MAX_MILLIS); + elapsed = elapsed.saturating_add(delay); + if elapsed > GIF_MAX_MILLIS || frames.len() >= GIF_MAX_FRAMES { + break; + } + frames.push(Frame::from_parts( + frame.into_buffer(), + 0, + 0, + Delay::from_numer_denom_ms(delay, 1), + )); + } + if frames.is_empty() { + return Err("Could not read the image frames.".into()); + } + let (width, height) = scaled_size(width, height); + Ok((width, height, frames)) +} + +/// Scales a frame pair down to the GIF width ceiling. +fn scaled_size(width: u32, height: u32) -> (u32, u32) { + if width <= GIF_MAX_SIDE { + return (width.max(1), height.max(1)); + } + let height = (height as f64 * GIF_MAX_SIDE as f64 / width as f64) + .round() + .max(1.0) as u32; + (GIF_MAX_SIDE, height) +} + +/// Resizes a frame to the given size with a smooth filter. +fn resize_rgba(image: &RgbaImage, width: u32, height: u32) -> RgbaImage { + if image.width() == width && image.height() == height { + return image.clone(); + } + image::imageops::resize(image, width, height, image::imageops::FilterType::Lanczos3) +} fn convert_media(input: &Path, target: &str, output_dir: Option<&Path>) -> Result { let ffmpeg = ffmpeg_path()?; @@ -395,7 +781,7 @@ fn convert_media(input: &Path, target: &str, output_dir: Option<&Path>) -> Resul other => return Err(format!("Unsupported media format: {other}")), }; let output = unique_output_in(input, output_dir, "", ext)?; - let mut command = Command::new(ffmpeg); + let mut command = background_command(&ffmpeg); command.args(["-y", "-i", input.to_string_lossy().as_ref()]); match target { "gif" => { @@ -403,6 +789,9 @@ fn convert_media(input: &Path, target: &str, output_dir: Option<&Path>) -> Resul // turns into a hundreds-of-megabytes animated file. command.args(["-t", GIF_MAX_SECONDS]); command.args(["-vf", GIF_FILTER, "-loop", "0"]); + // Cap the byte size too: ffmpeg stops writing once the file is + // this big, so a busy screen cannot produce a runaway GIF. + command.args(["-fs", GIF_MAX_BYTES_FFMPEG]); } "m4a" => { command.args(["-vn", "-c:a", "aac", "-b:a", "192k"]); @@ -439,7 +828,7 @@ fn convert_mp3_with_lame(input: &Path, output: &Path) -> Result })?; let ffmpeg = ffmpeg_path()?; let wav = std::env::temp_dir().join(format!("shift-{}.wav", uuid::Uuid::new_v4())); - let status = Command::new(ffmpeg) + let status = background_command(&ffmpeg) .args(["-y", "-i"]) .arg(input) .args(["-vn", "-ac", "2", "-ar", "44100"]) @@ -450,7 +839,7 @@ fn convert_mp3_with_lame(input: &Path, output: &Path) -> Result let _ = fs::remove_file(&wav); return Err("Could not extract audio for MP3 export.".into()); } - let status = Command::new(lame) + let status = background_command(&lame) .args(["--silent", "-b", "192"]) .arg(&wav) .arg(output) @@ -472,7 +861,7 @@ fn compress_media(input: &Path, output_dir: Option<&Path>) -> Result) -> Result Option { +pub(crate) fn doc_render_bridge() -> Option { let triple = match std::env::consts::ARCH { "aarch64" => "aarch64-apple-darwin", "x86_64" => "x86_64-apple-darwin", @@ -515,33 +904,84 @@ fn doc_render_bridge() -> Option { } #[cfg(target_os = "macos")] -fn native_document_pdf(input: &Path, output_dir: Option<&Path>) -> Result, String> { +fn native_document_pdf( + input: &Path, + output_dir: Option<&Path>, + on_progress: Option<&(dyn Fn(u32, u32, bool) + Sync)>, +) -> Result, String> { + if extension_of(input) == "docx" { + let work = std::env::temp_dir().join(format!("shift-docx-pdf-{}", uuid::Uuid::new_v4())); + fs::create_dir_all(&work).map_err(|error| error.to_string())?; + let target_dir = output_dir + .map(Path::to_path_buf) + .unwrap_or_else(|| directory_of(input)); + let result = (|| { + let Some(images) = office::document_page_images(input, &work)? else { + return Ok(None); + }; + let generated = + pdf::images_to_pdf_with_progress(&images, Some(&work), |done, total| { + if let Some(report) = on_progress { + report(done, total, false); + } + })?; + rename_office_output(generated, input, &target_dir).map(Some) + })(); + let _ = fs::remove_dir_all(work); + if let Some(output) = result? { + return Ok(Some(output)); + } + } let Some(renderer) = doc_render_bridge() else { return Ok(None); }; let extension = extension_of(input); let mut source = input.to_path_buf(); let mut temporary = None; + let mut base_dir = input + .parent() + .unwrap_or_else(|| Path::new(".")) + .to_path_buf(); if matches!(extension.as_str(), "md" | "markdown") { - if let Some(pandoc) = resolve_tool("pandoc") { - let path = - std::env::temp_dir().join(format!("shift-doc-{}.html", uuid::Uuid::new_v4())); - let status = Command::new(pandoc) + // Markdown must never reach the renderer as plain text: without that + // step the PDF is a printout of the markup. Pandoc is nicer when it is + // installed, and the built-in writer covers every other machine. + let path = std::env::temp_dir().join(format!("shift-doc-{}.html", uuid::Uuid::new_v4())); + let pandoc_rendered = resolve_tool("pandoc").is_some_and(|pandoc| { + background_command(&pandoc) .arg(input) .args(["-s", "-t", "html", "-o"]) .arg(&path) .status() - .map_err(|error| format!("Could not run pandoc: {error}"))?; - if status.success() && path.exists() { - source = path.clone(); - temporary = Some(path); - } + .map(|status| status.success() && path.exists()) + .unwrap_or(false) + }); + let rendered = pandoc_rendered + || match fs::read_to_string(input) { + Ok(text) => fs::write(&path, crate::markdown::to_html(&text)).is_ok(), + Err(_) => false, + }; + if !rendered { + let _ = fs::remove_file(&path); + return Err(format!( + "Could not render {} as HTML before creating the PDF.", + input.display() + )); } + source = path.clone(); + temporary = Some(path); + // Keep the original folder as the HTML base URL so relative images + // resolve even though the generated HTML lives in a temp directory. + base_dir = input + .parent() + .unwrap_or_else(|| Path::new(".")) + .to_path_buf(); } let output = unique_output_in(input, output_dir, "", "pdf")?; - let status = Command::new(renderer) + let status = background_command(&renderer) .arg(&source) .arg(&output) + .arg(&base_dir) .status() .map_err(|error| format!("Could not start the document renderer: {error}"))?; if let Some(path) = temporary { @@ -554,6 +994,58 @@ fn native_document_pdf(input: &Path, output_dir: Option<&Path>) -> Result