crabnebula-dev / drag-rs

Draggable for GUI apps
https://crabnebula.dev
Apache License 2.0
48 stars 9 forks source link
draggable linux macos rust tao tauri windows winit wry

drag-rs

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.

This project also includes a Tauri plugin for simplified usage on Tauri apps.

Setup

There's two ways to consume this crate API: from Rust code via the drag crate or from Tauri's frontend via tauri-plugin-drag or tauri-plugin-drag-as-window.

Rust

$ cargo add drag

Tauri Plugin

tauri-plugin-drag

$ cargo add tauri-plugin-drag

pnpm add @crabnebula/tauri-plugin-drag
# or
npm add @crabnebula/tauri-plugin-drag
# or
yarn add @crabnebula/tauri-plugin-drag

src-tauri/src/main.rs

fn main() {
    tauri::Builder::default()
        .plugin(tauri_plugin_drag::init())
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
import { startDrag } from "@crabnebula/tauri-plugin-drag";
startDrag({ item: ['/path/to/drag/file'], icon: '/path/to/icon/image' })

tauri-plugin-drag-as-window

$ cargo add tauri-plugin-drag-as-window

pnpm add @crabnebula/tauri-plugin-drag-as-window
# or
npm add @crabnebula/tauri-plugin-drag-as-window
# or
yarn add @crabnebula/tauri-plugin-drag-as-window

src-tauri/src/main.rs

fn main() {
    tauri::Builder::default()
        .plugin(tauri_plugin_drag_as_window::init())
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
import { dragAsWindow, dragBack } from "@crabnebula/tauri-plugin-drag-as-window";
import { appWindow, WebviewWindow } from "@tauri-apps/api/window";
// alternatively you can pass a DOM element instead of its selector
dragAsWindow('#my-drag-element', (payload) => {
  console.log('dropped!')
  // create the window with the content from the current element (that's is up to you!)
  new WebviewWindow('label', {
    x: payload.cursorPos.x,
    y: payload.cursorPos.y,
  })
})

const el = document.querySelector('#my-drag-element')
el.ondragstart = (event) => {
  event.preventDefault()

  dragBack(event.target, { data: 'some data' }, (payload) => {
    appWindow.close()
  })
}

Examples

Running the examples:

cargo run --bin [tauri-app|winit-app|tao-app|wry-app]

Additional drag as window examples are available for tauri and wry:

cargo run --bin [tauri-app-dragout|wry-app-dragout]

Licenses

MIT or MIT/Apache 2.0 where applicable.