DEV Community

Ismoil Vakhidov
Ismoil Vakhidov

Posted on

How Amberflip catches a file drag on Windows, Wayland and macOS

I make Amberflip, a small file converter. You drag a file, tap Shift, and a wheel of formats opens under the pointer. Drop the file on "JPG" and the converted copy appears next to the original. Nothing is uploaded: it all runs on your own computer. The app is written in Rust with egui and runs on Windows, Linux and macOS.

Converting the files turned out to be the easy part. The hard part was noticing that someone is dragging a file at all, while that drag belongs to another app: Explorer, Finder, Nautilus, a browser. This post is about how that works on each system, and about how the same engine became an MCP server for Claude and other AI apps.

The problem

winit 0.30, which egui runs on, reports hovered and dropped files on Windows and X11, but not on native Wayland. And no toolkit tells you where the pointer is during a drag that started in another application. So every system needed its own sensor.

Windows: a low-level mouse hook

A tiny resident process installs a WH_MOUSE_LL hook. It arms when the left button goes down over an Explorer file view (DirectUIHWND or SysListView32 inside CabinetWClass, Progman or WorkerW) and fires once the pointer has travelled past the drag threshold.

Things I learned on the way:

  • Windows silently removes a hook that stalls for about 300 ms. The callback does no I/O at all.
  • The wheel must not steal focus from the app you are dragging from. The window uses WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW. An unfocused window gets no key events, so the keys that switch rings are read with GetAsyncKeyState.
  • Transparency is not guaranteed. On a machine without a usable GPU (many VMs), OpenGL is missing and wgpu falls back to Direct3D 12 on WARP, whose swap chain has no per-pixel alpha. The "transparent" window comes out black. The fallback is an opaque wheel with the window cut to a circle by SetWindowRgn.
  • Elevation matters. Windows blocks drag and drop from an elevated window to a normal one, so a drag from an Explorer window running as administrator can't land on the wheel.

Wayland: there is no global drag detection

On Wayland this is by design: one client must not see another client's drag. So I looked for the closest legal equivalent.

Hyprland, Sway, KDE. The app puts a 2-pixel wlr-layer-shell strip on one screen edge, on the overlay layer, with a transparent buffer. When a file drag touches the strip, the compositor sends wl_data_device.enter with text/uri-list on offer, and the daemon opens the wheel at the pointer. On Hyprland the pointer position comes from its IPC socket, which keeps answering during a drag. The edge is configurable, because the strip also swallows clicks on the outermost 2 pixels.

Receiving the drop. winit can't receive a drop on Wayland either, so Amberflip binds a second wl_data_device on winit's own wl_display and filters events by the window's wl_surface. One detail matters a lot here: it accepts only the copy action, never move. With move, the source app deletes the original file after the drop is finished.

GNOME. Mutter has no layer-shell, and clients can't choose where their windows go. But Mutter emits dnd-enter and dnd-position-change(x, y) for every Wayland drag grab, so a small GNOME Shell extension forwards those signals over D-Bus and centres the wheel on the pointer. The catch: the extension can't tell a file drag from a text or tab drag, so the wheel closes by itself if nothing arrives.

Testing without a hand on the mouse. A small example program owns a text/uri-list data source and drives a zwlr_virtual_pointer: press, start the drag, move along a line through the screen edge onto a wheel sector, release. Hyprland accepts virtual-pointer input, so the whole path runs without uinput or manual dragging.

macOS: no permissions at all

I wanted no Accessibility or Input Monitoring prompt, and it worked out:

  • A global NSEvent monitor watches for the left mouse button going down. While it is held, the app reads the drag pasteboard every 25 ms. A new change count with public.file-url on it means files are being dragged. macOS 26 doesn't always bump the count, so a window at the drag-image level (seen through CGWindowListCopyWindowInfo) also counts.
  • Modifier keys are polled with CGEventSourceFlagsState, which needs no permission either.
  • The wheel is a borderless transparent window at NSStatusWindowLevel, on every Space and over full-screen apps, with the Accessory activation policy so Finder keeps focus.

One engine, three doors

The conversion engine is a crate with no UI and no OS calls. The same engine sits behind the wheel, behind a command line tool, and behind an MCP server:

npx amberflip-cli convert photo.heic --to jpg
npx amberflip-cli compress talk.mp4 --target 40% --height 720
Enter fullscreen mode Exit fullscreen mode

Giving it to AI apps over MCP

The MCP server has 38 tools: convert, compress, trim and join video, fix audio, crop and annotate pictures, merge, split and redact PDFs, strip metadata, zip and unzip. A few rules shaped it:

  • Paths in, paths out. The AI app and Amberflip share the disk, not the bytes. Answers contain the paths of the new files, never the contents of your files.
  • Never overwrite. Results are new files next to the originals, or in a folder you choose.
  • Look before you cut. The server's instructions tell the model to call inspect before an edit that needs numbers (a picture's size, a video's length, a PDF's page count), and list_operations to see what applies. An operation whose converter is missing (FFmpeg, Ghostscript, LibreOffice) simply isn't listed.
  • No port. It talks over stdio only.

Distribution uses the same pattern as esbuild: the amberflip-cli npm package lists one binary package per platform as optionalDependencies, so npm fetches only the binary for your system. bunx works too.

Claude Code:

claude mcp add --transport stdio --scope user amberflip -- npx -y amberflip-cli mcp
Enter fullscreen mode Exit fullscreen mode

Claude Desktop, Cursor and other clients:

{
  "mcpServers": {
    "amberflip": {
      "command": "npx",
      "args": ["-y", "amberflip-cli", "mcp"]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Then ask: "Make these 12 phone photos JPG, remove the GPS location, and put them in one ZIP." The files never leave your computer.

Try it

Amberflip is a paid app: free for 3 days, then a one-time licence key, no subscription. The app is at amberflip.com. Install guides for AI apps, the MCP manifests and the skills are on GitHub. That repository has docs, not the source code.

If you know a cleaner way to notice a global file drag on KDE or Sway than a 2-pixel edge strip, I'd really like to hear it in the comments.

I wrote this post with help from an AI assistant.

Top comments (1)

Collapse
 
mohakbajaj profile image
Mohak Bajaj •

The "one Rust core crate, three doors" split (desktop app, CLI, stdio MCP) is the exact layout I landed on when I rewrote my Mac capture app in Rust. Keeping the MCP layer paths-in, paths-out and non-destructive (mine skips a delete tool altogether and only allows archiving) makes it way easier to let agents touch real files on disk without worrying about them wiping anything. Also good to know about the macOS 26 drag pasteboard change count quirk and falling back to CGWindowListCopyWindowInfo.