Skip to content

Add file drag and drop support #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ include = ["../LICENSE-APACHE", "../LICENSE-MIT", "**/*.rs", "Cargo.toml"]
[dependencies]
bytemuck = "1.7"
egui = { version = "0.17.0", features = ["convert_bytemuck"] }
miniquad = { version = "=0.3.0-alpha.46" }
miniquad = { version = "0.3.0" }
quad-url = "0.1.0"

# https://github.com/not-fl3/miniquad/issues/172
Expand Down
Binary file modified docs/demo.wasm
Binary file not shown.
27 changes: 27 additions & 0 deletions docs/gl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,33 @@ var importObject = {
}
});

window.ondragover = function(e) {
e.preventDefault();
};

window.ondrop = async function(e) {
e.preventDefault();

wasm_exports.on_files_dropped_start();

for (let file of e.dataTransfer.files) {
const nameLen = file.name.length;
const nameVec = wasm_exports.allocate_vec_u8(nameLen);
const nameHeap = new Uint8Array(wasm_memory.buffer, nameVec, nameLen);
stringToUTF8(file.name, nameHeap, 0, nameLen);

const fileBuf = await file.arrayBuffer();
const fileLen = fileBuf.byteLength;
const fileVec = wasm_exports.allocate_vec_u8(fileLen);
const fileHeap = new Uint8Array(wasm_memory.buffer, fileVec, fileLen);
fileHeap.set(new Uint8Array(fileBuf), 0);

wasm_exports.on_file_dropped(nameVec, nameLen, fileVec, fileLen);
}

wasm_exports.on_files_dropped_finish();
};

window.requestAnimationFrame(animation);
},

Expand Down
38 changes: 38 additions & 0 deletions examples/demo.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use egui::DroppedFile;
use miniquad::Context;
use {egui_miniquad as egui_mq, miniquad as mq};

struct Stage {
egui_mq: egui_mq::EguiMq,
show_egui_demo_windows: bool,
egui_demo_windows: egui_demo_lib::DemoWindows,
color_test: egui_demo_lib::ColorTest,
dropped_files: Vec<DroppedFile>,
}

impl Stage {
Expand All @@ -14,6 +17,7 @@ impl Stage {
show_egui_demo_windows: true,
egui_demo_windows: Default::default(),
color_test: Default::default(),
dropped_files: Default::default(),
}
}
}
Expand Down Expand Up @@ -51,6 +55,36 @@ impl mq::EventHandler for Stage {
self.color_test.ui(ui);
});
});

// Collect dropped files:
if !egui_ctx.input().raw.dropped_files.is_empty() {
self.dropped_files = egui_ctx.input().raw.dropped_files.clone();
}

// Show dropped files (if any):
if !self.dropped_files.is_empty() {
let mut open = true;
egui::Window::new("Dropped files")
.open(&mut open)
.show(egui_ctx, |ui| {
for file in &self.dropped_files {
let mut info = if let Some(path) = &file.path {
path.display().to_string()
} else if !file.name.is_empty() {
file.name.clone()
} else {
"???".to_owned()
};
if let Some(bytes) = &file.bytes {
info += &format!(" ({} bytes)", bytes.len());
}
ui.label(info);
}
});
if !open {
self.dropped_files.clear();
}
}
});

// Draw things behind egui here
Expand Down Expand Up @@ -113,6 +147,10 @@ impl mq::EventHandler for Stage {
fn key_up_event(&mut self, _ctx: &mut mq::Context, keycode: mq::KeyCode, keymods: mq::KeyMods) {
self.egui_mq.key_up_event(keycode, keymods);
}

fn files_dropped_event(&mut self, _ctx: &mut Context) {
self.egui_mq.files_dropped_event();
}
}

fn main() {
Expand Down
14 changes: 13 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ mod painter;

// ----------------------------------------------------------------------------

use egui::CursorIcon;
use egui::{CursorIcon, DroppedFile};
use miniquad as mq;

#[cfg(target_os = "macos")] // https://github.com/not-fl3/miniquad/issues/172
Expand Down Expand Up @@ -313,6 +313,18 @@ impl EguiMq {
}
}

/// Call from your [`miniquad::EventHandler`].
pub fn files_dropped_event(&mut self) {
for i in 0..mq::dnd::dropped_file_count() {
self.egui_input.dropped_files.push(DroppedFile {
path: mq::dnd::dropped_file_path(i),
name: "".to_string(),
last_modified: None,
bytes: mq::dnd::dropped_file_bytes(i).map(|bytes| bytes.into()),
})
};
}

#[cfg(not(target_os = "macos"))]
fn set_clipboard(&mut self, mq_ctx: &mut mq::Context, text: String) {
mq::clipboard::set(mq_ctx, text.as_str());
Expand Down