{"id":15231952,"url":"https://github.com/crabnebula-dev/drag-rs","last_synced_at":"2025-04-10T04:56:27.451Z","repository":{"id":209663490,"uuid":"716056696","full_name":"crabnebula-dev/drag-rs","owner":"crabnebula-dev","description":"Draggable for GUI apps","archived":false,"fork":false,"pushed_at":"2024-04-25T11:30:30.000Z","size":10594,"stargazers_count":36,"open_issues_count":4,"forks_count":6,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-04-25T12:29:35.608Z","etag":null,"topics":["draggable","linux","macos","rust","tao","tauri","windows","winit","wry"],"latest_commit_sha":null,"homepage":"https://crabnebula.dev","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/crabnebula-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.spdx","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-11-08T11:30:47.000Z","updated_at":"2024-04-25T12:29:39.324Z","dependencies_parsed_at":"2024-04-25T12:42:53.009Z","dependency_job_id":null,"html_url":"https://github.com/crabnebula-dev/drag-rs","commit_stats":null,"previous_names":["crabnebula-dev/drag-rs"],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crabnebula-dev%2Fdrag-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crabnebula-dev%2Fdrag-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crabnebula-dev%2Fdrag-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crabnebula-dev%2Fdrag-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crabnebula-dev","download_url":"https://codeload.github.com/crabnebula-dev/drag-rs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248161261,"owners_count":21057554,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["draggable","linux","macos","rust","tao","tauri","windows","winit","wry"],"created_at":"2024-09-29T04:06:27.482Z","updated_at":"2025-04-10T04:56:27.263Z","avatar_url":"https://github.com/crabnebula-dev.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# drag-rs\n\nStart a drag operation out of a window on macOS, Windows and Linux (via GTK).\n\nTested for [tao](https://github.com/tauri-apps/tao) (latest), [winit](https://github.com/rust-windowing/winit) (latest), [wry](https://github.com/tauri-apps/wry) (v0.24) and [tauri](https://github.com/tauri-apps/tauri) (v1) windows.\nDue to the GTK-based implementation, winit currently cannot leverage this crate on Linux yet.\n\nThis project also includes a Tauri plugin for simplified usage on Tauri apps.\n\n## Setup\n\nThere'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`.\n\n### Rust\n\n- Add the `drag` dependency:\n\n`$ cargo add drag`\n\n- Define the drag item and preview icon:\n\n  ```rust\n  let item = drag::DragItem::Files(vec![std::fs::canonicalize(\"./examples/icon.png\").unwrap()]);\n  let preview_icon = drag::Image::Raw(include_bytes!(\"../../icon.png\").to_vec());\n  ```\n\n- Use the `drag::start_drag` function. It takes a `\u0026T: raw_window_handle::HasRawWindowHandle` type on macOS and Windows, and a `\u0026gtk::ApplicationWindow` on Linux:\n\n  - tao:\n  ```rust\n  let event_loop = tao::event_loop::EventLoop::new();\n  let window = tao::window::WindowBuilder::new().build(\u0026event_loop).unwrap();\n\n  drag::start_drag(\n    #[cfg(target_os = \"linux\")]\n    {\n      use tao::platform::unix::WindowExtUnix;\n      window.gtk_window()\n    },\n    #[cfg(not(target_os = \"linux\"))]\n    \u0026window,\n    item,\n    preview_icon,\n  );\n  ```\n\n  - wry:\n  ```rust\n  let event_loop = wry::application::event_loop::EventLoop::new();\n  let window = wry::application::window::WindowBuilder::new().build(\u0026event_loop).unwrap();\n  let webview = wry::webview::WebViewBuilder::new(window).unwrap().build().unwrap();\n\n  drag::start_drag(\n    #[cfg(target_os = \"linux\")]\n    {\n      use wry::application::platform::unix::WindowExtUnix;\n      webview.window().gtk_window()\n    },\n    #[cfg(not(target_os = \"linux\"))]\n    \u0026webview.window(),\n    item,\n    preview_icon,\n  );\n  ```\n\n  - winit:\n  ```rust\n  let event_loop = winit::event_loop::EventLoop::new().unwrap();\n  let window = winit::window::WindowBuilder::new().build(\u0026event_loop).unwrap();\n  let _ = drag::start_drag(\u0026window, item, preview_icon);\n  ```\n\n  - tauri:\n  ```rust\n  tauri::Builder::default()\n    .setup(|app| {\n      let window = app.get_window(\"main\").unwrap();\n\n      drag::start_drag(\n        #[cfg(target_os = \"linux\")]\n        \u0026window.gtk_window()?,\n        #[cfg(not(target_os = \"linux\"))]\n        \u0026window,\n        item,\n        preview_icon\n      );\n\n      Ok(())\n    })\n  ```\n\n### Tauri Plugin\n\n#### tauri-plugin-drag\n\n- Add the `tauri-plugin-drag` dependency:\n\n`$ cargo add tauri-plugin-drag`\n\n- Install the `@crabnebula/tauri-plugin-drag` NPM package containing the API bindings:\n\n```sh\npnpm add @crabnebula/tauri-plugin-drag\n# or\nnpm add @crabnebula/tauri-plugin-drag\n# or\nyarn add @crabnebula/tauri-plugin-drag\n```\n\n- Register the core plugin with Tauri:\n\n`src-tauri/src/main.rs`\n\n```rust\nfn main() {\n    tauri::Builder::default()\n        .plugin(tauri_plugin_drag::init())\n        .run(tauri::generate_context!())\n        .expect(\"error while running tauri application\");\n}\n```\n\n- Afterwards all the plugin's APIs are available through the JavaScript guest bindings:\n\n```javascript\nimport { startDrag } from \"@crabnebula/tauri-plugin-drag\";\nstartDrag({ item: ['/path/to/drag/file'], icon: '/path/to/icon/image' })\n```\n\n#### tauri-plugin-drag-as-window\n\n- Add the `tauri-plugin-drag-as-window` dependency:\n\n`$ cargo add tauri-plugin-drag-as-window`\n\n- Install the `@crabnebula/tauri-plugin-drag-as-window` NPM package containing the API bindings:\n\n```sh\npnpm add @crabnebula/tauri-plugin-drag-as-window\n# or\nnpm add @crabnebula/tauri-plugin-drag-as-window\n# or\nyarn add @crabnebula/tauri-plugin-drag-as-window\n```\n\n- Register the core plugin with Tauri:\n\n`src-tauri/src/main.rs`\n\n```rust\nfn main() {\n    tauri::Builder::default()\n        .plugin(tauri_plugin_drag_as_window::init())\n        .run(tauri::generate_context!())\n        .expect(\"error while running tauri application\");\n}\n```\n\n- Afterwards all the plugin's APIs are available through the JavaScript guest bindings:\n\n```javascript\nimport { dragAsWindow, dragBack } from \"@crabnebula/tauri-plugin-drag-as-window\";\nimport { appWindow, WebviewWindow } from \"@tauri-apps/api/window\";\n// alternatively you can pass a DOM element instead of its selector\ndragAsWindow('#my-drag-element', (payload) =\u003e {\n  console.log('dropped!')\n  // create the window with the content from the current element (that's is up to you!)\n  new WebviewWindow('label', {\n    x: payload.cursorPos.x,\n    y: payload.cursorPos.y,\n  })\n})\n\nconst el = document.querySelector('#my-drag-element')\nel.ondragstart = (event) =\u003e {\n  event.preventDefault()\n\n  dragBack(event.target, { data: 'some data' }, (payload) =\u003e {\n    appWindow.close()\n  })\n}\n```\n\n## Examples\n\nRunning the examples:\n\n```sh\ncargo run --bin [tauri-app|winit-app|tao-app|wry-app]\n```\n\nAdditional drag as window examples are available for tauri and wry:\n\n```sh\ncargo run --bin [tauri-app-dragout|wry-app-dragout]\n```\n\n## Licenses\n\nMIT or MIT/Apache 2.0 where applicable.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrabnebula-dev%2Fdrag-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrabnebula-dev%2Fdrag-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrabnebula-dev%2Fdrag-rs/lists"}