alloy: mac: Fix default popup window positioning (see issue #3244)

Popup windows will be created on the display that best matches the requested
coordinates. The requested size will apply to the content area (as required by
JS documentation) and window size will be reduced if necessary to fit within the
target display. The requested origin will apply to the window (including frame)
and will be modified if necessary so that the window is fully visible on the
target display.
This commit is contained in:
Nik Pavlov
2022-04-12 17:12:16 +00:00
committed by Marshall Greenblatt
parent 17cb26b8b9
commit 493232ce5a
2 changed files with 32 additions and 35 deletions

View File

@@ -166,27 +166,22 @@ bool CefBrowserPlatformDelegateNativeMac::CreateHostWindow() {
NSView* parentView = NSView* parentView =
CAST_CEF_WINDOW_HANDLE_TO_NSVIEW(window_info_.parent_view); CAST_CEF_WINDOW_HANDLE_TO_NSVIEW(window_info_.parent_view);
NSRect contentRect = {{static_cast<CGFloat>(window_info_.bounds.x),
static_cast<CGFloat>(window_info_.bounds.y)}, const CGFloat x = static_cast<CGFloat>(window_info_.bounds.x);
{static_cast<CGFloat>(window_info_.bounds.width), const CGFloat y = static_cast<CGFloat>(window_info_.bounds.y);
static_cast<CGFloat>(window_info_.bounds.height)}}; const CGFloat width = static_cast<CGFloat>(window_info_.bounds.width);
const CGFloat height = static_cast<CGFloat>(window_info_.bounds.height);
NSRect content_rect = {{x, y}, {width, height}};
if (parentView == nil) { if (parentView == nil) {
// Create a new window. // Create a new window.
NSRect screen_rect = [[NSScreen mainScreen] visibleFrame]; NSRect window_rect = {{x, y}, {width, height}};
NSRect window_rect = {
{static_cast<CGFloat>(window_info_.bounds.x),
screen_rect.size.height - static_cast<CGFloat>(window_info_.bounds.y)},
{static_cast<CGFloat>(window_info_.bounds.width),
static_cast<CGFloat>(window_info_.bounds.height)}};
if (window_rect.size.width == 0) if (window_rect.size.width == 0)
window_rect.size.width = 750; window_rect.size.width = 750;
if (window_rect.size.height == 0) if (window_rect.size.height == 0)
window_rect.size.height = 750; window_rect.size.height = 750;
contentRect.origin.x = 0; content_rect = {{0, 0}, {window_rect.size.width, window_rect.size.height}};
contentRect.origin.y = 0;
contentRect.size.width = window_rect.size.width;
contentRect.size.height = window_rect.size.height;
newWnd = [[UnderlayOpenGLHostingWindow alloc] newWnd = [[UnderlayOpenGLHostingWindow alloc]
initWithContentRect:window_rect initWithContentRect:window_rect
@@ -207,6 +202,11 @@ bool CefBrowserPlatformDelegateNativeMac::CreateHostWindow() {
// sub-views have layers. This is necessary to ensure correct layer // sub-views have layers. This is necessary to ensure correct layer
// ordering of all child views and their layers. // ordering of all child views and their layers.
[parentView setWantsLayer:YES]; [parentView setWantsLayer:YES];
// Transform input Y coodinate into the MacOS coordinate.
NSRect primary_screen_rect = [[[NSScreen screens] firstObject] frame];
const CGFloat transformed_y = NSMaxY(primary_screen_rect) - y;
[newWnd setFrameTopLeftPoint:NSMakePoint(x, transformed_y)];
} }
host_window_created_ = true; host_window_created_ = true;
@@ -216,7 +216,7 @@ bool CefBrowserPlatformDelegateNativeMac::CreateHostWindow() {
// Create the browser view. // Create the browser view.
CefBrowserHostView* browser_view = CefBrowserHostView* browser_view =
[[CefBrowserHostView alloc] initWithFrame:contentRect]; [[CefBrowserHostView alloc] initWithFrame:content_rect];
browser_view.browser = browser_; browser_view.browser = browser_;
[parentView addSubview:browser_view]; [parentView addSubview:browser_view];
[browser_view setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)]; [browser_view setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];

View File

@@ -61,11 +61,10 @@ NSButton* MakeButton(NSRect* rect, NSString* title, NSView* parent) {
return button; return button;
} }
NSRect GetScreenRectForWindow(NSWindow* window) { // Transform input Y coodinate into MacOS coordinate.
NSScreen* screen = [window screen]; CGFloat TransformY(int y) {
if (screen == nil) NSRect primary_screen_rect = [[[NSScreen screens] firstObject] frame];
screen = [NSScreen mainScreen]; return NSMaxY(primary_screen_rect) - y;
return [screen visibleFrame];
} }
} // namespace } // namespace
@@ -280,8 +279,6 @@ void RootWindowMacImpl::SetBounds(int x, int y, size_t width, size_t height) {
if (!window_) if (!window_)
return; return;
NSRect screen_rect = GetScreenRectForWindow(window_);
// Desired content rectangle. // Desired content rectangle.
NSRect content_rect; NSRect content_rect;
content_rect.size.width = static_cast<int>(width); content_rect.size.width = static_cast<int>(width);
@@ -291,7 +288,7 @@ void RootWindowMacImpl::SetBounds(int x, int y, size_t width, size_t height) {
// Convert to a frame rectangle. // Convert to a frame rectangle.
NSRect frame_rect = [window_ frameRectForContentRect:content_rect]; NSRect frame_rect = [window_ frameRectForContentRect:content_rect];
frame_rect.origin.x = x; frame_rect.origin.x = x;
frame_rect.origin.y = screen_rect.size.height - y; frame_rect.origin.y = TransformY(y) - frame_rect.size.height;
[window_ setFrame:frame_rect display:YES]; [window_ setFrame:frame_rect display:YES];
} }
@@ -371,8 +368,8 @@ void RootWindowMacImpl::CreateRootWindow(const CefBrowserSettings& settings,
// TODO(port): If no x,y position is specified the window will always appear // TODO(port): If no x,y position is specified the window will always appear
// in the upper-left corner. Maybe there's a better default place to put it? // in the upper-left corner. Maybe there's a better default place to put it?
int x = start_rect_.x; const int x = start_rect_.x;
int y = start_rect_.y; const int y = start_rect_.y;
int width, height; int width, height;
if (start_rect_.IsEmpty()) { if (start_rect_.IsEmpty()) {
// TODO(port): Also, maybe there's a better way to choose the default size. // TODO(port): Also, maybe there's a better way to choose the default size.
@@ -382,19 +379,20 @@ void RootWindowMacImpl::CreateRootWindow(const CefBrowserSettings& settings,
width = start_rect_.width; width = start_rect_.width;
height = start_rect_.height; height = start_rect_.height;
} }
const int height_with_controls =
with_controls_ ? height + URLBAR_HEIGHT : height;
// Create the main window. // The window Y coordinate is fixed in the setFrameTopLeftPoint call below
NSRect screen_rect = [[NSScreen mainScreen] visibleFrame]; const NSRect content_rect = NSMakeRect(x, y, width, height_with_controls);
NSRect window_rect =
NSMakeRect(x, screen_rect.size.height - y, width, height);
// The CEF framework library is loaded at runtime so we need to use this // The CEF framework library is loaded at runtime so we need to use this
// mechanism for retrieving the class. // mechanism for retrieving the class.
Class window_class = NSClassFromString(@"UnderlayOpenGLHostingWindow"); Class window_class = NSClassFromString(@"UnderlayOpenGLHostingWindow");
CHECK(window_class); CHECK(window_class);
// Create the main window.
window_ = [[window_class alloc] window_ = [[window_class alloc]
initWithContentRect:window_rect initWithContentRect:content_rect
styleMask:(NSTitledWindowMask | NSClosableWindowMask | styleMask:(NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask | NSResizableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask |
NSUnifiedTitleAndToolbarWindowMask) NSUnifiedTitleAndToolbarWindowMask)
@@ -441,8 +439,7 @@ void RootWindowMacImpl::CreateRootWindow(const CefBrowserSettings& settings,
if (with_controls_) { if (with_controls_) {
// Create the buttons. // Create the buttons.
NSRect button_rect = contentBounds; NSRect button_rect = contentBounds;
button_rect.origin.y = window_rect.size.height - URLBAR_HEIGHT + button_rect.origin.y = height + (URLBAR_HEIGHT - BUTTON_HEIGHT) / 2;
(URLBAR_HEIGHT - BUTTON_HEIGHT) / 2;
button_rect.size.height = BUTTON_HEIGHT; button_rect.size.height = BUTTON_HEIGHT;
button_rect.origin.x += BUTTON_MARGIN; button_rect.origin.x += BUTTON_MARGIN;
button_rect.size.width = BUTTON_WIDTH; button_rect.size.width = BUTTON_WIDTH;
@@ -484,6 +481,9 @@ void RootWindowMacImpl::CreateRootWindow(const CefBrowserSettings& settings,
[[url_textfield_ cell] setScrollable:YES]; [[url_textfield_ cell] setScrollable:YES];
} }
// Fix the window Y coordinate
[window_ setFrameTopLeftPoint:NSMakePoint(x, TransformY(y))];
if (!is_popup_) { if (!is_popup_) {
// Create the browser window. // Create the browser window.
browser_window_->CreateBrowser( browser_window_->CreateBrowser(
@@ -501,9 +501,6 @@ void RootWindowMacImpl::CreateRootWindow(const CefBrowserSettings& settings,
if (!initially_hidden) { if (!initially_hidden) {
// Show the window. // Show the window.
Show(RootWindow::ShowNormal); Show(RootWindow::ShowNormal);
// Size the window.
SetBounds(x, y, width, height);
} }
} }