| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/extensions/extension_uninstall_dialog.h" | 9 #include "chrome/browser/extensions/extension_uninstall_dialog.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/ui/browser_list.h" | 11 #include "chrome/browser/ui/browser_list.h" |
| 12 #include "chrome/browser/ui/browser_window.h" | 12 #include "chrome/browser/ui/browser_window.h" |
| 13 #include "chrome/browser/ui/views/window.h" | 13 #include "chrome/browser/ui/views/window.h" |
| 14 #include "chrome/common/extensions/extension.h" | 14 #include "chrome/common/extensions/extension.h" |
| 15 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
| 16 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
| 17 #include "ui/gfx/compositor/compositor.h" |
| 18 #include "ui/gfx/compositor/layer.h" |
| 17 #include "views/controls/image_view.h" | 19 #include "views/controls/image_view.h" |
| 18 #include "views/controls/label.h" | 20 #include "views/controls/label.h" |
| 19 #include "views/layout/layout_constants.h" | 21 #include "views/layout/layout_constants.h" |
| 20 #include "views/view.h" | 22 #include "views/view.h" |
| 21 #include "views/widget/widget.h" | 23 #include "views/widget/widget.h" |
| 22 #include "views/window/dialog_delegate.h" | 24 #include "views/window/dialog_delegate.h" |
| 23 | 25 |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 const int kRightColumnWidth = 210; | 28 const int kRightColumnWidth = 210; |
| 27 const int kIconSize = 69; | 29 const int kIconSize = 69; |
| 28 | 30 |
| 29 class ExtensionUninstallDialogView : public views::DialogDelegateView { | 31 class ExtensionUninstallDialogDelegateView; |
| 32 |
| 33 // Views implementation of the uninstall dialog. |
| 34 class ExtensionUninstallDialogViews : public ExtensionUninstallDialog { |
| 30 public: | 35 public: |
| 31 ExtensionUninstallDialogView(ExtensionUninstallDialog::Delegate* delegate, | 36 ExtensionUninstallDialogViews(Profile* profile, |
| 32 const Extension* extension, | 37 ExtensionUninstallDialog::Delegate* delegate); |
| 33 SkBitmap* icon) | 38 virtual ~ExtensionUninstallDialogViews(); |
| 34 : delegate_(delegate), | 39 |
| 35 icon_(NULL) { | 40 // Forwards the accept and cancels to the delegate. |
| 36 // Scale down to icon size, but allow smaller icons (don't scale up). | 41 void ExtensionUninstallAccepted(); |
| 37 gfx::Size size(icon->width(), icon->height()); | 42 void ExtensionUninstallCanceled(); |
| 38 if (size.width() > kIconSize || size.height() > kIconSize) | 43 |
| 39 size = gfx::Size(kIconSize, kIconSize); | 44 ExtensionUninstallDialogDelegateView* view() { return view_; } |
| 40 icon_ = new views::ImageView(); | |
| 41 icon_->SetImageSize(size); | |
| 42 icon_->SetImage(*icon); | |
| 43 AddChildView(icon_); | |
| 44 | |
| 45 heading_ = new views::Label(UTF16ToWide( | |
| 46 l10n_util::GetStringFUTF16(IDS_EXTENSION_UNINSTALL_PROMPT_HEADING, | |
| 47 UTF8ToUTF16(extension->name())))); | |
| 48 heading_->SetMultiLine(true); | |
| 49 heading_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
| 50 AddChildView(heading_); | |
| 51 } | |
| 52 | 45 |
| 53 private: | 46 private: |
| 54 // views::DialogDelegateView: | 47 void Show() OVERRIDE; |
| 48 |
| 49 ExtensionUninstallDialogDelegateView* view_; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(ExtensionUninstallDialogViews); |
| 52 }; |
| 53 |
| 54 // The dialog's view, owned by the views framework. |
| 55 class ExtensionUninstallDialogDelegateView : public views::DialogDelegateView { |
| 56 public: |
| 57 ExtensionUninstallDialogDelegateView( |
| 58 ExtensionUninstallDialogViews* dialog_view, |
| 59 const Extension* extension, |
| 60 SkBitmap* icon); |
| 61 virtual ~ExtensionUninstallDialogDelegateView(); |
| 62 |
| 63 // Called when the ExtensionUninstallDialog has been destroyed to make sure |
| 64 // we invalidate pointers. |
| 65 void DialogDestroyed() { dialog_ = NULL; } |
| 66 |
| 67 private: |
| 68 // views::DialogDelegate: |
| 55 virtual std::wstring GetDialogButtonLabel( | 69 virtual std::wstring GetDialogButtonLabel( |
| 56 MessageBoxFlags::DialogButton button) const OVERRIDE { | 70 MessageBoxFlags::DialogButton button) const OVERRIDE; |
| 57 switch (button) { | |
| 58 case MessageBoxFlags::DIALOGBUTTON_OK: | |
| 59 return UTF16ToWide( | |
| 60 l10n_util::GetStringUTF16( | |
| 61 IDS_EXTENSION_PROMPT_UNINSTALL_BUTTON)); | |
| 62 case MessageBoxFlags::DIALOGBUTTON_CANCEL: | |
| 63 return UTF16ToWide(l10n_util::GetStringUTF16(IDS_CANCEL)); | |
| 64 default: | |
| 65 NOTREACHED(); | |
| 66 return L""; | |
| 67 } | |
| 68 } | |
| 69 | 71 |
| 70 virtual int GetDefaultDialogButton() const OVERRIDE { | 72 virtual int GetDefaultDialogButton() const OVERRIDE { |
| 71 return MessageBoxFlags::DIALOGBUTTON_CANCEL; | 73 return MessageBoxFlags::DIALOGBUTTON_CANCEL; |
| 72 } | 74 } |
| 73 | 75 |
| 74 virtual bool Accept() OVERRIDE { | 76 virtual bool Accept() OVERRIDE; |
| 75 delegate_->ExtensionDialogAccepted(); | 77 virtual bool Cancel() OVERRIDE; |
| 76 return true; | |
| 77 } | |
| 78 | |
| 79 virtual bool Cancel() OVERRIDE { | |
| 80 delegate_->ExtensionDialogCanceled(); | |
| 81 return true; | |
| 82 } | |
| 83 | 78 |
| 84 // views::WidgetDelegate: | 79 // views::WidgetDelegate: |
| 85 virtual bool IsModal() const OVERRIDE { return true; } | 80 virtual bool IsModal() const OVERRIDE { return true; } |
| 86 virtual std::wstring GetWindowTitle() const OVERRIDE { | 81 virtual views::View* GetContentsView() OVERRIDE { return this; } |
| 87 return UTF16ToWide( | 82 virtual std::wstring GetWindowTitle() const OVERRIDE; |
| 88 l10n_util::GetStringUTF16(IDS_EXTENSION_UNINSTALL_PROMPT_TITLE)); | |
| 89 } | |
| 90 virtual views::View* GetContentsView() { return this; } | |
| 91 | 83 |
| 92 // views::View: | 84 // views::View: |
| 93 virtual gfx::Size GetPreferredSize() OVERRIDE { | 85 virtual gfx::Size GetPreferredSize() OVERRIDE; |
| 94 int width = kRightColumnWidth; | 86 |
| 95 width += kIconSize; | 87 virtual void Layout() OVERRIDE; |
| 96 width += views::kPanelHorizMargin * 3; | 88 |
| 97 | 89 ExtensionUninstallDialogViews* dialog_; |
| 98 int height = views::kPanelVertMargin * 2; | 90 |
| 99 height += heading_->GetHeightForWidth(kRightColumnWidth); | |
| 100 | |
| 101 return gfx::Size(width, | |
| 102 std::max(height, kIconSize + views::kPanelVertMargin * 2)); | |
| 103 } | |
| 104 | |
| 105 virtual void Layout() OVERRIDE { | |
| 106 int x = views::kPanelHorizMargin; | |
| 107 int y = views::kPanelVertMargin; | |
| 108 | |
| 109 heading_->SizeToFit(kRightColumnWidth); | |
| 110 | |
| 111 if (heading_->height() <= kIconSize) { | |
| 112 icon_->SetBounds(x, y, kIconSize, kIconSize); | |
| 113 x += kIconSize; | |
| 114 x += views::kPanelHorizMargin; | |
| 115 | |
| 116 heading_->SetX(x); | |
| 117 heading_->SetY(y + (kIconSize - heading_->height()) / 2); | |
| 118 } else { | |
| 119 icon_->SetBounds(x, | |
| 120 y + (heading_->height() - kIconSize) / 2, | |
| 121 kIconSize, | |
| 122 kIconSize); | |
| 123 x += kIconSize; | |
| 124 x += views::kPanelHorizMargin; | |
| 125 | |
| 126 heading_->SetX(x); | |
| 127 heading_->SetY(y); | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 ExtensionUninstallDialog::Delegate* delegate_; | |
| 132 views::ImageView* icon_; | 91 views::ImageView* icon_; |
| 133 views::Label* heading_; | 92 views::Label* heading_; |
| 134 | 93 |
| 135 DISALLOW_COPY_AND_ASSIGN(ExtensionUninstallDialogView); | 94 DISALLOW_COPY_AND_ASSIGN(ExtensionUninstallDialogDelegateView); |
| 136 }; | 95 }; |
| 137 | 96 |
| 138 } // namespace | 97 ExtensionUninstallDialogViews::ExtensionUninstallDialogViews( |
| 139 | 98 Profile* profile, ExtensionUninstallDialog::Delegate* delegate) |
| 140 // static | 99 : ExtensionUninstallDialog(profile, delegate) {} |
| 141 void ExtensionUninstallDialog::Show( | 100 |
| 142 Profile* profile, | 101 ExtensionUninstallDialogViews::~ExtensionUninstallDialogViews() { |
| 143 ExtensionUninstallDialog::Delegate* delegate, | 102 // Close the widget (the views framework will delete view_). |
| 144 const Extension* extension, | 103 if (view_) { |
| 145 SkBitmap* icon) { | 104 view_->DialogDestroyed(); |
| 146 Browser* browser = BrowserList::GetLastActiveWithProfile(profile); | 105 view_->GetWidget()->CloseNow(); |
| 106 } |
| 107 } |
| 108 |
| 109 void ExtensionUninstallDialogViews::Show() { |
| 110 Browser* browser = BrowserList::GetLastActiveWithProfile(profile_); |
| 147 if (!browser) { | 111 if (!browser) { |
| 148 delegate->ExtensionDialogCanceled(); | 112 delegate_->ExtensionUninstallCanceled(); |
| 149 return; | 113 return; |
| 150 } | 114 } |
| 151 | 115 |
| 152 BrowserWindow* window = browser->window(); | 116 BrowserWindow* window = browser->window(); |
| 153 if (!window) { | 117 if (!window) { |
| 154 delegate->ExtensionDialogCanceled(); | 118 delegate_->ExtensionUninstallCanceled(); |
| 155 return; | 119 return; |
| 156 } | 120 } |
| 157 | 121 |
| 158 browser::CreateViewsWindow(window->GetNativeHandle(), | 122 view_ = new ExtensionUninstallDialogDelegateView(this, extension_, &icon_); |
| 159 new ExtensionUninstallDialogView(delegate, extension, icon))->Show(); | 123 browser::CreateViewsWindow(window->GetNativeHandle(), view_)->Show(); |
| 160 } | 124 } |
| 125 |
| 126 void ExtensionUninstallDialogViews::ExtensionUninstallAccepted() { |
| 127 // The widget gets destroyed when the dialog is accepted. |
| 128 view_ = NULL; |
| 129 delegate_->ExtensionUninstallAccepted(); |
| 130 } |
| 131 |
| 132 void ExtensionUninstallDialogViews::ExtensionUninstallCanceled() { |
| 133 // The widget gets destroyed when the dialog is canceled. |
| 134 view_ = NULL; |
| 135 delegate_->ExtensionUninstallCanceled(); |
| 136 } |
| 137 |
| 138 ExtensionUninstallDialogDelegateView::ExtensionUninstallDialogDelegateView( |
| 139 ExtensionUninstallDialogViews* dialog_view, |
| 140 const Extension* extension, |
| 141 SkBitmap* icon) |
| 142 : dialog_(dialog_view) { |
| 143 // Scale down to icon size, but allow smaller icons (don't scale up). |
| 144 gfx::Size size(icon->width(), icon->height()); |
| 145 if (size.width() > kIconSize || size.height() > kIconSize) |
| 146 size = gfx::Size(kIconSize, kIconSize); |
| 147 icon_ = new views::ImageView(); |
| 148 icon_->SetImageSize(size); |
| 149 icon_->SetImage(*icon); |
| 150 AddChildView(icon_); |
| 151 |
| 152 heading_ = new views::Label(UTF16ToWide( |
| 153 l10n_util::GetStringFUTF16(IDS_EXTENSION_UNINSTALL_PROMPT_HEADING, |
| 154 UTF8ToUTF16(extension->name())))); |
| 155 heading_->SetMultiLine(true); |
| 156 AddChildView(heading_); |
| 157 } |
| 158 |
| 159 ExtensionUninstallDialogDelegateView::~ExtensionUninstallDialogDelegateView() { |
| 160 } |
| 161 |
| 162 std::wstring ExtensionUninstallDialogDelegateView::GetDialogButtonLabel( |
| 163 MessageBoxFlags::DialogButton button) const { |
| 164 switch (button) { |
| 165 case MessageBoxFlags::DIALOGBUTTON_OK: |
| 166 return UTF16ToWide( |
| 167 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_UNINSTALL_BUTTON)); |
| 168 case MessageBoxFlags::DIALOGBUTTON_CANCEL: |
| 169 return UTF16ToWide(l10n_util::GetStringUTF16(IDS_CANCEL)); |
| 170 default: |
| 171 NOTREACHED(); |
| 172 return L""; |
| 173 } |
| 174 } |
| 175 |
| 176 bool ExtensionUninstallDialogDelegateView::Accept() { |
| 177 if (dialog_) |
| 178 dialog_->ExtensionUninstallAccepted(); |
| 179 return true; |
| 180 } |
| 181 |
| 182 bool ExtensionUninstallDialogDelegateView::Cancel() { |
| 183 if (dialog_) |
| 184 dialog_->ExtensionUninstallCanceled(); |
| 185 return true; |
| 186 } |
| 187 |
| 188 std::wstring ExtensionUninstallDialogDelegateView::GetWindowTitle() const { |
| 189 return UTF16ToWide( |
| 190 l10n_util::GetStringUTF16(IDS_EXTENSION_UNINSTALL_PROMPT_TITLE)); |
| 191 } |
| 192 |
| 193 |
| 194 gfx::Size ExtensionUninstallDialogDelegateView::GetPreferredSize() { |
| 195 int width = kRightColumnWidth; |
| 196 width += kIconSize; |
| 197 width += views::kPanelHorizMargin * 3; |
| 198 |
| 199 int height = views::kPanelVertMargin * 2; |
| 200 height += heading_->GetHeightForWidth(kRightColumnWidth); |
| 201 |
| 202 return gfx::Size(width, |
| 203 std::max(height, kIconSize + views::kPanelVertMargin * 2)); |
| 204 } |
| 205 |
| 206 void ExtensionUninstallDialogDelegateView::Layout() { |
| 207 int x = views::kPanelHorizMargin; |
| 208 int y = views::kPanelVertMargin; |
| 209 |
| 210 heading_->SizeToFit(kRightColumnWidth); |
| 211 |
| 212 if (heading_->height() <= kIconSize) { |
| 213 icon_->SetBounds(x, y, kIconSize, kIconSize); |
| 214 x += kIconSize; |
| 215 x += views::kPanelHorizMargin; |
| 216 |
| 217 heading_->SetX(x); |
| 218 heading_->SetY(y + (kIconSize - heading_->height()) / 2); |
| 219 } else { |
| 220 icon_->SetBounds(x, |
| 221 y + (heading_->height() - kIconSize) / 2, |
| 222 kIconSize, |
| 223 kIconSize); |
| 224 x += kIconSize; |
| 225 x += views::kPanelHorizMargin; |
| 226 |
| 227 heading_->SetX(x); |
| 228 heading_->SetY(y); |
| 229 } |
| 230 } |
| 231 |
| 232 } // namespace |
| 233 |
| 234 // static |
| 235 ExtensionUninstallDialog* ExtensionUninstallDialog::Create( |
| 236 Profile* profile, Delegate* delegate) { |
| 237 return new ExtensionUninstallDialogViews(profile, delegate); |
| 238 } |
| OLD | NEW |