1// Copyright (c) 2012 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/location_bar/zoom_bubble_view.h" 6 7#include "base/i18n/rtl.h" 8#include "chrome/browser/chrome_notification_types.h" 9#include "chrome/browser/chrome_page_zoom.h" 10#include "chrome/browser/ui/browser.h" 11#include "chrome/browser/ui/browser_finder.h" 12#include "chrome/browser/ui/browser_window.h" 13#include "chrome/browser/ui/views/frame/browser_view.h" 14#include "chrome/browser/ui/views/location_bar/location_bar_view.h" 15#include "chrome/browser/ui/views/location_bar/zoom_view.h" 16#include "chrome/browser/ui/zoom/zoom_controller.h" 17#include "content/public/browser/notification_source.h" 18#include "grit/generated_resources.h" 19#include "ui/base/l10n/l10n_util.h" 20#include "ui/base/resource/resource_bundle.h" 21#include "ui/views/controls/button/label_button.h" 22#include "ui/views/controls/separator.h" 23#include "ui/views/layout/box_layout.h" 24#include "ui/views/layout/layout_constants.h" 25#include "ui/views/widget/widget.h" 26 27namespace { 28 29// The number of milliseconds the bubble should stay on the screen if it will 30// close automatically. 31const int kBubbleCloseDelay = 1500; 32 33// The bubble's padding from the screen edge, used in fullscreen. 34const int kFullscreenPaddingEnd = 20; 35 36} // namespace 37 38// static 39ZoomBubbleView* ZoomBubbleView::zoom_bubble_ = NULL; 40 41// static 42void ZoomBubbleView::ShowBubble(content::WebContents* web_contents, 43 bool auto_close) { 44 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); 45 DCHECK(browser && browser->window() && browser->fullscreen_controller()); 46 47 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser); 48 bool is_fullscreen = browser_view->IsFullscreen(); 49 bool anchor_to_view = !is_fullscreen || 50 browser_view->immersive_mode_controller()->IsRevealed(); 51 views::View* anchor_view = anchor_to_view ? 52 browser_view->GetLocationBarView()->zoom_view() : NULL; 53 54 // If the bubble is already showing in this window and its |auto_close_| value 55 // is equal to |auto_close|, the bubble can be reused and only the label text 56 // needs to be updated. 57 if (zoom_bubble_ && 58 zoom_bubble_->GetAnchorView() == anchor_view && 59 zoom_bubble_->auto_close_ == auto_close) { 60 zoom_bubble_->Refresh(); 61 } else { 62 // If the bubble is already showing but its |auto_close_| value is not equal 63 // to |auto_close|, the bubble's focus properties must change, so the 64 // current bubble must be closed and a new one created. 65 CloseBubble(); 66 67 zoom_bubble_ = new ZoomBubbleView(anchor_view, 68 web_contents, 69 auto_close, 70 browser_view->immersive_mode_controller(), 71 browser->fullscreen_controller()); 72 73 // If we do not have an anchor view, parent the bubble to the content area. 74 if (!anchor_to_view) { 75 zoom_bubble_->set_parent_window(web_contents->GetTopLevelNativeWindow()); 76 } 77 78 views::BubbleDelegateView::CreateBubble(zoom_bubble_); 79 80 // Adjust for fullscreen after creation as it relies on the content size. 81 if (is_fullscreen) 82 zoom_bubble_->AdjustForFullscreen(browser_view->GetBoundsInScreen()); 83 84 if (zoom_bubble_->use_focusless()) 85 zoom_bubble_->GetWidget()->ShowInactive(); 86 else 87 zoom_bubble_->GetWidget()->Show(); 88 } 89} 90 91// static 92void ZoomBubbleView::CloseBubble() { 93 if (zoom_bubble_) 94 zoom_bubble_->Close(); 95} 96 97// static 98bool ZoomBubbleView::IsShowing() { 99 // The bubble may be in the process of closing. 100 return zoom_bubble_ != NULL && zoom_bubble_->GetWidget()->IsVisible(); 101} 102 103// static 104const ZoomBubbleView* ZoomBubbleView::GetZoomBubbleForTest() { 105 return zoom_bubble_; 106} 107 108ZoomBubbleView::ZoomBubbleView( 109 views::View* anchor_view, 110 content::WebContents* web_contents, 111 bool auto_close, 112 ImmersiveModeController* immersive_mode_controller, 113 FullscreenController* fullscreen_controller) 114 : BubbleDelegateView(anchor_view, anchor_view ? 115 views::BubbleBorder::TOP_RIGHT : views::BubbleBorder::NONE), 116 label_(NULL), 117 web_contents_(web_contents), 118 auto_close_(auto_close), 119 immersive_mode_controller_(immersive_mode_controller) { 120 // Compensate for built-in vertical padding in the anchor view's image. 121 set_anchor_view_insets(gfx::Insets(5, 0, 5, 0)); 122 set_use_focusless(auto_close); 123 set_notify_enter_exit_on_child(true); 124 125 // Add observers to close the bubble if the fullscreen state or immersive 126 // fullscreen revealed state changes. 127 registrar_.Add(this, 128 chrome::NOTIFICATION_FULLSCREEN_CHANGED, 129 content::Source<FullscreenController>(fullscreen_controller)); 130 immersive_mode_controller_->AddObserver(this); 131} 132 133ZoomBubbleView::~ZoomBubbleView() { 134 if (immersive_mode_controller_) 135 immersive_mode_controller_->RemoveObserver(this); 136} 137 138void ZoomBubbleView::AdjustForFullscreen(const gfx::Rect& screen_bounds) { 139 if (GetAnchorView()) 140 return; 141 142 // TODO(dbeam): should RTL logic be done in views::BubbleDelegateView? 143 const size_t bubble_half_width = width() / 2; 144 const int x_pos = base::i18n::IsRTL() ? 145 screen_bounds.x() + bubble_half_width + kFullscreenPaddingEnd : 146 screen_bounds.right() - bubble_half_width - kFullscreenPaddingEnd; 147 SetAnchorRect(gfx::Rect(x_pos, screen_bounds.y(), 0, 0)); 148} 149 150void ZoomBubbleView::Refresh() { 151 ZoomController* zoom_controller = 152 ZoomController::FromWebContents(web_contents_); 153 int zoom_percent = zoom_controller->zoom_percent(); 154 label_->SetText( 155 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent)); 156 StartTimerIfNecessary(); 157} 158 159void ZoomBubbleView::Close() { 160 GetWidget()->Close(); 161} 162 163void ZoomBubbleView::StartTimerIfNecessary() { 164 if (auto_close_) { 165 if (timer_.IsRunning()) { 166 timer_.Reset(); 167 } else { 168 timer_.Start( 169 FROM_HERE, 170 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay), 171 this, 172 &ZoomBubbleView::Close); 173 } 174 } 175} 176 177void ZoomBubbleView::StopTimer() { 178 timer_.Stop(); 179} 180 181void ZoomBubbleView::OnMouseEntered(const ui::MouseEvent& event) { 182 set_use_focusless(false); 183 StopTimer(); 184} 185 186void ZoomBubbleView::OnMouseExited(const ui::MouseEvent& event) { 187 set_use_focusless(auto_close_); 188 StartTimerIfNecessary(); 189} 190 191void ZoomBubbleView::OnGestureEvent(ui::GestureEvent* event) { 192 if (!zoom_bubble_ || !zoom_bubble_->auto_close_ || 193 event->type() != ui::ET_GESTURE_TAP) { 194 return; 195 } 196 197 // If an auto-closing bubble was tapped, show a non-auto-closing bubble in 198 // its place. 199 ShowBubble(zoom_bubble_->web_contents_, false); 200 event->SetHandled(); 201} 202 203void ZoomBubbleView::ButtonPressed(views::Button* sender, 204 const ui::Event& event) { 205 chrome_page_zoom::Zoom(web_contents_, content::PAGE_ZOOM_RESET); 206} 207 208void ZoomBubbleView::Init() { 209 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 210 0, 0, views::kRelatedControlVerticalSpacing)); 211 212 ZoomController* zoom_controller = 213 ZoomController::FromWebContents(web_contents_); 214 int zoom_percent = zoom_controller->zoom_percent(); 215 label_ = new views::Label( 216 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent)); 217 label_->SetFontList( 218 ui::ResourceBundle::GetSharedInstance().GetFontList( 219 ui::ResourceBundle::MediumFont)); 220 AddChildView(label_); 221 222 views::LabelButton* set_default_button = new views::LabelButton( 223 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT)); 224 set_default_button->SetStyle(views::Button::STYLE_BUTTON); 225 set_default_button->SetHorizontalAlignment(gfx::ALIGN_CENTER); 226 AddChildView(set_default_button); 227 228 StartTimerIfNecessary(); 229} 230 231void ZoomBubbleView::Observe(int type, 232 const content::NotificationSource& source, 233 const content::NotificationDetails& details) { 234 DCHECK_EQ(type, chrome::NOTIFICATION_FULLSCREEN_CHANGED); 235 CloseBubble(); 236} 237 238void ZoomBubbleView::OnImmersiveRevealStarted() { 239 CloseBubble(); 240} 241 242void ZoomBubbleView::OnImmersiveModeControllerDestroyed() { 243 immersive_mode_controller_ = NULL; 244} 245 246void ZoomBubbleView::WindowClosing() { 247 // |zoom_bubble_| can be a new bubble by this point (as Close(); doesn't 248 // call this right away). Only set to NULL when it's this bubble. 249 if (zoom_bubble_ == this) 250 zoom_bubble_ = NULL; 251} 252