Skip to content

Commit d399c12

Browse files
authored
using viewDidChangeBackingProperties callback to detect scale factor … (#95)
* using viewDidChangeBackingProperties callback to detect scale factor changes and propagate via WindowEvent::Resized * null check on ns_window * changed match with if
1 parent 50c4175 commit d399c12

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

src/macos/view.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::ffi::c_void;
22

3-
use cocoa::appkit::{NSEvent, NSView};
3+
use cocoa::appkit::{NSEvent, NSView, NSWindow};
44
use cocoa::base::{id, nil, BOOL, YES, NO};
55
use cocoa::foundation::{NSArray, NSPoint, NSRect, NSSize};
66

@@ -13,7 +13,7 @@ use objc::{
1313
};
1414
use uuid::Uuid;
1515

16-
use crate::{Event, EventStatus, MouseButton, MouseEvent, Point, WindowOpenOptions};
16+
use crate::{Event, EventStatus, MouseButton, MouseEvent, Point, Size, WindowEvent, WindowInfo, WindowOpenOptions};
1717
use crate::MouseEvent::{ButtonPressed, ButtonReleased};
1818

1919
use super::window::WindowState;
@@ -145,6 +145,13 @@ unsafe fn create_view_class() -> &'static Class {
145145
mouse_moved as extern "C" fn(&Object, Sel, id),
146146
);
147147

148+
class.add_method(
149+
sel!(viewDidChangeBackingProperties:),
150+
view_did_change_backing_properties as extern "C" fn(&Object, Sel, id),
151+
);
152+
153+
154+
148155
add_simple_mouse_class_method!(
149156
class,
150157
mouseDown,
@@ -258,6 +265,31 @@ extern "C" fn release(this: &mut Object, _sel: Sel) {
258265
}
259266
}
260267

268+
extern "C" fn view_did_change_backing_properties(this: &Object, _:Sel, _:id) {
269+
unsafe {
270+
let ns_window: *mut Object = msg_send![this, window];
271+
272+
let scale_factor: f64 = if ns_window.is_null() {
273+
1.0
274+
} else {
275+
NSWindow::backingScaleFactor(ns_window) as f64
276+
};
277+
278+
let state: &mut WindowState = WindowState::from_field(this);
279+
280+
let bounds: NSRect = msg_send![this, bounds];
281+
282+
let window_info = WindowInfo::from_logical_size(
283+
Size::new(bounds.size.width, bounds.size.height),
284+
scale_factor
285+
);
286+
287+
state.trigger_event(
288+
Event::Window(WindowEvent::Resized(window_info))
289+
);
290+
}
291+
}
292+
261293

262294
/// Init/reinit tracking area
263295
///

0 commit comments

Comments
 (0)