summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/passwords/passwords_bubble_utils.mm
blob: 1ba951b62d9ff93f7ed0899828faf9fbb668b841 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2015 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/passwords/passwords_bubble_utils.h"

#include "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#include "chrome/browser/ui/chrome_style.h"
#include "skia/ext/skia_utils_mac.h"
#include "ui/base/cocoa/controls/hyperlink_text_view.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/resources/grit/ui_resources.h"

namespace {

HyperlinkTextView* LabelWithLink(const base::string16& text,
                                 SkColor color,
                                 NSFont* font,
                                 gfx::Range range,
                                 id<NSTextViewDelegate> delegate) {
  base::scoped_nsobject<HyperlinkTextView> textView(
      [[HyperlinkTextView alloc] initWithFrame:NSZeroRect]);
  NSColor* textColor = skia::SkColorToCalibratedNSColor(color);
  [textView setMessage:base::SysUTF16ToNSString(text)
              withFont:font
          messageColor:textColor];
  NSRange brandRange = range.ToNSRange();
  if (brandRange.length) {
    NSColor* linkColor =
        skia::SkColorToCalibratedNSColor(chrome_style::GetLinkColor());
    [textView addLinkRange:brandRange
                    withURL:nil
                  linkColor:linkColor];
    [textView setDelegate:delegate];

    // Create the link with no underlining.
    [textView setLinkTextAttributes:nil];
    NSTextStorage* text = [textView textStorage];
    [text addAttribute:NSUnderlineStyleAttributeName
                 value:@(NSUnderlineStyleNone)
                 range:brandRange];
  } else {
    // TODO(vasilii): remove if crbug.com/515189 is fixed.
    [textView setRefusesFirstResponder:YES];
  }

  return textView.autorelease();
}

}  // namespace

NSFont* LabelFont() {
  return [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
}

NSSize LabelSize(int resourceID) {
  return [l10n_util::GetNSString(resourceID)
      sizeWithAttributes:@{NSFontAttributeName : LabelFont()}];
}

void InitLabel(NSTextField* textField, const base::string16& text) {
  [textField setStringValue:base::SysUTF16ToNSString(text)];
  [textField setEditable:NO];
  [textField setSelectable:NO];
  [textField setDrawsBackground:NO];
  [textField setBezeled:NO];
  [textField setFont:LabelFont()];
  [[textField cell] setLineBreakMode:NSLineBreakByTruncatingTail];
  [textField sizeToFit];
}

std::pair<CGFloat, CGFloat> GetResizedColumns(
    CGFloat maxWidth,
    std::pair<CGFloat, CGFloat> columnsWidth) {
  // Free space can be negative.
  CGFloat freeSpace =
      maxWidth - (columnsWidth.first + columnsWidth.second + kItemLabelSpacing);
  if (freeSpace >= 0) {
    return std::make_pair(columnsWidth.first + freeSpace / 2,
                          columnsWidth.second + freeSpace / 2);
  }
  // Make sure that the sizes are nonnegative.
  CGFloat firstColumnPercent =
      columnsWidth.first / (columnsWidth.first + columnsWidth.second);
  return std::make_pair(
      columnsWidth.first + freeSpace * firstColumnPercent,
      columnsWidth.second + freeSpace * (1 - firstColumnPercent));
}

NSSecureTextField* PasswordLabel(const base::string16& text) {
  base::scoped_nsobject<NSSecureTextField> textField(
      [[NSSecureTextField alloc] initWithFrame:NSZeroRect]);
  InitLabel(textField, text);
  return textField.autorelease();
}

NSButton* DialogButton(NSString* title) {
  base::scoped_nsobject<NSButton> button(
      [[NSButton alloc] initWithFrame:NSZeroRect]);
  [button setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
  [button setTitle:title];
  [button setBezelStyle:NSRoundedBezelStyle];
  [[button cell] setControlSize:NSSmallControlSize];
  [button sizeToFit];
  return button.autorelease();
}

HyperlinkTextView* TitleBubbleLabelWithLink(const base::string16& text,
                                            gfx::Range range,
                                            id<NSTextViewDelegate> delegate) {
  return LabelWithLink(
      text, SK_ColorBLACK,
      [NSFont systemFontOfSize:[NSFont systemFontSize]],
      range, delegate);
}

HyperlinkTextView* TitleDialogLabelWithLink(const base::string16& text,
                                            gfx::Range range,
                                            id<NSTextViewDelegate> delegate) {
  return LabelWithLink(
      text, SK_ColorBLACK,
      ResourceBundle::GetSharedInstance()
          .GetFontList(chrome_style::kTitleFontStyle)
          .GetPrimaryFont()
          .GetNativeFont(),
      range, delegate);
}

HyperlinkTextView* LabelWithLink(const base::string16& text,
                                 SkColor color,
                                 gfx::Range range,
                                 id<NSTextViewDelegate> delegate) {
  return LabelWithLink(
      text, color,
      ResourceBundle::GetSharedInstance()
          .GetFontList(ResourceBundle::SmallFont)
          .GetPrimaryFont()
          .GetNativeFont(),
      range, delegate);
}