From 2832a6e42b1911f3bbf3a132c3448a364c020be7 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Sun, 15 Jan 2023 09:09:12 +0100 Subject: [PATCH 1/3] mac: Fix layer scale not being updated when contents is updated and window is on a new screen cc #67 --- src/cg.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/cg.rs b/src/cg.rs index 29103720..1309a43c 100644 --- a/src/cg.rs +++ b/src/cg.rs @@ -16,6 +16,7 @@ use std::sync::Arc; pub struct CGImpl { layer: CALayer, + window: id, } impl CGImpl { @@ -27,14 +28,13 @@ impl CGImpl { let subview: id = NSView::alloc(nil).initWithFrame_(NSView::frame(view)); layer.set_contents_gravity(ContentsGravity::TopLeft); layer.set_needs_display_on_bounds_change(false); - layer.set_contents_scale(window.backingScaleFactor()); subview.setLayer(layer.id()); subview.setAutoresizingMask_(NSViewWidthSizable | NSViewHeightSizable); view.addSubview_(subview); // retains subview (+1) = 2 let _: () = msg_send![subview, release]; // releases subview (-1) = 1 } - Ok(Self { layer }) + Ok(Self { layer, window }) } pub(crate) unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) { @@ -62,7 +62,11 @@ impl CGImpl { transaction::begin(); transaction::set_disable_actions(true); - unsafe { self.layer.set_contents(image.as_ptr() as id) }; + unsafe { + self.layer + .set_contents_scale(self.window.backingScaleFactor()); + self.layer.set_contents(image.as_ptr() as id); + }; transaction::commit(); } From 4c6631c7640c4b8fce2979e8f6909e1e008ad229 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Sun, 15 Jan 2023 17:04:57 +0100 Subject: [PATCH 2/3] mac: retain the NSWindow to ensure that the call to retrieving the backing scale factor in set_buffer works --- src/cg.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/cg.rs b/src/cg.rs index 1309a43c..54573feb 100644 --- a/src/cg.rs +++ b/src/cg.rs @@ -22,6 +22,7 @@ pub struct CGImpl { impl CGImpl { pub unsafe fn new(handle: AppKitWindowHandle) -> Result { let window = handle.ns_window as id; + let window: id = msg_send![window, retain]; let view = handle.ns_view as id; let layer = CALayer::new(); unsafe { @@ -71,3 +72,11 @@ impl CGImpl { transaction::commit(); } } + +impl Drop for CGImpl { + fn drop(&mut self) { + unsafe { + let _: () = msg_send![self.window, release]; + } + } +} From 7ca503cafe2a18998b4dd216f277f3a255f6a419 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Sun, 15 Jan 2023 17:35:52 +0100 Subject: [PATCH 3/3] add ChangeLog entry for the contents scale update change --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3d339c9..12191a38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# UNRELEASED + +* On MacOS, the contents scale is updated when set_buffer() is called, to adapt when the window is on a new screen. + # 0.2.0 * Add support for Redox/Orbital.