1
0
Fork 0

Run rustfmt (#154)

This commit is contained in:
Micah Johnston 2023-12-12 10:41:03 -06:00 committed by GitHub
parent d8e3b52acd
commit d08f7afd49
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 91 additions and 79 deletions

View file

@ -1,3 +1,3 @@
fn_args_layout = "Compressed" fn_params_layout = "Compressed"
use_small_heuristics = "Max" use_small_heuristics = "Max"
use_field_init_shorthand = true use_field_init_shorthand = true

View file

@ -157,6 +157,6 @@ pub enum EventStatus {
/// plugin window is in focus. /// plugin window is in focus.
Ignored, Ignored,
/// We are prepared to handle the data in the drag and dropping will /// We are prepared to handle the data in the drag and dropping will
/// result in [DropEffect] /// result in [DropEffect]
AcceptDrop(DropEffect), AcceptDrop(DropEffect),
} }

View file

@ -5,9 +5,8 @@ mod window;
use cocoa::foundation::NSUInteger; use cocoa::foundation::NSUInteger;
pub use window::*; pub use window::*;
const NSDragOperationNone: NSUInteger = 0; const NSDragOperationNone: NSUInteger = 0;
const NSDragOperationCopy: NSUInteger = 1; const NSDragOperationCopy: NSUInteger = 1;
const NSDragOperationLink: NSUInteger = 2; const NSDragOperationLink: NSUInteger = 2;
const NSDragOperationGeneric: NSUInteger = 4; const NSDragOperationGeneric: NSUInteger = 4;
const NSDragOperationMove: NSUInteger = 16; const NSDragOperationMove: NSUInteger = 16;

View file

