extension_uninstall_dialog_view.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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/extensions/extension_uninstall_dialog.h" 6 7#include "base/basictypes.h" 8#include "base/compiler_specific.h" 9#include "base/strings/string_util.h" 10#include "base/strings/utf_string_conversions.h" 11#include "chrome/browser/ui/app_list/app_list_service.h" 12#include "chrome/browser/ui/views/constrained_window_views.h" 13#include "extensions/common/extension.h" 14#include "grit/generated_resources.h" 15#include "ui/base/l10n/l10n_util.h" 16#include "ui/compositor/compositor.h" 17#include "ui/compositor/layer.h" 18#include "ui/views/controls/image_view.h" 19#include "ui/views/controls/label.h" 20#include "ui/views/layout/layout_constants.h" 21#include "ui/views/view.h" 22#include "ui/views/widget/widget.h" 23#include "ui/views/window/dialog_delegate.h" 24 25namespace { 26 27const int kRightColumnWidth = 210; 28const int kIconSize = 64; 29 30class ExtensionUninstallDialogDelegateView; 31 32// Views implementation of the uninstall dialog. 33class ExtensionUninstallDialogViews 34 : public extensions::ExtensionUninstallDialog { 35 public: 36 ExtensionUninstallDialogViews( 37 Profile* profile, 38 gfx::NativeWindow parent, 39 extensions::ExtensionUninstallDialog::Delegate* delegate); 40 virtual ~ExtensionUninstallDialogViews(); 41 42 // Forwards the accept and cancels to the delegate. 43 void ExtensionUninstallAccepted(); 44 void ExtensionUninstallCanceled(); 45 46 private: 47 virtual 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. 55class ExtensionUninstallDialogDelegateView : public views::DialogDelegateView { 56 public: 57 ExtensionUninstallDialogDelegateView( 58 ExtensionUninstallDialogViews* dialog_view, 59 const extensions::Extension* extension, 60 const extensions::Extension* triggering_extension, 61 gfx::ImageSkia* image); 62 virtual ~ExtensionUninstallDialogDelegateView(); 63 64 // Called when the ExtensionUninstallDialog has been destroyed to make sure 65 // we invalidate pointers. 66 void DialogDestroyed() { dialog_ = NULL; } 67 68 private: 69 // views::DialogDelegate: 70 virtual base::string16 GetDialogButtonLabel( 71 ui::DialogButton button) const OVERRIDE; 72 virtual int GetDefaultDialogButton() const OVERRIDE { 73 // Default to accept when triggered via chrome://extensions page. 74 return triggered_by_extension_ ? 75 ui::DIALOG_BUTTON_CANCEL : ui::DIALOG_BUTTON_OK; 76 } 77 virtual bool Accept() OVERRIDE; 78 virtual bool Cancel() OVERRIDE; 79 80 // views::WidgetDelegate: 81 virtual ui::ModalType GetModalType() const OVERRIDE { 82 return ui::MODAL_TYPE_WINDOW; 83 } 84 virtual base::string16 GetWindowTitle() const OVERRIDE; 85 86 // views::View: 87 virtual gfx::Size GetPreferredSize() const OVERRIDE; 88 89 virtual void Layout() OVERRIDE; 90 91 ExtensionUninstallDialogViews* dialog_; 92 93 views::ImageView* icon_; 94 views::Label* heading_; 95 bool triggered_by_extension_; 96 97 DISALLOW_COPY_AND_ASSIGN(ExtensionUninstallDialogDelegateView); 98}; 99 100ExtensionUninstallDialogViews::ExtensionUninstallDialogViews( 101 Profile* profile, 102 gfx::NativeWindow parent, 103 extensions::ExtensionUninstallDialog::Delegate* delegate) 104 : extensions::ExtensionUninstallDialog(profile, parent, delegate), 105 view_(NULL) { 106} 107 108ExtensionUninstallDialogViews::~ExtensionUninstallDialogViews() { 109 // Close the widget (the views framework will delete view_). 110 if (view_) { 111 view_->DialogDestroyed(); 112 view_->GetWidget()->CloseNow(); 113 } 114} 115 116void ExtensionUninstallDialogViews::Show() { 117 view_ = new ExtensionUninstallDialogDelegateView( 118 this, extension_, triggering_extension_, &icon_); 119 CreateBrowserModalDialogViews(view_, parent_)->Show(); 120} 121 122void ExtensionUninstallDialogViews::ExtensionUninstallAccepted() { 123 // The widget gets destroyed when the dialog is accepted. 124 view_ = NULL; 125 delegate_->ExtensionUninstallAccepted(); 126} 127 128void ExtensionUninstallDialogViews::ExtensionUninstallCanceled() { 129 // The widget gets destroyed when the dialog is canceled. 130 view_ = NULL; 131 delegate_->ExtensionUninstallCanceled(); 132} 133 134ExtensionUninstallDialogDelegateView::ExtensionUninstallDialogDelegateView( 135 ExtensionUninstallDialogViews* dialog_view, 136 const extensions::Extension* extension, 137 const extensions::Extension* triggering_extension, 138 gfx::ImageSkia* image) 139 : dialog_(dialog_view), 140 triggered_by_extension_(triggering_extension != NULL) { 141 // Scale down to icon size, but allow smaller icons (don't scale up). 142 gfx::Size size(image->width(), image->height()); 143 if (size.width() > kIconSize || size.height() > kIconSize) 144 size = gfx::Size(kIconSize, kIconSize); 145 icon_ = new views::ImageView(); 146 icon_->SetImageSize(size); 147 icon_->SetImage(*image); 148 AddChildView(icon_); 149 150 heading_ = new views::Label(base::UTF8ToUTF16(dialog_->GetHeadingText())); 151 heading_->SetMultiLine(true); 152 heading_->SetHorizontalAlignment(gfx::ALIGN_LEFT); 153 AddChildView(heading_); 154} 155 156ExtensionUninstallDialogDelegateView::~ExtensionUninstallDialogDelegateView() { 157} 158 159base::string16 ExtensionUninstallDialogDelegateView::GetDialogButtonLabel( 160 ui::DialogButton button) const { 161 return l10n_util::GetStringUTF16((button == ui::DIALOG_BUTTON_OK) ? 162 IDS_EXTENSION_PROMPT_UNINSTALL_BUTTON : IDS_CANCEL); 163} 164 165bool ExtensionUninstallDialogDelegateView::Accept() { 166 if (dialog_) 167 dialog_->ExtensionUninstallAccepted(); 168 return true; 169} 170 171bool ExtensionUninstallDialogDelegateView::Cancel() { 172 if (dialog_) 173 dialog_->ExtensionUninstallCanceled(); 174 return true; 175} 176 177base::string16 ExtensionUninstallDialogDelegateView::GetWindowTitle() const { 178 return l10n_util::GetStringUTF16(IDS_EXTENSION_UNINSTALL_PROMPT_TITLE); 179} 180 181gfx::Size ExtensionUninstallDialogDelegateView::GetPreferredSize() const { 182 int width = kRightColumnWidth; 183 width += kIconSize; 184 width += views::kButtonHEdgeMarginNew * 2; 185 width += views::kRelatedControlHorizontalSpacing; 186 187 int height = views::kPanelVertMargin * 2; 188 height += heading_->GetHeightForWidth(kRightColumnWidth); 189 190 return gfx::Size(width, 191 std::max(height, kIconSize + views::kPanelVertMargin * 2)); 192} 193 194void ExtensionUninstallDialogDelegateView::Layout() { 195 int x = views::kButtonHEdgeMarginNew; 196 int y = views::kPanelVertMargin; 197 198 heading_->SizeToFit(kRightColumnWidth); 199 200 if (heading_->height() <= kIconSize) { 201 icon_->SetBounds(x, y, kIconSize, kIconSize); 202 x += kIconSize; 203 x += views::kRelatedControlHorizontalSpacing; 204 205 heading_->SetX(x); 206 heading_->SetY(y + (kIconSize - heading_->height()) / 2); 207 } else { 208 icon_->SetBounds(x, 209 y + (heading_->height() - kIconSize) / 2, 210 kIconSize, 211 kIconSize); 212 x += kIconSize; 213 x += views::kRelatedControlHorizontalSpacing; 214 215 heading_->SetX(x); 216 heading_->SetY(y); 217 } 218} 219 220} // namespace 221 222// static 223extensions::ExtensionUninstallDialog* 224extensions::ExtensionUninstallDialog::Create(Profile* profile, 225 gfx::NativeWindow parent, 226 Delegate* delegate) { 227 return new ExtensionUninstallDialogViews(profile, parent, delegate); 228} 229