Skip to content

Commit 1ec0327

Browse files
authored
Merge branch 'master' into x11-fix-mods
2 parents 042f0c6 + 3724a00 commit 1ec0327

8 files changed

Lines changed: 67 additions & 15 deletions

File tree

.github/workflows/rust.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,5 @@ jobs:
3131
run: cargo test --workspace --all-targets --all-features --verbose
3232
- name: Check docs
3333
run: cargo doc --examples --all-features --no-deps
34-
- name: Clippy
35-
run: cargo clippy --workspace --all-targets --all-features -- -D warnings
3634
- name: Check Formatting (rustfmt)
3735
run: cargo fmt --all -- --check

src/gl/macos.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// This is required because the objc crate is causing a lot of warnings: https://github.com/SSheldon/rust-objc/issues/125
2+
// Eventually we should migrate to the objc2 crate and remove this.
3+
#![allow(unexpected_cfgs)]
4+
15
use std::ffi::c_void;
26
use std::str::FromStr;
37

@@ -91,7 +95,6 @@ impl GlContext {
9195

9296
view.setWantsBestResolutionOpenGLSurface_(YES);
9397

94-
let () = msg_send![view, retain];
9598
NSOpenGLView::display_(view);
9699
parent_view.addSubview_(view);
97100

src/gl/win.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ impl GlContext {
160160
let symbol = CString::new("wglCreateContextAttribsARB").unwrap();
161161
let addr = wglGetProcAddress(symbol.as_ptr());
162162
if !addr.is_null() {
163+
#[allow(clippy::missing_transmute_annotations)]
163164
Some(std::mem::transmute(addr))
164165
} else {
165166
None
@@ -171,6 +172,7 @@ impl GlContext {
171172
let symbol = CString::new("wglChoosePixelFormatARB").unwrap();
172173
let addr = wglGetProcAddress(symbol.as_ptr());
173174
if !addr.is_null() {
175+
#[allow(clippy::missing_transmute_annotations)]
174176
Some(std::mem::transmute(addr))
175177
} else {
176178
None
@@ -182,13 +184,15 @@ impl GlContext {
182184
let symbol = CString::new("wglSwapIntervalEXT").unwrap();
183185
let addr = wglGetProcAddress(symbol.as_ptr());
184186
if !addr.is_null() {
187+
#[allow(clippy::missing_transmute_annotations)]
185188
Some(std::mem::transmute(addr))
186189
} else {
187190
None
188191
}
189192
};
190193

191194
wglMakeCurrent(hdc_tmp, std::ptr::null_mut());
195+
wglDeleteContext(hglrc_tmp);
192196
ReleaseDC(hwnd_tmp, hdc_tmp);
193197
UnregisterClassW(class as *const WCHAR, hinstance);
194198
DestroyWindow(hwnd_tmp);

src/gl/x11.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ impl GlContext {
9191
if addr.is_null() {
9292
return Err(GlError::CreationFailed(CreationFailedError::GetProcAddressFailed));
9393
} else {
94+
#[allow(clippy::missing_transmute_annotations)]
9495
std::mem::transmute(addr)
9596
}
9697
};
@@ -101,6 +102,7 @@ impl GlContext {
101102
if addr.is_null() {
102103
return Err(GlError::CreationFailed(CreationFailedError::GetProcAddressFailed));
103104
} else {
105+
#[allow(clippy::missing_transmute_annotations)]
104106
std::mem::transmute(addr)
105107
}
106108
};

src/macos/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// This is required because the objc crate is causing a lot of warnings: https://github.com/SSheldon/rust-objc/issues/125
2+
// Eventually we should migrate to the objc2 crate and remove this.
3+
#![allow(unexpected_cfgs)]
4+
15
mod keyboard;
26
mod view;
37
mod window;

src/macos/view.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,22 @@ extern "C" fn become_first_responder(this: &Object, _sel: Sel) -> BOOL {
245245
let state = unsafe { WindowState::from_view(this) };
246246
let is_key_window = unsafe {
247247
let window: id = msg_send![this, window];
248-
let is_key_window: BOOL = msg_send![window, isKeyWindow];
249-
is_key_window == YES
248+
if window != nil {
249+
let is_key_window: BOOL = msg_send![window, isKeyWindow];
250+
is_key_window == YES
251+
} else {
252+
false
253+
}
250254
};
251255
if is_key_window {
252-
state.trigger_event(Event::Window(WindowEvent::Focused));
256+
state.trigger_deferrable_event(Event::Window(WindowEvent::Focused));
253257
}
254258
YES
255259
}
256260

257261
extern "C" fn resign_first_responder(this: &Object, _sel: Sel) -> BOOL {
258262
let state = unsafe { WindowState::from_view(this) };
259-
state.trigger_event(Event::Window(WindowEvent::Unfocused));
263+
state.trigger_deferrable_event(Event::Window(WindowEvent::Unfocused));
260264
YES
261265
}
262266

@@ -347,8 +351,6 @@ extern "C" fn view_will_move_to_window(this: &Object, _self: Sel, new_window: id
347351
let tracking_areas: *mut Object = msg_send![this, trackingAreas];
348352
let tracking_area_count = NSArray::count(tracking_areas);
349353

350-
let _: () = msg_send![class!(NSEvent), setMouseCoalescingEnabled: NO];
351-
352354
if new_window == nil {
353355
if tracking_area_count != 0 {
354356
let tracking_area = NSArray::objectAtIndex(tracking_areas, 0);

src/macos/window.rs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::cell::{Cell, RefCell};
2+
use std::collections::VecDeque;
23
use std::ffi::c_void;
34
use std::ptr;
45
use std::rc::Rc;
@@ -71,7 +72,6 @@ impl WindowInner {
7172
pub(super) fn close(&self) {
7273
if self.open.get() {
7374
self.open.set(false);
74-
7575
unsafe {
7676
// Take back ownership of the NSView's Rc<WindowState>
7777
let state_ptr: *const c_void = *(*self.ns_view).get_ivar(BASEVIEW_STATE_IVAR);
@@ -267,6 +267,7 @@ impl<'a> Window<'a> {
267267
keyboard_state: KeyboardState::new(),
268268
frame_timer: Cell::new(None),
269269
window_info: Cell::new(window_info),
270+
deferred_events: RefCell::default(),
270271
});
271272

272273
let window_state_ptr = Rc::into_raw(Rc::clone(&window_state));
@@ -360,6 +361,9 @@ pub(super) struct WindowState {
360361
frame_timer: Cell<Option<CFRunLoopTimer>>,
361362
/// The last known window info for this window.
362363
pub window_info: Cell<WindowInfo>,
364+
365+
/// Events that will be triggered at the end of `window_handler`'s borrow.
366+
deferred_events: RefCell<VecDeque<Event>>,
363367
}
364368

365369
impl WindowState {
@@ -378,14 +382,34 @@ impl WindowState {
378382
state
379383
}
380384

385+
/// Trigger the event immediately and return the event status.
386+
/// Will panic if `window_handler` is already borrowed (see `trigger_deferrable_event`).
381387
pub(super) fn trigger_event(&self, event: Event) -> EventStatus {
382388
let mut window = crate::Window::new(Window { inner: &self.window_inner });
383-
self.window_handler.borrow_mut().on_event(&mut window, event)
389+
let mut window_handler = self.window_handler.borrow_mut();
390+
let status = window_handler.on_event(&mut window, event);
391+
self.send_deferred_events(window_handler.as_mut());
392+
status
393+
}
394+
395+
/// Trigger the event immediately if `window_handler` can be borrowed mutably,
396+
/// otherwise add the event to a queue that will be cleared once `window_handler`'s mutable borrow ends.
397+
/// As this method might result in the event triggering asynchronously, it can't reliably return the event status.
398+
pub(super) fn trigger_deferrable_event(&self, event: Event) {
399+
if let Ok(mut window_handler) = self.window_handler.try_borrow_mut() {
400+
let mut window = crate::Window::new(Window { inner: &self.window_inner });
401+
window_handler.on_event(&mut window, event);
402+
self.send_deferred_events(window_handler.as_mut());
403+
} else {
404+
self.deferred_events.borrow_mut().push_back(event);
405+
}
384406
}
385407

386408
pub(super) fn trigger_frame(&self) {
387409
let mut window = crate::Window::new(Window { inner: &self.window_inner });
388-
self.window_handler.borrow_mut().on_frame(&mut window);
410+
let mut window_handler = self.window_handler.borrow_mut();
411+
window_handler.on_frame(&mut window);
412+
self.send_deferred_events(window_handler.as_mut());
389413
}
390414

391415
pub(super) fn keyboard_state(&self) -> &KeyboardState {
@@ -419,6 +443,18 @@ impl WindowState {
419443

420444
(*window_state_ptr).frame_timer.set(Some(timer));
421445
}
446+
447+
fn send_deferred_events(&self, window_handler: &mut dyn WindowHandler) {
448+
let mut window = crate::Window::new(Window { inner: &self.window_inner });
449+
loop {
450+
let next_event = self.deferred_events.borrow_mut().pop_front();
451+
if let Some(event) = next_event {
452+
window_handler.on_event(&mut window, event);
453+
} else {
454+
break;
455+
}
456+
}
457+
}
422458
}
423459

424460
unsafe impl<'a> HasRawWindowHandle for Window<'a> {

src/win/drop_target.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::rc::{Rc, Weak};
77
use winapi::shared::guiddef::{IsEqualIID, REFIID};
88
use winapi::shared::minwindef::{DWORD, WPARAM};
99
use winapi::shared::ntdef::{HRESULT, ULONG};
10-
use winapi::shared::windef::POINTL;
10+
use winapi::shared::windef::{POINT, POINTL};
1111
use winapi::shared::winerror::{E_NOINTERFACE, E_UNEXPECTED, S_OK};
1212
use winapi::shared::wtypes::DVASPECT_CONTENT;
1313
use winapi::um::objidl::{IDataObject, FORMATETC, STGMEDIUM, TYMED_HGLOBAL};
@@ -17,7 +17,7 @@ use winapi::um::oleidl::{
1717
};
1818
use winapi::um::shellapi::{DragQueryFileW, HDROP};
1919
use winapi::um::unknwnbase::{IUnknown, IUnknownVtbl};
20-
use winapi::um::winuser::CF_HDROP;
20+
use winapi::um::winuser::{ScreenToClient, CF_HDROP};
2121
use winapi::Interface;
2222

2323
use crate::{DropData, DropEffect, Event, EventStatus, MouseEvent, PhyPoint, Point};
@@ -47,6 +47,8 @@ const DROP_PTR: unsafe extern "system" fn(
4747
pt: POINTL,
4848
pdwEffect: *mut DWORD,
4949
) -> HRESULT = DropTarget::drop;
50+
51+
#[allow(clippy::missing_transmute_annotations)]
5052
const DROP_TARGET_VTBL: IDropTargetVtbl = IDropTargetVtbl {
5153
parent: IUnknownVtbl {
5254
QueryInterface: DropTarget::query_interface,
@@ -112,7 +114,8 @@ impl DropTarget {
112114
let Some(window_state) = self.window_state.upgrade() else {
113115
return;
114116
};
115-
117+
let mut pt = POINT { x: pt.x, y: pt.y };
118+
unsafe { ScreenToClient(window_state.hwnd, &mut pt as *mut POINT) };
116119
let phy_point = PhyPoint::new(pt.x, pt.y);
117120
self.drag_position = phy_point.to_logical(&window_state.window_info());
118121
}

0 commit comments

Comments
 (0)