summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/rect_path_utils.mm
blob: 11503a1cee7f5f05c9c4b634a9d0823e8eb23a10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/ui/cocoa/rect_path_utils.h"

#import "third_party/google_toolbox_for_mac/src/AppKit/GTMNSBezierPath+RoundRect.h"

namespace rect_path_utils {

NSBezierPath* RectPathWithInset(RoundedCornerFlags roundedCornerFlags,
                                const NSRect frame,
                                const CGFloat insetX,
                                const CGFloat insetY,
                                const CGFloat outerRadius) {
  NSRect insetFrame = NSInsetRect(frame, insetX, insetY);

  if (outerRadius > 0.0) {
    CGFloat leftRadius = outerRadius - insetX;
    CGFloat rightRadius =
        (roundedCornerFlags == RoundedCornerLeft) ? 0 : leftRadius;

    return [NSBezierPath gtm_bezierPathWithRoundRect:insetFrame
                                 topLeftCornerRadius:leftRadius
                                topRightCornerRadius:rightRadius
                              bottomLeftCornerRadius:leftRadius
                             bottomRightCornerRadius:rightRadius];
  } else {
    return [NSBezierPath bezierPathWithRect:insetFrame];
  }
}

// Similar to |NSRectFill()|, additionally sets |color| as the fill
// color.  |outerRadius| greater than 0.0 uses rounded corners, with
// inset backed out of the radius.
void FillRectWithInset(RoundedCornerFlags roundedCornerFlags,
                       const NSRect frame,
                       const CGFloat insetX,
                       const CGFloat insetY,
                       const CGFloat outerRadius,
                       NSColor* color) {
  NSBezierPath* path =
      RectPathWithInset(roundedCornerFlags, frame, insetX, insetY, outerRadius);
  [color setFill];
  [path fill];
}

// Similar to |NSFrameRectWithWidth()|, additionally sets |color| as
// the stroke color (as opposed to the fill color).  |outerRadius|
// greater than 0.0 uses rounded corners, with inset backed out of the
// radius.
void FrameRectWithInset(RoundedCornerFlags roundedCornerFlags,
                        const NSRect frame,
                        const CGFloat insetX,
                        const CGFloat insetY,
                        const CGFloat outerRadius,
                        const CGFloat lineWidth,
                        NSColor* color) {
  const CGFloat finalInsetX = insetX + (lineWidth / 2.0);
  const CGFloat finalInsetY = insetY + (lineWidth / 2.0);
  NSBezierPath* path =
      RectPathWithInset(roundedCornerFlags, frame, finalInsetX, finalInsetY,
                        outerRadius);
  [color setStroke];
  [path setLineWidth:lineWidth];
  [path stroke];
}

}  // namespace rect_path_utils