summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/tabpose_window.mm
blob: c69c5c16b91cb7594513afa03cf49713b00bb04a (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
// Copyright (c) 2010 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/cocoa/tabpose_window.h"

#import <QuartzCore/QuartzCore.h>

const int kTopGradientHeight  = 15;

// CAGradientLayer is 10.6-only -- roll our own.
@interface DarkGradientLayer : CALayer
- (void)drawInContext:(CGContextRef)context;
@end

@implementation DarkGradientLayer
- (void)drawInContext:(CGContextRef)context {
  scoped_cftyperef<CGColorSpaceRef> grayColorSpace(
      CGColorSpaceCreateWithName(kCGColorSpaceGenericGray));
  CGFloat grays[] = { 0.277, 1.0, 0.39, 1.0 };
  CGFloat locations[] = { 0, 1 };
  scoped_cftyperef<CGGradientRef> gradient(CGGradientCreateWithColorComponents(
      grayColorSpace.get(), grays, locations, arraysize(locations)));
  CGPoint topLeft = CGPointMake(0.0, kTopGradientHeight);
  CGContextDrawLinearGradient(context, gradient.get(), topLeft, CGPointZero, 0);
}
@end

@interface TabposeWindow (Private)
- (id)initForWindow:(NSWindow*)parent rect:(NSRect)rect slomo:(BOOL)slomo;
- (void)setUpLayers:(NSRect)bgLayerRect;
@end

@implementation TabposeWindow

+ (id)openTabposeFor:(NSWindow*)parent rect:(NSRect)rect slomo:(BOOL)slomo {
  // Releases itself when closed.
  return [[TabposeWindow alloc] initForWindow:parent rect:rect slomo:slomo];
}

- (id)initForWindow:(NSWindow*)parent rect:(NSRect)rect slomo:(BOOL)slomo {
  NSRect frame = [parent frame];
  if ((self = [super initWithContentRect:frame
                               styleMask:NSBorderlessWindowMask
                                 backing:NSBackingStoreBuffered
                                   defer:NO])) {
    [self setReleasedWhenClosed:YES];
    [self setOpaque:NO];
    [self setBackgroundColor:[NSColor clearColor]];
    [self setUpLayers:rect];
    [parent addChildWindow:self ordered:NSWindowAbove];
    [self makeKeyAndOrderFront:self];
  }
  return self;
}

- (void)setUpLayers:(NSRect)bgLayerRect {
  // Root layer -- covers whole window.
  rootLayer_ = [CALayer layer];
  [[self contentView] setLayer:rootLayer_];
  [[self contentView] setWantsLayer:YES];

  // Background layer -- the visible part of the window.
  gray_.reset(CGColorCreateGenericGray(0.39, 1.0));
  bgLayer_ = [CALayer layer];
  bgLayer_.backgroundColor = gray_;
  bgLayer_.frame = NSRectToCGRect(bgLayerRect);
  bgLayer_.masksToBounds = YES;
  [rootLayer_ addSublayer:bgLayer_];

  // Top gradient.
  CALayer* gradientLayer = [DarkGradientLayer layer];
  gradientLayer.frame = CGRectMake(
      0,
      NSHeight(bgLayerRect) - kTopGradientHeight,
      NSWidth(bgLayerRect),
      kTopGradientHeight);
  [gradientLayer setNeedsDisplay];  // Draw once.
  [bgLayer_ addSublayer:gradientLayer];
}

- (BOOL)canBecomeKeyWindow {
 return YES;
}

- (void)keyDown:(NSEvent*)event {
  // Overridden to prevent beeps.
}

- (void)keyUp:(NSEvent*)event {
  NSString* characters = [event characters];
  if ([characters length] < 1)
    return;

  unichar character = [characters characterAtIndex:0];
  switch (character) {
    case NSEnterCharacter:
    case NSNewlineCharacter:
    case NSCarriageReturnCharacter:
    case ' ':
    case '\e':  // Escape
      [self close];
      break;
  }
}

- (void)mouseDown:(NSEvent*)event {
 [self close];
}

- (void)swipeWithEvent:(NSEvent*)event {
  if ([event deltaY] > 0.5)  // Swipe up
    [self close];
}

- (void)close {
  // Prevent parent window from disappearing.
  [[self parentWindow] removeChildWindow:self];
  [super close];
}

- (void)commandDispatch:(id)sender {
  // Without this, -validateUserInterfaceItem: is not called.
}

- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item {
  // Disable all browser-related menu items.
  return NO;
}

@end