summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/tab_contents/overlayable_contents_controller.mm
blob: 89f5b0ddfbc1d90161adabd80e4c1d67807503d4 (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
// Copyright 2011 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/tab_contents/overlayable_contents_controller.h"

#include "chrome/browser/ui/cocoa/tab_contents/instant_overlay_controller_mac.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"

@interface OverlayableContentsController()
- (void)viewDidResize:(NSNotification*)note;
- (void)layoutViews;
- (CGFloat)overlayHeightInPixels;
@end

@implementation OverlayableContentsController

- (id)initWithBrowser:(Browser*)browser
     windowController:(BrowserWindowController*)windowController {
  if ((self = [super init])) {
    base::scoped_nsobject<NSView> view(
        [[NSView alloc] initWithFrame:NSZeroRect]);
    [view setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
    [[NSNotificationCenter defaultCenter]
        addObserver:self
           selector:@selector(viewDidResize:)
               name:NSViewFrameDidChangeNotification
             object:view];
    [self setView:view];

    activeContainer_.reset([[NSView alloc] initWithFrame:NSZeroRect]);
    [activeContainer_ setAutoresizingMask:NSViewHeightSizable |
                                          NSViewWidthSizable];
    [view addSubview:activeContainer_];

    instantOverlayController_.reset(
        new InstantOverlayControllerMac(browser, windowController, self));
  }
  return self;
}

- (void)dealloc {
  [[NSNotificationCenter defaultCenter] removeObserver:self];
  [super dealloc];
}

- (void)setOverlay:(content::WebContents*)overlay
            height:(CGFloat)height
       heightUnits:(InstantSizeUnits)heightUnits
    drawDropShadow:(BOOL)drawDropShadow {
  if (overlayContents_ == overlay &&
      overlayHeight_ == height &&
      overlayHeightUnits_ == heightUnits) {
    return;
  }

  // Remove any old overlay contents before showing the new one.
  if (overlayContents_) {
    if (overlayContents_ != overlay)
      overlayContents_->WasHidden();
    [overlayContents_->GetView()->GetNativeView() removeFromSuperview];
  }

  overlayContents_ = overlay;
  overlayHeight_ = height;
  overlayHeightUnits_ = heightUnits;

  // Add the overlay contents.
  if (overlayContents_) {
    [[[self view] window] disableScreenUpdatesUntilFlush];
    [[self view] addSubview:overlayContents_->GetView()->GetNativeView()];
  }

  [self layoutViews];

  if (overlayContents_)
    overlayContents_->WasShown();
}

- (void)onActivateTabWithContents:(content::WebContents*)contents {
  if (overlayContents_ == contents) {
    if (overlayContents_) {
      [overlayContents_->GetView()->GetNativeView() removeFromSuperview];
      overlayContents_ = NULL;
    }
    [self setOverlay:NULL
              height:0
         heightUnits:INSTANT_SIZE_PIXELS
      drawDropShadow:NO];
  }
}

- (InstantOverlayControllerMac*)instantOverlayController {
  return instantOverlayController_.get();
}

- (BOOL)isShowingOverlay {
  return overlayContents_ != nil;
}

- (NSView*)activeContainer {
  return activeContainer_.get();
}

- (void)viewDidResize:(NSNotification*)note {
  [self layoutViews];
}

- (void)layoutViews {
  if (!overlayContents_)
    return;

  // Layout the overlay.
  NSRect bounds = [[self view] bounds];
  NSRect overlayFrame = bounds;
  overlayFrame.size.height = [self overlayHeightInPixels];
  overlayFrame.origin.y = NSMaxY(bounds) - NSHeight(overlayFrame);
  [overlayContents_->GetView()->GetNativeView() setFrame:overlayFrame];
}

- (CGFloat)overlayHeightInPixels {
  CGFloat height = NSHeight([[self view] bounds]);
  switch (overlayHeightUnits_) {
    case INSTANT_SIZE_PERCENT:
      return std::min(height, (height * overlayHeight_) / 100);
    case INSTANT_SIZE_PIXELS:
      return std::min(height, overlayHeight_);
  }
}

@end