From d399c1275522ae75f5a82caadd904df2685c8660 Mon Sep 17 00:00:00 2001 From: anunge Date: Mon, 29 Mar 2021 21:39:53 +0300 Subject: [PATCH] =?UTF-8?q?using=20viewDidChangeBackingProperties=20callba?= =?UTF-8?q?ck=20to=20detect=20scale=20factor=20=E2=80=A6=20(#95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * using viewDidChangeBackingProperties callback to detect scale factor changes and propagate via WindowEvent::Resized * null check on ns_window * changed match with if --- src/macos/view.rs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/macos/view.rs b/src/macos/view.rs index 17cbda0..5d017ae 100644 --- a/src/macos/view.rs +++ b/src/macos/view.rs @@ -1,6 +1,6 @@ use std::ffi::c_void; -use cocoa::appkit::{NSEvent, NSView}; +use cocoa::appkit::{NSEvent, NSView, NSWindow}; use cocoa::base::{id, nil, BOOL, YES, NO}; use cocoa::foundation::{NSArray, NSPoint, NSRect, NSSize}; @@ -13,7 +13,7 @@ use objc::{ }; use uuid::Uuid; -use crate::{Event, EventStatus, MouseButton, MouseEvent, Point, WindowOpenOptions}; +use crate::{Event, EventStatus, MouseButton, MouseEvent, Point, Size, WindowEvent, WindowInfo, WindowOpenOptions}; use crate::MouseEvent::{ButtonPressed, ButtonReleased}; use super::window::WindowState; @@ -145,6 +145,13 @@ unsafe fn create_view_class() -> &'static Class { mouse_moved 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_simple_mouse_class_method!( class, mouseDown, @@ -258,6 +265,31 @@ extern "C" fn release(this: &mut Object, _sel: Sel) { } } +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 window_info = WindowInfo::from_logical_size( + Size::new(bounds.size.width, bounds.size.height), + scale_factor + ); + + state.trigger_event( + Event::Window(WindowEvent::Resized(window_info)) + ); + } +} + /// Init/reinit tracking area ///