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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
// 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/autofill/autofill_notification_controller.h"
#include <algorithm>
#include "base/logging.h"
#include "base/mac/foundation_util.h"
#include "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#include "chrome/browser/ui/autofill/autofill_dialog_types.h"
#include "chrome/browser/ui/autofill/autofill_dialog_view_delegate.h"
#include "chrome/browser/ui/chrome_style.h"
#include "chrome/browser/ui/cocoa/autofill/autofill_dialog_constants.h"
#import "chrome/browser/ui/cocoa/autofill/autofill_tooltip_controller.h"
#include "grit/theme_resources.h"
#include "skia/ext/skia_utils_mac.h"
#import "ui/base/cocoa/controls/hyperlink_text_view.h"
@interface AutofillNotificationView : NSView {
@private
// Weak, determines anchor point for arrow.
NSView* arrowAnchorView_;
BOOL hasArrow_;
base::scoped_nsobject<NSColor> backgroundColor_;
base::scoped_nsobject<NSColor> borderColor_;
}
@property (nonatomic, assign) NSView* anchorView;
@property (nonatomic, assign) BOOL hasArrow;
@property (nonatomic, retain) NSColor* backgroundColor;
@property (nonatomic, retain) NSColor* borderColor;
@end
@implementation AutofillNotificationView
@synthesize hasArrow = hasArrow_;
@synthesize anchorView = arrowAnchorView_;
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
NSBezierPath* path;
NSRect bounds = [self bounds];
if (!hasArrow_) {
path = [NSBezierPath bezierPathWithRect:bounds];
} else {
// The upper tip of the arrow.
NSPoint anchorPoint = NSMakePoint(NSMidX([arrowAnchorView_ bounds]), 0);
anchorPoint = [self convertPoint:anchorPoint fromView:arrowAnchorView_];
anchorPoint.y = NSMaxY(bounds);
// The minimal rectangle that encloses the arrow.
NSRect arrowRect = NSMakeRect(anchorPoint.x - autofill::kArrowWidth / 2.0,
anchorPoint.y - autofill::kArrowHeight,
autofill::kArrowWidth,
autofill::kArrowHeight);
// Include the arrow and the rectangular non-arrow region in the same path,
// so that the stroke is easier to draw. Start at the upper-left of the
// rectangular region, and proceed clockwise.
path = [NSBezierPath bezierPath];
[path moveToPoint:NSMakePoint(NSMinX(bounds), NSMinY(arrowRect))];
[path lineToPoint:arrowRect.origin];
[path lineToPoint:NSMakePoint(NSMidX(arrowRect), NSMaxY(arrowRect))];
[path lineToPoint:NSMakePoint(NSMaxX(arrowRect), NSMinY(arrowRect))];
[path lineToPoint:NSMakePoint(NSMaxX(bounds), NSMinY(arrowRect))];
[path lineToPoint:NSMakePoint(NSMaxX(bounds), NSMinY(bounds))];
[path lineToPoint:NSMakePoint(NSMinX(bounds), NSMinY(bounds))];
[path closePath];
}
[backgroundColor_ setFill];
[path fill];
[borderColor_ setStroke];
[path stroke];
}
- (NSColor*)backgroundColor {
return backgroundColor_;
}
- (void)setBackgroundColor:(NSColor*)backgroundColor {
backgroundColor_.reset([backgroundColor retain]);
}
- (NSColor*)borderColor {
return borderColor_;
}
- (void)setBorderColor:(NSColor*)borderColor {
borderColor_.reset([borderColor retain]);
}
@end
@implementation AutofillNotificationController
- (id)initWithNotification:(const autofill::DialogNotification*)notification
delegate:(autofill::AutofillDialogViewDelegate*)delegate {
if (self = [super init]) {
delegate_ = delegate;
notificationType_ = notification->type();
base::scoped_nsobject<AutofillNotificationView> view(
[[AutofillNotificationView alloc] initWithFrame:NSZeroRect]);
[view setBackgroundColor:
gfx::SkColorToCalibratedNSColor(notification->GetBackgroundColor())];
[view setBorderColor:
gfx::SkColorToCalibratedNSColor(notification->GetBorderColor())];
[self setView:view];
textview_.reset([[HyperlinkTextView alloc] initWithFrame:NSZeroRect]);
NSColor* textColor =
gfx::SkColorToCalibratedNSColor(notification->GetTextColor());
[textview_ setMessage:base::SysUTF16ToNSString(notification->display_text())
withFont:[NSFont labelFontOfSize:[[textview_ font] pointSize]]
messageColor:textColor];
if (!notification->link_range().is_empty()) {
// This class is not currently able to render links as checkbox labels.
DCHECK(!notification->HasCheckbox());
[textview_ setDelegate:self];
[textview_ addLinkRange:notification->link_range().ToNSRange()
withName:self
linkColor:[NSColor blueColor]];
linkURL_ = notification->link_url();
}
[textview_ setHidden:notification->HasCheckbox()];
checkbox_.reset([[NSButton alloc] initWithFrame:NSZeroRect]);
[checkbox_ setButtonType:NSSwitchButton];
[checkbox_ setHidden:!notification->HasCheckbox()];
[checkbox_ setState:(notification->checked() ? NSOnState : NSOffState)];
[checkbox_ setAttributedTitle:[textview_ textStorage]];
[checkbox_ setTarget:self];
[checkbox_ setAction:@selector(checkboxClicked:)];
// Set the size that preferredSizeForWidth will use. Do this here because
// (1) preferredSizeForWidth is logically const, and so shouldn't have a
// side-effect of updating the checkbox's frame, and
// (2) this way, the sizing computation can be cached.
[checkbox_ sizeToFit];
tooltipController_.reset([[AutofillTooltipController alloc]
initWithArrowLocation:info_bubble::kTopRight]);
[tooltipController_ setImage:
ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(
IDR_AUTOFILL_TOOLTIP_ICON).ToNSImage()];
[tooltipController_ setMessage:
base::SysUTF16ToNSString(notification->tooltip_text())];
[[tooltipController_ view] setHidden:
[[tooltipController_ message] length] == 0];
[view setSubviews:@[ textview_, checkbox_, [tooltipController_ view] ]];
}
return self;
}
- (AutofillNotificationView*)notificationView {
return base::mac::ObjCCastStrict<AutofillNotificationView>([self view]);
}
- (void)setHasArrow:(BOOL)hasArrow withAnchorView:(NSView*)anchorView {
[[self notificationView] setAnchorView:anchorView];
[[self notificationView] setHasArrow:hasArrow];
}
- (BOOL)hasArrow {
return [[self notificationView] hasArrow];
}
- (NSTextView*)textview {
return textview_;
}
- (NSButton*)checkbox {
return checkbox_;
}
- (NSView*)tooltipView {
return [tooltipController_ view];
}
- (NSSize)preferredSizeForWidth:(CGFloat)width {
width -= 2 * chrome_style::kHorizontalPadding;
if (![[tooltipController_ view] isHidden]) {
width -= NSWidth([[tooltipController_ view] frame]) +
chrome_style::kHorizontalPadding;
}
// TODO(isherman): Restore the DCHECK below once I figure out why it causes
// unit tests to fail.
//DCHECK_GT(width, 0);
NSSize preferredSize;
if (![textview_ isHidden]) {
// This method is logically const. Hence, cache the original frame so that
// it can be restored once the preferred size has been computed.
NSRect frame = [textview_ frame];
// Compute preferred size.
[textview_ setFrameSize:NSMakeSize(width, frame.size.height)];
[textview_ setVerticallyResizable:YES];
[textview_ sizeToFit];
preferredSize = [textview_ frame].size;
// Restore original properties, since this method is logically const.
[textview_ setFrame:frame];
[textview_ setVerticallyResizable:NO];
} else {
// Unlike textfields, checkboxes (NSButtons, really) are not designed to
// support multi-line labels. Hence, ignore the |width| and simply use the
// size that fits fit the checkbox's contents.
// NOTE: This logic will need to be updated if there is ever a need to
// support checkboxes with multi-line labels.
DCHECK(![checkbox_ isHidden]);
preferredSize = [checkbox_ frame].size;
}
if ([[self notificationView] hasArrow])
preferredSize.height += autofill::kArrowHeight;
preferredSize.height += 2 * autofill::kNotificationPadding;
return preferredSize;
}
- (NSSize)preferredSize {
NOTREACHED();
return NSZeroSize;
}
- (void)performLayout {
NSRect bounds = [[self view] bounds];
if ([[self notificationView] hasArrow])
bounds.size.height -= autofill::kArrowHeight;
// Calculate the frame size, leaving room for padding around the notification,
// as well as for the tooltip if it is visible.
NSRect labelFrame = NSInsetRect(bounds,
chrome_style::kHorizontalPadding,
autofill::kNotificationPadding);
NSView* tooltipView = [tooltipController_ view];
if (![tooltipView isHidden]) {
labelFrame.size.width -=
NSWidth([tooltipView frame]) + chrome_style::kHorizontalPadding;
}
NSView* label = [checkbox_ isHidden] ? textview_.get() : checkbox_.get();
[label setFrame:labelFrame];
if (![tooltipView isHidden]) {
NSPoint tooltipOrigin =
NSMakePoint(
NSMaxX(labelFrame) + chrome_style::kHorizontalPadding,
NSMidY(labelFrame) - (NSHeight([tooltipView frame]) / 2.0));
[tooltipView setFrameOrigin:tooltipOrigin];
}
}
- (IBAction)checkboxClicked:(id)sender {
DCHECK(sender == checkbox_.get());
BOOL isChecked = ([checkbox_ state] == NSOnState);
delegate_->NotificationCheckboxStateChanged(notificationType_, isChecked);
}
- (BOOL)textView:(NSTextView *)textView
clickedOnLink:(id)link
atIndex:(NSUInteger)charIndex {
delegate_->LinkClicked(linkURL_);
return YES;
}
@end
|