Skip to content

Commit 787958b

Browse files
authored
Merge pull request #68 from slint-ui/simon/macos-scale-on-setbuffer
mac: Fix layer scale not being updated when contents is updated and window is on a new screen
2 parents 33a4c37 + 7ca503c commit 787958b

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# UNRELEASED
2+
3+
* On MacOS, the contents scale is updated when set_buffer() is called, to adapt when the window is on a new screen.
4+
15
# 0.2.0
26

37
* Add support for Redox/Orbital.

src/cg.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,26 @@ use std::sync::Arc;
1616

1717
pub struct CGImpl {
1818
layer: CALayer,
19+
window: id,
1920
}
2021

2122
impl CGImpl {
2223
pub unsafe fn new(handle: AppKitWindowHandle) -> Result<Self, SoftBufferError> {
2324
let window = handle.ns_window as id;
25+
let window: id = msg_send![window, retain];
2426
let view = handle.ns_view as id;
2527
let layer = CALayer::new();
2628
unsafe {
2729
let subview: id = NSView::alloc(nil).initWithFrame_(NSView::frame(view));
2830
layer.set_contents_gravity(ContentsGravity::TopLeft);
2931
layer.set_needs_display_on_bounds_change(false);
30-
layer.set_contents_scale(window.backingScaleFactor());
3132
subview.setLayer(layer.id());
3233
subview.setAutoresizingMask_(NSViewWidthSizable | NSViewHeightSizable);
3334

3435
view.addSubview_(subview); // retains subview (+1) = 2
3536
let _: () = msg_send![subview, release]; // releases subview (-1) = 1
3637
}
37-
Ok(Self { layer })
38+
Ok(Self { layer, window })
3839
}
3940

4041
pub(crate) unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {
@@ -62,8 +63,20 @@ impl CGImpl {
6263
transaction::begin();
6364
transaction::set_disable_actions(true);
6465

65-
unsafe { self.layer.set_contents(image.as_ptr() as id) };
66+
unsafe {
67+
self.layer
68+
.set_contents_scale(self.window.backingScaleFactor());
69+
self.layer.set_contents(image.as_ptr() as id);
70+
};
6671

6772
transaction::commit();
6873
}
6974
}
75+
76+
impl Drop for CGImpl {
77+
fn drop(&mut self) {
78+
unsafe {
79+
let _: () = msg_send![self.window, release];
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)