Chromium Code Reviews| Index: win8/metro_driver/secondary_tile.cc |
| diff --git a/win8/metro_driver/secondary_tile.cc b/win8/metro_driver/secondary_tile.cc |
| index c784fdcf3904d7b01c3cf91408aeb09442b14cda..183e971412e9b84d94aaf6604774cec14a1ce266 100644 |
| --- a/win8/metro_driver/secondary_tile.cc |
| +++ b/win8/metro_driver/secondary_tile.cc |
| @@ -10,14 +10,25 @@ |
| #include "base/base_paths.h" |
| #include "base/bind.h" |
| #include "base/file_path.h" |
| +#include "base/file_util.h" |
| #include "base/logging.h" |
| +#include "base/memory/ref_counted_memory.h" |
| #include "base/path_service.h" |
| #include "base/string_number_conversions.h" |
| #include "base/utf_string_conversions.h" |
| +#include "chrome/common/chrome_paths.h" |
| #include "crypto/sha2.h" |
| #include "googleurl/src/gurl.h" |
| -#include "win8/metro_driver/chrome_app_view.h" |
| -#include "win8/metro_driver/winrt_utils.h" |
| +#include "metro_driver/chrome_app_view.h" |
| +#include "metro_driver/winrt_utils.h" |
| +#include "third_party/skia/include/core/SkPaint.h" |
| +#include "ui/base/layout.h" |
| +#include "ui/gfx/canvas.h" |
| +#include "ui/gfx/codec/png_codec.h" |
| +#include "ui/gfx/color_analysis.h" |
| +#include "ui/gfx/color_utils.h" |
| +#include "ui/gfx/rect.h" |
| +#include "ui/gfx/size.h" |
| namespace { |
| @@ -28,12 +39,91 @@ string16 GenerateTileId(const string16& url_str) { |
| return UTF8ToUTF16(hash_str); |
| } |
| -string16 GetLogoUrlString() { |
| - FilePath module_path; |
| - PathService::Get(base::DIR_MODULE, &module_path); |
| - string16 scheme(L"ms-appx:///"); |
| - return scheme.append(module_path.BaseName().value()) |
| - .append(L"/SecondaryTile.png"); |
| +bool CreateLogoFromFavicon(const SkBitmap* icon_bitmap, |
| + const FilePath& tile_path) { |
| + static int k_logo_width = 120; |
|
sky
2012/10/19 16:09:56
constants like this are kFooBar.
benwells
2012/10/22 06:20:47
Done.
|
| + static int k_logo_height = 120; |
| + static int k_box_width = 40; |
| + static int k_box_height = 40; |
| + static int k_caption_height = 20; |
| + static double k_box_fade = 0.75; |
|
cpu_(ooo_6.6-7.5)
2012/10/19 19:39:08
I don't see any metro specific calls here. Wouldn'
benwells
2012/10/22 06:20:47
OK, yeah that sounds like a great idea! It will ma
|
| + |
| + // Use a canvas to paint the tile logo. |
| + gfx::Canvas canvas(gfx::Size(k_logo_width, k_logo_height), |
| + ui::SCALE_FACTOR_100P, |
| + true); |
| + // Fill the tile logo with the average color from bitmap. To do this we need |
| + // to work out the 'average color' which is calculated using PNG encoded data |
| + // of the bitmap. |
| + SkPaint paint; |
| + std::vector<unsigned char> icon_png; |
| + if (!gfx::PNGCodec::EncodeBGRASkBitmap(*icon_bitmap, true, &icon_png)) |
| + return false; |
| + |
| + scoped_refptr<base::RefCountedStaticMemory> icon_mem( |
| + new base::RefCountedStaticMemory(&icon_png.front(), icon_png.size())); |
| + color_utils::GridSampler sampler; |
| + SkColor mean_color = |
| + color_utils::CalculateKMeanColorOfPNG(icon_mem, 100, 665, sampler); |
| + paint.setColor(mean_color); |
| + canvas.DrawRect(gfx::Rect(0, 0, k_logo_width, k_logo_height), paint); |
| + |
| + // Now paint a faded square for the favicon to go in. |
| + color_utils::HSL shift = {-1, -1, k_box_fade}; |
| + paint.setColor(color_utils::HSLShift(mean_color, shift)); |
| + int box_left = (k_logo_width - k_box_width) / 2; |
| + int box_top = (k_logo_height - k_caption_height - k_box_height) / 2; |
| + canvas.DrawRect(gfx::Rect(box_left, box_top, k_box_width, k_box_height), |
| + paint); |
| + |
| + // Now paint the favicon into the tile, leaving some room at the bottom for |
| + // the caption. |
| + gfx::ImageSkia icon_image(*icon_bitmap); |
|
sky
2012/10/19 16:09:56
Maybe this should take an ImageSkia rather than Sk
benwells
2012/10/22 06:20:47
Done.
|
| + int left = (k_logo_width - icon_image.width()) / 2; |
| + int top = (k_logo_height - k_caption_height - icon_image.height()) / 2; |
| + canvas.DrawImageInt(icon_image, left, top); |
| + |
| + SkBitmap logo_bitmap = canvas.ExtractImageRep().sk_bitmap(); |
| + std::vector<unsigned char> logo_png; |
| + if (!gfx::PNGCodec::EncodeBGRASkBitmap(logo_bitmap, true, &logo_png)) |
| + return false; |
| + |
| + return file_util::WriteFile(tile_path, |
| + reinterpret_cast<char*>(&logo_png[0]), |
| + logo_png.size()) > 0; |
| +} |
| + |
| +string16 GetLogoUrlString(const SkBitmap* favicon, const string16& tile_id) { |
| + FilePath logo_dir; |
| + DCHECK(PathService::Get(chrome::DIR_USER_DATA, &logo_dir)); |
| + logo_dir = logo_dir.Append(L"TileImages"); |
| + if (!file_util::DirectoryExists(logo_dir) && |
| + !file_util::CreateDirectory(logo_dir)) |
| + return string16(); |
| + |
| + string16 scheme(L"file:///"); |
| + if (favicon) { |
| + FilePath logo_path = logo_dir.Append(tile_id) |
| + .ReplaceExtension(L".png"); |
| + if (CreateLogoFromFavicon(favicon, logo_path)) |
| + return scheme.append(logo_path.value()); |
| + } |
| + |
| + // Use default tile image. If it doesn't exist, copy it out of the install |
| + // folder. The version in the install folder is not used as it may disappear |
| + // after an upgrade, causing tiles to lose their images if Windows rebuilds |
| + // its tile image cache. |
| + static const wchar_t kDefaultLogoFileName[] = L"SecondaryTile.png"; |
| + FilePath logo_path = logo_dir.Append(kDefaultLogoFileName); |
| + if (!file_util::PathExists(logo_path)) { |
| + FilePath default_logo_path; |
| + DCHECK(PathService::Get(base::DIR_MODULE, &default_logo_path)); |
| + default_logo_path = default_logo_path.Append(kDefaultLogoFileName); |
| + if (!file_util::CopyFile(default_logo_path, logo_path)) |
|
sky
2012/10/19 16:09:56
What thread is this run on?
benwells
2012/10/22 06:20:47
This is all happening in the metro thread. I'll pu
|
| + return string16(); |
| + } |
| + |
| + return scheme.append(logo_path.value()); |
| } |
| BOOL IsPinnedToStartScreen(const string16& url_str) { |
| @@ -77,8 +167,10 @@ void DeleteTileFromStartScreen(const string16& url_str) { |
| } |
| void CreateTileOnStartScreen(const string16& title_str, |
| - const string16& url_str) { |
| + const string16& url_str, |
| + const SkBitmap* bitmap) { |
| VLOG(1) << __FUNCTION__; |
| + |
| mswr::ComPtr<winui::StartScreen::ISecondaryTileFactory> tile_factory; |
| HRESULT hr = winrt_utils::CreateActivationFactory( |
| RuntimeClass_Windows_UI_StartScreen_SecondaryTile, |
| @@ -89,8 +181,11 @@ void CreateTileOnStartScreen(const string16& title_str, |
| winui::StartScreen::TileOptions_ShowNameOnLogo; |
| mswrw::HString title; |
| title.Attach(MakeHString(title_str)); |
| + |
| + string16 id_str = GenerateTileId(url_str); |
| mswrw::HString id; |
| - id.Attach(MakeHString(GenerateTileId(url_str))); |
| + id.Attach(MakeHString(id_str)); |
| + |
| mswrw::HString args; |
| // The url is just passed into the tile agruments as is. Metro and desktop |
| // chrome will see the arguments as command line parameters. |
| @@ -105,7 +200,7 @@ void CreateTileOnStartScreen(const string16& title_str, |
| CheckHR(hr, "Failed to create URIFactory"); |
| mswrw::HString logo_url; |
| - logo_url.Attach(MakeHString(GetLogoUrlString())); |
| + logo_url.Attach(MakeHString(GetLogoUrlString(bitmap, id_str))); |
| mswr::ComPtr<winfoundtn::IUriRuntimeClass> uri; |
| hr = uri_factory->CreateUri(logo_url.Get(), &uri); |
| CheckHR(hr, "Failed to create URI"); |
| @@ -142,7 +237,7 @@ void TogglePinnedToStartScreen(const string16& title_str, |
| return; |
| } |
| - CreateTileOnStartScreen(title_str, url_str); |
| + CreateTileOnStartScreen(title_str, url_str, NULL); |
| } |
| } // namespace |
| @@ -158,3 +253,18 @@ void MetroTogglePinnedToStartScreen(const string16& title, |
| globals.appview_msg_loop->PostTask( |
| FROM_HERE, base::Bind(&TogglePinnedToStartScreen, title, url)); |
| } |
| + |
| +void MetroUnPinFromStartScreen(const string16& url) { |
| + globals.appview_msg_loop->PostTask( |
| + FROM_HERE, base::Bind(&DeleteTileFromStartScreen, url)); |
| +} |
| + |
| +void MetroPinToStartScreen(const string16& title, |
| + const string16& url, |
| + const SkBitmap* bitmap) { |
| + globals.appview_msg_loop->PostTask( |
| + FROM_HERE, base::Bind(&CreateTileOnStartScreen, |
| + title, |
| + url, |
| + bitmap)); |
| +} |