summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller.mm
blob: 7cae3295c62e8e7379be2f2a625256bd1223dc46 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// 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/previewable_contents_controller.h"

#include "base/mac/bundle_locations.h"
#include "chrome/browser/ui/cocoa/browser_window_controller.h"
#include "chrome/browser/ui/cocoa/tab_contents/instant_preview_controller_mac.h"
#include "chrome/browser/ui/cocoa/tab_contents/preview_drop_shadow_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"

@interface PreviewableContentsController()
- (void)viewDidResize:(NSNotification*)note;
- (void)layoutViews;
- (CGFloat)previewHeightInPixels;
@end

@implementation PreviewableContentsController

@synthesize drawDropShadow = drawDropShadow_;
@synthesize activeContainerOffset = activeContainerOffset_;

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

    activeContainer_.reset([[NSView alloc] initWithFrame:NSZeroRect]);
    [view addSubview:activeContainer_];

    instantPreviewController_.reset(
        new InstantPreviewControllerMac(browser, windowController, self));
  }
  return self;
}

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

- (void)setPreview:(content::WebContents*)preview
            height:(CGFloat)height
       heightUnits:(InstantSizeUnits)heightUnits
    drawDropShadow:(BOOL)drawDropShadow {
  // If drawing drop shadow, clip the bottom 1-px-thick separator out of
  // preview.
  // TODO(sail): remove this when GWS gives chrome the height without the
  // separator.
  if (drawDropShadow && heightUnits != INSTANT_SIZE_PERCENT)
    --height;

  if (previewContents_ == preview &&
      previewHeight_ == height &&
      previewHeightUnits_ == heightUnits &&
      drawDropShadow_ == drawDropShadow) {
    return;
  }

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

  previewContents_ = preview;
  previewHeight_ = height;
  previewHeightUnits_ = heightUnits;
  drawDropShadow_ = drawDropShadow;

  // Add the preview contents.
  if (previewContents_) {
    [[[self view] window] disableScreenUpdatesUntilFlush];
    previewContents_->GetView()->SetAllowOverlappingViews(true);
    [[self view] addSubview:previewContents_->GetView()->GetNativeView()];
  }

  if (drawDropShadow_) {
    if (!dropShadowView_) {
      dropShadowView_.reset(
          [[PreviewDropShadowView alloc] initWithFrame:NSZeroRect]);
      [[self view] addSubview:dropShadowView_];
    }
  } else {
    [dropShadowView_ removeFromSuperview];
    dropShadowView_.reset();
  }

  [self layoutViews];

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

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

- (BOOL)isShowingPreview {
  return previewContents_ != nil;
}

- (InstantPreviewControllerMac*)instantPreviewController {
  return instantPreviewController_.get();
}

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

- (NSView*)dropShadowView {
  return dropShadowView_.get();
}

- (void)setActiveContainerOffset:(CGFloat)activeContainerOffset {
  if (activeContainerOffset_ == activeContainerOffset)
    return;

  activeContainerOffset_ = activeContainerOffset;
  [self layoutViews];
}

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

- (void)layoutViews {
  NSRect bounds = [[self view] bounds];

  if (previewContents_) {
    NSRect previewFrame = bounds;
    previewFrame.size.height = [self previewHeightInPixels];
    previewFrame.origin.y = NSMaxY(bounds) - NSHeight(previewFrame);
    [previewContents_->GetView()->GetNativeView() setFrame:previewFrame];

    if (dropShadowView_) {
      NSRect dropShadowFrame = bounds;
      dropShadowFrame.size.height = [PreviewDropShadowView preferredHeight];
      dropShadowFrame.origin.y =
          NSMinY(previewFrame) - NSHeight(dropShadowFrame);
      [dropShadowView_ setFrame:dropShadowFrame];
    }
  }

  NSRect activeFrame = bounds;
  activeFrame.size.height -= activeContainerOffset_;
  [activeContainer_ setFrame:activeFrame];
}

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

@end