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#import "chrome/browser/ui/cocoa/nsview_additions.h"
6
7#include "base/logging.h"
8
9#if !defined(MAC_OS_X_VERSION_10_7) || \
10    MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
11
12@interface NSView (LionAPI)
13- (NSSize)convertSizeFromBacking:(NSSize)size;
14@end
15
16#endif  // 10.7
17
18@implementation NSView (ChromeAdditions)
19
20- (CGFloat)cr_lineWidth {
21  // All shipping retina macs run at least 10.7.
22  if (![self respondsToSelector:@selector(convertSizeFromBacking:)])
23    return 1;
24  return [self convertSizeFromBacking:NSMakeSize(1, 1)].width;
25}
26
27- (BOOL)cr_isMouseInView {
28  NSPoint mouseLoc = [[self window] mouseLocationOutsideOfEventStream];
29  mouseLoc = [[self superview] convertPoint:mouseLoc fromView:nil];
30  return [self hitTest:mouseLoc] == self;
31}
32
33- (BOOL)cr_isBelowView:(NSView*)otherView {
34  NSArray* subviews = [[self superview] subviews];
35
36  NSUInteger selfIndex = [subviews indexOfObject:self];
37  DCHECK_NE(NSNotFound, selfIndex);
38
39  NSUInteger otherIndex = [subviews indexOfObject:otherView];
40  DCHECK_NE(NSNotFound, otherIndex);
41
42  return selfIndex < otherIndex;
43}
44
45- (BOOL)cr_isAboveView:(NSView*)otherView {
46  return ![self cr_isBelowView:otherView];
47}
48
49- (void)cr_ensureSubview:(NSView*)subview
50            isPositioned:(NSWindowOrderingMode)place
51              relativeTo:(NSView *)otherView {
52  DCHECK(place == NSWindowAbove || place == NSWindowBelow);
53  BOOL isAbove = place == NSWindowAbove;
54  if ([[subview superview] isEqual:self] &&
55      [subview cr_isAboveView:otherView] == isAbove) {
56    return;
57  }
58
59  [subview removeFromSuperview];
60  [self addSubview:subview
61        positioned:place
62        relativeTo:otherView];
63}
64
65- (NSColor*)cr_keyboardFocusIndicatorColor {
66  return [[NSColor keyboardFocusIndicatorColor]
67      colorWithAlphaComponent:0.5 / [self cr_lineWidth]];
68}
69
70- (void)cr_recursivelySetNeedsDisplay:(BOOL)flag {
71  [self setNeedsDisplay:YES];
72  for (NSView* child in [self subviews])
73    [child cr_recursivelySetNeedsDisplay:flag];
74}
75
76@end
77