1 // Copyright 2013 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 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_sync_promo_controller.h"
7 #include "base/strings/sys_string_conversions.h"
8 #include "chrome/browser/signin/signin_promo.h"
9 #include "chrome/browser/ui/chrome_pages.h"
10 #include "chrome/browser/ui/chrome_style.h"
11 #include "chrome/grit/generated_resources.h"
12 #include "skia/ext/skia_utils_mac.h"
13 #include "third_party/skia/include/core/SkColor.h"
14 #import "ui/base/cocoa/controls/hyperlink_text_view.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/base/l10n/l10n_util_mac.h"
20 // Remove underlining from the specified range of characters in a text view.
21 void RemoveUnderlining(NSTextView* textView, int offset, int length) {
22 [textView setLinkTextAttributes:nil];
23 NSTextStorage* text = [textView textStorage];
24 NSRange range = NSMakeRange(offset, length);
25 [text addAttribute:NSUnderlineStyleAttributeName
26 value:[NSNumber numberWithInt:NSUnderlineStyleNone]
30 const SkColor kTextColor = SkColorSetRGB(0x66, 0x66, 0x66);
31 const SkColor kBackgroundColor = SkColorSetRGB(0xf5, 0xf5, 0xf5);
32 const SkColor kBorderColor = SkColorSetRGB(0xe5, 0xe5, 0xe5);
34 // Vertical padding of the promo (dp).
35 const CGFloat kVerticalPadding = 15;
37 // Width of the border (dp).
38 const CGFloat kBorderWidth = 1.0;
40 // Font size of the promo text (pt).
41 const int kFontSize = 11;
45 @implementation BookmarkSyncPromoController
47 - (id)initWithBrowser:(Browser*)browser {
48 if ((self = [super init])) {
54 - (CGFloat)borderWidth {
58 - (CGFloat)preferredHeightForWidth:(CGFloat)width {
59 CGFloat availableWidth =
60 width - (2 * chrome_style::kHorizontalPadding) - (2 * kBorderWidth);
61 NSRect frame = [[textView_ textStorage]
62 boundingRectWithSize:NSMakeSize(availableWidth, 0.0)
63 options:NSStringDrawingUsesLineFragmentOrigin];
64 return frame.size.height + (2 * kVerticalPadding) + (2 * kBorderWidth);
68 NSBox* promoView = [[[NSBox alloc] init] autorelease];
69 [promoView setBoxType:NSBoxCustom];
70 [promoView setFillColor:gfx::SkColorToDeviceNSColor(kBackgroundColor)];
71 [promoView setContentViewMargins:NSMakeSize(chrome_style::kHorizontalPadding,
73 [promoView setBorderType:NSLineBorder];
74 [promoView setBorderWidth:kBorderWidth];
75 [promoView setBorderColor:gfx::SkColorToDeviceNSColor(kBorderColor)];
77 // Add the sync promo text.
79 const base::string16 linkText = l10n_util::GetStringUTF16(
80 IDS_BOOKMARK_SYNC_PROMO_LINK);
81 const base::string16 promoText = l10n_util::GetStringFUTF16(
82 IDS_BOOKMARK_SYNC_PROMO_MESSAGE,
84 NSString* nsPromoText = SysUTF16ToNSString(promoText);
85 NSString* nsLinkText = SysUTF16ToNSString(linkText);
86 NSFont* font = [NSFont labelFontOfSize:kFontSize];
87 NSColor* linkColor = gfx::SkColorToCalibratedNSColor(
88 chrome_style::GetLinkColor());
90 textView_.reset([[HyperlinkTextView alloc] init]);
91 [textView_ setMessage:nsPromoText
93 messageColor:gfx::SkColorToDeviceNSColor(kTextColor)];
94 [textView_ addLinkRange:NSMakeRange(offset, [nsLinkText length])
97 [textView_ setRefusesFirstResponder:YES];
98 [[textView_ textContainer] setLineFragmentPadding:0.0];
99 RemoveUnderlining(textView_, offset, linkText.size());
100 [textView_ setDelegate:self];
102 [promoView setContentView:textView_];
104 [self setView:promoView];
107 - (BOOL)textView:(NSTextView *)textView
108 clickedOnLink:(id)link
109 atIndex:(NSUInteger)charIndex {
110 chrome::ShowBrowserSignin(browser_, signin_metrics::SOURCE_BOOKMARK_BUBBLE);