Skip to content
This repository has been archived by the owner on Dec 23, 2022. It is now read-only.

Fix scaling of the buffer on macOS #6

Merged
merged 2 commits into from
Dec 21, 2022
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
5 changes: 4 additions & 1 deletion examples/fruit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ fn main() {
}).collect::<Vec<_>>();

let event_loop = EventLoop::new();
let window = WindowBuilder::new().build(&event_loop).unwrap();
let window = WindowBuilder::new()
.with_inner_size(winit::dpi::PhysicalSize::new(fruit.width(), fruit.height()))
.build(&event_loop)
.unwrap();

#[cfg(target_arch = "wasm32")]
{
Expand Down
6 changes: 4 additions & 2 deletions src/cg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core_graphics::data_provider::CGDataProvider;
use core_graphics::image::CGImage;

use cocoa::base::{id, nil};
use cocoa::appkit::{NSView, NSViewWidthSizable, NSViewHeightSizable};
use cocoa::appkit::{NSView, NSViewWidthSizable, NSViewHeightSizable, NSWindow};
use cocoa::quartzcore::{CALayer, ContentsGravity};
use foreign_types::ForeignType;

Expand All @@ -18,11 +18,13 @@ pub struct CGImpl {

impl CGImpl {
pub unsafe fn new(handle: AppKitWindowHandle) -> Result<Self, SwBufError> {
let window = handle.ns_window as id;
let view = handle.ns_view as id;
let layer = CALayer::new();
let subview: id = NSView::alloc(nil).initWithFrame_(view.frame());
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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure if this will always be correct, e.g. if the view is moved to another monitor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this won't update automatically if the window's backing scale factor changes dynamically :(. I haven't looked into doing that, but agree that it would make sense to do, generally. I'd prefer it separate from this PR, as the lack of any scaling right now is pretty bad as-is :)

subview.setLayer(layer.id());
subview.setAutoresizingMask_(NSViewWidthSizable | NSViewHeightSizable);

Expand Down