8 releases (3 stable)

2.1.1 May 1, 2026
2.1.0 Feb 24, 2025
2.0.0 Oct 22, 2024
0.4.1 Feb 24, 2025
0.1.0 Nov 10, 2023

#354 in GUI

Download history 1052/week @ 2026-04-21 1023/week @ 2026-04-28 1851/week @ 2026-05-05 2499/week @ 2026-05-12 1785/week @ 2026-05-19 1826/week @ 2026-05-26 1970/week @ 2026-06-02 2445/week @ 2026-06-09 2315/week @ 2026-06-16 1968/week @ 2026-06-23 2866/week @ 2026-06-30 3390/week @ 2026-07-07 4696/week @ 2026-07-14 4144/week @ 2026-07-21 4168/week @ 2026-07-28 6415/week @ 2026-08-04

19,938 downloads per month
Used in 2 crates

Apache-2.0 OR MIT

50KB
860 lines

Start a drag operation out of a window on macOS, Windows and Linux (via GTK).

Tested for tao (latest), winit (latest), wry (v0.24) and tauri (v1) windows.

Due to the GTK-based implementation, winit currently cannot leverage this crate on Linux yet.

  • Add the drag dependency:

$ cargo add drag

  • Use the drag::start_drag function. It takes a &T: raw_window_handle::HasWindowHandle type on macOS and Windows, and a &gtk::ApplicationWindow on Linux:

  • tao:

    let event_loop = tao::event_loop::EventLoop::new();
    let window = tao::window::WindowBuilder::new().build(&event_loop).unwrap();
    
    let item = drag::DragItem::Files(vec![std::fs::canonicalize("./examples/icon.png").unwrap()]);
    let preview_icon = drag::Image::File("./examples/icon.png".into());
    
    drag::start_drag(
      #[cfg(target_os = "linux")]
      {
        use tao::platform::unix::WindowExtUnix;
        window.gtk_window()
      },
      #[cfg(not(target_os = "linux"))]
      &window,
      item,
      preview_icon,
      |result, cursor_position| {
        println!("drag result: {result:?}");
      },
      drag::Options::default(),
    );
    
    • wry:
    let event_loop = tao::event_loop::EventLoop::new();
    let window = tao::window::WindowBuilder::new().build(&event_loop).unwrap();
    let webview = wry::WebViewBuilder::new().build(&window).unwrap();
    
    let item = drag::DragItem::Files(vec![std::fs::canonicalize("./examples/icon.png").unwrap()]);
    let preview_icon = drag::Image::File("./examples/icon.png".into());
    
    drag::start_drag(
      #[cfg(target_os = "linux")]
      {
        use tao::platform::unix::WindowExtUnix;
        window.gtk_window()
      },
      #[cfg(not(target_os = "linux"))]
      &window,
      item,
      preview_icon,
      |result, cursor_position| {
        println!("drag result: {result:?}");
      },
      drag::Options::default(),
    );
    
    • winit:
    let window = ...winit window;
    
    let item = drag::DragItem::Files(vec![std::fs::canonicalize("./examples/icon.png").unwrap()]);
    let preview_icon = drag::Image::File("./examples/icon.png".into());
    
    let _ = drag::start_drag(&window, item, preview_icon, |result, cursor_position| {
      println!("drag result: {result:?}");
    }, Default::default());
    

Dependencies

~0.7–43MB
~619K SLoC