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 #import "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h"
12 #include "third_party/skia/include/core/SkColor.h"
13 #include "ui/app_list/app_list_constants.h"
14 #import "ui/app_list/cocoa/blue_label_button.h"
15 #include "ui/app_list/signin_delegate.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 gfx::Font baseFont = rb.GetFont(ui::ResourceBundle::BaseFont);
136 NSString* titleText = base::SysUTF16ToNSString(delegate_->GetSigninHeading());
137 NSFont* titleFont = baseFont.DeriveFont(kTitleFontSizeDelta).GetNativeFont();
138 NSRect rect = NSMakeRect(kLeftPadding, NSHeight(frame) - kTopPadding,
139 NSWidth(frame) - kLeftPadding - kRightPadding, 0);
140 NSTextField* titleTextView = [self makeTextFieldWithText:titleText
143 [signinView addSubview:titleTextView];
145 NSString* signinText = base::SysUTF16ToNSString(delegate_->GetSigninText());
146 rect.origin.y = floor(
147 NSMinY([titleTextView frame]) + [titleFont descender] - kHeadingPadding);
148 NSFont* signinTextFont = baseFont.GetNativeFont();
149 NSTextField* signinTextView = [self makeTextFieldWithText:signinText
152 [signinView addSubview:signinTextView];
154 base::scoped_nsobject<BlueLabelButton> button(
155 [[BlueLabelButton alloc] initWithFrame:NSZeroRect]);
156 [button setTitle:base::SysUTF16ToNSString(delegate_->GetSigninButtonText())];
157 [button setKeyEquivalent:@"\r"];
158 [button setTarget:self];
159 [button setAction:@selector(onSigninButtonClick:)];
161 NSPoint buttonOrigin = NSMakePoint(kLeftPadding, floor(
162 NSMinY([signinTextView frame]) - [signinTextFont descender] -
163 NSHeight([button frame]) - kButtonPadding));
164 [button setFrameOrigin:buttonOrigin];
165 [signinView addSubview:button];
167 NSString* settingsLinkText =
168 base::SysUTF16ToNSString(delegate_->GetSettingsLinkText());
169 NSButton* settingsLink =
170 [self makeLinkButtonWithTitle:settingsLinkText
171 origin:NSMakePoint(kLeftPadding, kBottomPadding)
172 action:@selector(onSettingsButtonClick:)];
173 NSPoint origin = NSMakePoint(kLeftPadding,
174 NSMaxY([settingsLink frame]));
175 NSString* learnMoreLinkText =
176 base::SysUTF16ToNSString(delegate_->GetLearnMoreLinkText());
177 NSButton* learnMoreLink =
178 [self makeLinkButtonWithTitle:learnMoreLinkText
180 action:@selector(onLearnMoreButtonClick:)];
181 [signinView addSubview:learnMoreLink];
182 [signinView addSubview:settingsLink];
187 @implementation AppsSigninView
189 @synthesize cornerRadius = cornerRadius_;
191 - (void)drawRect:(NSRect)dirtyRect {
192 [gfx::SkColorToSRGBNSColor(app_list::kContentsBackgroundColor) set];
193 [[NSBezierPath bezierPathWithRoundedRect:[self bounds]
194 xRadius:cornerRadius_
195 yRadius:cornerRadius_] fill];