summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/tab_cell.mm
blob: a8e927686c1ba6bfe2f43e9d893b0103dc3bdee5 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) 2009 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.

#include "chrome/browser/cocoa/tab_cell.h"

#define INSET_MULTIPLIER 2.0/3.0
#define CP1_MULTIPLIER 1.0/3.0
#define CP2_MULTIPLIER 3.0/8.0

@implementation TabCell

// Override drawing the button so that it looks like a Chromium tab instead
// of just a normal MacOS button.
// TODO(pinkerton/alcor): Clearly this is a work in progress. Comments need
// to be added to the flow once we get a better idea of exactly what we
// want and the kinks worked out of the visual appearance/tracking.
// TODO(pinkerton/alcor): Document what most of these constants are.
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
  [[NSGraphicsContext currentContext] saveGraphicsState];

  // create a rounded rect path with which we'll stroke the outine of the
  // tab.
  NSRect rect = cellFrame;
  //  rect.origin.y += 20;
  NSBezierPath *path =
      [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(rect, 0.5, 0.5)
                                      xRadius:10.0
                                      yRadius:10.0];

  [[NSColor lightGrayColor] setStroke];
  BOOL isKey = [[controlView window] isKeyWindow];
  if (1) { //[self state] || [self isHighlighted]) {
    [[NSColor colorWithCalibratedHue:218.0 / 360.0
                          saturation:0.05
                          brightness:1.0
                               alpha:1.0] setFill];
    // [[NSColor colorWithCalibratedHue:210.0/360.0 saturation:0.36
    //     brightness:0.73 alpha:1.0] setStroke];
    path = [NSBezierPath bezierPath];
    rect = NSInsetRect(rect, 0.5, 0.5);
    float radius = 4.0;
    if (radius > 0.0) {
      // Clamp radius to be no larger than half the rect's width or height.
      radius = MIN(radius, 0.5 * MIN(rect.size.width, rect.size.height));

      NSPoint bottomLeft = NSMakePoint(NSMinX(rect), NSMaxY(rect));
      NSPoint bottomRight = NSMakePoint(NSMaxX(rect), NSMaxY(rect));
      NSPoint topRight =
          NSMakePoint(NSMaxX(rect) - INSET_MULTIPLIER * NSHeight(rect),
                      NSMinY(rect));
      NSPoint topLeft =
          NSMakePoint(NSMinX(rect)  + INSET_MULTIPLIER * NSHeight(rect),
                      NSMinY(rect));

      float baseControlPointOutset = NSHeight(rect) * CP1_MULTIPLIER;
      float bottomControlPointInset = NSHeight(rect) * CP2_MULTIPLIER;

      [path moveToPoint:NSMakePoint(bottomLeft.x - 2, bottomLeft.y + 2)];
      [path lineToPoint:NSMakePoint(bottomLeft.x - 2, bottomLeft.y)];
      [path lineToPoint:bottomLeft];

      [path curveToPoint:topLeft
           controlPoint1:NSMakePoint(bottomLeft.x + baseControlPointOutset,
                                     bottomLeft.y)
           controlPoint2:NSMakePoint(topLeft.x - bottomControlPointInset,
                                     topLeft.y)];

      [path lineToPoint:topRight];

      [path curveToPoint:bottomRight
           controlPoint1:NSMakePoint(topRight.x + bottomControlPointInset,
                                     topRight.y)
           controlPoint2:NSMakePoint(bottomRight.x - baseControlPointOutset,
                                     bottomRight.y)];

      [path lineToPoint:NSMakePoint(bottomRight.x + 1, bottomRight.y)];
      [path lineToPoint:NSMakePoint(bottomRight.x + 1, bottomRight.y + 1)];
    }
    BOOL selected = [(NSButton *)controlView state];

    NSGradient *gradient = nil;

    if (!selected)
      [[NSBezierPath bezierPathWithRect:NSOffsetRect(cellFrame, 0, -1)]
          addClip];
    if (selected) {
      NSColor* startingColor = [NSColor colorWithCalibratedWhite:1.0 alpha:1.0];
      NSColor* endingColor =
          [NSColor colorWithCalibratedWhite:isKey ? 0.95 : 0.98 alpha:1.0];
      gradient =
          [[[NSGradient alloc]
              initWithStartingColor:startingColor endingColor:endingColor]
                  autorelease];
      [[NSColor colorWithCalibratedWhite:0.0 alpha:isKey ? 0.5 : 0.3] set];
    } else {
      NSColor* startingColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.5];
      NSColor* endingColor = [NSColor colorWithCalibratedWhite:0.95 alpha:0.5];
      gradient =
          [[[NSGradient alloc]
              initWithStartingColor:startingColor endingColor:endingColor]
                  autorelease];
      [[NSColor colorWithCalibratedWhite:0.0 alpha:isKey ? 0.3 : 0.1] set];
    }

    if (isKey || selected)
      [gradient drawInBezierPath:path angle:90.0];

    [path stroke];
  }

  [[NSGraphicsContext currentContext] restoreGraphicsState];
  [self drawInteriorWithFrame:NSInsetRect(cellFrame, 12.0, 0)
                       inView:controlView];
}

- (void)drawImage:(NSImage*)image withFrame:(NSRect)frame
    inView:(NSView*)controlView {
  NSSize size = [image size];
  [image setFlipped: YES];

  float opacity = [self isEnabled] ? 1.0 : 0.25;

  [image drawInRect:NSMakeRect(frame.origin.x, frame.origin.y, 16.0, 16.0)
           fromRect:NSMakeRect(0, 0, size.width, size.height)
          operation:NSCompositeSourceOver fraction:opacity];
}

- (void)highlight:(BOOL)flag withFrame:(NSRect)cellFrame
    inView:(NSView *)controlView {
  // Don't do normal highlighting
}

@end