forked from RustAudio/baseview
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathview.rs
More file actions
374 lines (298 loc) · 12.7 KB
/
Copy pathview.rs
File metadata and controls
374 lines (298 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
use std::ffi::c_void;
use cocoa::appkit::{NSEvent, NSView, NSWindow};
use cocoa::base::{id, nil, BOOL, NO, YES};
use cocoa::foundation::{NSArray, NSPoint, NSRect, NSSize};
use objc::{
class,
declare::ClassDecl,
msg_send,
runtime::{Class, Object, Sel},
sel, sel_impl,
};
use uuid::Uuid;
use crate::MouseEvent::{ButtonPressed, ButtonReleased};
use crate::{
Event, EventStatus, MouseButton, MouseEvent, Point, ScrollDelta, Size, WindowEvent, WindowInfo,
WindowOpenOptions,
};
use super::keyboard::make_modifiers;
use super::window::WindowState;
/// Name of the field used to store the `WindowState` pointer.
pub(super) const BASEVIEW_STATE_IVAR: &str = "baseview_state";
macro_rules! add_simple_mouse_class_method {
($class:ident, $sel:ident, $event:expr) => {
#[allow(non_snake_case)]
extern "C" fn $sel(this: &Object, _: Sel, _: id){
let state: &mut WindowState = unsafe {
WindowState::from_field(this)
};
state.trigger_event(Event::Mouse($event));
}
$class.add_method(
sel!($sel:),
$sel as extern "C" fn(&Object, Sel, id),
);
};
}
/// Similar to [add_simple_mouse_class_method!], but this creates its own event object for the
/// press/release event and adds the active modifier keys to that event.
macro_rules! add_mouse_button_class_method {
($class:ident, $sel:ident, $event_ty:ident, $button:expr) => {
#[allow(non_snake_case)]
extern "C" fn $sel(this: &Object, _: Sel, event: id){
let state: &mut WindowState = unsafe {
WindowState::from_field(this)
};
let modifiers = unsafe { NSEvent::modifierFlags(event) };
state.trigger_event(Event::Mouse($event_ty {
button: $button,
modifiers: make_modifiers(modifiers),
}));
}
$class.add_method(
sel!($sel:),
$sel as extern "C" fn(&Object, Sel, id),
);
};
}
macro_rules! add_simple_keyboard_class_method {
($class:ident, $sel:ident) => {
#[allow(non_snake_case)]
extern "C" fn $sel(this: &Object, _: Sel, event: id){
let state: &mut WindowState = unsafe {
WindowState::from_field(this)
};
if let Some(key_event) = state.process_native_key_event(event){
let status = state.trigger_event(Event::Keyboard(key_event));
if let EventStatus::Ignored = status {
unsafe {
let superclass = msg_send![this, superclass];
let () = msg_send![super(this, superclass), $sel:event];
}
}
}
}
$class.add_method(
sel!($sel:),
$sel as extern "C" fn(&Object, Sel, id),
);
};
}
pub(super) unsafe fn create_view(window_options: &WindowOpenOptions) -> id {
let class = create_view_class();
let view: id = msg_send![class, alloc];
let size = window_options.size;
view.initWithFrame_(NSRect::new(NSPoint::new(0., 0.), NSSize::new(size.width, size.height)));
view
}
unsafe fn create_view_class() -> &'static Class {
// Use unique class names so that there are no conflicts between different
// instances. The class is deleted when the view is released. Previously,
// the class was stored in a OnceCell after creation. This way, we didn't
// have to recreate it each time a view was opened, but now we don't leave
// any class definitions lying around when the plugin is closed.
let class_name = format!("BaseviewNSView_{}", Uuid::new_v4().to_simple());
let mut class = ClassDecl::new(&class_name, class!(NSView)).unwrap();
class.add_method(
sel!(acceptsFirstResponder),
property_yes as extern "C" fn(&Object, Sel) -> BOOL,
);
class.add_method(sel!(isFlipped), property_yes as extern "C" fn(&Object, Sel) -> BOOL);
class.add_method(
sel!(preservesContentInLiveResize),
property_no as extern "C" fn(&Object, Sel) -> BOOL,
);
class.add_method(
sel!(acceptsFirstMouse:),
accepts_first_mouse as extern "C" fn(&Object, Sel, id) -> BOOL,
);
class.add_method(sel!(release), release as extern "C" fn(&mut Object, Sel));
class.add_method(sel!(dealloc), dealloc as extern "C" fn(&mut Object, Sel));
class.add_method(
sel!(viewWillMoveToWindow:),
view_will_move_to_window as extern "C" fn(&Object, Sel, id),
);
class.add_method(
sel!(updateTrackingAreas:),
update_tracking_areas as extern "C" fn(&Object, Sel, id),
);
class.add_method(sel!(mouseMoved:), mouse_moved as extern "C" fn(&Object, Sel, id));
class.add_method(sel!(mouseDragged:), mouse_moved as extern "C" fn(&Object, Sel, id));
class.add_method(sel!(rightMouseDragged:), mouse_moved as extern "C" fn(&Object, Sel, id));
class.add_method(sel!(otherMouseDragged:), mouse_moved as extern "C" fn(&Object, Sel, id));
class.add_method(sel!(scrollWheel:), scroll_wheel as extern "C" fn(&Object, Sel, id));
class.add_method(
sel!(viewDidChangeBackingProperties:),
view_did_change_backing_properties as extern "C" fn(&Object, Sel, id),
);
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, rightMouseDown, ButtonPressed, MouseButton::Right);
add_mouse_button_class_method!(class, rightMouseUp, ButtonReleased, MouseButton::Right);
add_mouse_button_class_method!(class, otherMouseDown, ButtonPressed, MouseButton::Middle);
add_mouse_button_class_method!(class, otherMouseUp, ButtonReleased, MouseButton::Middle);
add_simple_mouse_class_method!(class, mouseEntered, MouseEvent::CursorEntered);
add_simple_mouse_class_method!(class, mouseExited, MouseEvent::CursorLeft);
add_simple_keyboard_class_method!(class, keyDown);
add_simple_keyboard_class_method!(class, keyUp);
add_simple_keyboard_class_method!(class, flagsChanged);
class.add_ivar::<*mut c_void>(BASEVIEW_STATE_IVAR);
class.register()
}
extern "C" fn property_yes(_this: &Object, _sel: Sel) -> BOOL {
YES
}
extern "C" fn property_no(_this: &Object, _sel: Sel) -> BOOL {
NO
}
extern "C" fn accepts_first_mouse(_this: &Object, _sel: Sel, _event: id) -> BOOL {
YES
}
extern "C" fn release(this: &mut Object, _sel: Sel) {
// Hack for breaking circular references. We store the value of retainCount
// after build(), and then when retainCount drops back to that value, we
// drop the WindowState, hoping that any circular references it holds back
// to the NSView (e.g. wgpu surfaces) get released.
//
// This is definitely broken, since it can be thwarted by e.g. creating a
// wgpu surface at some point after build() (which will mean the NSView
// never gets dealloced) or dropping a wgpu surface at some point before
// drop() (which will mean the WindowState gets dropped early).
//
// TODO: Find a better solution for circular references.
unsafe {
let retain_count: usize = msg_send![this, retainCount];
let state_ptr: *mut c_void = *this.get_ivar(BASEVIEW_STATE_IVAR);
if !state_ptr.is_null() {
let retain_count_after_build = WindowState::from_field(this).retain_count_after_build;
if retain_count <= retain_count_after_build {
WindowState::stop_and_free(this);
}
}
}
unsafe {
let superclass = msg_send![this, superclass];
let () = msg_send![super(this, superclass), release];
}
}
extern "C" fn dealloc(this: &mut Object, _sel: Sel) {
unsafe {
let class = msg_send![this, class];
let superclass = msg_send![this, superclass];
let () = msg_send![super(this, superclass), dealloc];
// Delete class
::objc::runtime::objc_disposeClassPair(class);
}
}
extern "C" fn view_did_change_backing_properties(this: &Object, _: Sel, _: id) {
unsafe {
let ns_window: *mut Object = msg_send![this, window];
let scale_factor: f64 =
if ns_window.is_null() { 1.0 } else { NSWindow::backingScaleFactor(ns_window) as f64 };
let state: &mut WindowState = WindowState::from_field(this);
let bounds: NSRect = msg_send![this, bounds];
let new_window_info = WindowInfo::from_logical_size(
Size::new(bounds.size.width, bounds.size.height),
scale_factor,
);
// Only send the event when the window's size has actually changed to be in line with the
// other platform implementations
if new_window_info.physical_size() != state.window_info.physical_size() {
state.window_info = new_window_info;
state.trigger_event(Event::Window(WindowEvent::Resized(new_window_info)));
}
}
}
/// Init/reinit tracking area
///
/// Info:
/// https://developer.apple.com/documentation/appkit/nstrackingarea
/// https://developer.apple.com/documentation/appkit/nstrackingarea/options
/// https://developer.apple.com/documentation/appkit/nstrackingareaoptions
unsafe fn reinit_tracking_area(this: &Object, tracking_area: *mut Object) {
let options: usize = {
let mouse_entered_and_exited = 0x01;
let tracking_mouse_moved = 0x02;
let tracking_cursor_update = 0x04;
let tracking_active_in_active_app = 0x40;
let tracking_in_visible_rect = 0x200;
let tracking_enabled_during_mouse_drag = 0x400;
mouse_entered_and_exited
| tracking_mouse_moved
| tracking_cursor_update
| tracking_active_in_active_app
| tracking_in_visible_rect
| tracking_enabled_during_mouse_drag
};
let bounds: NSRect = msg_send![this, bounds];
*tracking_area = msg_send![tracking_area,
initWithRect:bounds
options:options
owner:this
userInfo:nil
];
}
extern "C" fn view_will_move_to_window(this: &Object, _self: Sel, new_window: id) {
unsafe {
let tracking_areas: *mut Object = msg_send![this, trackingAreas];
let tracking_area_count = NSArray::count(tracking_areas);
let _: () = msg_send![class!(NSEvent), setMouseCoalescingEnabled: NO];
if new_window == nil {
if tracking_area_count != 0 {
let tracking_area = NSArray::objectAtIndex(tracking_areas, 0);
let _: () = msg_send![this, removeTrackingArea: tracking_area];
let _: () = msg_send![tracking_area, release];
}
} else {
if tracking_area_count == 0 {
let class = Class::get("NSTrackingArea").unwrap();
let tracking_area: *mut Object = msg_send![class, alloc];
reinit_tracking_area(this, tracking_area);
let _: () = msg_send![this, addTrackingArea: tracking_area];
}
let _: () = msg_send![new_window, setAcceptsMouseMovedEvents: YES];
let _: () = msg_send![new_window, makeFirstResponder: this];
}
}
unsafe {
let superclass = msg_send![this, superclass];
let () = msg_send![super(this, superclass), viewWillMoveToWindow: new_window];
}
}
extern "C" fn update_tracking_areas(this: &Object, _self: Sel, _: id) {
unsafe {
let tracking_areas: *mut Object = msg_send![this, trackingAreas];
let tracking_area = NSArray::objectAtIndex(tracking_areas, 0);
reinit_tracking_area(this, tracking_area);
}
}
extern "C" fn mouse_moved(this: &Object, _sel: Sel, event: id) {
let state: &mut WindowState = unsafe { WindowState::from_field(this) };
let point: NSPoint = unsafe {
let point = NSEvent::locationInWindow(event);
msg_send![this, convertPoint:point fromView:nil]
};
let modifiers = unsafe { NSEvent::modifierFlags(event) };
let position = Point { x: point.x, y: point.y };
state.trigger_event(Event::Mouse(MouseEvent::CursorMoved {
position,
modifiers: make_modifiers(modifiers),
}));
}
extern "C" fn scroll_wheel(this: &Object, _: Sel, event: id) {
let state: &mut WindowState = unsafe { WindowState::from_field(this) };
let delta = unsafe {
let x = NSEvent::scrollingDeltaX(event) as f32;
let y = NSEvent::scrollingDeltaY(event) as f32;
if NSEvent::hasPreciseScrollingDeltas(event) != NO {
ScrollDelta::Pixels { x, y }
} else {
ScrollDelta::Lines { x, y }
}
};
let modifiers = unsafe { NSEvent::modifierFlags(event) };
state.trigger_event(Event::Mouse(MouseEvent::WheelScrolled {
delta,
modifiers: make_modifiers(modifiers),
}));
}