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