macOS: Fix center window with fixed bounds size (#8475)

This PR fixes window showing up in the center of the monitor when
`center: true` option is provided.

The idea is to set the `window_wize` when creating the `window` using
`native_window.initWithContentRect_styleMask_backing_defer_screen_()`

Before: 

<img width="851" alt="SCR-20240227-qokf"
src="https://github.com/zed-industries/zed/assets/20229808/27494966-2e97-4771-8837-ccb6658ced78">

After:

<img width="1132" alt="SCR-20240227-qlmg"
src="https://github.com/zed-industries/zed/assets/20229808/439568da-d380-4331-8d19-cd501f211c4c">


Release Notes:

- N/A
This commit is contained in:
Quadri A. Adekunle 2024-03-05 11:08:29 +11:00 committed by GitHub
parent d223fe446d
commit 4167c66b86
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 21 deletions

View file

@ -23,7 +23,15 @@ impl Render for HelloWorld {
fn main() { fn main() {
App::new().run(|cx: &mut AppContext| { App::new().run(|cx: &mut AppContext| {
cx.open_window(WindowOptions::default(), |cx| { let options = WindowOptions {
bounds: WindowBounds::Fixed(Bounds {
size: size(px(600.0), px(600.0)).into(),
origin: Default::default(),
}),
center: true,
..Default::default()
};
cx.open_window(options, |cx| {
cx.new_view(|_cx| HelloWorld { cx.new_view(|_cx| HelloWorld {
text: "World".into(), text: "World".into(),
}) })

View file

@ -530,8 +530,27 @@ impl MacWindow {
} }
} }
let window_rect = match options.bounds {
WindowBounds::Fullscreen => {
// Set a temporary size as we will asynchronously resize the window
NSRect::new(NSPoint::new(0., 0.), NSSize::new(1024., 768.))
}
WindowBounds::Maximized => {
let display_bounds = display.bounds();
global_bounds_to_ns_rect(display_bounds)
}
WindowBounds::Fixed(bounds) => {
let display_bounds = display.bounds();
if bounds.intersects(&display_bounds) {
global_bounds_to_ns_rect(bounds)
} else {
global_bounds_to_ns_rect(display_bounds)
}
}
};
let native_window = native_window.initWithContentRect_styleMask_backing_defer_screen_( let native_window = native_window.initWithContentRect_styleMask_backing_defer_screen_(
NSRect::new(NSPoint::new(0., 0.), NSSize::new(1024., 768.)), window_rect,
style_mask, style_mask,
NSBackingStoreBuffered, NSBackingStoreBuffered,
NO, NO,
@ -685,25 +704,10 @@ impl MacWindow {
native_window.orderFront_(nil); native_window.orderFront_(nil);
} }
let screen = native_window.screen(); if options.bounds == WindowBounds::Fullscreen {
match options.bounds { // We need to toggle full screen asynchronously as doing so may
WindowBounds::Fullscreen => { // call back into the platform handlers.
// We need to toggle full screen asynchronously as doing so may window.toggle_full_screen();
// call back into the platform handlers.
window.toggle_full_screen()
}
WindowBounds::Maximized => {
native_window.setFrame_display_(screen.visibleFrame(), YES);
}
WindowBounds::Fixed(bounds) => {
let display_bounds = display.bounds();
let frame = if bounds.intersects(&display_bounds) {
global_bounds_to_ns_rect(bounds)
} else {
global_bounds_to_ns_rect(display_bounds)
};
native_window.setFrame_display_(frame, YES);
}
} }
window.0.lock().move_traffic_light(); window.0.lock().move_traffic_light();