diff --git a/libcef/browser/browser_host_impl_mac.mm b/libcef/browser/browser_host_impl_mac.mm index d0043723c..9d099a18f 100644 --- a/libcef/browser/browser_host_impl_mac.mm +++ b/libcef/browser/browser_host_impl_mac.mm @@ -64,15 +64,40 @@ @interface CefWindowDelegate : NSObject { @private CefBrowserHostImpl* browser_; // weak + NSWindow* window_; } - -@property (nonatomic, assign) CefBrowserHostImpl* browser; - +- (id)initWithWindow:(NSWindow*)window andBrowser:(CefBrowserHostImpl*)browser; @end @implementation CefWindowDelegate -@synthesize browser = browser_; +- (id)initWithWindow:(NSWindow*)window andBrowser:(CefBrowserHostImpl*)browser { + if (self = [super init]) { + window_ = window; + browser_ = browser; + + [window_ setDelegate:self]; + + // Register for application hide/unhide notifications. + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(applicationDidHide:) + name:NSApplicationDidHideNotification + object:nil]; + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(applicationDidUnhide:) + name:NSApplicationDidUnhideNotification + object:nil]; + } + return self; +} + +- (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; + + [super dealloc]; +} // Called when we are activated (when we gain focus). - (void)windowDidBecomeKey:(NSNotification*)notification { @@ -101,7 +126,7 @@ // Called when the application has been hidden. - (void)applicationDidHide:(NSNotification *)notification { // If the window is miniaturized then nothing has really changed. - if (![[notification object] isMiniaturized]) { + if (![window_ isMiniaturized]) { if (browser_) browser_->SetWindowVisibility(false); } @@ -110,7 +135,7 @@ // Called when the application has been unhidden. - (void)applicationDidUnhide:(NSNotification *)notification { // If the window is miniaturized then nothing has really changed. - if (![[notification object] isMiniaturized]) { + if (![window_ isMiniaturized]) { if (browser_) browser_->SetWindowVisibility(true); } @@ -365,10 +390,6 @@ bool CefBrowserHostImpl::PlatformCreateWindow() { contentRect.size.width = window_rect.size.width; contentRect.size.height = window_rect.size.height; - // Create the delegate for control and browser window events. - CefWindowDelegate* delegate = [[CefWindowDelegate alloc] init]; - delegate.browser = this; - newWnd = [[UnderlayOpenGLHostingWindow alloc] initWithContentRect:window_rect styleMask:(NSTitledWindowMask | @@ -378,7 +399,10 @@ bool CefBrowserHostImpl::PlatformCreateWindow() { NSUnifiedTitleAndToolbarWindowMask ) backing:NSBackingStoreBuffered defer:NO]; - [newWnd setDelegate:delegate]; + + // Create the delegate for control and browser window events. + [[CefWindowDelegate alloc] initWithWindow:newWnd andBrowser:this]; + parentView = [newWnd contentView]; window_info_.parent_view = parentView; } diff --git a/libcef/browser/render_widget_host_view_osr_mac.mm b/libcef/browser/render_widget_host_view_osr_mac.mm index 2c71000cf..39e3accac 100644 --- a/libcef/browser/render_widget_host_view_osr_mac.mm +++ b/libcef/browser/render_widget_host_view_osr_mac.mm @@ -35,6 +35,10 @@ void CefRenderWidgetHostViewOSR::SetTakesFocusOnlyOnMouseDown(bool flag) { } void CefRenderWidgetHostViewOSR::SetWindowVisibility(bool visible) { + if (visible) + WasShown(); + else + WasHidden(); } void CefRenderWidgetHostViewOSR::WindowFrameChanged() { diff --git a/tests/cefclient/cefclient_mac.mm b/tests/cefclient/cefclient_mac.mm index c08fb7e16..704a7dd90 100644 --- a/tests/cefclient/cefclient_mac.mm +++ b/tests/cefclient/cefclient_mac.mm @@ -129,7 +129,11 @@ const int kWindowHeight = 600; // Receives notifications from controls and the browser window. Will delete // itself when done. -@interface ClientWindowDelegate : NSObject +@interface ClientWindowDelegate : NSObject { + @private + NSWindow* window_; +} +- (id)initWithWindow:(NSWindow*)window; - (IBAction)goBack:(id)sender; - (IBAction)goForward:(id)sender; - (IBAction)reload:(id)sender; @@ -143,6 +147,32 @@ const int kWindowHeight = 600; @implementation ClientWindowDelegate +- (id)initWithWindow:(NSWindow*)window { + if (self = [super init]) { + window_ = window; + [window_ setDelegate:self]; + + // Register for application hide/unhide notifications. + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(applicationDidHide:) + name:NSApplicationDidHideNotification + object:nil]; + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(applicationDidUnhide:) + name:NSApplicationDidUnhideNotification + object:nil]; + } + return self; +} + +- (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; + + [super dealloc]; +} + - (IBAction)goBack:(id)sender { if (g_handler.get() && g_handler->GetBrowserId()) g_handler->GetBrowser()->GoBack(); @@ -258,7 +288,7 @@ const int kWindowHeight = 600; // Called when the application has been hidden. - (void)applicationDidHide:(NSNotification *)notification { // If the window is miniaturized then nothing has really changed. - if (![[notification object] isMiniaturized]) { + if (![window_ isMiniaturized]) { if (g_handler.get()) { CefRefPtr browser = g_handler->GetBrowser(); if (browser.get()) @@ -270,7 +300,7 @@ const int kWindowHeight = 600; // Called when the application has been unhidden. - (void)applicationDidUnhide:(NSNotification *)notification { // If the window is miniaturized then nothing has really changed. - if (![[notification object] isMiniaturized]) { + if (![window_ isMiniaturized]) { if (g_handler.get()) { CefRefPtr browser = g_handler->GetBrowser(); if (browser.get()) @@ -382,9 +412,6 @@ NSButton* MakeButton(NSRect* rect, NSString* title, NSView* parent) { [testItem setSubmenu:testMenu]; [menubar addItem:testItem]; - // Create the delegate for control and browser window events. - ClientWindowDelegate* delegate = [[ClientWindowDelegate alloc] init]; - // Create the main application window. NSRect screen_rect = [[NSScreen mainScreen] visibleFrame]; NSRect window_rect = { {0, screen_rect.size.height - kWindowHeight}, @@ -398,7 +425,10 @@ NSButton* MakeButton(NSRect* rect, NSString* title, NSView* parent) { backing:NSBackingStoreBuffered defer:NO]; [mainWnd setTitle:@"cefclient"]; - [mainWnd setDelegate:delegate]; + + // Create the delegate for control and browser window events. + ClientWindowDelegate* delegate = + [[ClientWindowDelegate alloc] initWithWindow:mainWnd]; // Rely on the window delegate to clean us up rather than immediately // releasing when the window gets closed. We use the delegate to do