@ -1,6 +1,6 @@
use std::ffi::c_void; use std::ffi::c_void;
use cocoa::appkit::{NSEvent, NSView, NSWindow, NSFilenamesPboardType}; use cocoa::appkit::{NSEvent, NSFilenamesPboardType, NSView, NSWindow};
use cocoa::base::{id, nil, BOOL, NO, YES}; use cocoa::base::{id, nil, BOOL, NO, YES};
use cocoa::foundation::{NSArray, NSPoint, NSRect, NSSize, NSUInteger}; use cocoa::foundation::{NSArray, NSPoint, NSRect, NSSize, NSUInteger};
@ -15,13 +15,16 @@ use uuid::Uuid;
use crate::MouseEvent::{ButtonPressed, ButtonReleased}; use crate::MouseEvent::{ButtonPressed, ButtonReleased};
use crate::{ use crate::{
Event, EventStatus, MouseButton, MouseEvent, Point, ScrollDelta, Size, WindowEvent, WindowInfo, DropData, DropEffect, Event, EventStatus, MouseButton, MouseEvent, Point, ScrollDelta, Size,
WindowOpenOptions, DropData, DropEffect, WindowEvent, WindowInfo, WindowOpenOptions,
}; };
use super::{NSDragOperationGeneric, NSDragOperationCopy, NSDragOperationMove, NSDragOperationLink, NSDragOperationNone}; use super::keyboard::{from_nsstring, make_modifiers};
use super::keyboard::{make_modifiers, from_nsstring};
use super::window::WindowState; use super::window::WindowState;
use super::{
NSDragOperationCopy, NSDragOperationGeneric, NSDragOperationLink, NSDragOperationMove,
NSDragOperationNone,
};
/// Name of the field used to store the `WindowState` pointer. /// Name of the field used to store the `WindowState` pointer.
pub(super) const BASEVIEW_STATE_IVAR: &str = "baseview_state"; pub(super) const BASEVIEW_STATE_IVAR: &str = "baseview_state";
@ -106,8 +109,11 @@ pub(super) unsafe fn create_view(window_options: &WindowOpenOptions) -> id {
view.initWithFrame_(NSRect::new(NSPoint::new(0., 0.), NSSize::new(size.width, size.height))); view.initWithFrame_(NSRect::new(NSPoint::new(0., 0.), NSSize::new(size.width, size.height)));
let _: id = msg_send![view, registerForDraggedTypes: NSArray::arrayWithObjects(nil, &[NSFilenamesPboardType])]; let _: id = msg_send![
view,
registerForDraggedTypes: NSArray::arrayWithObjects(nil, &[NSFilenamesPboardType])
];
view view
} }
@ -173,10 +179,7 @@ unsafe fn create_view_class() -> &'static Class {
sel!(draggingUpdated:), sel!(draggingUpdated:),
dragging_updated as extern "C" fn(&Object, Sel, id) -> NSUInteger, dragging_updated as extern "C" fn(&Object, Sel, id) -> NSUInteger,
); );
class.add_method( class.add_method(sel!(draggingExited:), dragging_exited as extern "C" fn(&Object, Sel, id));
sel!(draggingExited:),
dragging_exited as extern "C" fn(&Object, Sel, id),
);
add_mouse_button_class_method!(class, mouseDown, ButtonPressed, MouseButton::Left); add_mouse_button_class_method!(class, mouseDown, ButtonPressed, MouseButton::Left);
add_mouse_button_class_method!(class, mouseUp, ButtonReleased, MouseButton::Left); add_mouse_button_class_method!(class, mouseUp, ButtonReleased, MouseButton::Left);
@ -433,7 +436,7 @@ fn on_event(window_state: &mut WindowState, event: MouseEvent) -> NSUInteger {
EventStatus::AcceptDrop(DropEffect::Link) => NSDragOperationLink, EventStatus::AcceptDrop(DropEffect::Link) => NSDragOperationLink,
EventStatus::AcceptDrop(DropEffect::Scroll) => NSDragOperationGeneric, EventStatus::AcceptDrop(DropEffect::Scroll) => NSDragOperationGeneric,
_ => NSDragOperationNone, _ => NSDragOperationNone,
} }
} }
extern "C" fn dragging_entered(this: &Object, _sel: Sel, sender: id) -> NSUInteger { extern "C" fn dragging_entered(this: &Object, _sel: Sel, sender: id) -> NSUInteger {

View file

@ -2,31 +2,51 @@ use std::ffi::OsString;
use std::mem::transmute; use std::mem::transmute;
use std::os::windows::prelude::OsStringExt; use std::os::windows::prelude::OsStringExt;
use std::ptr::null_mut; use std::ptr::null_mut;
use std::rc::{Weak, Rc}; use std::rc::{Rc, Weak};
use winapi::Interface; use winapi::shared::guiddef::{IsEqualIID, REFIID};
use winapi::shared::guiddef::{REFIID, IsEqualIID};
use winapi::shared::minwindef::{DWORD, WPARAM}; use winapi::shared::minwindef::{DWORD, WPARAM};
use winapi::shared::ntdef::{HRESULT, ULONG}; use winapi::shared::ntdef::{HRESULT, ULONG};
use winapi::shared::windef::POINTL; use winapi::shared::windef::POINTL;
use winapi::shared::winerror::{S_OK, E_NOINTERFACE, E_UNEXPECTED}; use winapi::shared::winerror::{E_NOINTERFACE, E_UNEXPECTED, S_OK};
use winapi::shared::wtypes::DVASPECT_CONTENT; use winapi::shared::wtypes::DVASPECT_CONTENT;
use winapi::um::objidl::{IDataObject, FORMATETC, TYMED_HGLOBAL, STGMEDIUM}; use winapi::um::objidl::{IDataObject, FORMATETC, STGMEDIUM, TYMED_HGLOBAL};
use winapi::um::oleidl::{IDropTarget, IDropTargetVtbl, DROPEFFECT_COPY, DROPEFFECT_MOVE, DROPEFFECT_LINK, DROPEFFECT_SCROLL, DROPEFFECT_NONE}; use winapi::um::oleidl::{
IDropTarget, IDropTargetVtbl, DROPEFFECT_COPY, DROPEFFECT_LINK, DROPEFFECT_MOVE,
DROPEFFECT_NONE, DROPEFFECT_SCROLL,
};
use winapi::um::shellapi::DragQueryFileW; use winapi::um::shellapi::DragQueryFileW;
use winapi::um::unknwnbase::{IUnknownVtbl, IUnknown}; use winapi::um::unknwnbase::{IUnknown, IUnknownVtbl};
use winapi::um::winuser::CF_HDROP; use winapi::um::winuser::CF_HDROP;
use winapi::Interface;
use crate::{Point, DropData, MouseEvent, Event, EventStatus, DropEffect, PhyPoint}; use crate::{DropData, DropEffect, Event, EventStatus, MouseEvent, PhyPoint, Point};
use super::WindowState; use super::WindowState;
// These function pointers have to be stored in a (const) variable before they can be transmuted // These function pointers have to be stored in a (const) variable before they can be transmuted
// Transmuting is needed because winapi has a bug where the pt parameter has an incorrect // Transmuting is needed because winapi has a bug where the pt parameter has an incorrect
// type `*const POINTL` // type `*const POINTL`
const DRAG_ENTER_PTR: unsafe extern "system" fn(this: *mut IDropTarget, pDataObj: *const IDataObject, grfKeyState: DWORD, pt: POINTL, pdwEffect: *mut DWORD) -> HRESULT = DropTarget::drag_enter; const DRAG_ENTER_PTR: unsafe extern "system" fn(
const DRAG_OVER_PTR: unsafe extern "system" fn(this: *mut IDropTarget, grfKeyState: DWORD, pt: POINTL, pdwEffect: *mut DWORD) -> HRESULT = DropTarget::drag_over; this: *mut IDropTarget,
const DROP_PTR: unsafe extern "system" fn(this: *mut IDropTarget, pDataObj: *const IDataObject, grfKeyState: DWORD, pt: POINTL, pdwEffect: *mut DWORD) -> HRESULT = DropTarget::drop; pDataObj: *const IDataObject,
grfKeyState: DWORD,
pt: POINTL,
pdwEffect: *mut DWORD,
) -> HRESULT = DropTarget::drag_enter;
const DRAG_OVER_PTR: unsafe extern "system" fn(
this: *mut IDropTarget,
grfKeyState: DWORD,
pt: POINTL,
pdwEffect: *mut DWORD,
) -> HRESULT = DropTarget::drag_over;
const DROP_PTR: unsafe extern "system" fn(
this: *mut IDropTarget,
pDataObj: *const IDataObject,
grfKeyState: DWORD,
pt: POINTL,
pdwEffect: *mut DWORD,
) -> HRESULT = DropTarget::drop;
const DROP_TARGET_VTBL: IDropTargetVtbl = IDropTargetVtbl { const DROP_TARGET_VTBL: IDropTargetVtbl = IDropTargetVtbl {
parent: IUnknownVtbl { parent: IUnknownVtbl {
QueryInterface: DropTarget::query_interface, QueryInterface: DropTarget::query_interface,
@ -71,9 +91,10 @@ impl DropTarget {
unsafe { unsafe {
let mut window = window_state.create_window(); let mut window = window_state.create_window();
let mut window = crate::Window::new(&mut window); let mut window = crate::Window::new(&mut window);
let event = Event::Mouse(event); let event = Event::Mouse(event);
let event_status = window_state.handler_mut().as_mut().unwrap().on_event(&mut window, event); let event_status =
window_state.handler_mut().as_mut().unwrap().on_event(&mut window, event);
if let Some(pdwEffect) = pdwEffect { if let Some(pdwEffect) = pdwEffect {
match event_status { match event_status {
@ -82,8 +103,8 @@ impl DropTarget {
EventStatus::AcceptDrop(DropEffect::Link) => *pdwEffect = DROPEFFECT_LINK, EventStatus::AcceptDrop(DropEffect::Link) => *pdwEffect = DROPEFFECT_LINK,
EventStatus::AcceptDrop(DropEffect::Scroll) => *pdwEffect = DROPEFFECT_SCROLL, EventStatus::AcceptDrop(DropEffect::Scroll) => *pdwEffect = DROPEFFECT_SCROLL,
_ => *pdwEffect = DROPEFFECT_NONE, _ => *pdwEffect = DROPEFFECT_NONE,
} }
} }
} }
} }
@ -105,11 +126,7 @@ impl DropTarget {
tymed: TYMED_HGLOBAL, tymed: TYMED_HGLOBAL,
}; };
let mut medium = STGMEDIUM { let mut medium = STGMEDIUM { tymed: 0, u: null_mut(), pUnkForRelease: null_mut() };
tymed: 0,
u: null_mut(),
pUnkForRelease: null_mut(),
};
unsafe { unsafe {
let hresult = data_object.GetData(&format, &mut medium); let hresult = data_object.GetData(&format, &mut medium);
@ -119,13 +136,13 @@ impl DropTarget {
} }
let hdrop = transmute((*medium.u).hGlobal()); let hdrop = transmute((*medium.u).hGlobal());
let item_count = DragQueryFileW(hdrop, 0xFFFFFFFF, null_mut(), 0); let item_count = DragQueryFileW(hdrop, 0xFFFFFFFF, null_mut(), 0);
if item_count == 0 { if item_count == 0 {
self.drop_data = DropData::None; self.drop_data = DropData::None;
return; return;
} }
let mut paths = Vec::with_capacity(item_count as usize); let mut paths = Vec::with_capacity(item_count as usize);
for i in 0..item_count { for i in 0..item_count {
@ -133,7 +150,12 @@ impl DropTarget {
let buffer_size = characters as usize + 1; let buffer_size = characters as usize + 1;
let mut buffer = Vec::<u16>::with_capacity(buffer_size); let mut buffer = Vec::<u16>::with_capacity(buffer_size);
DragQueryFileW(hdrop, i, transmute(buffer.spare_capacity_mut().as_mut_ptr()), buffer_size as u32); DragQueryFileW(
hdrop,
i,
transmute(buffer.spare_capacity_mut().as_mut_ptr()),
buffer_size as u32,
);
buffer.set_len(buffer_size); buffer.set_len(buffer_size);
paths.push(OsString::from_wide(&buffer[..characters as usize]).into()) paths.push(OsString::from_wide(&buffer[..characters as usize]).into())
@ -144,20 +166,17 @@ impl DropTarget {
} }
unsafe extern "system" fn query_interface( unsafe extern "system" fn query_interface(
this: *mut IUnknown, this: *mut IUnknown, riid: REFIID, ppvObject: *mut *mut winapi::ctypes::c_void,
riid: REFIID, ) -> HRESULT {
ppvObject: *mut *mut winapi::ctypes::c_void, if IsEqualIID(&*riid, &IUnknown::uuidof()) || IsEqualIID(&*riid, &IDropTarget::uuidof()) {
) -> HRESULT
{
if IsEqualIID(&*riid, &IUnknown::uuidof()) || IsEqualIID(&*riid, &IDropTarget::uuidof()){
Self::add_ref(this); Self::add_ref(this);
*ppvObject = this as *mut winapi::ctypes::c_void; *ppvObject = this as *mut winapi::ctypes::c_void;
return S_OK; return S_OK;
} }
return E_NOINTERFACE; return E_NOINTERFACE;
} }
unsafe extern "system" fn add_ref(this: *mut IUnknown) -> ULONG { unsafe extern "system" fn add_ref(this: *mut IUnknown) -> ULONG {
let arc = Rc::from_raw(this); let arc = Rc::from_raw(this);
let result = Rc::strong_count(&arc) + 1; let result = Rc::strong_count(&arc) + 1;
@ -167,7 +186,7 @@ impl DropTarget {
result as ULONG result as ULONG
} }
unsafe extern "system" fn release(this: *mut IUnknown) -> ULONG { unsafe extern "system" fn release(this: *mut IUnknown) -> ULONG {
let arc = Rc::from_raw(this); let arc = Rc::from_raw(this);
let result = Rc::strong_count(&arc) - 1; let result = Rc::strong_count(&arc) - 1;
@ -177,21 +196,18 @@ impl DropTarget {
result as ULONG result as ULONG
} }
unsafe extern "system" fn drag_enter( unsafe extern "system" fn drag_enter(
this: *mut IDropTarget, this: *mut IDropTarget, pDataObj: *const IDataObject, grfKeyState: DWORD, pt: POINTL,
pDataObj: *const IDataObject,
grfKeyState: DWORD,
pt: POINTL,
pdwEffect: *mut DWORD, pdwEffect: *mut DWORD,
) -> HRESULT ) -> HRESULT {
{
let drop_target = &mut *(this as *mut DropTarget); let drop_target = &mut *(this as *mut DropTarget);
let Some(window_state) = drop_target.window_state.upgrade() else { let Some(window_state) = drop_target.window_state.upgrade() else {
return E_UNEXPECTED; return E_UNEXPECTED;
}; };
let modifiers = window_state.keyboard_state().get_modifiers_from_mouse_wparam(grfKeyState as WPARAM); let modifiers =
window_state.keyboard_state().get_modifiers_from_mouse_wparam(grfKeyState as WPARAM);
drop_target.parse_coordinates(pt); drop_target.parse_coordinates(pt);
drop_target.parse_drop_data(&*pDataObj); drop_target.parse_drop_data(&*pDataObj);
@ -205,20 +221,17 @@ impl DropTarget {
drop_target.on_event(Some(pdwEffect), event); drop_target.on_event(Some(pdwEffect), event);
S_OK S_OK
} }
unsafe extern "system" fn drag_over( unsafe extern "system" fn drag_over(
this: *mut IDropTarget, this: *mut IDropTarget, grfKeyState: DWORD, pt: POINTL, pdwEffect: *mut DWORD,
grfKeyState: DWORD, ) -> HRESULT {
pt: POINTL,
pdwEffect: *mut DWORD,
) -> HRESULT
{
let drop_target = &mut *(this as *mut DropTarget); let drop_target = &mut *(this as *mut DropTarget);
let Some(window_state) = drop_target.window_state.upgrade() else { let Some(window_state) = drop_target.window_state.upgrade() else {
return E_UNEXPECTED; return E_UNEXPECTED;
}; };
let modifiers = window_state.keyboard_state().get_modifiers_from_mouse_wparam(grfKeyState as WPARAM); let modifiers =
window_state.keyboard_state().get_modifiers_from_mouse_wparam(grfKeyState as WPARAM);
drop_target.parse_coordinates(pt); drop_target.parse_coordinates(pt);
@ -231,27 +244,24 @@ impl DropTarget {
drop_target.on_event(Some(pdwEffect), event); drop_target.on_event(Some(pdwEffect), event);
S_OK S_OK
} }
unsafe extern "system" fn drag_leave(this: *mut IDropTarget) -> HRESULT { unsafe extern "system" fn drag_leave(this: *mut IDropTarget) -> HRESULT {
let drop_target = &mut *(this as *mut DropTarget); let drop_target = &mut *(this as *mut DropTarget);
drop_target.on_event(None, MouseEvent::DragLeft); drop_target.on_event(None, MouseEvent::DragLeft);
S_OK S_OK
} }
unsafe extern "system" fn drop( unsafe extern "system" fn drop(
this: *mut IDropTarget, this: *mut IDropTarget, pDataObj: *const IDataObject, grfKeyState: DWORD, pt: POINTL,
pDataObj: *const IDataObject,
grfKeyState: DWORD,
pt: POINTL,
pdwEffect: *mut DWORD, pdwEffect: *mut DWORD,
) -> HRESULT ) -> HRESULT {
{
let drop_target = &mut *(this as *mut DropTarget); let drop_target = &mut *(this as *mut DropTarget);
let Some(window_state) = drop_target.window_state.upgrade() else { let Some(window_state) = drop_target.window_state.upgrade() else {
return E_UNEXPECTED; return E_UNEXPECTED;
}; };
let modifiers = window_state.keyboard_state().get_modifiers_from_mouse_wparam(grfKeyState as WPARAM); let modifiers =
window_state.keyboard_state().get_modifiers_from_mouse_wparam(grfKeyState as WPARAM);
drop_target.parse_coordinates(pt); drop_target.parse_coordinates(pt);
drop_target.parse_drop_data(&*pDataObj); drop_target.parse_drop_data(&*pDataObj);

View file

@ -2,7 +2,7 @@ use winapi::shared::guiddef::GUID;
use winapi::shared::minwindef::{ATOM, FALSE, LPARAM, LRESULT, UINT, WPARAM}; use winapi::shared::minwindef::{ATOM, FALSE, LPARAM, LRESULT, UINT, WPARAM};
use winapi::shared::windef::{HWND, RECT}; use winapi::shared::windef::{HWND, RECT};
use winapi::um::combaseapi::CoCreateGuid; use winapi::um::combaseapi::CoCreateGuid;
use winapi::um::ole2::{RegisterDragDrop, OleInitialize, RevokeDragDrop}; use winapi::um::ole2::{OleInitialize, RegisterDragDrop, RevokeDragDrop};
use winapi::um::oleidl::LPDROPTARGET; use winapi::um::oleidl::LPDROPTARGET;
use winapi::um::winuser::{ use winapi::um::winuser::{
AdjustWindowRectEx, CreateWindowExW, DefWindowProcW, DestroyWindow, DispatchMessageW, AdjustWindowRectEx, CreateWindowExW, DefWindowProcW, DestroyWindow, DispatchMessageW,
@ -18,7 +18,7 @@ use winapi::um::winuser::{
XBUTTON1, XBUTTON2, XBUTTON1, XBUTTON2,
}; };
use std::cell::{Cell, RefCell, Ref, RefMut}; use std::cell::{Cell, Ref, RefCell, RefMut};
use std::collections::VecDeque; use std::collections::VecDeque;
use std::ffi::{c_void, OsStr}; use std::ffi::{c_void, OsStr};
use std::marker::PhantomData; use std::marker::PhantomData;