From edd00dc563361478c6c0d7c4c8132b119e180f94 Mon Sep 17 00:00:00 2001 From: zyl Date: Thu, 23 Jan 2025 13:54:26 -0800 Subject: [PATCH] Implement drag and drop file support --- src/input.rs | 7 ++++++- src/lib.rs | 27 +++++++++++++++++++++++++++ src/prelude.rs | 2 +- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/input.rs b/src/input.rs index 3f9afd84..29fb1f4b 100644 --- a/src/input.rs +++ b/src/input.rs @@ -2,10 +2,10 @@ use std::collections::HashSet; -use crate::get_context; use crate::prelude::screen_height; use crate::prelude::screen_width; use crate::Vec2; +use crate::{get_context, DroppedFile}; pub use miniquad::{KeyCode, MouseButton}; #[derive(Clone, Copy, Debug, Eq, PartialEq)] @@ -212,6 +212,11 @@ pub fn is_quit_requested() -> bool { get_context().quit_requested } +/// Gets the files which have been dropped on the window. +pub fn get_dropped_files() -> Vec { + get_context().dropped_files() +} + /// Functions for advanced input processing. /// /// Functions in this module should be used by external tools that uses miniquad system, like different UI libraries. User shouldn't use this function. diff --git a/src/lib.rs b/src/lib.rs index c9d5b131..821dd3df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -229,6 +229,8 @@ struct Context { textures: crate::texture::TexturesContext, update_on: conf::UpdateTrigger, + + dropped_files: Vec, } #[derive(Clone)] @@ -367,6 +369,8 @@ impl Context { default_filter_mode, textures: crate::texture::TexturesContext::new(), update_on, + + dropped_files: Vec::new(), } } @@ -385,6 +389,11 @@ impl Context { } } + /// Returns the files which have been dropped onto the window. + pub fn dropped_files(&mut self) -> Vec { + std::mem::take(&mut self.dropped_files) + } + fn begin_frame(&mut self) { telemetry::begin_gpu_query("GPU"); @@ -441,6 +450,8 @@ impl Context { touch.phase = input::TouchPhase::Stationary; } } + + self.dropped_files.clear(); } pub(crate) fn pixel_perfect_projection_matrix(&self) -> glam::Mat4 { @@ -700,6 +711,16 @@ impl EventHandler for Stage { } } + fn files_dropped_event(&mut self) { + let context = get_context(); + for i in 0..miniquad::window::dropped_file_count() { + context.dropped_files.push(DroppedFile { + path: miniquad::window::dropped_file_path(i), + bytes: miniquad::window::dropped_file_bytes(i), + }); + } + } + fn draw(&mut self) { { let _z = telemetry::ZoneGuard::new("Event::draw"); @@ -886,3 +907,9 @@ impl Window { }); } } + +/// Information about a dropped file. +pub struct DroppedFile { + pub path: Option, + pub bytes: Option>, +} diff --git a/src/prelude.rs b/src/prelude.rs index f70b48d2..5a524c36 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -24,6 +24,6 @@ pub use crate::experimental::*; pub use crate::logging::*; -pub use crate::color_u8; +pub use crate::{color_u8, DroppedFile}; pub use image::ImageFormat;