| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/views/browser_bubble.h" | |
| 6 | |
| 7 #include "chrome/browser/ui/browser.h" | |
| 8 #include "chrome/browser/ui/views/frame/browser_view.h" | |
| 9 #include "ui/views/widget/widget.h" | |
| 10 | |
| 11 BrowserBubble::BrowserBubble(Browser* browser, | |
| 12 views::View* view, | |
| 13 const gfx::Rect& relative_to, | |
| 14 views::BubbleBorder::ArrowLocation arrow_location) | |
| 15 : frame_(NULL), | |
| 16 view_(view), | |
| 17 relative_to_(relative_to), | |
| 18 arrow_location_(arrow_location), | |
| 19 delegate_(NULL), | |
| 20 attached_(false), | |
| 21 bubble_host_(NULL) { | |
| 22 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser); | |
| 23 frame_ = browser_view->GetWidget(); | |
| 24 bubble_host_ = browser_view; | |
| 25 // Keep relative_to_ in frame-relative coordinates to aid in drag | |
| 26 // positioning. | |
| 27 gfx::Point origin = relative_to_.origin(); | |
| 28 views::View::ConvertPointToView(NULL, frame_->GetRootView(), &origin); | |
| 29 relative_to_.set_origin(origin); | |
| 30 | |
| 31 // Use half the corner radius as contents margins so that contents fit better | |
| 32 // in the bubble. See http://crbug.com/80416. | |
| 33 int corner_inset = views::BubbleBorder::GetCornerRadius() / 2; | |
| 34 gfx::Insets content_margins(corner_inset, corner_inset, | |
| 35 corner_inset, corner_inset); | |
| 36 InitPopup(content_margins); | |
| 37 } | |
| 38 | |
| 39 BrowserBubble::~BrowserBubble() { | |
| 40 DCHECK(!attached_); | |
| 41 popup_->Close(); | |
| 42 | |
| 43 // Don't call DetachFromBrowser from here. It needs to talk to the | |
| 44 // BrowserView to deregister itself, and if BrowserBubble is owned | |
| 45 // by a child of BrowserView, then it's possible that this stack frame | |
| 46 // is a descendant of BrowserView's destructor, which leads to problems. | |
| 47 // In that case, Detach doesn't need to get called anyway since BrowserView | |
| 48 // will do the necessary cleanup. | |
| 49 } | |
| 50 | |
| 51 void BrowserBubble::DetachFromBrowser() { | |
| 52 DCHECK(attached_); | |
| 53 if (!attached_) | |
| 54 return; | |
| 55 attached_ = false; | |
| 56 | |
| 57 if (bubble_host_) | |
| 58 bubble_host_->DetachBrowserBubble(this); | |
| 59 } | |
| 60 | |
| 61 void BrowserBubble::AttachToBrowser() { | |
| 62 DCHECK(!attached_); | |
| 63 if (attached_) | |
| 64 return; | |
| 65 | |
| 66 if (bubble_host_) | |
| 67 bubble_host_->AttachBrowserBubble(this); | |
| 68 | |
| 69 attached_ = true; | |
| 70 } | |
| 71 | |
| 72 void BrowserBubble::BrowserWindowMoved() { | |
| 73 if (delegate_) | |
| 74 delegate_->BubbleBrowserWindowMoved(this); | |
| 75 else | |
| 76 Hide(); | |
| 77 if (popup_->IsVisible()) | |
| 78 Reposition(); | |
| 79 } | |
| 80 | |
| 81 void BrowserBubble::BrowserWindowClosing() { | |
| 82 if (delegate_) | |
| 83 delegate_->BubbleBrowserWindowClosing(this); | |
| 84 else | |
| 85 Hide(); | |
| 86 } | |
| 87 | |
| 88 void BrowserBubble::SetBounds(int x, int y, int w, int h) { | |
| 89 // If the UI layout is RTL, we don't need to mirror coordinates, since | |
| 90 // View logic will do that for us. | |
| 91 bounds_.SetRect(x, y, w, h); | |
| 92 Reposition(); | |
| 93 } | |
| 94 | |
| 95 void BrowserBubble::MoveTo(int x, int y) { | |
| 96 SetBounds(x, y, bounds_.width(), bounds_.height()); | |
| 97 } | |
| 98 | |
| 99 void BrowserBubble::Reposition() { | |
| 100 gfx::Point top_left; | |
| 101 views::View::ConvertPointToScreen(frame_->GetRootView(), &top_left); | |
| 102 MovePopup(top_left.x() + bounds_.x(), | |
| 103 top_left.y() + bounds_.y(), | |
| 104 bounds_.width(), | |
| 105 bounds_.height()); | |
| 106 } | |
| 107 | |
| 108 gfx::Rect BrowserBubble::GetAbsoluteRelativeTo() { | |
| 109 // |relative_to_| is in browser-relative coordinates, so convert it to | |
| 110 // screen coordinates for use in placing the popup widgets. | |
| 111 gfx::Rect relative_rect = relative_to_; | |
| 112 gfx::Point relative_origin = relative_rect.origin(); | |
| 113 views::View::ConvertPointToScreen(frame_->GetRootView(), &relative_origin); | |
| 114 relative_rect.set_origin(relative_origin); | |
| 115 | |
| 116 return relative_rect; | |
| 117 } | |
| 118 | |
| 119 void BrowserBubble::SetAbsoluteBounds(const gfx::Rect& window_bounds) { | |
| 120 // Convert screen coordinates to frame relative. | |
| 121 gfx::Point relative_origin = window_bounds.origin(); | |
| 122 views::View::ConvertPointToView(NULL, frame_->GetRootView(), | |
| 123 &relative_origin); | |
| 124 SetBounds(relative_origin.x(), relative_origin.y(), | |
| 125 window_bounds.width(), window_bounds.height()); | |
| 126 } | |
| 127 | |
| 128 void BrowserBubble::MovePopup(int x, int y, int w, int h) { | |
| 129 popup_->SetBounds(gfx::Rect(x, y, w, h)); | |
| 130 } | |
| OLD | NEW |