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