This source file includes following definitions.
- ShowBubble
- CloseBubble
- IsShowing
- GetZoomBubbleForTest
- immersive_mode_controller_
- AdjustForFullscreen
- Refresh
- Close
- StartTimerIfNecessary
- StopTimer
- OnMouseEntered
- OnMouseExited
- OnGestureEvent
- ButtonPressed
- Init
- Observe
- OnImmersiveRevealStarted
- OnImmersiveModeControllerDestroyed
- WindowClosing
#include "chrome/browser/ui/views/location_bar/zoom_bubble_view.h"
#include "base/i18n/rtl.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chrome_page_zoom.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
#include "chrome/browser/ui/views/location_bar/zoom_view.h"
#include "chrome/browser/ui/zoom/zoom_controller.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/web_contents_view.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/separator.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/layout_constants.h"
#include "ui/views/widget/widget.h"
namespace {
const int kBubbleCloseDelay = 1500;
const int kFullscreenPaddingEnd = 20;
}
ZoomBubbleView* ZoomBubbleView::zoom_bubble_ = NULL;
void ZoomBubbleView::ShowBubble(content::WebContents* web_contents,
bool auto_close) {
Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
DCHECK(browser && browser->window() && browser->fullscreen_controller());
BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
bool is_fullscreen = browser_view->IsFullscreen();
bool anchor_to_view = !is_fullscreen ||
browser_view->immersive_mode_controller()->IsRevealed();
views::View* anchor_view = anchor_to_view ?
browser_view->GetLocationBarView()->zoom_view() : NULL;
if (zoom_bubble_ &&
zoom_bubble_->GetAnchorView() == anchor_view &&
zoom_bubble_->auto_close_ == auto_close) {
zoom_bubble_->Refresh();
} else {
CloseBubble();
zoom_bubble_ = new ZoomBubbleView(anchor_view,
web_contents,
auto_close,
browser_view->immersive_mode_controller(),
browser->fullscreen_controller());
if (!anchor_to_view) {
zoom_bubble_->set_parent_window(
web_contents->GetView()->GetTopLevelNativeWindow());
}
views::BubbleDelegateView::CreateBubble(zoom_bubble_);
if (is_fullscreen)
zoom_bubble_->AdjustForFullscreen(browser_view->GetBoundsInScreen());
if (zoom_bubble_->use_focusless())
zoom_bubble_->GetWidget()->ShowInactive();
else
zoom_bubble_->GetWidget()->Show();
}
}
void ZoomBubbleView::CloseBubble() {
if (zoom_bubble_)
zoom_bubble_->Close();
}
bool ZoomBubbleView::IsShowing() {
return zoom_bubble_ != NULL && zoom_bubble_->GetWidget()->IsVisible();
}
const ZoomBubbleView* ZoomBubbleView::GetZoomBubbleForTest() {
return zoom_bubble_;
}
ZoomBubbleView::ZoomBubbleView(
views::View* anchor_view,
content::WebContents* web_contents,
bool auto_close,
ImmersiveModeController* immersive_mode_controller,
FullscreenController* fullscreen_controller)
: BubbleDelegateView(anchor_view, anchor_view ?
views::BubbleBorder::TOP_RIGHT : views::BubbleBorder::NONE),
label_(NULL),
web_contents_(web_contents),
auto_close_(auto_close),
immersive_mode_controller_(immersive_mode_controller) {
set_anchor_view_insets(gfx::Insets(5, 0, 5, 0));
set_use_focusless(auto_close);
set_notify_enter_exit_on_child(true);
registrar_.Add(this,
chrome::NOTIFICATION_FULLSCREEN_CHANGED,
content::Source<FullscreenController>(fullscreen_controller));
immersive_mode_controller_->AddObserver(this);
}
ZoomBubbleView::~ZoomBubbleView() {
if (immersive_mode_controller_)
immersive_mode_controller_->RemoveObserver(this);
}
void ZoomBubbleView::AdjustForFullscreen(const gfx::Rect& screen_bounds) {
if (GetAnchorView())
return;
const size_t bubble_half_width = width() / 2;
const int x_pos = base::i18n::IsRTL() ?
screen_bounds.x() + bubble_half_width + kFullscreenPaddingEnd :
screen_bounds.right() - bubble_half_width - kFullscreenPaddingEnd;
SetAnchorRect(gfx::Rect(x_pos, screen_bounds.y(), 0, 0));
}
void ZoomBubbleView::Refresh() {
ZoomController* zoom_controller =
ZoomController::FromWebContents(web_contents_);
int zoom_percent = zoom_controller->zoom_percent();
label_->SetText(
l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent));
StartTimerIfNecessary();
}
void ZoomBubbleView::Close() {
GetWidget()->Close();
}
void ZoomBubbleView::StartTimerIfNecessary() {
if (auto_close_) {
if (timer_.IsRunning()) {
timer_.Reset();
} else {
timer_.Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kBubbleCloseDelay),
this,
&ZoomBubbleView::Close);
}
}
}
void ZoomBubbleView::StopTimer() {
timer_.Stop();
}
void ZoomBubbleView::OnMouseEntered(const ui::MouseEvent& event) {
set_use_focusless(false);
StopTimer();
}
void ZoomBubbleView::OnMouseExited(const ui::MouseEvent& event) {
set_use_focusless(auto_close_);
StartTimerIfNecessary();
}
void ZoomBubbleView::OnGestureEvent(ui::GestureEvent* event) {
if (!zoom_bubble_ || !zoom_bubble_->auto_close_ ||
event->type() != ui::ET_GESTURE_TAP) {
return;
}
ShowBubble(zoom_bubble_->web_contents_, false);
event->SetHandled();
}
void ZoomBubbleView::ButtonPressed(views::Button* sender,
const ui::Event& event) {
chrome_page_zoom::Zoom(web_contents_, content::PAGE_ZOOM_RESET);
}
void ZoomBubbleView::Init() {
SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical,
0, 0, views::kRelatedControlVerticalSpacing));
ZoomController* zoom_controller =
ZoomController::FromWebContents(web_contents_);
int zoom_percent = zoom_controller->zoom_percent();
label_ = new views::Label(
l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent));
label_->SetFontList(
ui::ResourceBundle::GetSharedInstance().GetFontList(
ui::ResourceBundle::MediumFont));
AddChildView(label_);
views::LabelButton* set_default_button = new views::LabelButton(
this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT));
set_default_button->SetStyle(views::Button::STYLE_BUTTON);
set_default_button->SetHorizontalAlignment(gfx::ALIGN_CENTER);
AddChildView(set_default_button);
StartTimerIfNecessary();
}
void ZoomBubbleView::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK_EQ(type, chrome::NOTIFICATION_FULLSCREEN_CHANGED);
CloseBubble();
}
void ZoomBubbleView::OnImmersiveRevealStarted() {
CloseBubble();
}
void ZoomBubbleView::OnImmersiveModeControllerDestroyed() {
immersive_mode_controller_ = NULL;
}
void ZoomBubbleView::WindowClosing() {
if (zoom_bubble_ == this)
zoom_bubble_ = NULL;
}