summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/bubble_sync_promo_controller.mm
blob: 08345fd9ac6c8c198676e58dde059a6cb2d40e70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2013 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.

#import "chrome/browser/ui/cocoa/bubble_sync_promo_controller.h"

#include <stddef.h>

#include "base/strings/sys_string_conversions.h"
#include "chrome/browser/signin/signin_promo.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/chrome_style.h"
#include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h"
#include "skia/ext/skia_utils_mac.h"
#include "third_party/skia/include/core/SkColor.h"
#import "ui/base/cocoa/controls/hyperlink_text_view.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/l10n/l10n_util_mac.h"

namespace {

// Remove underlining from the specified range of characters in a text view.
void RemoveUnderlining(NSTextView* textView, int offset, int length) {
  [textView setLinkTextAttributes:nil];
  NSTextStorage* text = [textView textStorage];
  NSRange range = NSMakeRange(offset, length);
  [text addAttribute:NSUnderlineStyleAttributeName
               value:[NSNumber numberWithInt:NSUnderlineStyleNone]
               range:range];
}

const SkColor kTextColor = SkColorSetRGB(0x66, 0x66, 0x66);
const SkColor kBackgroundColor = SkColorSetRGB(0xf5, 0xf5, 0xf5);
const SkColor kBorderColor = SkColorSetRGB(0xe5, 0xe5, 0xe5);

// Vertical padding of the promo (dp).
const CGFloat kVerticalPadding = 15;

// Width of the border (dp).
const CGFloat kBorderWidth = 1.0;

// Font size of the promo text (pt).
const int kFontSize = 11;

}  // namespace

@implementation BubbleSyncPromoController

- (id)initWithBrowser:(Browser*)browser
        promoStringId:(int)promoStringId
         linkStringId:(int)linkStringId
          accessPoint:(signin_metrics::AccessPoint)accessPoint {
  if ((self = [super init])) {
    browser_ = browser;
    promoStringId_ = promoStringId;
    linkStringId_ = linkStringId;
    accessPoint_ = accessPoint;
  }
  return self;
}

- (CGFloat)borderWidth {
  return kBorderWidth;
}

- (CGFloat)preferredHeightForWidth:(CGFloat)width {
  CGFloat availableWidth =
      width - (2 * chrome_style::kHorizontalPadding) - (2 * kBorderWidth);
  NSRect frame = [[textView_ textStorage]
      boundingRectWithSize:NSMakeSize(availableWidth, 0.0)
                   options:NSStringDrawingUsesLineFragmentOrigin];
  return frame.size.height + (2 * kVerticalPadding) + (2 * kBorderWidth);
}

- (void)loadView {
  NSBox* promoView = [[[NSBox alloc] init] autorelease];
  [promoView setBoxType:NSBoxCustom];
  [promoView setFillColor:skia::SkColorToDeviceNSColor(kBackgroundColor)];
  [promoView setContentViewMargins:NSMakeSize(chrome_style::kHorizontalPadding,
                                              kVerticalPadding)];
  [promoView setBorderType:NSLineBorder];
  [promoView setBorderWidth:kBorderWidth];
  [promoView setBorderColor:skia::SkColorToDeviceNSColor(kBorderColor)];

  // Add the sync promo text.
  size_t offset;
  const base::string16 linkText = l10n_util::GetStringUTF16(linkStringId_);
  const base::string16 promoText =
      l10n_util::GetStringFUTF16(promoStringId_, linkText, &offset);
  NSString* nsPromoText = SysUTF16ToNSString(promoText);
  NSString* nsLinkText = SysUTF16ToNSString(linkText);
  NSFont* font = [NSFont labelFontOfSize:kFontSize];
  NSColor* linkColor = skia::SkColorToCalibratedNSColor(
      chrome_style::GetLinkColor());

  textView_.reset([[HyperlinkTextView alloc] init]);
  [textView_ setMessage:nsPromoText
               withFont:font
           messageColor:skia::SkColorToDeviceNSColor(kTextColor)];
  [textView_ addLinkRange:NSMakeRange(offset, [nsLinkText length])
                  withURL:nil
                linkColor:linkColor];
  [textView_ setRefusesFirstResponder:YES];
  [[textView_ textContainer] setLineFragmentPadding:0.0];
  RemoveUnderlining(textView_, offset, linkText.size());
  [textView_ setDelegate:self];

  [promoView setContentView:textView_];

  [self setView:promoView];
}

- (BOOL)textView:(NSTextView *)textView
   clickedOnLink:(id)link
         atIndex:(NSUInteger)charIndex {
  chrome::ShowBrowserSignin(browser_, accessPoint_);
  return YES;
}

@end