Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
19 changes: 16 additions & 3 deletions src/cg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,26 @@ use std::sync::Arc;

pub struct CGImpl {
layer: CALayer,
window: id,
}

impl CGImpl {
pub unsafe fn new(handle: AppKitWindowHandle) -> Result<Self, SoftBufferError> {
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) {
Expand Down Expand Up @@ -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];
}
}
}