From 95c84a3c322d43f42bf3b63f83d5ccdb09937789 Mon Sep 17 00:00:00 2001 From: Dennis Kehrig Date: Wed, 3 Oct 2012 19:12:08 +0200 Subject: [PATCH] Fixes #1147: Menu bar is only a thin line because it is placed too high up. The content area is now at most 1000x700 pixels, and properly shrunk so it fits into the available screen space (taking menu and dock position/size into account) if necessary Also, the window is positioned in the center of the available screen space. --- appshell/cefclient_mac.mm | 61 +++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/appshell/cefclient_mac.mm b/appshell/cefclient_mac.mm index 09559853e..c6c7ebd6c 100644 --- a/appshell/cefclient_mac.mm +++ b/appshell/cefclient_mac.mm @@ -38,8 +38,8 @@ #endif // SHOW_TOOLBAR_UI // Content area size for newly created windows. -const int kWindowWidth = 1000; -const int kWindowHeight = 700; +const int kContentWidth = 1000; +const int kContentHeight = 700; // Memory AutoRelease pool. static NSAutoreleasePool* g_autopool = nil; @@ -273,17 +273,45 @@ - (void)createApp:(id)object { // Create the delegate for control and browser window events. ClientWindowDelegate* delegate = [[ClientWindowDelegate alloc] init]; + + CGFloat contentWidth = kContentWidth; + CGFloat contentHeight = kContentHeight; +#ifdef SHOW_TOOLBAR_UI + contentHeight += URLBAR_HEIGHT; +#endif + + // Style mask for the window + NSUInteger styleMask = (NSTitledWindowMask | + NSClosableWindowMask | + NSMiniaturizableWindowMask | + NSResizableWindowMask); - // Create the main application window. + // Preferred size for the content + NSRect content_rect = { {0, 0}, {contentWidth, contentHeight} }; + // Necessary window size for the content_frame using the above styleMask + NSRect window_rect = [NSWindow frameRectForContentRect:content_rect styleMask:styleMask]; + // Available space and position for the window, taking menu and dock size/position into account NSRect screen_rect = [[NSScreen mainScreen] visibleFrame]; - NSRect window_rect = { {0, screen_rect.size.height - kWindowHeight}, - {kWindowWidth, kWindowHeight} }; + + // Make sure the window fits into the available screen space + if (screen_rect.size.width < window_rect.size.width) { + window_rect.size.width = screen_rect.size.width; + } + if (screen_rect.size.height < window_rect.size.height) { + window_rect.size.height = screen_rect.size.height; + } + + // Make sure the window is in the center of the available screen space + window_rect.origin.x = screen_rect.origin.x + floor((screen_rect.size.width - window_rect.size.width) / 2); + window_rect.origin.y = screen_rect.origin.y + floor((screen_rect.size.height - window_rect.size.height) / 2); + + // Update the content_rect + content_rect = [NSWindow contentRectForFrameRect:window_rect styleMask:styleMask]; + + // Create the main application window. NSWindow* mainWnd = [[UnderlayOpenGLHostingWindow alloc] - initWithContentRect:window_rect - styleMask:(NSTitledWindowMask | - NSClosableWindowMask | - NSMiniaturizableWindowMask | - NSResizableWindowMask ) + initWithContentRect:content_rect + styleMask:styleMask backing:NSBackingStoreBuffered defer:NO]; [mainWnd setTitle:APP_NAME]; @@ -353,23 +381,12 @@ - (void)createApp:(id)object { settings.web_security_disabled = true; - window_info.SetAsChild(contentView, 0, 0, kWindowWidth, kWindowHeight); + window_info.SetAsChild(contentView, 0, 0, content_rect.size.width, content_rect.size.height); CefBrowserHost::CreateBrowser(window_info, g_handler.get(), [[startupUrl absoluteString] UTF8String], settings); // Show the window. [mainWnd makeKeyAndOrderFront: nil]; - - // Size the window. - NSRect r = [mainWnd contentRectForFrameRect:[mainWnd frame]]; - r.size.width = kWindowWidth; - r.size.height = kWindowHeight -#ifdef SHOW_TOOLBAR_UI - + URLBAR_HEIGHT -#endif - ; - - [mainWnd setFrame:[mainWnd frameRectForContentRect:r] display:YES]; } // Sent by the default notification center immediately before the application