1// Copyright (c) 2013 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/rect_path_utils.h"
6
7#import "third_party/google_toolbox_for_mac/src/AppKit/GTMNSBezierPath+RoundRect.h"
8
9namespace rect_path_utils {
10
11NSBezierPath* RectPathWithInset(RoundedCornerFlags roundedCornerFlags,
12                                const NSRect frame,
13                                const CGFloat insetX,
14                                const CGFloat insetY,
15                                const CGFloat outerRadius) {
16  NSRect insetFrame = NSInsetRect(frame, insetX, insetY);
17
18  if (outerRadius > 0.0) {
19    CGFloat leftRadius = outerRadius - insetX;
20    CGFloat rightRadius =
21        (roundedCornerFlags == RoundedCornerLeft) ? 0 : leftRadius;
22
23    return [NSBezierPath gtm_bezierPathWithRoundRect:insetFrame
24                                 topLeftCornerRadius:leftRadius
25                                topRightCornerRadius:rightRadius
26                              bottomLeftCornerRadius:leftRadius
27                             bottomRightCornerRadius:rightRadius];
28  } else {
29    return [NSBezierPath bezierPathWithRect:insetFrame];
30  }
31}
32
33// Similar to |NSRectFill()|, additionally sets |color| as the fill
34// color.  |outerRadius| greater than 0.0 uses rounded corners, with
35// inset backed out of the radius.
36void FillRectWithInset(RoundedCornerFlags roundedCornerFlags,
37                       const NSRect frame,
38                       const CGFloat insetX,
39                       const CGFloat insetY,
40                       const CGFloat outerRadius,
41                       NSColor* color) {
42  NSBezierPath* path =
43      RectPathWithInset(roundedCornerFlags, frame, insetX, insetY, outerRadius);
44  [color setFill];
45  [path fill];
46}
47
48// Similar to |NSFrameRectWithWidth()|, additionally sets |color| as
49// the stroke color (as opposed to the fill color).  |outerRadius|
50// greater than 0.0 uses rounded corners, with inset backed out of the
51// radius.
52void FrameRectWithInset(RoundedCornerFlags roundedCornerFlags,
53                        const NSRect frame,
54                        const CGFloat insetX,
55                        const CGFloat insetY,
56                        const CGFloat outerRadius,
57                        const CGFloat lineWidth,
58                        NSColor* color) {
59  const CGFloat finalInsetX = insetX + (lineWidth / 2.0);
60  const CGFloat finalInsetY = insetY + (lineWidth / 2.0);
61  NSBezierPath* path =
62      RectPathWithInset(roundedCornerFlags, frame, finalInsetX, finalInsetY,
63                        outerRadius);
64  [color setStroke];
65  [path setLineWidth:lineWidth];
66  [path stroke];
67}
68
69}  // namespace rect_path_utils
70