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 #import "chrome/browser/ui/cocoa/hyperlink_text_view.h"
12 #include "grit/generated_resources.h"
13 #include "skia/ext/skia_utils_mac.h"
14 #include "third_party/skia/include/core/SkColor.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 const base::string16 promoTextWithoutLink =
85 promoText.substr(0, offset) +
86 promoText.substr(offset + linkText.size());
88 NSFont* font = [NSFont labelFontOfSize:kFontSize];
89 NSColor* linkColor = gfx::SkColorToCalibratedNSColor(
90 chrome_style::GetLinkColor());
92 textView_.reset([[HyperlinkTextView alloc] init]);
93 [textView_ setMessageAndLink:base::SysUTF16ToNSString(promoTextWithoutLink)
94 withLink:base::SysUTF16ToNSString(linkText)
97 messageColor:gfx::SkColorToDeviceNSColor(kTextColor)
99 [textView_ setAcceptsFirstResponder:NO];
100 [[textView_ textContainer] setLineFragmentPadding:0.0];
101 RemoveUnderlining(textView_, offset, linkText.size());
102 [textView_ setDelegate:self];
104 [promoView setContentView:textView_];
106 [self setView:promoView];
109 - (BOOL)textView:(NSTextView *)textView
110 clickedOnLink:(id)link
111 atIndex:(NSUInteger)charIndex {
112 chrome::ShowBrowserSignin(browser_, signin::SOURCE_BOOKMARK_BUBBLE);