Index: content/browser/renderer_host/render_widget_host_view_mac.mm |
diff --git a/content/browser/renderer_host/render_widget_host_view_mac.mm b/content/browser/renderer_host/render_widget_host_view_mac.mm |
index 5c2244a3d200140319495e3d1a4d466840d53eea..01b6e3f64316d73ce7c0d8cf542672f091d1f942 100644 |
--- a/content/browser/renderer_host/render_widget_host_view_mac.mm |
+++ b/content/browser/renderer_host/render_widget_host_view_mac.mm |
@@ -38,8 +38,10 @@ |
#include "content/common/plugin_messages.h" |
#include "content/common/view_messages.h" |
#include "content/public/browser/browser_thread.h" |
+#import "content/public/browser/event_hook_application_mac.h" |
#include "content/public/browser/native_web_keyboard_event.h" |
#import "content/public/browser/render_widget_host_view_mac_delegate.h" |
+#include "content/public/browser/web_contents_view_delegate.h" |
#include "skia/ext/platform_canvas.h" |
#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" |
#include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h" |
@@ -55,6 +57,7 @@ |
#include "ui/gfx/rect_conversions.h" |
#include "ui/gfx/size_conversions.h" |
#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
+#include "ui/gfx/native_widget_types.h" |
#include "ui/surface/io_surface_support_mac.h" |
#include "webkit/plugins/npapi/webplugin.h" |
@@ -167,6 +170,75 @@ static const short kIOHIDEventTypeScroll = 6; |
@end |
+@interface RenderWidgetPopupWindow : NSWindow<EventHookProtocol> |
+@end |
+ |
+@implementation RenderWidgetPopupWindow |
+ |
+- (id)initWithContentRect:(NSRect)contentRect |
+ styleMask:(NSUInteger)windowStyle |
+ backing:(NSBackingStoreType)bufferingType |
+ defer:(BOOL)deferCreation { |
+ if (self = [super initWithContentRect:contentRect |
+ styleMask:windowStyle |
+ backing:bufferingType |
+ defer:deferCreation]) { |
+ [self setOpaque:NO]; |
+ [self setBackgroundColor:[NSColor clearColor]]; |
+ [self startObservingClick]; |
+ } |
+ return self; |
+} |
+ |
+- (void)close { |
+ [self stopObservingClick]; |
+ [super close]; |
+} |
+ |
+// Gets called for all events in application. Watching for a click outside the |
+// window so we can close. |
+- (void)hookForEvent:(NSEvent*)theEvent { |
+ if ([theEvent window] == self) |
+ return; |
+ NSEventType eventType = [theEvent type]; |
+ if (eventType == NSLeftMouseDown || eventType == NSRightMouseDown) { |
+ [self close]; |
+ } |
+} |
+ |
+// Gets called when the menubar is clicked. |
+// Needed because the hookForEvent method doesn't see the click on the menubar. |
+- (void)begunTracking:(NSNotification *)notification { |
+ [self close]; |
+} |
+ |
+// Install the callback. |
+- (void)startObservingClick { |
+ EventHookApplication* app = static_cast<EventHookApplication*>( |
+ [EventHookApplication sharedApplication]); |
+ [app addEventHook:self]; |
+ |
+ NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; |
+ [nc addObserver:self |
+ selector:@selector(begunTracking:) |
+ name:NSMenuDidBeginTrackingNotification |
+ object:[NSApp mainMenu]]; |
+} |
+ |
+// Remove the callback. |
+- (void)stopObservingClick { |
+ EventHookApplication* app = static_cast<EventHookApplication*>( |
+ [EventHookApplication sharedApplication]); |
+ [app removeEventHook:self]; |
+ |
+ NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; |
+ [nc removeObserver:self |
+ name:NSMenuDidBeginTrackingNotification |
+ object:[NSApp mainMenu]]; |
+} |
+ |
+@end |
+ |
namespace { |
// Maximum number of characters we allow in a tooltip. |
@@ -329,8 +401,7 @@ void RenderWidgetHostViewMac::InitAsChild( |
} |
void RenderWidgetHostViewMac::InitAsPopup( |
- RenderWidgetHostView* parent_host_view, |
- const gfx::Rect& pos) { |
+ RenderWidgetHostView* parent_host_view, const gfx::Rect& pos) { |
bool activatable = popup_type_ == WebKit::WebPopupTypeNone; |
[cocoa_view_ setCloseOnDeactivate:YES]; |
[cocoa_view_ setCanBeKeyView:activatable ? YES : NO]; |
@@ -341,15 +412,25 @@ void RenderWidgetHostViewMac::InitAsPopup( |
origin_global.y = [[[NSScreen screens] objectAtIndex:0] frame].size.height - |
pos.height() - origin_global.y; |
} |
- NSPoint origin_window = |
- [[cocoa_view_ window] convertScreenToBase:origin_global]; |
- NSPoint origin_view = |
- [cocoa_view_ convertPoint:origin_window fromView:nil]; |
- NSRect initial_frame = NSMakeRect(origin_view.x, |
- origin_view.y, |
- pos.width(), |
- pos.height()); |
- [cocoa_view_ setFrame:initial_frame]; |
+ |
+ popup_window_.reset([[RenderWidgetPopupWindow alloc] |
+ initWithContentRect:NSMakeRect(origin_global.x, origin_global.y, |
+ pos.width(), pos.height()) |
+ styleMask:NSBorderlessWindowMask |
+ backing:NSBackingStoreBuffered |
+ defer:NO]); |
+ [popup_window_ setLevel:NSPopUpMenuWindowLevel]; |
+ [popup_window_ setReleasedWhenClosed:NO]; |
+ [popup_window_ makeKeyAndOrderFront:nil]; |
+ [[popup_window_ contentView] addSubview:cocoa_view_]; |
+ [cocoa_view_ setFrame:[[popup_window_ contentView] bounds]]; |
+ [cocoa_view_ setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; |
+ [popup_window_ setParentWindow:[parent_host_view->GetNativeView() window]]; |
+ [[NSNotificationCenter defaultCenter] |
+ addObserver:cocoa_view_ |
+ selector:@selector(popupWindowWillClose:) |
+ name:NSWindowWillCloseNotification |
+ object:popup_window_]; |
} |
// This function creates the fullscreen window and hides the dock and menubar if |
@@ -458,24 +539,17 @@ void RenderWidgetHostViewMac::SetBounds(const gfx::Rect& rect) { |
// The position of |rect| is screen coordinate system and we have to |
// consider Cocoa coordinate system is upside-down and also multi-screen. |
NSPoint origin_global = NSPointFromCGPoint(rect.origin().ToCGPoint()); |
+ NSSize size = NSMakeSize(rect.width(), rect.height()); |
+ size = [cocoa_view_ convertSize:size toView:nil]; |
if ([[NSScreen screens] count] > 0) { |
- NSSize size = NSMakeSize(rect.width(), rect.height()); |
- size = [cocoa_view_ convertSize:size toView:nil]; |
NSScreen* screen = |
static_cast<NSScreen*>([[NSScreen screens] objectAtIndex:0]); |
origin_global.y = |
NSHeight([screen frame]) - size.height - origin_global.y; |
} |
- |
- // Then |origin_global| is converted to client coordinate system. |
- DCHECK([cocoa_view_ window]); |
- NSPoint origin_window = |
- [[cocoa_view_ window] convertScreenToBase:origin_global]; |
- NSPoint origin_view = |
- [[cocoa_view_ superview] convertPoint:origin_window fromView:nil]; |
- NSRect frame = NSMakeRect(origin_view.x, origin_view.y, |
- rect.width(), rect.height()); |
- [cocoa_view_ setFrame:frame]; |
+ [popup_window_ setFrame:NSMakeRect(origin_global.x, origin_global.y, |
+ size.width, size.height) |
+ display:YES]; |
Avi (use Gerrit)
2012/12/11 21:59:37
Yes, I like this.
|
} else { |
DCHECK([[cocoa_view_ superview] isKindOfClass:[BaseView class]]); |
BaseView* superview = static_cast<BaseView*>([cocoa_view_ superview]); |
@@ -730,6 +804,9 @@ void RenderWidgetHostViewMac::Destroy() { |
[cocoa_view_ removeFromSuperview]; |
[cocoa_view_ autorelease]; |
+ [popup_window_ close]; |
+ popup_window_.autorelease(); |
+ |
[fullscreen_window_manager_ exitFullscreenMode]; |
fullscreen_window_manager_.reset(); |
[pepper_fullscreen_window_ close]; |
@@ -3353,6 +3430,10 @@ extern NSString *NSTextInputReplacementRangeAttributeName; |
[[self window] invalidateCursorRectsForView:self]; |
} |
+- (void)popupWindowWillClose:(NSNotification *)notification { |
+ [self resignFirstResponder]; |
+} |
+ |
@end |
// |