OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/ui/views/create_application_shortcut_view.h" | 5 #include "chrome/browser/ui/views/create_application_shortcut_view.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 23 matching lines...) Expand all Loading... | |
34 #include "net/url_request/url_request.h" | 34 #include "net/url_request/url_request.h" |
35 #include "skia/ext/image_operations.h" | 35 #include "skia/ext/image_operations.h" |
36 #include "third_party/skia/include/core/SkBitmap.h" | 36 #include "third_party/skia/include/core/SkBitmap.h" |
37 #include "third_party/skia/include/core/SkPaint.h" | 37 #include "third_party/skia/include/core/SkPaint.h" |
38 #include "third_party/skia/include/core/SkRect.h" | 38 #include "third_party/skia/include/core/SkRect.h" |
39 #include "ui/base/l10n/l10n_util.h" | 39 #include "ui/base/l10n/l10n_util.h" |
40 #include "ui/base/layout.h" | 40 #include "ui/base/layout.h" |
41 #include "ui/base/resource/resource_bundle.h" | 41 #include "ui/base/resource/resource_bundle.h" |
42 #include "ui/gfx/canvas.h" | 42 #include "ui/gfx/canvas.h" |
43 #include "ui/gfx/codec/png_codec.h" | 43 #include "ui/gfx/codec/png_codec.h" |
44 #include "ui/gfx/image/image_family.h" | |
44 #include "ui/gfx/image/image_skia.h" | 45 #include "ui/gfx/image/image_skia.h" |
45 #include "ui/gfx/image/image_skia_rep.h" | |
46 #include "ui/views/controls/button/checkbox.h" | 46 #include "ui/views/controls/button/checkbox.h" |
47 #include "ui/views/controls/image_view.h" | 47 #include "ui/views/controls/image_view.h" |
48 #include "ui/views/controls/label.h" | 48 #include "ui/views/controls/label.h" |
49 #include "ui/views/layout/grid_layout.h" | 49 #include "ui/views/layout/grid_layout.h" |
50 #include "ui/views/layout/layout_constants.h" | 50 #include "ui/views/layout/layout_constants.h" |
51 #include "ui/views/widget/widget.h" | 51 #include "ui/views/widget/widget.h" |
52 #include "ui/views/window/dialog_client_view.h" | 52 #include "ui/views/window/dialog_client_view.h" |
53 | 53 |
54 namespace { | 54 namespace { |
55 | 55 |
56 const int kIconPreviewSizePixels = 32; | 56 const int kIconPreviewSizePixels = 32; |
57 | 57 |
58 // AppInfoView shows the application icon and title. | 58 // AppInfoView shows the application icon and title. |
59 class AppInfoView : public views::View { | 59 class AppInfoView : public views::View { |
60 public: | 60 public: |
61 AppInfoView(const string16& title, | 61 AppInfoView(const string16& title, |
62 const string16& description, | 62 const string16& description, |
63 const SkBitmap& icon); | 63 const gfx::ImageFamily& icon); |
64 | 64 |
65 // Updates the title/description of the web app. | 65 // Updates the title/description of the web app. |
66 void UpdateText(const string16& title, const string16& description); | 66 void UpdateText(const string16& title, const string16& description); |
67 | 67 |
68 // Updates the icon of the web app. | 68 // Updates the icon of the web app. |
69 void UpdateIcon(const gfx::Image& image); | 69 void UpdateIcon(const gfx::ImageFamily& image); |
70 | 70 |
71 // Overridden from views::View: | 71 // Overridden from views::View: |
72 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; | 72 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; |
73 | 73 |
74 private: | 74 private: |
75 // Initializes the controls | 75 // Initializes the controls |
76 void Init(const string16& title, | 76 void Init(const string16& title, |
77 const string16& description, const SkBitmap& icon); | 77 const string16& description, const gfx::ImageFamily& icon); |
78 | 78 |
79 // Creates or updates description label. | 79 // Creates or updates description label. |
80 void PrepareDescriptionLabel(const string16& description); | 80 void PrepareDescriptionLabel(const string16& description); |
81 | 81 |
82 // Sets up layout manager. | 82 // Sets up layout manager. |
83 void SetupLayout(); | 83 void SetupLayout(); |
84 | 84 |
85 views::ImageView* icon_; | 85 views::ImageView* icon_; |
86 views::Label* title_; | 86 views::Label* title_; |
87 views::Label* description_; | 87 views::Label* description_; |
88 }; | 88 }; |
89 | 89 |
90 AppInfoView::AppInfoView(const string16& title, | 90 AppInfoView::AppInfoView(const string16& title, |
91 const string16& description, | 91 const string16& description, |
92 const SkBitmap& icon) | 92 const gfx::ImageFamily& icon) |
93 : icon_(NULL), | 93 : icon_(NULL), |
94 title_(NULL), | 94 title_(NULL), |
95 description_(NULL) { | 95 description_(NULL) { |
96 Init(title, description, icon); | 96 Init(title, description, icon); |
97 } | 97 } |
98 | 98 |
99 void AppInfoView::Init(const string16& title_text, | 99 void AppInfoView::Init(const string16& title_text, |
100 const string16& description_text, | 100 const string16& description_text, |
101 const SkBitmap& icon) { | 101 const gfx::ImageFamily& icon) { |
102 icon_ = new views::ImageView(); | 102 icon_ = new views::ImageView(); |
103 icon_->SetImage(gfx::ImageSkia::CreateFrom1xBitmap(icon)); | 103 UpdateIcon(icon); |
104 icon_->SetImageSize(gfx::Size(kIconPreviewSizePixels, | 104 icon_->SetImageSize(gfx::Size(kIconPreviewSizePixels, |
105 kIconPreviewSizePixels)); | 105 kIconPreviewSizePixels)); |
106 | 106 |
107 title_ = new views::Label(title_text); | 107 title_ = new views::Label(title_text); |
108 title_->SetMultiLine(true); | 108 title_->SetMultiLine(true); |
109 title_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 109 title_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
110 title_->SetFont(ui::ResourceBundle::GetSharedInstance().GetFont( | 110 title_->SetFont(ui::ResourceBundle::GetSharedInstance().GetFont( |
111 ui::ResourceBundle::BaseFont).DeriveFont(0, gfx::Font::BOLD)); | 111 ui::ResourceBundle::BaseFont).DeriveFont(0, gfx::Font::BOLD)); |
112 | 112 |
113 PrepareDescriptionLabel(description_text); | 113 PrepareDescriptionLabel(description_text); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 } | 162 } |
163 | 163 |
164 void AppInfoView::UpdateText(const string16& title, | 164 void AppInfoView::UpdateText(const string16& title, |
165 const string16& description) { | 165 const string16& description) { |
166 title_->SetText(title); | 166 title_->SetText(title); |
167 PrepareDescriptionLabel(description); | 167 PrepareDescriptionLabel(description); |
168 | 168 |
169 SetupLayout(); | 169 SetupLayout(); |
170 } | 170 } |
171 | 171 |
172 void AppInfoView::UpdateIcon(const gfx::Image& image) { | 172 void AppInfoView::UpdateIcon(const gfx::ImageFamily& image) { |
173 if (image.IsEmpty()) | 173 // Get the icon closest to the desired preview size. |
174 const gfx::Image* icon = image.Get(kIconPreviewSizePixels, | |
175 kIconPreviewSizePixels); | |
176 if (!icon || icon->IsEmpty()) | |
177 // The family has no icons. Leave the image blank. | |
174 return; | 178 return; |
175 | 179 const SkBitmap& bitmap = *icon->ToSkBitmap(); |
176 // image contains a single ImageSkia with all of the icons at different sizes. | |
177 // Create a new ImageSkia with just a single icon at the preferred size. | |
178 const gfx::ImageSkia& multires_image_skia = *(image.ToImageSkia()); | |
179 std::vector<gfx::ImageSkiaRep> image_reps = multires_image_skia.image_reps(); | |
180 // Find the smallest icon bigger or equal to the desired size. If it cannot be | |
181 // found, find the biggest icon smaller than the desired size. An icon's size | |
182 // is measured as the minimum of its width and height. | |
183 const gfx::ImageSkiaRep* smallest_larger = NULL; | |
184 const gfx::ImageSkiaRep* largest_smaller = NULL; | |
185 int smallest_larger_size = 0; | |
186 int largest_smaller_size = 0; | |
187 for (std::vector<gfx::ImageSkiaRep>::const_iterator it = image_reps.begin(); | |
188 it != image_reps.end(); ++it) { | |
189 const gfx::ImageSkiaRep& image = *it; | |
190 int image_size = std::min(image.pixel_width(), image.pixel_height()); | |
191 if (image_size >= kIconPreviewSizePixels) { | |
192 if (!smallest_larger || image_size < smallest_larger_size) { | |
193 smallest_larger = ℑ | |
194 smallest_larger_size = image_size; | |
195 } | |
196 } else { | |
197 if (!largest_smaller || image_size > largest_smaller_size) { | |
198 largest_smaller = ℑ | |
199 largest_smaller_size = image_size; | |
200 } | |
201 } | |
202 } | |
203 if (!smallest_larger && !largest_smaller) { | |
204 // Should never happen unless the image has no representations. | |
205 return; | |
206 } | |
207 const SkBitmap& bitmap = smallest_larger ? | |
208 smallest_larger->sk_bitmap() : | |
209 largest_smaller->sk_bitmap(); | |
210 if (bitmap.width() == kIconPreviewSizePixels && | 180 if (bitmap.width() == kIconPreviewSizePixels && |
211 bitmap.height() == kIconPreviewSizePixels) { | 181 bitmap.height() == kIconPreviewSizePixels) { |
212 icon_->SetImage(gfx::ImageSkia::CreateFrom1xBitmap(bitmap)); | 182 icon_->SetImage(gfx::ImageSkia::CreateFrom1xBitmap(bitmap)); |
213 } else { | 183 } else { |
214 // Resize the image to the desired size. | 184 // Resize the image to the desired size. |
215 SkBitmap resized_bitmap = skia::ImageOperations::Resize( | 185 SkBitmap resized_bitmap = skia::ImageOperations::Resize( |
216 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, | 186 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, |
217 kIconPreviewSizePixels, kIconPreviewSizePixels); | 187 kIconPreviewSizePixels, kIconPreviewSizePixels); |
218 | 188 |
219 icon_->SetImage(gfx::ImageSkia::CreateFrom1xBitmap(resized_bitmap)); | 189 icon_->SetImage(gfx::ImageSkia::CreateFrom1xBitmap(resized_bitmap)); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
278 create_shortcuts_label_(NULL), | 248 create_shortcuts_label_(NULL), |
279 desktop_check_box_(NULL), | 249 desktop_check_box_(NULL), |
280 menu_check_box_(NULL), | 250 menu_check_box_(NULL), |
281 quick_launch_check_box_(NULL) {} | 251 quick_launch_check_box_(NULL) {} |
282 | 252 |
283 CreateApplicationShortcutView::~CreateApplicationShortcutView() {} | 253 CreateApplicationShortcutView::~CreateApplicationShortcutView() {} |
284 | 254 |
285 void CreateApplicationShortcutView::InitControls() { | 255 void CreateApplicationShortcutView::InitControls() { |
286 // Create controls | 256 // Create controls |
287 app_info_ = new AppInfoView(shortcut_info_.title, shortcut_info_.description, | 257 app_info_ = new AppInfoView(shortcut_info_.title, shortcut_info_.description, |
288 shortcut_info_.favicon.IsEmpty() ? SkBitmap() : | 258 shortcut_info_.favicon); |
289 *shortcut_info_.favicon.ToSkBitmap()); | |
290 create_shortcuts_label_ = new views::Label( | 259 create_shortcuts_label_ = new views::Label( |
291 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_LABEL)); | 260 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_LABEL)); |
292 create_shortcuts_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 261 create_shortcuts_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
293 | 262 |
294 desktop_check_box_ = AddCheckbox( | 263 desktop_check_box_ = AddCheckbox( |
295 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_DESKTOP_CHKBOX), | 264 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_DESKTOP_CHKBOX), |
296 profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateOnDesktop)); | 265 profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateOnDesktop)); |
297 | 266 |
298 menu_check_box_ = NULL; | 267 menu_check_box_ = NULL; |
299 quick_launch_check_box_ = NULL; | 268 quick_launch_check_box_ = NULL; |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
465 InitControls(); | 434 InitControls(); |
466 } | 435 } |
467 | 436 |
468 CreateUrlApplicationShortcutView::~CreateUrlApplicationShortcutView() { | 437 CreateUrlApplicationShortcutView::~CreateUrlApplicationShortcutView() { |
469 } | 438 } |
470 | 439 |
471 bool CreateUrlApplicationShortcutView::Accept() { | 440 bool CreateUrlApplicationShortcutView::Accept() { |
472 if (!CreateApplicationShortcutView::Accept()) | 441 if (!CreateApplicationShortcutView::Accept()) |
473 return false; | 442 return false; |
474 | 443 |
475 extensions::TabHelper::FromWebContents(web_contents_)-> | 444 // Get the smallest icon in the icon family (should have only 1). |
476 SetAppIcon(shortcut_info_.favicon.IsEmpty() | 445 const gfx::Image* icon = shortcut_info_.favicon.Get(0, 0); |
benwells
2013/04/04 04:58:30
So the way you get the smallest icon is to ask for
Matt Giuca
2013/04/04 05:15:36
It feels intuitive to me, when you consider that t
benwells
2013/04/04 23:52:36
Ah, I see. Perhaps Get is unintuitive then. I woul
| |
477 ? SkBitmap() | 446 SkBitmap bitmap = (icon && !icon->IsEmpty()) ? *icon->ToSkBitmap() : |
478 : *shortcut_info_.favicon.ToSkBitmap()); | 447 SkBitmap(); |
448 extensions::TabHelper::FromWebContents(web_contents_)->SetAppIcon(bitmap); | |
479 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); | 449 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); |
480 if (browser) | 450 if (browser) |
481 chrome::ConvertTabToAppWindow(browser, web_contents_); | 451 chrome::ConvertTabToAppWindow(browser, web_contents_); |
482 return true; | 452 return true; |
483 } | 453 } |
484 | 454 |
485 void CreateUrlApplicationShortcutView::FetchIcon() { | 455 void CreateUrlApplicationShortcutView::FetchIcon() { |
486 // There should only be fetch job at a time. | 456 // There should only be fetch job at a time. |
487 DCHECK_EQ(-1, pending_download_id_); | 457 DCHECK_EQ(-1, pending_download_id_); |
488 | 458 |
(...skipping 28 matching lines...) Expand all Loading... | |
517 web_contents_->GetRenderViewHost()->GetView()->GetNativeView()); | 487 web_contents_->GetRenderViewHost()->GetView()->GetNativeView()); |
518 scale_factors.push_back(scale_factor); | 488 scale_factors.push_back(scale_factor); |
519 size_t closest_index = FaviconUtil::SelectBestFaviconFromBitmaps( | 489 size_t closest_index = FaviconUtil::SelectBestFaviconFromBitmaps( |
520 bitmaps, | 490 bitmaps, |
521 scale_factors, | 491 scale_factors, |
522 requested_size); | 492 requested_size); |
523 image = bitmaps[closest_index]; | 493 image = bitmaps[closest_index]; |
524 } | 494 } |
525 | 495 |
526 if (!image.isNull()) { | 496 if (!image.isNull()) { |
527 shortcut_info_.favicon = gfx::Image::CreateFrom1xBitmap(image); | 497 shortcut_info_.favicon.Add(gfx::ImageSkia::CreateFrom1xBitmap(image)); |
528 static_cast<AppInfoView*>(app_info_)->UpdateIcon(shortcut_info_.favicon); | 498 static_cast<AppInfoView*>(app_info_)->UpdateIcon(shortcut_info_.favicon); |
529 } else { | 499 } else { |
530 FetchIcon(); | 500 FetchIcon(); |
531 } | 501 } |
532 } | 502 } |
533 | 503 |
534 CreateChromeApplicationShortcutView::CreateChromeApplicationShortcutView( | 504 CreateChromeApplicationShortcutView::CreateChromeApplicationShortcutView( |
535 Profile* profile, | 505 Profile* profile, |
536 const extensions::Extension* app) : | 506 const extensions::Extension* app) : |
537 CreateApplicationShortcutView(profile), | 507 CreateApplicationShortcutView(profile), |
(...skipping 15 matching lines...) Expand all Loading... | |
553 CreateChromeApplicationShortcutView::~CreateChromeApplicationShortcutView() {} | 523 CreateChromeApplicationShortcutView::~CreateChromeApplicationShortcutView() {} |
554 | 524 |
555 // Called when the app's ShortcutInfo (with icon) is loaded. | 525 // Called when the app's ShortcutInfo (with icon) is loaded. |
556 void CreateChromeApplicationShortcutView::OnShortcutInfoLoaded( | 526 void CreateChromeApplicationShortcutView::OnShortcutInfoLoaded( |
557 const ShellIntegration::ShortcutInfo& shortcut_info) { | 527 const ShellIntegration::ShortcutInfo& shortcut_info) { |
558 shortcut_info_ = shortcut_info; | 528 shortcut_info_ = shortcut_info; |
559 | 529 |
560 CHECK(app_info_); | 530 CHECK(app_info_); |
561 static_cast<AppInfoView*>(app_info_)->UpdateIcon(shortcut_info_.favicon); | 531 static_cast<AppInfoView*>(app_info_)->UpdateIcon(shortcut_info_.favicon); |
562 } | 532 } |
OLD | NEW |