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. diff --git a/src/cg.rs b/src/cg.rs index 29103720..54573feb 100644 --- a/src/cg.rs +++ b/src/cg.rs @@ -16,25 +16,26 @@ use std::sync::Arc; pub struct CGImpl { layer: CALayer, + window: id, } 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 { 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,8 +63,20 @@ 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(); } } + +impl Drop for CGImpl { + fn drop(&mut self) { + unsafe { + let _: () = msg_send![self.window, release]; + } + } +}