Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / extensions / bundle_util.mm
blob0315a9dd8ccef3ffe7613cfadc254c53a01a9d63
1 // Copyright 2015 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.
5 #include "chrome/browser/ui/cocoa/extensions/bundle_util.h"
7 #include "base/mac/mac_util.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/strings/sys_string_conversions.h"
10 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
11 #include "ui/gfx/image/image_skia.h"
12 #include "ui/gfx/image/image_skia_util_mac.h"
14 namespace {
16 const CGFloat kExtensionIconSize = 32;
18 }  // namespace
20 CGFloat PopulateBundleItemsList(
21     const extensions::BundleInstaller::ItemList& items,
22     NSView* items_field) {
23   const CGFloat title_width =
24       [items_field frame].size.width - kExtensionIconSize;
25   CGFloat offset = 0;
26   // Go over the items backwards, since Cocoa coords go from the bottom up.
27   for (size_t i = items.size(); i > 0; --i) {
28     const extensions::BundleInstaller::Item& item = items[i - 1];
30     NSString* title = base::SysUTF16ToNSString(item.GetNameForDisplay());
31     base::scoped_nsobject<NSTextField> title_view([[NSTextField alloc]
32         initWithFrame:NSMakeRect(kExtensionIconSize, offset, title_width, 0)]);
33     [title_view setBordered:NO];
34     [title_view setEditable:NO];
35     [title_view setStringValue:title];
36     [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField:title_view];
38     NSRect title_frame = [title_view frame];
39     NSRect icon_frame =
40         NSMakeRect(0, offset, kExtensionIconSize, kExtensionIconSize);
42     // Vertically center-align icon and title.
43     CGFloat align = (icon_frame.size.height - title_frame.size.height) / 2;
44     if (align > 0) {
45       title_frame.origin.y += align;
46       [title_view setFrame:title_frame];
47     } else {
48       icon_frame.origin.y -= align;
49     }
51     gfx::ImageSkia skia_image = gfx::ImageSkia::CreateFrom1xBitmap(item.icon);
52     NSImage* image = gfx::NSImageFromImageSkiaWithColorSpace(
53         skia_image, base::mac::GetSystemColorSpace());
54     base::scoped_nsobject<NSImageView> icon_view(
55         [[NSImageView alloc] initWithFrame:icon_frame]);
56     [icon_view setImage:image];
58     [items_field addSubview:icon_view];
59     [items_field addSubview:title_view];
61     offset = NSMaxY(NSUnionRect(title_frame, icon_frame));
62   }
63   return offset;