OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/mac/mac_util.h" | |
6 #include "base/mac/sdk_forward_declarations.h" | |
7 #import "chrome/browser/ui/cocoa/nsview_additions.h" | |
8 #include "chrome/common/chrome_switches.h" | |
9 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | |
10 | |
11 #include "base/logging.h" | |
12 | |
13 @implementation NSView (ChromeAdditions) | |
14 | |
15 - (CGFloat)cr_lineWidth { | |
16 // All shipping retina macs run at least 10.7. | |
17 if (![self respondsToSelector:@selector(convertSizeFromBacking:)]) | |
18 return 1; | |
19 return [self convertSizeFromBacking:NSMakeSize(1, 1)].width; | |
20 } | |
21 | |
22 - (BOOL)cr_isMouseInView { | |
23 NSPoint mouseLoc = [[self window] mouseLocationOutsideOfEventStream]; | |
24 mouseLoc = [[self superview] convertPoint:mouseLoc fromView:nil]; | |
25 return [self hitTest:mouseLoc] == self; | |
26 } | |
27 | |
28 - (BOOL)cr_isBelowView:(NSView*)otherView { | |
29 NSArray* subviews = [[self superview] subviews]; | |
30 | |
31 NSUInteger selfIndex = [subviews indexOfObject:self]; | |
32 DCHECK_NE(NSNotFound, selfIndex); | |
33 | |
34 NSUInteger otherIndex = [subviews indexOfObject:otherView]; | |
35 DCHECK_NE(NSNotFound, otherIndex); | |
36 | |
37 return selfIndex < otherIndex; | |
38 } | |
39 | |
40 - (BOOL)cr_isAboveView:(NSView*)otherView { | |
41 return ![self cr_isBelowView:otherView]; | |
42 } | |
43 | |
44 - (void)cr_ensureSubview:(NSView*)subview | |
45 isPositioned:(NSWindowOrderingMode)place | |
46 relativeTo:(NSView *)otherView { | |
47 DCHECK(place == NSWindowAbove || place == NSWindowBelow); | |
48 BOOL isAbove = place == NSWindowAbove; | |
49 if ([[subview superview] isEqual:self] && | |
50 [subview cr_isAboveView:otherView] == isAbove) { | |
51 return; | |
52 } | |
53 | |
54 [subview removeFromSuperview]; | |
55 [self addSubview:subview | |
56 positioned:place | |
57 relativeTo:otherView]; | |
58 } | |
59 | |
60 - (NSColor*)cr_keyboardFocusIndicatorColor { | |
61 return [[NSColor keyboardFocusIndicatorColor] | |
62 colorWithAlphaComponent:0.5 / [self cr_lineWidth]]; | |
63 } | |
64 | |
65 - (void)cr_recursivelySetNeedsDisplay:(BOOL)flag { | |
66 [self setNeedsDisplay:YES]; | |
67 for (NSView* child in [self subviews]) | |
68 [child cr_recursivelySetNeedsDisplay:flag]; | |
69 } | |
70 | |
71 static NSView* g_ancestorBeingDrawnFrom = nil; | |
72 static NSView* g_childBeingDrawnTo = nil; | |
73 | |
74 - (void)cr_drawUsingAncestor:(NSView*)ancestorView inRect:(NSRect)rect { | |
75 gfx::ScopedNSGraphicsContextSaveGState scopedGSState; | |
76 NSRect frame = [self convertRect:[self bounds] toView:ancestorView]; | |
77 NSAffineTransform* transform = [NSAffineTransform transform]; | |
78 if ([self isFlipped] == [ancestorView isFlipped]) { | |
79 [transform translateXBy:-NSMinX(frame) yBy:-NSMinY(frame)]; | |
80 } else { | |
81 [transform translateXBy:-NSMinX(frame) yBy:NSMaxY(frame)]; | |
82 [transform scaleXBy:1.0 yBy:-1.0]; | |
83 } | |
84 [transform concat]; | |
85 | |
86 // This can be made robust to recursive calls, but is as of yet unneeded. | |
87 DCHECK(!g_ancestorBeingDrawnFrom && !g_childBeingDrawnTo); | |
88 g_ancestorBeingDrawnFrom = ancestorView; | |
89 g_childBeingDrawnTo = self; | |
90 [ancestorView drawRect:[ancestorView bounds]]; | |
91 g_childBeingDrawnTo = nil; | |
92 g_ancestorBeingDrawnFrom = nil; | |
93 } | |
94 | |
95 - (NSView*)cr_viewBeingDrawnTo { | |
96 if (!g_ancestorBeingDrawnFrom) | |
97 return self; | |
98 DCHECK(g_ancestorBeingDrawnFrom == self); | |
99 return g_childBeingDrawnTo; | |
100 } | |
101 | |
102 @end | |
OLD | NEW |