diff options
author | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-09 07:35:32 +0000 |
---|---|---|
committer | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-09 07:35:32 +0000 |
commit | 213dac2f0bff9162502fe325b6ebb85a255efcb2 (patch) | |
tree | 3640cb1f19976e38677b8632537d2d41f8444d0f /chrome/browser/ui/views/tab_icon_view.cc | |
parent | 6de53d401aa8dc6c7e0a9874c71a95ce88ade50d (diff) | |
download | chromium_src-213dac2f0bff9162502fe325b6ebb85a255efcb2.zip chromium_src-213dac2f0bff9162502fe325b6ebb85a255efcb2.tar.gz chromium_src-213dac2f0bff9162502fe325b6ebb85a255efcb2.tar.bz2 |
Move browser/views to browser/ui/views
TBR=brettw
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/4694005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@65508 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/ui/views/tab_icon_view.cc')
-rw-r--r-- | chrome/browser/ui/views/tab_icon_view.cc | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/chrome/browser/ui/views/tab_icon_view.cc b/chrome/browser/ui/views/tab_icon_view.cc new file mode 100644 index 0000000..547b039 --- /dev/null +++ b/chrome/browser/ui/views/tab_icon_view.cc @@ -0,0 +1,156 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/views/tab_icon_view.h" + +#if defined(OS_WIN) +#include <windows.h> +#include <shellapi.h> +#endif + +#include "app/resource_bundle.h" +#include "app/theme_provider.h" +#include "base/file_util.h" +#include "base/logging.h" +#include "chrome/app/chrome_command_ids.h" +#include "chrome/browser/tab_contents/tab_contents.h" +#include "gfx/canvas.h" +#include "gfx/favicon_size.h" +#include "grit/app_resources.h" +#include "grit/theme_resources.h" + +#if defined(OS_WIN) +#include "chrome/browser/app_icon_win.h" +#include "gfx/icon_util.h" +#endif + +static bool g_initialized = false; +static SkBitmap* g_default_fav_icon = NULL; + +// static +void TabIconView::InitializeIfNeeded() { + if (!g_initialized) { + g_initialized = true; + +#if defined(OS_WIN) + // The default window icon is the application icon, not the default + // favicon. + HICON app_icon = GetAppIcon(); + g_default_fav_icon = + IconUtil::CreateSkBitmapFromHICON(app_icon, gfx::Size(16, 16)); + DestroyIcon(app_icon); +#else + g_default_fav_icon = + ResourceBundle::GetSharedInstance().GetBitmapNamed(IDR_PRODUCT_LOGO_16); +#endif + } +} + +TabIconView::TabIconView(TabIconViewModel* model) + : model_(model), + throbber_running_(false), + is_light_(false), + throbber_frame_(0) { + InitializeIfNeeded(); +} + +TabIconView::~TabIconView() { +} + +void TabIconView::Update() { + static bool initialized = false; + static int throbber_frame_count = 0; + if (!initialized) { + initialized = true; + SkBitmap throbber( + *ResourceBundle::GetSharedInstance().GetBitmapNamed(IDR_THROBBER)); + throbber_frame_count = throbber.width() / throbber.height(); + } + + if (throbber_running_) { + // We think the tab is loading. + if (!model_->ShouldTabIconViewAnimate()) { + // Woops, tab is invalid or not loading, reset our status and schedule + // a paint. + throbber_running_ = false; + SchedulePaint(); + } else { + // The tab is still loading, increment the frame. + throbber_frame_ = (throbber_frame_ + 1) % throbber_frame_count; + SchedulePaint(); + } + } else if (model_->ShouldTabIconViewAnimate()) { + // We didn't think we were loading, but the tab is loading. Reset the + // frame and status and schedule a paint. + throbber_running_ = true; + throbber_frame_ = 0; + SchedulePaint(); + } +} + +void TabIconView::PaintThrobber(gfx::Canvas* canvas) { + SkBitmap throbber(*GetThemeProvider()->GetBitmapNamed( + is_light_ ? IDR_THROBBER_LIGHT : IDR_THROBBER)); + int image_size = throbber.height(); + PaintIcon(canvas, throbber, throbber_frame_ * image_size, 0, image_size, + image_size, false); +} + +void TabIconView::PaintFavIcon(gfx::Canvas* canvas, const SkBitmap& bitmap) { + PaintIcon(canvas, bitmap, 0, 0, bitmap.width(), bitmap.height(), true); +} + +void TabIconView::PaintIcon(gfx::Canvas* canvas, + const SkBitmap& bitmap, + int src_x, + int src_y, + int src_w, + int src_h, + bool filter) { + // For source images smaller than the favicon square, scale them as if they + // were padded to fit the favicon square, so we don't blow up tiny favicons + // into larger or nonproportional results. + float float_src_w = static_cast<float>(src_w); + float float_src_h = static_cast<float>(src_h); + float scalable_w, scalable_h; + if (src_w <= kFavIconSize && src_h <= kFavIconSize) { + scalable_w = scalable_h = kFavIconSize; + } else { + scalable_w = float_src_w; + scalable_h = float_src_h; + } + + // Scale proportionately. + float scale = std::min(static_cast<float>(width()) / scalable_w, + static_cast<float>(height()) / scalable_h); + int dest_w = static_cast<int>(float_src_w * scale); + int dest_h = static_cast<int>(float_src_h * scale); + + // Center the scaled image. + canvas->DrawBitmapInt(bitmap, src_x, src_y, src_w, src_h, + (width() - dest_w) / 2, (height() - dest_h) / 2, dest_w, + dest_h, filter); +} + +void TabIconView::Paint(gfx::Canvas* canvas) { + bool rendered = false; + + if (throbber_running_) { + rendered = true; + PaintThrobber(canvas); + } else { + SkBitmap favicon = model_->GetFavIconForTabIconView(); + if (!favicon.isNull()) { + rendered = true; + PaintFavIcon(canvas, favicon); + } + } + + if (!rendered) + PaintFavIcon(canvas, *g_default_fav_icon); +} + +gfx::Size TabIconView::GetPreferredSize() { + return gfx::Size(kFavIconSize, kFavIconSize); +} |