Menu

Guides

Drags into embedded browsers

Decide what another application may drag into a browser embedded in your own window.

App::on_drag_enter decides a drag from another application as it enters a browser embedded in your own window. The drag may carry files from a file manager, a link or selected text.

Chromium asks only about embedded browsers, the ones AppInstance::create_child_browser makes. A drag into one of Kurogane’s own windows isn’t asked about. Its page gets the drop as a browser’s would.

Accept only some files

The hook runs before the page sees the drag. DragDecision::Refuse keeps the drag from the page:

use kurogane::{App, DragDecision, RuntimeError};

fn main() -> Result<(), RuntimeError> {
    let runtime = App::new("content")
        .on_drag_enter(|drag, _app| {
            let images = !drag.files().is_empty()
                && drag.files().iter().all(|file| file.extension().is_some_and(|ext| ext == "png"));
            if images { DragDecision::Allow } else { DragDecision::Refuse }
        })
        .start_embedded()?;

    // Create child browsers in your window and pump from your loop
    runtime.shutdown();
    Ok(())
}
DragDecision What happens
Allow the drag goes on; the page sees it and may take its drop
Refuse the drag stops at the window; the page sees nothing and the pointer shows no drop
Default Kurogane’s policy, which allows the drag

What the hook sees and the page doesn’t

The hook sees the paths of the files dragged. The page never does. It gets a dropped file’s name and contents only.

The hook runs as the drag enters, not when it drops. The user may still move away. Decide there and leave acting on the files to the drop.

Know what is dragged

DragEnter describes the drag:

Method Gives
files() the paths of the files dragged; empty for a drag of no file
link_url() the link dragged
text() the text dragged
origin() the origin of the page the drag enters
browser() the browser the drag enters

Rules for the hook

  • It runs on the UI thread and must not block.
  • A hook that panics refuses the drag.
  • Drags of DevTools and of a page’s own content within the window aren’t asked about.
  • A later call replaces an earlier hook.