Skip to content

Commit

Permalink
Implement winapi dropping files
Browse files Browse the repository at this point in the history
  • Loading branch information
zyllian committed Jan 24, 2025
1 parent 8b8dc62 commit 06e714d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ winapi = { version = "0.3", features = [
"windowsx",
"winbase",
"hidusage",
"shellapi",
] }

[target.'cfg(target_os = "android")'.dependencies]
Expand Down
23 changes: 22 additions & 1 deletion src/native/windows.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{ffi::OsString, os::windows::ffi::OsStringExt, path::PathBuf};

use crate::{
conf::{Conf, Icon},
event::{KeyMods, MouseButton},
Expand All @@ -8,13 +10,14 @@ use crate::{
use winapi::{
shared::{
hidusage::{HID_USAGE_GENERIC_MOUSE, HID_USAGE_PAGE_GENERIC},
minwindef::{DWORD, HIWORD, LOWORD, LPARAM, LRESULT, UINT, WPARAM},
minwindef::{DWORD, HIWORD, LOWORD, LPARAM, LRESULT, MAX_PATH, TRUE, UINT, WPARAM},
ntdef::NULL,
windef::{HBRUSH, HCURSOR, HDC, HICON, HWND, POINT, RECT},
windowsx::{GET_X_LPARAM, GET_Y_LPARAM},
},
um::{
libloaderapi::{GetModuleHandleW, GetProcAddress},
shellapi::{DragAcceptFiles, DragQueryFileW, HDROP},
shellscalingapi::*,
wingdi::*,
winuser::*,
Expand Down Expand Up @@ -511,6 +514,22 @@ unsafe extern "system" fn win32_wndproc(
WM_EXITSIZEMOVE | WM_EXITMENULOOP => {
KillTimer(hwnd, &mut payload.modal_resizing_timer as *mut _ as usize);
}
WM_DROPFILES => {
let hdrop = wparam as HDROP;
let mut path: [u16; MAX_PATH] = std::mem::uninitialized();

Check warning on line 519 in src/native/windows.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, x86_64-pc-windows-gnu)

use of deprecated function `std::mem::uninitialized`: use `mem::MaybeUninit` instead

Check warning on line 519 in src/native/windows.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, x86_64-pc-windows-gnu)

the type `[u16; 260]` does not permit being left uninitialized

Check warning on line 519 in src/native/windows.rs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, x86_64-pc-windows-msvc)

use of deprecated function `std::mem::uninitialized`: use `mem::MaybeUninit` instead

Check warning on line 519 in src/native/windows.rs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, x86_64-pc-windows-msvc)

the type `[u16; 260]` does not permit being left uninitialized
let num_drops = DragQueryFileW(hdrop, u32::MAX, std::ptr::null_mut(), 0);

let mut d = crate::native_display().lock().unwrap();
for i in 0..num_drops {
let path_len =
DragQueryFileW(hdrop, i, path.as_mut_ptr(), MAX_PATH as u32) as usize;
if path_len > 0 {
let path: PathBuf = OsString::from_wide(&path[0..path_len]).into();
d.dropped_files.bytes.push(std::fs::read(&path).unwrap());
d.dropped_files.paths.push(path);
}
}
}
_ => {}
}

Expand Down Expand Up @@ -693,6 +712,8 @@ unsafe fn create_window(
let dc = GetDC(hwnd);
assert!(dc.is_null() == false);

DragAcceptFiles(hwnd, TRUE);

(hwnd, dc)
}

Expand Down

0 comments on commit 06e714d

Please sign in to comment.