[email protected] | a32bd211 | 2010-03-30 20:51:19 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | ded49f8 | 2010-01-13 16:10:45 | [diff] [blame] | 5 | #include "chrome/browser/views/theme_install_bubble_view.h" |
| 6 | |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 7 | #include "app/l10n_util.h" |
| 8 | #include "app/resource_bundle.h" |
[email protected] | ded49f8 | 2010-01-13 16:10:45 | [diff] [blame] | 9 | #include "chrome/browser/tab_contents/tab_contents.h" |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 10 | #include "grit/generated_resources.h" |
[email protected] | ded49f8 | 2010-01-13 16:10:45 | [diff] [blame] | 11 | #include "views/widget/widget.h" |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 12 | |
| 13 | namespace { |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 14 | |
[email protected] | a99f5b8a | 2009-10-15 15:55:33 | [diff] [blame] | 15 | // The roundedness of the edges of our bubble. |
| 16 | static const int kBubbleCornerRadius = 4; |
| 17 | |
| 18 | // Padding around text in popup box. |
| 19 | static const int kTextHorizPadding = 90; |
| 20 | static const int kTextVertPadding = 45; |
| 21 | |
| 22 | // Multiple loads can be started at once. Only show one bubble, and keep |
| 23 | // track of number of loads happening. Close bubble when num_loads < 1. |
| 24 | static int num_loads_extant_ = 0; |
| 25 | |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | ThemeInstallBubbleView::ThemeInstallBubbleView(TabContents* tab_contents) |
| 29 | : popup_(NULL) { |
| 30 | if (!tab_contents) |
| 31 | Close(); |
| 32 | |
[email protected] | ded49f8 | 2010-01-13 16:10:45 | [diff] [blame] | 33 | text_ = l10n_util::GetString(IDS_THEME_LOADING_TITLE); |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 34 | ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 35 | gfx::Font font(rb.GetFont(ResourceBundle::LargeFont)); |
| 36 | SetFont(font); |
| 37 | |
| 38 | // We can't check for the size of tab_contents before we've generated |
| 39 | // the string and the font that determine the size of the bubble. |
| 40 | tab_contents->GetContainerBounds(&tab_contents_bounds_); |
| 41 | if (tab_contents_bounds_.height() < GetPreferredSize().height()) |
| 42 | Close(); |
| 43 | |
| 44 | // Close when theme has been installed. |
| 45 | registrar_.Add( |
| 46 | this, |
| 47 | NotificationType::BROWSER_THEME_CHANGED, |
| 48 | NotificationService::AllSources()); |
| 49 | |
| 50 | // Close when we are installing an extension, not a theme. |
| 51 | registrar_.Add( |
| 52 | this, |
| 53 | NotificationType::NO_THEME_DETECTED, |
| 54 | NotificationService::AllSources()); |
| 55 | registrar_.Add( |
| 56 | this, |
| 57 | NotificationType::EXTENSION_INSTALLED, |
| 58 | NotificationService::AllSources()); |
[email protected] | f4ea1128 | 2009-10-14 00:19:31 | [diff] [blame] | 59 | registrar_.Add( |
| 60 | this, |
| 61 | NotificationType::EXTENSION_INSTALL_ERROR, |
| 62 | NotificationService::AllSources()); |
[email protected] | 201ebfe | 2009-10-29 18:34:12 | [diff] [blame] | 63 | |
| 64 | // Don't let the bubble overlap the confirm dialog. |
| 65 | registrar_.Add( |
| 66 | this, |
| 67 | NotificationType::EXTENSION_WILL_SHOW_CONFIRM_DIALOG, |
| 68 | NotificationService::AllSources()); |
| 69 | |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 70 | gfx::Rect rc(0, 0, 0, 0); |
[email protected] | ded49f8 | 2010-01-13 16:10:45 | [diff] [blame] | 71 | popup_ = views::Widget::CreatePopupWidget(views::Widget::Transparent, |
| 72 | views::Widget::NotAcceptEvents, |
| 73 | views::Widget::DeleteOnDestroy); |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 74 | popup_->SetOpacity(0xCC); |
| 75 | popup_->Init(tab_contents->GetNativeView(), rc); |
| 76 | popup_->SetContentsView(this); |
| 77 | Reposition(); |
| 78 | popup_->Show(); |
| 79 | |
| 80 | SchedulePaint(); |
| 81 | } |
| 82 | |
| 83 | ThemeInstallBubbleView::~ThemeInstallBubbleView() { |
| 84 | num_loads_extant_ = 0; |
| 85 | } |
| 86 | |
| 87 | gfx::Size ThemeInstallBubbleView::GetPreferredSize() { |
[email protected] | a32bd211 | 2010-03-30 20:51:19 | [diff] [blame] | 88 | return gfx::Size(views::Label::font().GetStringWidth(text_) + |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 89 | kTextHorizPadding, |
| 90 | ResourceBundle::GetSharedInstance().GetFont( |
| 91 | ResourceBundle::LargeFont).height() + kTextVertPadding); |
| 92 | } |
| 93 | |
| 94 | void ThemeInstallBubbleView::Reposition() { |
| 95 | if (!popup_) |
| 96 | Close(); |
| 97 | |
| 98 | gfx::Size size = GetPreferredSize(); |
| 99 | int mid_x = tab_contents_bounds_.x() + |
| 100 | (tab_contents_bounds_.right() - tab_contents_bounds_.x()) / 2; |
| 101 | |
[email protected] | c2f4bdb7 | 2010-05-10 23:15:21 | [diff] [blame] | 102 | int x = base::i18n::IsRTL() ? |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 103 | mid_x + size.width() / 2 : mid_x - size.width() / 2; |
| 104 | int y = static_cast<int>(tab_contents_bounds_.y() + |
| 105 | (tab_contents_bounds_.bottom() - tab_contents_bounds_.y()) / 2 - |
| 106 | size.height() / 2); |
| 107 | |
[email protected] | ded49f8 | 2010-01-13 16:10:45 | [diff] [blame] | 108 | popup_->SetBounds(gfx::Rect(x, y, size.width(), size.height())); |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void ThemeInstallBubbleView::Paint(gfx::Canvas* canvas) { |
| 112 | SkScalar rad[8]; |
| 113 | for (int i = 0; i < 8; ++i) |
| 114 | rad[i] = SkIntToScalar(kBubbleCornerRadius); |
| 115 | |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 116 | SkPaint paint; |
| 117 | paint.setStyle(SkPaint::kFill_Style); |
| 118 | paint.setFlags(SkPaint::kAntiAlias_Flag); |
| 119 | paint.setColor(SK_ColorBLACK); |
| 120 | |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 121 | SkRect rect; |
| 122 | rect.set(0, 0, |
[email protected] | ded49f8 | 2010-01-13 16:10:45 | [diff] [blame] | 123 | SkIntToScalar(width()), |
| 124 | SkIntToScalar(height())); |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 125 | SkPath path; |
| 126 | path.addRoundRect(rect, rad, SkPath::kCW_Direction); |
| 127 | canvas->drawPath(path, paint); |
| 128 | |
[email protected] | a32bd211 | 2010-03-30 20:51:19 | [diff] [blame] | 129 | int text_width = views::Label::font().GetStringWidth(text_); |
[email protected] | ded49f8 | 2010-01-13 16:10:45 | [diff] [blame] | 130 | gfx::Rect body_bounds(kTextHorizPadding / 2, 0, text_width, height()); |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 131 | body_bounds.set_x(MirroredLeftPointForRect(body_bounds)); |
| 132 | |
| 133 | SkColor text_color = SK_ColorWHITE; |
[email protected] | a32bd211 | 2010-03-30 20:51:19 | [diff] [blame] | 134 | canvas->DrawStringInt(text_, views::Label::font(), text_color, |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 135 | body_bounds.x(), body_bounds.y(), body_bounds.width(), |
| 136 | body_bounds.height()); |
| 137 | } |
| 138 | |
| 139 | void ThemeInstallBubbleView::Close() { |
| 140 | --num_loads_extant_; |
| 141 | if (!popup_) { |
| 142 | num_loads_extant_ = 0; |
| 143 | return; |
| 144 | } |
| 145 | if (num_loads_extant_ < 1) { |
| 146 | registrar_.RemoveAll(); |
| 147 | popup_->Close(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void ThemeInstallBubbleView::Observe(NotificationType type, |
| 152 | const NotificationSource& source, |
| 153 | const NotificationDetails& details) { |
| 154 | Close(); |
| 155 | } |
| 156 | |
| 157 | // static |
| 158 | void ThemeInstallBubbleView::Show(TabContents* tab_contents) { |
| 159 | ++num_loads_extant_; |
| 160 | if (num_loads_extant_ < 2) |
| 161 | new ThemeInstallBubbleView(tab_contents); |
| 162 | } |
| 163 | |