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/download/download_show_all_button.h" | 5 #import "chrome/browser/ui/cocoa/download/download_show_all_button.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #import "chrome/browser/ui/cocoa/download/download_show_all_cell.h" | 8 #import "chrome/browser/ui/cocoa/download/download_show_all_cell.h" |
9 #import "chrome/browser/ui/cocoa/view_id_util.h" | 9 #import "chrome/browser/ui/cocoa/view_id_util.h" |
10 #include "grit/theme_resources.h" | 10 #include "grit/theme_resources.h" |
11 #import "ui/base/cocoa/nsview_additions.h" | 11 #import "ui/base/cocoa/nsview_additions.h" |
12 #include "ui/base/resource/resource_bundle.h" | 12 #include "ui/base/resource/resource_bundle.h" |
13 #include "ui/gfx/image/image.h" | 13 #include "ui/gfx/image/image.h" |
14 | 14 |
15 @implementation DownloadShowAllButton | 15 @implementation DownloadShowAllButton |
16 | 16 |
17 - (void)awakeFromNib { | 17 - (void)awakeFromNib { |
18 DCHECK([[self cell] isKindOfClass:[DownloadShowAllCell class]]); | 18 DCHECK([[self cell] isKindOfClass:[DownloadShowAllCell class]]); |
19 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 19 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
20 NSImage* favicon = rb.GetNativeImageNamed(IDR_DOWNLOADS_FAVICON).ToNSImage(); | 20 NSImage* favicon = rb.GetNativeImageNamed(IDR_DOWNLOADS_FAVICON).ToNSImage(); |
21 [self setImage:favicon]; | 21 [self setImage:favicon]; |
22 } | 22 } |
23 | 23 |
24 - (void)dealloc { | |
25 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
26 | |
27 [super dealloc]; | |
28 } | |
29 | |
24 // GTM's layout tweaker calls sizeToFit to receive the desired width of views. | 30 // GTM's layout tweaker calls sizeToFit to receive the desired width of views. |
25 // By default, buttons will be only 14px high, but the Show All button needs to | 31 // By default, buttons will be only 14px high, but the Show All button needs to |
26 // be higher. | 32 // be higher. |
27 - (void)sizeToFit { | 33 - (void)sizeToFit { |
28 NSRect oldRect = [self frame]; | 34 NSRect oldRect = [self frame]; |
29 [super sizeToFit]; | 35 [super sizeToFit]; |
30 NSRect newRect = [self frame]; | 36 NSRect newRect = [self frame]; |
31 | 37 |
32 // Keep old height. | 38 // Keep old height. |
33 newRect.origin.y = oldRect.origin.y; | 39 newRect.origin.y = oldRect.origin.y; |
34 newRect.size.height = oldRect.size.height; | 40 newRect.size.height = oldRect.size.height; |
35 | 41 |
36 [self setFrame:newRect]; | 42 [self setFrame:newRect]; |
37 } | 43 } |
38 | 44 |
39 - (BOOL)isOpaque { | 45 - (BOOL)isOpaque { |
40 // Make this control opaque so that sub-pixel anti-aliasing works when | 46 // Make this control opaque so that sub-pixel anti-aliasing works when |
41 // CoreAnimation is enabled. | 47 // CoreAnimation is enabled. |
42 return YES; | 48 return YES; |
43 } | 49 } |
44 | 50 |
51 - (void)downloadShelfFrameDidChange:(NSNotification*)aNotification { | |
52 [[NSNotificationCenter defaultCenter] | |
53 removeObserver:self | |
54 name:NSViewFrameDidChangeNotification | |
55 object:[self ancestorWithViewID:VIEW_ID_DOWNLOAD_SHELF]]; | |
56 | |
57 [self setNeedsDisplay:YES]; | |
58 } | |
59 | |
45 - (void)drawRect:(NSRect)rect { | 60 - (void)drawRect:(NSRect)rect { |
46 NSView* downloadShelfView = [self ancestorWithViewID:VIEW_ID_DOWNLOAD_SHELF]; | 61 NSView* downloadShelfView = [self ancestorWithViewID:VIEW_ID_DOWNLOAD_SHELF]; |
47 [self cr_drawUsingAncestor:downloadShelfView inRect:rect]; | 62 if ([downloadShelfView bounds].size.height < [self bounds].size.height) { |
63 // Ordinarily the show all button uses cr_drawUsingAncestor:inRect: to use | |
64 // the download shelf to draw its background. However when the download | |
65 // shelf has zero height, the shelf's drawing methods don't work as expected | |
66 // because they constrain their drawing to the shelf's bounds rect. This | |
67 // situation occurs sometimes when the shelf is about to become visible, | |
68 // and the result is a very dark show all button. | |
69 // | |
70 // To work around this problem, we'll draw a color that roughly matches | |
71 // the shelf's background gradient, and then wait for the shelf to change | |
72 // its frame, at which point we'll schedule a redraw (which will work | |
73 // correctly because the shelf will have a non-zero height). | |
74 [[NSColor colorWithCalibratedWhite:0.8 alpha:1.0] set]; | |
asanka
2015/05/08 17:49:39
Is there a way to determine this at runtime withou
shrike
2015/05/08 18:27:48
Not cleanly. The code that we need lives in [Backg
asanka
2015/05/12 00:20:51
The only thing I'm worried about is whether this w
| |
75 NSRectFill(rect); | |
76 NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter]; | |
77 [defaultCenter addObserver:self | |
78 selector:@selector(downloadShelfFrameDidChange:) | |
79 name:NSViewFrameDidChangeNotification | |
80 object:downloadShelfView]; | |
81 } else { | |
asanka
2015/05/08 17:48:36
Do we need the observer if there was a successful
shrike
2015/05/08 18:27:48
No, we should not. Are you asking because you see
asanka
2015/05/12 00:20:51
Right. Misread.
| |
82 [self cr_drawUsingAncestor:downloadShelfView inRect:rect]; | |
83 } | |
48 [super drawRect:rect]; | 84 [super drawRect:rect]; |
49 } | 85 } |
50 | 86 |
51 // ThemedWindowDrawing implementation. | 87 // ThemedWindowDrawing implementation. |
52 | 88 |
53 - (void)windowDidChangeTheme { | 89 - (void)windowDidChangeTheme { |
54 [self setNeedsDisplay:YES]; | 90 [self setNeedsDisplay:YES]; |
55 } | 91 } |
56 | 92 |
57 - (void)windowDidChangeActive { | 93 - (void)windowDidChangeActive { |
58 [self setNeedsDisplay:YES]; | 94 [self setNeedsDisplay:YES]; |
59 } | 95 } |
60 | 96 |
61 @end | 97 @end |
OLD | NEW |