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