OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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. | |
4 | |
5 #import <Cocoa/Cocoa.h> | |
6 | |
7 #import "chrome/browser/ui/cocoa/theme_install_bubble_view.h" | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/memory/scoped_nsobject.h" | |
11 #include "chrome/common/chrome_notification_types.h" | |
12 #include "content/public/browser/notification_service.h" | |
13 #include "grit/generated_resources.h" | |
14 #include "ui/base/l10n/l10n_util_mac.h" | |
15 | |
16 namespace { | |
17 | |
18 // The alpha of the bubble. | |
19 static const float kBubbleAlpha = 0.75; | |
20 | |
21 // The roundedness of the edges of our bubble. | |
22 static const int kBubbleCornerRadius = 4; | |
23 | |
24 // Padding around text in popup box. | |
25 static const int kTextHorizPadding = 90; | |
26 static const int kTextVertPadding = 45; | |
27 | |
28 // Point size of the text in the box. | |
29 static const int kLoadingTextSize = 24; | |
30 | |
31 } | |
32 | |
33 // static | |
34 ThemeInstallBubbleView* ThemeInstallBubbleView::view_ = NULL; | |
35 | |
36 // The Cocoa view to draw a gray rounded rect with "Loading..." in it. | |
37 @interface ThemeInstallBubbleViewCocoa : NSView { | |
38 @private | |
39 scoped_nsobject<NSAttributedString> message_; | |
40 | |
41 NSRect grayRect_; | |
42 NSRect textRect_; | |
43 } | |
44 | |
45 - (id)init; | |
46 | |
47 // The size of the gray rect that will be drawn. | |
48 - (NSSize)preferredSize; | |
49 // Forces size calculations of where everything will be drawn. | |
50 - (void)layout; | |
51 | |
52 @end | |
53 | |
54 ThemeInstallBubbleView::ThemeInstallBubbleView(NSWindow* window) | |
55 : cocoa_view_([[ThemeInstallBubbleViewCocoa alloc] init]), | |
56 num_loads_extant_(1) { | |
57 DCHECK(window); | |
58 | |
59 NSView* parent_view = [window contentView]; | |
60 NSRect parent_bounds = [parent_view bounds]; | |
61 if (parent_bounds.size.height < [cocoa_view_ preferredSize].height) | |
62 Close(); | |
63 | |
64 // Close when theme has been installed. | |
65 registrar_.Add( | |
66 this, | |
67 chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | |
68 content::NotificationService::AllBrowserContextsAndSources()); | |
69 | |
70 // Close when we are installing an extension, not a theme. | |
71 registrar_.Add( | |
72 this, | |
73 chrome::NOTIFICATION_NO_THEME_DETECTED, | |
74 content::NotificationService::AllSources()); | |
75 registrar_.Add( | |
76 this, | |
77 chrome::NOTIFICATION_EXTENSION_INSTALLED, | |
78 content::NotificationService::AllSources()); | |
79 registrar_.Add( | |
80 this, | |
81 chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, | |
82 content::NotificationService::AllSources()); | |
83 | |
84 // Don't let the bubble overlap the confirm dialog. | |
85 registrar_.Add( | |
86 this, | |
87 chrome::NOTIFICATION_EXTENSION_WILL_SHOW_CONFIRM_DIALOG, | |
88 content::NotificationService::AllSources()); | |
89 | |
90 // Add the view. | |
91 [cocoa_view_ setFrame:parent_bounds]; | |
92 [cocoa_view_ setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; | |
93 [parent_view addSubview:cocoa_view_ | |
94 positioned:NSWindowAbove | |
95 relativeTo:nil]; | |
96 [cocoa_view_ layout]; | |
97 } | |
98 | |
99 ThemeInstallBubbleView::~ThemeInstallBubbleView() { | |
100 // Need to delete self; the real work happens in Close(). | |
101 } | |
102 | |
103 void ThemeInstallBubbleView::Close() { | |
104 --num_loads_extant_; | |
105 if (num_loads_extant_ < 1) { | |
106 registrar_.RemoveAll(); | |
107 if (cocoa_view_ && [cocoa_view_ superview]) { | |
108 [cocoa_view_ removeFromSuperview]; | |
109 [cocoa_view_ release]; | |
110 } | |
111 view_ = NULL; | |
112 delete this; | |
113 // this is deleted; nothing more! | |
114 } | |
115 } | |
116 | |
117 void ThemeInstallBubbleView::Observe( | |
118 int type, | |
119 const content::NotificationSource& source, | |
120 const content::NotificationDetails& details) { | |
121 Close(); | |
122 } | |
123 | |
124 // static | |
125 void ThemeInstallBubbleView::Show(NSWindow* window) { | |
126 if (view_) | |
127 ++view_->num_loads_extant_; | |
128 else | |
129 view_ = new ThemeInstallBubbleView(window); | |
130 } | |
131 | |
132 @implementation ThemeInstallBubbleViewCocoa | |
133 | |
134 - (id)init { | |
135 self = [super initWithFrame:NSZeroRect]; | |
136 if (self) { | |
137 NSString* loadingString = | |
138 l10n_util::GetNSStringWithFixup(IDS_THEME_LOADING_TITLE); | |
139 NSFont* loadingFont = [NSFont systemFontOfSize:kLoadingTextSize]; | |
140 NSColor* textColor = [NSColor whiteColor]; | |
141 NSDictionary* loadingAttrs = [NSDictionary dictionaryWithObjectsAndKeys: | |
142 loadingFont, NSFontAttributeName, | |
143 textColor, NSForegroundColorAttributeName, | |
144 nil]; | |
145 message_.reset([[NSAttributedString alloc] initWithString:loadingString | |
146 attributes:loadingAttrs]); | |
147 | |
148 // TODO(avi): find a white-on-black spinner | |
149 } | |
150 return self; | |
151 } | |
152 | |
153 - (NSSize)preferredSize { | |
154 NSSize size = [message_.get() size]; | |
155 size.width += kTextHorizPadding; | |
156 size.height += kTextVertPadding; | |
157 return size; | |
158 } | |
159 | |
160 // Update the layout to keep the view centered when the window is resized. | |
161 - (void)resizeWithOldSuperviewSize:(NSSize)oldBoundsSize { | |
162 [super resizeWithOldSuperviewSize:oldBoundsSize]; | |
163 [self layout]; | |
164 } | |
165 | |
166 - (void)layout { | |
167 NSRect bounds = [self bounds]; | |
168 | |
169 grayRect_.size = [self preferredSize]; | |
170 grayRect_.origin.x = (bounds.size.width - grayRect_.size.width) / 2; | |
171 grayRect_.origin.y = bounds.size.height / 2; | |
172 | |
173 textRect_.size = [message_.get() size]; | |
174 textRect_.origin.x = (bounds.size.width - [message_.get() size].width) / 2; | |
175 textRect_.origin.y = (bounds.size.height + kTextVertPadding) / 2; | |
176 } | |
177 | |
178 - (void)drawRect:(NSRect)dirtyRect { | |
179 [[NSColor clearColor] set]; | |
180 NSRectFillUsingOperation([self bounds], NSCompositeSourceOver); | |
181 | |
182 [[[NSColor blackColor] colorWithAlphaComponent:kBubbleAlpha] set]; | |
183 [[NSBezierPath bezierPathWithRoundedRect:grayRect_ | |
184 xRadius:kBubbleCornerRadius | |
185 yRadius:kBubbleCornerRadius] fill]; | |
186 | |
187 [message_.get() drawInRect:textRect_]; | |
188 } | |
189 | |
190 @end | |
OLD | NEW |