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 =
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)},
{static_cast<CGFloat>(window_info_.bounds.width),
static_cast<CGFloat>(window_info_.bounds.height)}};
const CGFloat x = static_cast<CGFloat>(window_info_.bounds.x);
const CGFloat y = static_cast<CGFloat>(window_info_.bounds.y);
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) {
// Create a new window.
NSRect screen_rect = [[NSScreen mainScreen] visibleFrame];
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)}};
NSRect window_rect = {{x, y}, {width, height}};
if (window_rect.size.width == 0)
window_rect.size.width = 750;
if (window_rect.size.height == 0)
window_rect.size.height = 750;
contentRect.origin.x = 0;
contentRect.origin.y = 0;
contentRect.size.width = window_rect.size.width;
contentRect.size.height = window_rect.size.height;
content_rect = {{0, 0}, {window_rect.size.width, window_rect.size.height}};
newWnd = [[UnderlayOpenGLHostingWindow alloc]
initWithContentRect:window_rect
@ -207,6 +202,11 @@ bool CefBrowserPlatformDelegateNativeMac::CreateHostWindow() {
// sub-views have layers. This is necessary to ensure correct layer
// ordering of all child views and their layers.
[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;
@ -216,7 +216,7 @@ bool CefBrowserPlatformDelegateNativeMac::CreateHostWindow() {
// Create the browser view.
CefBrowserHostView* browser_view =
[[CefBrowserHostView alloc] initWithFrame:contentRect];
[[CefBrowserHostView alloc] initWithFrame:content_rect];
browser_view.browser = browser_;
[parentView addSubview:browser_view];
[browser_view setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];

View File

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