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 "ui/app_list/cocoa/signin_view_controller.h"
7 #include "base/mac/foundation_util.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/strings/sys_string_conversions.h"
10 #include "skia/ext/skia_utils_mac.h"
11 #include "third_party/skia/include/core/SkColor.h"
12 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
13 #include "ui/app_list/app_list_constants.h"
14 #include "ui/app_list/signin_delegate.h"
15 #import "ui/base/cocoa/controls/blue_label_button.h"
16 #import "ui/base/cocoa/controls/hyperlink_button_cell.h"
17 #include "ui/base/resource/resource_bundle.h"
21 const CGFloat kTopPadding = 40;
22 const CGFloat kBottomPadding = 40;
23 const CGFloat kLeftPadding = 40;
24 const CGFloat kRightPadding = 40;
25 const CGFloat kHeadingPadding = 30;
26 const CGFloat kButtonPadding = 40;
28 const CGFloat kTitleFontSizeDelta = 5;
29 const SkColor kLinkColor = SkColorSetRGB(0x11, 0x55, 0xcc);
30 const SkColor kTextColor = SkColorSetRGB(0x33, 0x33, 0x33);
32 // Padding on the left of the text in the NSTextField and HyperlinkButtonCell.
33 const CGFloat kTextFieldPadding = 2;
37 @interface SigninViewController ()
39 - (void)onLearnMoreButtonClick:(id)sender;
40 - (void)onSettingsButtonClick:(id)sender;
41 - (void)onSigninButtonClick:(id)sender;
42 - (NSButton*)makeLinkButtonWithTitle:(NSString*)title
43 origin:(NSPoint)origin
45 - (NSTextField*)makeTextFieldWithText:(NSString*)text
48 - (void)populateAndLayoutView;
52 @interface AppsSigninView : NSView {
54 CGFloat cornerRadius_;
57 @property(assign, nonatomic) CGFloat cornerRadius;
61 @implementation SigninViewController;
63 - (id)initWithFrame:(NSRect)frame
64 cornerRadius:(CGFloat)cornerRadius
65 delegate:(app_list::SigninDelegate*)delegate {
66 if ((self = [super init])) {
67 base::scoped_nsobject<AppsSigninView> appsSigninView(
68 [[AppsSigninView alloc] initWithFrame:frame]);
69 [appsSigninView setCornerRadius:cornerRadius];
71 [self setView:appsSigninView];
72 [self populateAndLayoutView];
77 - (void)onLearnMoreButtonClick:(id)sender {
78 delegate_->OpenLearnMore();
81 - (void)onSettingsButtonClick:(id)sender {
82 delegate_->OpenSettings();
85 - (void)onSigninButtonClick:(id)sender {
86 delegate_->ShowSignin();
89 - (NSButton*)makeLinkButtonWithTitle:(NSString*)title
90 origin:(NSPoint)origin
92 base::scoped_nsobject<NSButton> button(
93 [[HyperlinkButtonCell buttonWithString:title] retain]);
94 [[button cell] setShouldUnderline:NO];
95 [[button cell] setTextColor:gfx::SkColorToSRGBNSColor(kLinkColor)];
97 origin.x -= kTextFieldPadding;
98 [button setFrameOrigin:origin];
99 [button setTarget:self];
100 [button setAction:action];
101 return button.autorelease();
104 - (NSTextField*)makeTextFieldWithText:(NSString*)text
106 frame:(NSRect)frame {
107 base::scoped_nsobject<NSTextField> textField(
108 [[NSTextField alloc] initWithFrame:frame]);
109 [textField setEditable:NO];
110 [textField setSelectable:NO];
111 [textField setDrawsBackground:NO];
112 [textField setBezeled:NO];
114 NSDictionary* textAttributes = @{
115 NSFontAttributeName : font,
116 NSForegroundColorAttributeName : gfx::SkColorToSRGBNSColor(kTextColor)
118 base::scoped_nsobject<NSAttributedString> attributedText(
119 [[NSAttributedString alloc] initWithString:text
120 attributes:textAttributes]);
121 [textField setAttributedStringValue:attributedText];
122 [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField:textField];
123 NSPoint origin = [textField frame].origin;
124 origin.x -= kTextFieldPadding;
125 origin.y -= NSHeight([textField bounds]);
126 [textField setFrameOrigin:origin];
127 return textField.autorelease();
130 - (void)populateAndLayoutView {
131 NSView* signinView = [self view];
132 NSRect frame = [signinView frame];
133 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
134 const gfx::FontList& baseFontList =
135 rb.GetFontList(ui::ResourceBundle::BaseFont);
137 NSString* titleText = base::SysUTF16ToNSString(delegate_->GetSigninHeading());
138 NSFont* titleFont = baseFontList.DeriveWithSizeDelta(kTitleFontSizeDelta)
139 .GetPrimaryFont().GetNativeFont();
140 NSRect rect = NSMakeRect(kLeftPadding, NSHeight(frame) - kTopPadding,
141 NSWidth(frame) - kLeftPadding - kRightPadding, 0);
142 NSTextField* titleTextView = [self makeTextFieldWithText:titleText
145 [signinView addSubview:titleTextView];
147 NSString* signinText = base::SysUTF16ToNSString(delegate_->GetSigninText());
148 rect.origin.y = floor(
149 NSMinY([titleTextView frame]) + [titleFont descender] - kHeadingPadding);
150 NSFont* signinTextFont = baseFontList.GetPrimaryFont().GetNativeFont();
151 NSTextField* signinTextView = [self makeTextFieldWithText:signinText
154 [signinView addSubview:signinTextView];
156 base::scoped_nsobject<BlueLabelButton> button(
157 [[BlueLabelButton alloc] initWithFrame:NSZeroRect]);
158 [button setTitle:base::SysUTF16ToNSString(delegate_->GetSigninButtonText())];
159 [button setKeyEquivalent:@"\r"];
160 [button setTarget:self];
161 [button setAction:@selector(onSigninButtonClick:)];
163 NSPoint buttonOrigin = NSMakePoint(kLeftPadding, floor(
164 NSMinY([signinTextView frame]) - [signinTextFont descender] -
165 NSHeight([button frame]) - kButtonPadding));
166 [button setFrameOrigin:buttonOrigin];
167 [signinView addSubview:button];
169 NSString* settingsLinkText =
170 base::SysUTF16ToNSString(delegate_->GetSettingsLinkText());
171 NSButton* settingsLink =
172 [self makeLinkButtonWithTitle:settingsLinkText
173 origin:NSMakePoint(kLeftPadding, kBottomPadding)
174 action:@selector(onSettingsButtonClick:)];
175 NSPoint origin = NSMakePoint(kLeftPadding,
176 NSMaxY([settingsLink frame]));
177 NSString* learnMoreLinkText =
178 base::SysUTF16ToNSString(delegate_->GetLearnMoreLinkText());
179 NSButton* learnMoreLink =
180 [self makeLinkButtonWithTitle:learnMoreLinkText
182 action:@selector(onLearnMoreButtonClick:)];
183 [signinView addSubview:learnMoreLink];
184 [signinView addSubview:settingsLink];
189 @implementation AppsSigninView
191 @synthesize cornerRadius = cornerRadius_;
193 - (void)drawRect:(NSRect)dirtyRect {
194 [gfx::SkColorToSRGBNSColor(app_list::kContentsBackgroundColor) set];
195 [[NSBezierPath bezierPathWithRoundedRect:[self bounds]
196 xRadius:cornerRadius_
197 yRadius:cornerRadius_] fill];