OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "chrome/browser/ui/cocoa/autofill/autofill_notification_controller.h" | 5 #import "chrome/browser/ui/cocoa/autofill/autofill_notification_controller.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/mac/foundation_util.h" | 10 #include "base/mac/foundation_util.h" |
11 #include "base/mac/scoped_nsobject.h" | 11 #include "base/mac/scoped_nsobject.h" |
12 #include "base/strings/sys_string_conversions.h" | |
13 #include "chrome/browser/ui/autofill/autofill_dialog_types.h" | |
12 #include "chrome/browser/ui/chrome_style.h" | 14 #include "chrome/browser/ui/chrome_style.h" |
13 #include "chrome/browser/ui/cocoa/autofill/autofill_dialog_constants.h" | 15 #include "chrome/browser/ui/cocoa/autofill/autofill_dialog_constants.h" |
16 #include "grit/theme_resources.h" | |
17 #include "skia/ext/skia_utils_mac.h" | |
14 | 18 |
15 @interface AutofillNotificationView : NSView { | 19 @interface AutofillNotificationView : NSView { |
16 @private | 20 @private |
17 // Weak, determines anchor point for arrow. | 21 // Weak, determines anchor point for arrow. |
18 NSView* arrowAnchorView_; | 22 NSView* arrowAnchorView_; |
19 BOOL hasArrow_; | 23 BOOL hasArrow_; |
20 base::scoped_nsobject<NSColor> backgroundColor_; | 24 base::scoped_nsobject<NSColor> backgroundColor_; |
21 } | 25 } |
22 | 26 |
23 @property (nonatomic, assign) NSView* anchorView; | 27 @property (nonatomic, assign) NSView* anchorView; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
61 } | 65 } |
62 | 66 |
63 - (void)setBackgroundColor:(NSColor*)backgroundColor { | 67 - (void)setBackgroundColor:(NSColor*)backgroundColor { |
64 backgroundColor_.reset([backgroundColor retain]); | 68 backgroundColor_.reset([backgroundColor retain]); |
65 } | 69 } |
66 | 70 |
67 @end | 71 @end |
68 | 72 |
69 @implementation AutofillNotificationController | 73 @implementation AutofillNotificationController |
70 | 74 |
71 - (id)init { | 75 - (id)initWithNotification:(const autofill::DialogNotification*)notification { |
72 if (self = [super init]) { | 76 if (self = [super init]) { |
73 base::scoped_nsobject<AutofillNotificationView> view( | 77 base::scoped_nsobject<AutofillNotificationView> view( |
74 [[AutofillNotificationView alloc] initWithFrame:NSZeroRect]); | 78 [[AutofillNotificationView alloc] initWithFrame:NSZeroRect]); |
79 [view setBackgroundColor: | |
80 gfx::SkColorToCalibratedNSColor(notification->GetBackgroundColor())]; | |
75 [self setView:view]; | 81 [self setView:view]; |
76 | 82 |
77 textfield_.reset([[NSTextField alloc] initWithFrame:NSZeroRect]); | 83 textfield_.reset([[NSTextField alloc] initWithFrame:NSZeroRect]); |
78 [textfield_ setEditable:NO]; | 84 [textfield_ setEditable:NO]; |
79 [textfield_ setBordered:NO]; | 85 [textfield_ setBordered:NO]; |
80 [textfield_ setDrawsBackground:NO]; | 86 [textfield_ setDrawsBackground:NO]; |
87 [textfield_ setTextColor: | |
88 gfx::SkColorToCalibratedNSColor(notification->GetTextColor())]; | |
89 [textfield_ setStringValue: | |
90 base::SysUTF16ToNSString(notification->display_text())]; | |
91 [textfield_ setHidden:notification->HasCheckbox()]; | |
81 | 92 |
82 checkbox_.reset([[NSButton alloc] initWithFrame:NSZeroRect]); | 93 checkbox_.reset([[NSButton alloc] initWithFrame:NSZeroRect]); |
83 [checkbox_ setButtonType:NSSwitchButton]; | 94 [checkbox_ setButtonType:NSSwitchButton]; |
84 [checkbox_ setHidden:YES]; | 95 [checkbox_ setHidden:!notification->HasCheckbox()]; |
85 [view setSubviews:@[textfield_, checkbox_]]; | 96 [checkbox_ setState:(notification->checked() ? NSOnState : NSOffState)]; |
97 [checkbox_ setAttributedTitle:[textfield_ attributedStringValue]]; | |
98 | |
99 tooltipIcon_.reset([[NSImageView alloc] initWithFrame:NSZeroRect]); | |
100 [tooltipIcon_ setImage: | |
101 ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed( | |
102 IDR_AUTOFILL_TOOLTIP_ICON).ToNSImage()]; | |
103 [tooltipIcon_ setFrameSize:[[tooltipIcon_ image] size]]; | |
104 [tooltipIcon_ setToolTip: | |
105 base::SysUTF16ToNSString(notification->tooltip_text())]; | |
106 [tooltipIcon_ setHidden:[[tooltipIcon_ toolTip] length] == 0]; | |
107 | |
108 [view setSubviews:@[textfield_, checkbox_, tooltipIcon_]]; | |
86 } | 109 } |
87 return self; | 110 return self; |
88 } | 111 } |
89 | 112 |
90 - (AutofillNotificationView*)notificationView { | 113 - (AutofillNotificationView*)notificationView { |
91 return base::mac::ObjCCastStrict<AutofillNotificationView>([self view]); | 114 return base::mac::ObjCCastStrict<AutofillNotificationView>([self view]); |
92 } | 115 } |
93 | 116 |
94 - (void)setHasArrow:(BOOL)hasArrow withAnchorView:(NSView*)anchorView { | 117 - (void)setHasArrow:(BOOL)hasArrow withAnchorView:(NSView*)anchorView { |
95 [[self notificationView] setAnchorView:anchorView]; | 118 [[self notificationView] setAnchorView:anchorView]; |
96 [[self notificationView] setHasArrow:hasArrow]; | 119 [[self notificationView] setHasArrow:hasArrow]; |
97 } | 120 } |
98 | 121 |
99 - (BOOL)hasArrow { | 122 - (BOOL)hasArrow { |
100 return [[self notificationView] hasArrow]; | 123 return [[self notificationView] hasArrow]; |
101 } | 124 } |
102 | 125 |
103 - (void)setHasCheckbox:(BOOL)hasCheckbox { | |
104 [checkbox_ setHidden:!hasCheckbox]; | |
105 [textfield_ setHidden:hasCheckbox]; | |
106 } | |
107 | |
108 - (NSString*)text { | |
109 return [textfield_ stringValue]; | |
110 } | |
111 | |
112 - (void)setText:(NSString*)string { | |
113 [textfield_ setStringValue:string]; | |
114 [checkbox_ setAttributedTitle:[textfield_ attributedStringValue]]; | |
115 } | |
116 | |
117 - (NSTextField*)textfield { | 126 - (NSTextField*)textfield { |
118 return textfield_; | 127 return textfield_; |
119 } | 128 } |
120 | 129 |
121 - (NSButton*)checkbox { | 130 - (NSButton*)checkbox { |
122 return checkbox_; | 131 return checkbox_; |
123 } | 132 } |
124 | 133 |
125 - (NSColor*)backgroundColor { | 134 - (NSImageView*)tooltipIcon { |
126 return [[self notificationView] backgroundColor]; | 135 return tooltipIcon_; |
127 } | |
128 | |
129 - (void)setBackgroundColor:(NSColor*)backgroundColor { | |
130 [[self notificationView] setBackgroundColor:backgroundColor]; | |
131 } | |
132 | |
133 - (NSColor*)textColor { | |
134 return [textfield_ textColor]; | |
135 } | |
136 | |
137 - (void)setTextColor:(NSColor*)textColor { | |
138 [textfield_ setTextColor:textColor]; | |
139 } | 136 } |
140 | 137 |
141 - (NSSize)preferredSizeForWidth:(CGFloat)width { | 138 - (NSSize)preferredSizeForWidth:(CGFloat)width { |
139 width -= 2 * chrome_style::kHorizontalPadding; | |
140 if (![tooltipIcon_ isHidden]) | |
141 width -= [tooltipIcon_ frame].size.width + chrome_style::kHorizontalPadding; | |
142 DCHECK_GT(width, 0); | |
143 | |
142 NSCell* cell = [checkbox_ isHidden] ? [textfield_ cell] : [checkbox_ cell]; | 144 NSCell* cell = [checkbox_ isHidden] ? [textfield_ cell] : [checkbox_ cell]; |
143 NSSize preferredSize = | 145 NSSize preferredSize = |
144 [cell cellSizeForBounds:NSMakeRect(0, 0, width, CGFLOAT_MAX)]; | 146 [cell cellSizeForBounds:NSMakeRect(0, 0, width, CGFLOAT_MAX)]; |
145 | 147 |
146 if ([[self notificationView] hasArrow]) | 148 if ([[self notificationView] hasArrow]) |
147 preferredSize.height += autofill::kArrowHeight; | 149 preferredSize.height += autofill::kArrowHeight; |
148 | 150 |
149 preferredSize.height += 2 * autofill::kNotificationPadding; | 151 preferredSize.height += 2 * autofill::kNotificationPadding; |
150 return preferredSize; | 152 return preferredSize; |
151 } | 153 } |
152 | 154 |
153 - (NSSize)preferredSize { | 155 - (NSSize)preferredSize { |
154 NOTREACHED(); | 156 NOTREACHED(); |
155 return NSZeroSize; | 157 return NSZeroSize; |
156 } | 158 } |
157 | 159 |
158 - (void)performLayout { | 160 - (void)performLayout { |
159 NSRect bounds = [[self view] bounds]; | 161 NSRect bounds = [[self view] bounds]; |
160 if ([[self notificationView] hasArrow]) | 162 if ([[self notificationView] hasArrow]) |
161 bounds.size.height -= autofill::kArrowHeight; | 163 bounds.size.height -= autofill::kArrowHeight; |
162 | 164 |
163 NSRect textFrame = NSInsetRect(bounds, | 165 // Calculate the frame size, leaving room for padding around the notification, |
166 // as well as for the tooltip if it is visible. | |
167 NSRect labelFrame = NSInsetRect(bounds, | |
164 chrome_style::kHorizontalPadding, | 168 chrome_style::kHorizontalPadding, |
165 autofill::kNotificationPadding); | 169 autofill::kNotificationPadding); |
166 NSControl* control = | 170 if (![tooltipIcon_ isHidden]) { |
167 [checkbox_ isHidden] ? textfield_.get() : checkbox_.get(); | 171 labelFrame.size.width -= |
168 [control setFrame:textFrame]; | 172 [tooltipIcon_ frame].size.width + chrome_style::kHorizontalPadding; |
173 } | |
174 | |
175 NSView* label = [checkbox_ isHidden] ? textfield_.get() : checkbox_.get(); | |
176 [label setFrame:labelFrame]; | |
177 | |
178 NSPoint tooltipOrigin = | |
groby-ooo-7-16
2013/11/02 01:38:50
Move into ![tooltipIcon isHidden] ?
Ilya Sherman
2013/11/02 01:42:27
Good call. Done.
| |
179 NSMakePoint(NSMaxX(labelFrame) + chrome_style::kHorizontalPadding, | |
180 NSMidY(labelFrame) - (NSHeight([tooltipIcon_ frame]) / 2.0)); | |
181 [tooltipIcon_ setFrameOrigin:tooltipOrigin]; | |
169 } | 182 } |
170 | 183 |
171 @end | 184 @end |
OLD | NEW |