OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/constrained_window/cw_alert.h" | 5 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_alert.h" |
6 | 6 |
7 #import "base/logging.h" | 7 #import "base/logging.h" |
8 #import "chrome/browser/ui/cocoa/constrained_window/cw_button.h" | 8 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_button.h" |
9 #import "chrome/browser/ui/cocoa/constrained_window/cw_window.h" | 9 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_wi
ndow.h" |
10 #import "chrome/browser/ui/cocoa/hover_close_button.h" | 10 #import "chrome/browser/ui/cocoa/hover_close_button.h" |
11 #import "chrome/browser/ui/constrained_window.h" | 11 #import "chrome/browser/ui/constrained_window.h" |
12 #include "skia/ext/skia_utils_mac.h" | 12 #include "skia/ext/skia_utils_mac.h" |
13 #include "ui/base/cocoa/window_size_constants.h" | 13 #include "ui/base/cocoa/window_size_constants.h" |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 const CGFloat kWindowMinWidth = 500; | 17 const CGFloat kWindowMinWidth = 500; |
18 const CGFloat kButtonGap = 6; | 18 const CGFloat kButtonGap = 6; |
19 const CGFloat kButtonMinWidth = 72; | 19 const CGFloat kButtonMinWidth = 72; |
20 const CGFloat kCloseButtonSize = 16; | 20 const CGFloat kCloseButtonSize = 16; |
21 | 21 |
| 22 // Creates a label control. |
22 scoped_nsobject<NSTextField> CreateLabel() { | 23 scoped_nsobject<NSTextField> CreateLabel() { |
23 scoped_nsobject<NSTextField> label( | 24 scoped_nsobject<NSTextField> label( |
24 [[NSTextField alloc] initWithFrame:NSZeroRect]); | 25 [[NSTextField alloc] initWithFrame:NSZeroRect]); |
25 [label setEditable:NO]; | 26 [label setEditable:NO]; |
26 [label setSelectable:NO]; | 27 [label setSelectable:NO]; |
27 [label setBezeled:NO]; | 28 [label setBezeled:NO]; |
28 [label setDrawsBackground:NO]; | 29 [label setDrawsBackground:NO]; |
29 return label; | 30 return label; |
30 } | 31 } |
31 | 32 |
| 33 // Calculates the control size that will fit within the given width. |
32 NSSize CalculateDesiredSizeForWidth(NSTextField* text_field, CGFloat width) { | 34 NSSize CalculateDesiredSizeForWidth(NSTextField* text_field, CGFloat width) { |
33 NSRect rect = NSMakeRect(0, 0, width, 1000); | 35 NSRect rect = NSMakeRect(0, 0, width, 1000); |
34 return [[text_field cell] cellSizeForBounds:rect]; | 36 return [[text_field cell] cellSizeForBounds:rect]; |
35 } | 37 } |
36 | 38 |
| 39 // Helper function to create constrained window label string with the given |
| 40 // font. |
37 NSAttributedString* GetAttributedLabelString( | 41 NSAttributedString* GetAttributedLabelString( |
38 NSString* string, | 42 NSString* string, |
39 ui::ResourceBundle::FontStyle font_style) { | 43 ui::ResourceBundle::FontStyle font_style) { |
40 const gfx::Font& font = | 44 const gfx::Font& font = |
41 ui::ResourceBundle::GetSharedInstance().GetFont(font_style); | 45 ui::ResourceBundle::GetSharedInstance().GetFont(font_style); |
42 NSColor* color = | 46 NSColor* color = |
43 gfx::SkColorToCalibratedNSColor(ConstrainedWindow::kTextColor); | 47 gfx::SkColorToCalibratedNSColor(ConstrainedWindow::kTextColor); |
44 NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys: | 48 NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys: |
45 font.GetNativeFont(), NSFontAttributeName, | 49 font.GetNativeFont(), NSFontAttributeName, |
46 color, NSForegroundColorAttributeName, | 50 color, NSForegroundColorAttributeName, |
47 nil]; | 51 nil]; |
48 return [[[NSAttributedString alloc] initWithString:string | 52 return [[[NSAttributedString alloc] initWithString:string |
49 attributes:attributes] autorelease]; | 53 attributes:attributes] autorelease]; |
50 } | 54 } |
51 | 55 |
52 } // namespace | 56 } // namespace |
53 | 57 |
54 @interface CWAlert() | 58 @interface ConstrainedWindowAlert () |
55 - (void)layoutButtonsWithWindowWidth: (CGFloat)windowWidth; | 59 // Position the alert buttons within the given window width. |
| 60 - (void)layoutButtonsWithWindowWidth:(CGFloat)windowWidth; |
| 61 // Resize the given text field to fit within the window and position it starting |
| 62 // at |yPos|. Returns the new value of yPos. |
56 - (CGFloat)layoutTextField:(NSTextField*)textField | 63 - (CGFloat)layoutTextField:(NSTextField*)textField |
57 yPos:(CGFloat)yPos | 64 yPos:(CGFloat)yPos |
58 windowWidth:(CGFloat)windowWidth; | 65 windowWidth:(CGFloat)windowWidth; |
| 66 // Positions the accessory view starting at yPos. Returns the new value of yPos. |
59 - (CGFloat)layoutAccessoryViewAtYPos:(CGFloat)yPos; | 67 - (CGFloat)layoutAccessoryViewAtYPos:(CGFloat)yPos; |
| 68 // Update the position of the close button. |
60 - (void)layoutCloseButtonWithWindowWidth:(CGFloat)windowWidth; | 69 - (void)layoutCloseButtonWithWindowWidth:(CGFloat)windowWidth; |
61 @end | 70 @end |
62 | 71 |
63 @implementation CWAlert | 72 @implementation ConstrainedWindowAlert |
64 | 73 |
65 - (NSString*)informativeText { | 74 - (NSString*)informativeText { |
66 return [informativeTextField_ stringValue]; | 75 return [informativeTextField_ stringValue]; |
67 } | 76 } |
68 | 77 |
69 - (void)setInformativeText:(NSString*)string { | 78 - (void)setInformativeText:(NSString*)string { |
70 [informativeTextField_ setAttributedStringValue: | 79 [informativeTextField_ setAttributedStringValue: |
71 GetAttributedLabelString(string, ConstrainedWindow::kTextFontStyle)]; | 80 GetAttributedLabelString(string, ConstrainedWindow::kTextFontStyle)]; |
72 } | 81 } |
73 | 82 |
(...skipping 23 matching lines...) Expand all Loading... |
97 - (NSButton*)closeButton { | 106 - (NSButton*)closeButton { |
98 return closeButton_; | 107 return closeButton_; |
99 } | 108 } |
100 | 109 |
101 - (NSWindow*)window { | 110 - (NSWindow*)window { |
102 return window_; | 111 return window_; |
103 } | 112 } |
104 | 113 |
105 - (id)init { | 114 - (id)init { |
106 if ((self = [super init])) { | 115 if ((self = [super init])) { |
107 window_.reset([[CWWindow alloc] | 116 window_.reset([[ConstrainedWindowCustomWindow alloc] |
108 initWithContentRect:ui::kWindowSizeDeterminedLater]); | 117 initWithContentRect:ui::kWindowSizeDeterminedLater]); |
109 [window_ setReleasedWhenClosed:NO]; | 118 [window_ setReleasedWhenClosed:NO]; |
110 NSView* content_view = [window_ contentView]; | 119 NSView* content_view = [window_ contentView]; |
111 | 120 |
112 informativeTextField_ = CreateLabel(); | 121 informativeTextField_ = CreateLabel(); |
113 [content_view addSubview:informativeTextField_]; | 122 [content_view addSubview:informativeTextField_]; |
114 messageTextField_ = CreateLabel(); | 123 messageTextField_ = CreateLabel(); |
115 [content_view addSubview:messageTextField_]; | 124 [content_view addSubview:messageTextField_]; |
116 | 125 |
117 closeButton_.reset([[HoverCloseButton alloc] initWithFrame:NSZeroRect]); | 126 closeButton_.reset([[HoverCloseButton alloc] initWithFrame:NSZeroRect]); |
118 [content_view addSubview:closeButton_]; | 127 [content_view addSubview:closeButton_]; |
119 } | 128 } |
120 return self; | 129 return self; |
121 } | 130 } |
122 | 131 |
123 - (void)addButtonWithTitle:(NSString*)title | 132 - (void)addButtonWithTitle:(NSString*)title |
124 keyEquivalent:(NSString*)keyEquivalent { | 133 keyEquivalent:(NSString*)keyEquivalent |
| 134 target:(id)target |
| 135 action:(SEL)action { |
125 if (!buttons_.get()) | 136 if (!buttons_.get()) |
126 buttons_.reset([[NSMutableArray alloc] init]); | 137 buttons_.reset([[NSMutableArray alloc] init]); |
127 NSButton* button = [[CWButton alloc] initWithFrame:NSZeroRect]; | 138 NSButton* button = [[ConstrainedWindowButton alloc] initWithFrame:NSZeroRect]; |
128 [button setTitle:title]; | 139 [button setTitle:title]; |
129 [button setKeyEquivalent:keyEquivalent]; | 140 [button setKeyEquivalent:keyEquivalent]; |
| 141 [button setTarget:target]; |
| 142 [button setAction:action]; |
130 [buttons_ addObject:button]; | 143 [buttons_ addObject:button]; |
131 [[window_ contentView] addSubview:button]; | 144 [[window_ contentView] addSubview:button]; |
132 } | 145 } |
133 | 146 |
134 - (void)layout { | 147 - (void)layout { |
135 // Button width | 148 // Button width |
136 CGFloat buttonWidth = 0; | 149 CGFloat buttonWidth = 0; |
137 for (NSButton* button in buttons_.get()) { | 150 for (NSButton* button in buttons_.get()) { |
138 [button sizeToFit]; | 151 [button sizeToFit]; |
139 NSSize size = [button frame].size; | 152 NSSize size = [button frame].size; |
(...skipping 23 matching lines...) Expand all Loading... |
163 yPos:curY | 176 yPos:curY |
164 windowWidth:windowWidth - kCloseButtonSize - kButtonGap]; | 177 windowWidth:windowWidth - kCloseButtonSize - kButtonGap]; |
165 [self layoutCloseButtonWithWindowWidth:windowWidth]; | 178 [self layoutCloseButtonWithWindowWidth:windowWidth]; |
166 | 179 |
167 // Update window frame | 180 // Update window frame |
168 curY += ConstrainedWindow::kVerticalPadding; | 181 curY += ConstrainedWindow::kVerticalPadding; |
169 [window_ setFrame:NSMakeRect(0, 0, windowWidth, curY) | 182 [window_ setFrame:NSMakeRect(0, 0, windowWidth, curY) |
170 display:NO]; | 183 display:NO]; |
171 } | 184 } |
172 | 185 |
173 - (void)layoutButtonsWithWindowWidth: (CGFloat)windowWidth { | 186 - (void)layoutButtonsWithWindowWidth:(CGFloat)windowWidth { |
174 // Layout first 2 button right to left. | 187 // Layout first 2 button right to left. |
175 CGFloat curX = windowWidth - ConstrainedWindow::kHorizontalPadding; | 188 CGFloat curX = windowWidth - ConstrainedWindow::kHorizontalPadding; |
176 const int buttonCount = [buttons_ count]; | 189 const int buttonCount = [buttons_ count]; |
177 DCHECK_GE(buttonCount, 2); | 190 DCHECK_GE(buttonCount, 2); |
178 for (int i = 0; i < 2; ++i) { | 191 for (int i = 0; i < 2; ++i) { |
179 NSButton* button = [buttons_ objectAtIndex:i]; | 192 NSButton* button = [buttons_ objectAtIndex:i]; |
180 NSRect rect = [button frame]; | 193 NSRect rect = [button frame]; |
181 rect.origin.x = curX - NSWidth(rect); | 194 rect.origin.x = curX - NSWidth(rect); |
182 rect.origin.y = ConstrainedWindow::kVerticalPadding; | 195 rect.origin.y = ConstrainedWindow::kVerticalPadding; |
183 [button setFrameOrigin:rect.origin]; | 196 [button setFrameOrigin:rect.origin]; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 NSRect frame; | 239 NSRect frame; |
227 frame.size.width = kCloseButtonSize; | 240 frame.size.width = kCloseButtonSize; |
228 frame.size.height = kCloseButtonSize; | 241 frame.size.height = kCloseButtonSize; |
229 frame.origin.x = | 242 frame.origin.x = |
230 windowWidth - ConstrainedWindow::kHorizontalPadding - NSWidth(frame); | 243 windowWidth - ConstrainedWindow::kHorizontalPadding - NSWidth(frame); |
231 frame.origin.y = NSMaxY([messageTextField_ frame]) - NSHeight(frame); | 244 frame.origin.y = NSMaxY([messageTextField_ frame]) - NSHeight(frame); |
232 [closeButton_ setFrame:frame]; | 245 [closeButton_ setFrame:frame]; |
233 } | 246 } |
234 | 247 |
235 @end | 248 @end |
OLD | NEW |