Skip to content
This repository was archived by the owner on Sep 2, 2021. It is now read-only.

Commit f4d580d

Browse files
committed
Modified fix for adobe/brackets#1147 based on Glenn's work in #120
Makes sure that the default size definitely fits the available space. It will only be used on the first launch of Brackets. Subsequent launches use stored window size and position which is adjusted automatically by the OS.
1 parent b52af87 commit f4d580d

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

appshell/cefclient_mac.mm

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -275,17 +275,54 @@ - (void)createApp:(id)object {
275275
ClientWindowDelegate* delegate = [[ClientWindowDelegate alloc] init];
276276

277277
// Create the main application window.
278+
NSUInteger styleMask = (NSTitledWindowMask |
279+
NSClosableWindowMask |
280+
NSMiniaturizableWindowMask |
281+
NSResizableWindowMask );
282+
283+
// Get the available screen space
278284
NSRect screen_rect = [[NSScreen mainScreen] visibleFrame];
279-
NSRect window_rect = { {0, screen_rect.size.height - kWindowHeight},
280-
{kWindowWidth, kWindowHeight} };
285+
// Start out with the content being as big as possible
286+
NSRect content_rect = [NSWindow contentRectForFrameRect:screen_rect styleMask:styleMask];
287+
288+
// Determine the maximum height
289+
const int maxHeight = kWindowHeight
290+
#ifdef SHOW_TOOLBAR_UI
291+
+ URLBAR_HEIGHT
292+
#endif
293+
;
294+
// Make the content rect fit into maxHeight and kWindowWidth
295+
if (content_rect.size.height > maxHeight) {
296+
// First move the window up as much as we reduce it's height so it opens in the top left corner
297+
content_rect.origin.y += content_rect.size.height - maxHeight;
298+
content_rect.size.height = maxHeight;
299+
}
300+
if (content_rect.size.width > kWindowWidth) {
301+
content_rect.size.width = kWindowWidth;
302+
}
303+
304+
// Initialize the window with the adjusted default size
281305
NSWindow* mainWnd = [[UnderlayOpenGLHostingWindow alloc]
282-
initWithContentRect:window_rect
283-
styleMask:(NSTitledWindowMask |
284-
NSClosableWindowMask |
285-
NSMiniaturizableWindowMask |
286-
NSResizableWindowMask )
306+
initWithContentRect:content_rect
307+
styleMask:styleMask
287308
backing:NSBackingStoreBuffered
288309
defer:NO];
310+
311+
// "Preclude the window controller from changing a window’s position from the
312+
// one saved in the defaults system" (NSWindow Class Reference)
313+
[[mainWnd windowController] setShouldCascadeWindows: NO];
314+
315+
// Set the "autosave" name for the window. If there is a previously stored
316+
// size for the window, it will be loaded here and used to resize the window.
317+
// It appears that if the stored size is too big for the screen,
318+
// it is automatically adjusted to fit.
319+
[mainWnd setFrameAutosaveName:APP_NAME @"MainWindow"];
320+
321+
// Get the actual content size of the window since setFrameAutosaveName could
322+
// result in the window size changing.
323+
content_rect = [mainWnd contentRectForFrameRect:[mainWnd frame]];
324+
325+
// Configure the rest of the window
289326
[mainWnd setTitle:APP_NAME];
290327
[mainWnd setDelegate:delegate];
291328
[mainWnd setCollectionBehavior: (1 << 7) /* NSWindowCollectionBehaviorFullScreenPrimary */];
@@ -301,7 +338,7 @@ - (void)createApp:(id)object {
301338
#ifdef SHOW_TOOLBAR_UI
302339
// Create the buttons.
303340
NSRect button_rect = [contentView bounds];
304-
button_rect.origin.y = window_rect.size.height - URLBAR_HEIGHT +
341+
button_rect.origin.y = content_rect.size.height - URLBAR_HEIGHT +
305342
(URLBAR_HEIGHT - BUTTON_HEIGHT) / 2;
306343
button_rect.size.height = BUTTON_HEIGHT;
307344
button_rect.origin.x += BUTTON_MARGIN;
@@ -353,34 +390,7 @@ - (void)createApp:(id)object {
353390

354391
settings.web_security_disabled = true;
355392

356-
[[mainWnd windowController] setShouldCascadeWindows: NO];
357-
358-
// Set the initial default size of the window.
359-
NSRect defSize = [mainWnd contentRectForFrameRect:[mainWnd frame]];
360-
defSize.size.width = kWindowWidth;
361-
defSize.size.height = kWindowHeight
362-
#ifdef SHOW_TOOLBAR_UI
363-
+ URLBAR_HEIGHT
364-
#endif
365-
;
366-
367-
[mainWnd setFrame:[mainWnd frameRectForContentRect:defSize] display:NO];
368-
369-
// Set the "autosave" name for the window. If there is a previously stored
370-
// size for the window, it will be loaded here.
371-
[mainWnd setFrameAutosaveName:APP_NAME @"MainWindow"];
372-
373-
// Get the actual content size of the window since setFrameAutosaveName could
374-
// result in the window size changing.
375-
NSRect r = [mainWnd contentRectForFrameRect:[mainWnd frame]];
376-
377-
window_info.SetAsChild(contentView, 0, 0,
378-
r.size.width,
379-
r.size.height
380-
#ifdef SHOW_TOOLBAR_UI
381-
+ URLBAR_HEIGHT
382-
#endif
383-
);
393+
window_info.SetAsChild(contentView, 0, 0, content_rect.size.width, content_rect.size.height);
384394

385395
CefBrowserHost::CreateBrowser(window_info, g_handler.get(),
386396
[[startupUrl absoluteString] UTF8String], settings);

0 commit comments

Comments
 (0)