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 if (!icon) | |
177 // The family has no icons. Leave the image blank. | |
175 return; | 178 return; |
176 | 179 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 && | 180 if (bitmap.width() == kIconPreviewSizePixels && |
212 bitmap.height() == kIconPreviewSizePixels) { | 181 bitmap.height() == kIconPreviewSizePixels) { |
213 icon_->SetImage(gfx::ImageSkia::CreateFrom1xBitmap(bitmap)); | 182 icon_->SetImage(gfx::ImageSkia::CreateFrom1xBitmap(bitmap)); |
pkotwicz
2013/03/18 03:22:43
Comment: In a separate CL, it would be nice to cal
| |
214 } else { | 183 } else { |
215 // Resize the image to the desired size. | 184 // Resize the image to the desired size. |
216 SkBitmap resized_bitmap = skia::ImageOperations::Resize( | 185 SkBitmap resized_bitmap = skia::ImageOperations::Resize( |
217 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, | 186 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, |
218 kIconPreviewSizePixels, kIconPreviewSizePixels); | 187 kIconPreviewSizePixels, kIconPreviewSizePixels); |
219 | 188 |
220 icon_->SetImage(gfx::ImageSkia::CreateFrom1xBitmap(resized_bitmap)); | 189 icon_->SetImage(gfx::ImageSkia::CreateFrom1xBitmap(resized_bitmap)); |
221 } | 190 } |
222 } | 191 } |
223 | 192 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
279 create_shortcuts_label_(NULL), | 248 create_shortcuts_label_(NULL), |
280 desktop_check_box_(NULL), | 249 desktop_check_box_(NULL), |
281 menu_check_box_(NULL), | 250 menu_check_box_(NULL), |
282 quick_launch_check_box_(NULL) {} | 251 quick_launch_check_box_(NULL) {} |
283 | 252 |
284 CreateApplicationShortcutView::~CreateApplicationShortcutView() {} | 253 CreateApplicationShortcutView::~CreateApplicationShortcutView() {} |
285 | 254 |
286 void CreateApplicationShortcutView::InitControls() { | 255 void CreateApplicationShortcutView::InitControls() { |
287 // Create controls | 256 // Create controls |
288 app_info_ = new AppInfoView(shortcut_info_.title, shortcut_info_.description, | 257 app_info_ = new AppInfoView(shortcut_info_.title, shortcut_info_.description, |
289 shortcut_info_.favicon.IsEmpty() ? SkBitmap() : | 258 shortcut_info_.favicon); |
290 *shortcut_info_.favicon.ToSkBitmap()); | |
291 create_shortcuts_label_ = new views::Label( | 259 create_shortcuts_label_ = new views::Label( |
292 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_LABEL)); | 260 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_LABEL)); |
293 create_shortcuts_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 261 create_shortcuts_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
294 | 262 |
295 desktop_check_box_ = AddCheckbox( | 263 desktop_check_box_ = AddCheckbox( |
296 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_DESKTOP_CHKBOX), | 264 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_DESKTOP_CHKBOX), |
297 profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateOnDesktop)); | 265 profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateOnDesktop)); |
298 | 266 |
299 menu_check_box_ = NULL; | 267 menu_check_box_ = NULL; |
300 quick_launch_check_box_ = NULL; | 268 quick_launch_check_box_ = NULL; |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
466 InitControls(); | 434 InitControls(); |
467 } | 435 } |
468 | 436 |
469 CreateUrlApplicationShortcutView::~CreateUrlApplicationShortcutView() { | 437 CreateUrlApplicationShortcutView::~CreateUrlApplicationShortcutView() { |
470 } | 438 } |
471 | 439 |
472 bool CreateUrlApplicationShortcutView::Accept() { | 440 bool CreateUrlApplicationShortcutView::Accept() { |
473 if (!CreateApplicationShortcutView::Accept()) | 441 if (!CreateApplicationShortcutView::Accept()) |
474 return false; | 442 return false; |
475 | 443 |
476 extensions::TabHelper::FromWebContents(web_contents_)-> | 444 // Get the smallest icon in the icon family (should have only 1). |
477 SetAppIcon(shortcut_info_.favicon.IsEmpty() | 445 const gfx::ImageSkia* icon = shortcut_info_.favicon.Get(0); |
478 ? SkBitmap() | 446 SkBitmap bitmap = icon ? *icon->bitmap() : SkBitmap(); |
479 : *shortcut_info_.favicon.ToSkBitmap()); | 447 extensions::TabHelper::FromWebContents(web_contents_)->SetAppIcon(bitmap); |
480 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); | 448 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); |
481 if (browser) | 449 if (browser) |
482 chrome::ConvertTabToAppWindow(browser, web_contents_); | 450 chrome::ConvertTabToAppWindow(browser, web_contents_); |
483 return true; | 451 return true; |
484 } | 452 } |
485 | 453 |
486 void CreateUrlApplicationShortcutView::FetchIcon() { | 454 void CreateUrlApplicationShortcutView::FetchIcon() { |
487 // There should only be fetch job at a time. | 455 // There should only be fetch job at a time. |
488 DCHECK_EQ(-1, pending_download_id_); | 456 DCHECK_EQ(-1, pending_download_id_); |
489 | 457 |
(...skipping 27 matching lines...) Expand all Loading... | |
517 web_contents_->GetRenderViewHost()->GetView()->GetNativeView()); | 485 web_contents_->GetRenderViewHost()->GetView()->GetNativeView()); |
518 scale_factors.push_back(scale_factor); | 486 scale_factors.push_back(scale_factor); |
519 size_t closest_index = FaviconUtil::SelectBestFaviconFromBitmaps( | 487 size_t closest_index = FaviconUtil::SelectBestFaviconFromBitmaps( |
520 bitmaps, | 488 bitmaps, |
521 scale_factors, | 489 scale_factors, |
522 requested_size); | 490 requested_size); |
523 image = bitmaps[closest_index]; | 491 image = bitmaps[closest_index]; |
524 } | 492 } |
525 | 493 |
526 if (!image.isNull()) { | 494 if (!image.isNull()) { |
527 shortcut_info_.favicon = gfx::Image::CreateFrom1xBitmap(image); | 495 shortcut_info_.favicon.Add(gfx::ImageSkia::CreateFrom1xBitmap(image)); |
528 static_cast<AppInfoView*>(app_info_)->UpdateIcon(shortcut_info_.favicon); | 496 static_cast<AppInfoView*>(app_info_)->UpdateIcon(shortcut_info_.favicon); |
529 } else { | 497 } else { |
530 FetchIcon(); | 498 FetchIcon(); |
531 } | 499 } |
532 } | 500 } |
533 | 501 |
534 CreateChromeApplicationShortcutView::CreateChromeApplicationShortcutView( | 502 CreateChromeApplicationShortcutView::CreateChromeApplicationShortcutView( |
535 Profile* profile, | 503 Profile* profile, |
536 const extensions::Extension* app) : | 504 const extensions::Extension* app) : |
537 CreateApplicationShortcutView(profile), | 505 CreateApplicationShortcutView(profile), |
(...skipping 15 matching lines...) Expand all Loading... | |
553 CreateChromeApplicationShortcutView::~CreateChromeApplicationShortcutView() {} | 521 CreateChromeApplicationShortcutView::~CreateChromeApplicationShortcutView() {} |
554 | 522 |
555 // Called when the app's ShortcutInfo (with icon) is loaded. | 523 // Called when the app's ShortcutInfo (with icon) is loaded. |
556 void CreateChromeApplicationShortcutView::OnShortcutInfoLoaded( | 524 void CreateChromeApplicationShortcutView::OnShortcutInfoLoaded( |
557 const ShellIntegration::ShortcutInfo& shortcut_info) { | 525 const ShellIntegration::ShortcutInfo& shortcut_info) { |
558 shortcut_info_ = shortcut_info; | 526 shortcut_info_ = shortcut_info; |
559 | 527 |
560 CHECK(app_info_); | 528 CHECK(app_info_); |
561 static_cast<AppInfoView*>(app_info_)->UpdateIcon(shortcut_info_.favicon); | 529 static_cast<AppInfoView*>(app_info_)->UpdateIcon(shortcut_info_.favicon); |
562 } | 530 } |
OLD | NEW |