| 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/tab_icon_view.h" | 5 #include "chrome/browser/ui/views/tab_icon_view.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 #include <shellapi.h> | 9 #include <shellapi.h> |
| 10 #endif | 10 #endif |
| 11 | 11 |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 14 #include "chrome/app/chrome_command_ids.h" | 15 #include "chrome/app/chrome_command_ids.h" |
| 15 #include "grit/theme_resources.h" | 16 #include "grit/theme_resources.h" |
| 16 #include "grit/ui_resources.h" | 17 #include "grit/ui_resources.h" |
| 17 #include "grit/ui_resources_standard.h" | 18 #include "grit/ui_resources_standard.h" |
| 18 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
| 19 #include "ui/base/theme_provider.h" | 20 #include "ui/base/theme_provider.h" |
| 20 #include "ui/gfx/canvas.h" | 21 #include "ui/gfx/canvas.h" |
| 21 #include "ui/gfx/favicon_size.h" | 22 #include "ui/gfx/favicon_size.h" |
| 22 | 23 |
| 23 #if defined(OS_WIN) | 24 #if defined(OS_WIN) |
| 24 #include "chrome/browser/app_icon_win.h" | 25 #include "chrome/browser/app_icon_win.h" |
| 25 #include "ui/gfx/icon_util.h" | 26 #include "ui/gfx/icon_util.h" |
| 26 #endif | 27 #endif |
| 27 | 28 |
| 28 static bool g_initialized = false; | 29 static bool g_initialized = false; |
| 29 static SkBitmap* g_default_favicon = NULL; | 30 static gfx::ImageSkia* g_default_favicon = NULL; |
| 30 | 31 |
| 31 // static | 32 // static |
| 32 void TabIconView::InitializeIfNeeded() { | 33 void TabIconView::InitializeIfNeeded() { |
| 33 if (!g_initialized) { | 34 if (!g_initialized) { |
| 34 g_initialized = true; | 35 g_initialized = true; |
| 35 | 36 |
| 36 #if defined(OS_WIN) | 37 #if defined(OS_WIN) |
| 37 // The default window icon is the application icon, not the default | 38 // The default window icon is the application icon, not the default |
| 38 // favicon. | 39 // favicon. |
| 39 HICON app_icon = GetAppIcon(); | 40 HICON app_icon = GetAppIcon(); |
| 40 g_default_favicon = | 41 scoped_ptr<SkBitmap> bitmap( |
| 41 IconUtil::CreateSkBitmapFromHICON(app_icon, gfx::Size(16, 16)); | 42 IconUtil::CreateSkBitmapFromHICON(app_icon, gfx::Size(16, 16))); |
| 43 g_default_favicon = new gfx::ImageSkia(*bitmap); |
| 42 DestroyIcon(app_icon); | 44 DestroyIcon(app_icon); |
| 43 #else | 45 #else |
| 44 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 46 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 45 g_default_favicon = rb.GetBitmapNamed(IDR_PRODUCT_LOGO_16); | 47 g_default_favicon = rb.GetImageSkiaNamed(IDR_PRODUCT_LOGO_16); |
| 46 #endif | 48 #endif |
| 47 } | 49 } |
| 48 } | 50 } |
| 49 | 51 |
| 50 TabIconView::TabIconView(TabIconViewModel* model) | 52 TabIconView::TabIconView(TabIconViewModel* model) |
| 51 : model_(model), | 53 : model_(model), |
| 52 throbber_running_(false), | 54 throbber_running_(false), |
| 53 is_light_(false), | 55 is_light_(false), |
| 54 throbber_frame_(0) { | 56 throbber_frame_(0) { |
| 55 InitializeIfNeeded(); | 57 InitializeIfNeeded(); |
| 56 } | 58 } |
| 57 | 59 |
| 58 TabIconView::~TabIconView() { | 60 TabIconView::~TabIconView() { |
| 59 } | 61 } |
| 60 | 62 |
| 61 void TabIconView::Update() { | 63 void TabIconView::Update() { |
| 62 static bool initialized = false; | 64 static bool initialized = false; |
| 63 static int throbber_frame_count = 0; | 65 static int throbber_frame_count = 0; |
| 64 if (!initialized) { | 66 if (!initialized) { |
| 65 initialized = true; | 67 initialized = true; |
| 66 SkBitmap throbber( | 68 gfx::ImageSkia throbber(*ui::ResourceBundle::GetSharedInstance(). |
| 67 *ui::ResourceBundle::GetSharedInstance().GetBitmapNamed(IDR_THROBBER)); | 69 GetImageSkiaNamed(IDR_THROBBER)); |
| 68 throbber_frame_count = throbber.width() / throbber.height(); | 70 throbber_frame_count = throbber.width() / throbber.height(); |
| 69 } | 71 } |
| 70 | 72 |
| 71 if (throbber_running_) { | 73 if (throbber_running_) { |
| 72 // We think the tab is loading. | 74 // We think the tab is loading. |
| 73 if (!model_->ShouldTabIconViewAnimate()) { | 75 if (!model_->ShouldTabIconViewAnimate()) { |
| 74 // Woops, tab is invalid or not loading, reset our status and schedule | 76 // Woops, tab is invalid or not loading, reset our status and schedule |
| 75 // a paint. | 77 // a paint. |
| 76 throbber_running_ = false; | 78 throbber_running_ = false; |
| 77 SchedulePaint(); | 79 SchedulePaint(); |
| 78 } else { | 80 } else { |
| 79 // The tab is still loading, increment the frame. | 81 // The tab is still loading, increment the frame. |
| 80 throbber_frame_ = (throbber_frame_ + 1) % throbber_frame_count; | 82 throbber_frame_ = (throbber_frame_ + 1) % throbber_frame_count; |
| 81 SchedulePaint(); | 83 SchedulePaint(); |
| 82 } | 84 } |
| 83 } else if (model_->ShouldTabIconViewAnimate()) { | 85 } else if (model_->ShouldTabIconViewAnimate()) { |
| 84 // We didn't think we were loading, but the tab is loading. Reset the | 86 // We didn't think we were loading, but the tab is loading. Reset the |
| 85 // frame and status and schedule a paint. | 87 // frame and status and schedule a paint. |
| 86 throbber_running_ = true; | 88 throbber_running_ = true; |
| 87 throbber_frame_ = 0; | 89 throbber_frame_ = 0; |
| 88 SchedulePaint(); | 90 SchedulePaint(); |
| 89 } | 91 } |
| 90 } | 92 } |
| 91 | 93 |
| 92 void TabIconView::PaintThrobber(gfx::Canvas* canvas) { | 94 void TabIconView::PaintThrobber(gfx::Canvas* canvas) { |
| 93 SkBitmap throbber(*GetThemeProvider()->GetBitmapNamed( | 95 gfx::ImageSkia throbber(*GetThemeProvider()->GetImageSkiaNamed( |
| 94 is_light_ ? IDR_THROBBER_LIGHT : IDR_THROBBER)); | 96 is_light_ ? IDR_THROBBER_LIGHT : IDR_THROBBER)); |
| 95 int image_size = throbber.height(); | 97 int image_size = throbber.height(); |
| 96 PaintIcon(canvas, throbber, throbber_frame_ * image_size, 0, image_size, | 98 PaintIcon(canvas, throbber, throbber_frame_ * image_size, 0, image_size, |
| 97 image_size, false); | 99 image_size, false); |
| 98 } | 100 } |
| 99 | 101 |
| 100 void TabIconView::PaintFavicon(gfx::Canvas* canvas, const SkBitmap& bitmap) { | 102 void TabIconView::PaintFavicon(gfx::Canvas* canvas, const SkBitmap& image) { |
| 101 PaintIcon(canvas, bitmap, 0, 0, bitmap.width(), bitmap.height(), true); | 103 PaintIcon(canvas, image, 0, 0, image.width(), image.height(), true); |
| 102 } | 104 } |
| 103 | 105 |
| 104 void TabIconView::PaintIcon(gfx::Canvas* canvas, | 106 void TabIconView::PaintIcon(gfx::Canvas* canvas, |
| 105 const SkBitmap& bitmap, | 107 const gfx::ImageSkia& image, |
| 106 int src_x, | 108 int src_x, |
| 107 int src_y, | 109 int src_y, |
| 108 int src_w, | 110 int src_w, |
| 109 int src_h, | 111 int src_h, |
| 110 bool filter) { | 112 bool filter) { |
| 111 // For source images smaller than the favicon square, scale them as if they | 113 // For source images smaller than the favicon square, scale them as if they |
| 112 // were padded to fit the favicon square, so we don't blow up tiny favicons | 114 // were padded to fit the favicon square, so we don't blow up tiny favicons |
| 113 // into larger or nonproportional results. | 115 // into larger or nonproportional results. |
| 114 float float_src_w = static_cast<float>(src_w); | 116 float float_src_w = static_cast<float>(src_w); |
| 115 float float_src_h = static_cast<float>(src_h); | 117 float float_src_h = static_cast<float>(src_h); |
| 116 float scalable_w, scalable_h; | 118 float scalable_w, scalable_h; |
| 117 if (src_w <= gfx::kFaviconSize && src_h <= gfx::kFaviconSize) { | 119 if (src_w <= gfx::kFaviconSize && src_h <= gfx::kFaviconSize) { |
| 118 scalable_w = scalable_h = gfx::kFaviconSize; | 120 scalable_w = scalable_h = gfx::kFaviconSize; |
| 119 } else { | 121 } else { |
| 120 scalable_w = float_src_w; | 122 scalable_w = float_src_w; |
| 121 scalable_h = float_src_h; | 123 scalable_h = float_src_h; |
| 122 } | 124 } |
| 123 | 125 |
| 124 // Scale proportionately. | 126 // Scale proportionately. |
| 125 float scale = std::min(static_cast<float>(width()) / scalable_w, | 127 float scale = std::min(static_cast<float>(width()) / scalable_w, |
| 126 static_cast<float>(height()) / scalable_h); | 128 static_cast<float>(height()) / scalable_h); |
| 127 int dest_w = static_cast<int>(float_src_w * scale); | 129 int dest_w = static_cast<int>(float_src_w * scale); |
| 128 int dest_h = static_cast<int>(float_src_h * scale); | 130 int dest_h = static_cast<int>(float_src_h * scale); |
| 129 | 131 |
| 130 // Center the scaled image. | 132 // Center the scaled image. |
| 131 canvas->DrawBitmapInt(bitmap, src_x, src_y, src_w, src_h, | 133 canvas->DrawBitmapInt(image, src_x, src_y, src_w, src_h, |
| 132 (width() - dest_w) / 2, (height() - dest_h) / 2, dest_w, | 134 (width() - dest_w) / 2, (height() - dest_h) / 2, dest_w, |
| 133 dest_h, filter); | 135 dest_h, filter); |
| 134 } | 136 } |
| 135 | 137 |
| 136 void TabIconView::OnPaint(gfx::Canvas* canvas) { | 138 void TabIconView::OnPaint(gfx::Canvas* canvas) { |
| 137 bool rendered = false; | 139 bool rendered = false; |
| 138 | 140 |
| 139 if (throbber_running_) { | 141 if (throbber_running_) { |
| 140 rendered = true; | 142 rendered = true; |
| 141 PaintThrobber(canvas); | 143 PaintThrobber(canvas); |
| 142 } else { | 144 } else { |
| 143 SkBitmap favicon = model_->GetFaviconForTabIconView(); | 145 SkBitmap favicon = model_->GetFaviconForTabIconView(); |
| 144 if (!favicon.isNull()) { | 146 if (!favicon.isNull()) { |
| 145 rendered = true; | 147 rendered = true; |
| 146 PaintFavicon(canvas, favicon); | 148 PaintFavicon(canvas, favicon); |
| 147 } | 149 } |
| 148 } | 150 } |
| 149 | 151 |
| 150 if (!rendered) | 152 if (!rendered) |
| 151 PaintFavicon(canvas, *g_default_favicon); | 153 PaintFavicon(canvas, *g_default_favicon); |
| 152 } | 154 } |
| 153 | 155 |
| 154 gfx::Size TabIconView::GetPreferredSize() { | 156 gfx::Size TabIconView::GetPreferredSize() { |
| 155 return gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize); | 157 return gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize); |
| 156 } | 158 } |
| OLD | NEW |