Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / base_bubble_controller.mm
index f35bc66..6dc5736 100644 (file)
 #include "ui/base/l10n/l10n_util.h"
 
 @interface BaseBubbleController (Private)
+- (void)registerForNotifications;
 - (void)updateOriginFromAnchor;
 - (void)activateTabWithContents:(content::WebContents*)newContents
                previousContents:(content::WebContents*)oldContents
                         atIndex:(NSInteger)index
                          reason:(int)reason;
+- (void)recordAnchorOffset;
+- (void)parentWindowDidResize:(NSNotification*)notification;
+- (void)parentWindowWillClose:(NSNotification*)notification;
 - (void)closeCleanup;
 @end
 
     anchor_ = anchoredAt;
     shouldOpenAsKeyWindow_ = YES;
     shouldCloseOnResignKey_ = YES;
-
-    // Watch to see if the parent window closes, and if so, close this one.
-    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
-    [center addObserver:self
-               selector:@selector(parentWindowWillClose:)
-                   name:NSWindowWillCloseNotification
-                 object:parentWindow_];
+    [self registerForNotifications];
   }
   return self;
 }
     [theWindow setContentView:contentView.get()];
     bubble_ = contentView.get();
 
-    // Watch to see if the parent window closes, and if so, close this one.
-    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
-    [center addObserver:self
-               selector:@selector(parentWindowWillClose:)
-                   name:NSWindowWillCloseNotification
-                 object:parentWindow_];
-
+    [self registerForNotifications];
     [self awakeFromNib];
   }
   return self;
   [super dealloc];
 }
 
+- (void)registerForNotifications {
+  NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
+  // Watch to see if the parent window closes, and if so, close this one.
+  [center addObserver:self
+             selector:@selector(parentWindowWillClose:)
+                 name:NSWindowWillCloseNotification
+               object:parentWindow_];
+  // Watch for parent window's resizing, to ensure this one is always
+  // anchored correctly.
+  [center addObserver:self
+             selector:@selector(parentWindowDidResize:)
+                 name:NSWindowDidResizeNotification
+               object:parentWindow_];
+}
+
 - (void)setAnchorPoint:(NSPoint)anchor {
   anchor_ = anchor;
   [self updateOriginFromAnchor];
 }
 
+- (void)recordAnchorOffset {
+  // The offset of the anchor from the parent's upper-left-hand corner is kept
+  // to ensure the bubble stays anchored correctly if the parent is resized.
+  anchorOffset_ = NSMakePoint(NSMinX([parentWindow_ frame]),
+                              NSMaxY([parentWindow_ frame]));
+  anchorOffset_.x -= anchor_.x;
+  anchorOffset_.y -= anchor_.y;
+}
+
 - (NSBox*)separatorWithFrame:(NSRect)frame {
   frame.size.height = 1.0;
   base::scoped_nsobject<NSBox> spacer([[NSBox alloc] initWithFrame:frame]);
   return [spacer.release() autorelease];
 }
 
+- (void)parentWindowDidResize:(NSNotification*)notification {
+  DCHECK_EQ(parentWindow_, [notification object]);
+  NSPoint newOrigin = NSMakePoint(NSMinX([parentWindow_ frame]),
+                                  NSMaxY([parentWindow_ frame]));
+  newOrigin.x -= anchorOffset_.x;
+  newOrigin.y -= anchorOffset_.y;
+  [self setAnchorPoint:newOrigin];
+}
+
 - (void)parentWindowWillClose:(NSNotification*)notification {
   parentWindow_ = nil;
   [self close];
   else
     [window orderFront:nil];
   [self registerKeyStateEventTap];
+  [self recordAnchorOffset];
 }
 
 - (void)close {