Mac: Reduce resource usage on SetWindowVisibility(false) and fix delivery of application hide/unhide notifications (issue #1369)

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@1830 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt 2014-09-10 21:32:30 +00:00
parent 082845619c
commit fa5aa5fe4e
3 changed files with 76 additions and 18 deletions

View File

@ -64,15 +64,40 @@
@interface CefWindowDelegate : NSObject <NSWindowDelegate> {
@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;
}

View File

@ -35,6 +35,10 @@ void CefRenderWidgetHostViewOSR::SetTakesFocusOnlyOnMouseDown(bool flag) {
}
void CefRenderWidgetHostViewOSR::SetWindowVisibility(bool visible) {
if (visible)
WasShown();
else
WasHidden();
}
void CefRenderWidgetHostViewOSR::WindowFrameChanged() {

View File

@ -129,7 +129,11 @@ const int kWindowHeight = 600;
// Receives notifications from controls and the browser window. Will delete
// itself when done.
@interface ClientWindowDelegate : NSObject <NSWindowDelegate>
@interface ClientWindowDelegate : NSObject <NSWindowDelegate> {
@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<CefBrowser> 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<CefBrowser> 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