diff options
author | sail@chromium.org <sail@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-06 18:57:21 +0000 |
---|---|---|
committer | sail@chromium.org <sail@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-06 18:57:21 +0000 |
commit | 8c548de9006110ba1d7e47d767743011363a825a (patch) | |
tree | 8447a6e76a789c6e5d8518479eac38af268c1eb8 /chrome | |
parent | 684edea7fa5cdc5d45cf7f153cf2aeae125e7226 (diff) | |
download | chromium_src-8c548de9006110ba1d7e47d767743011363a825a.zip chromium_src-8c548de9006110ba1d7e47d767743011363a825a.tar.gz chromium_src-8c548de9006110ba1d7e47d767743011363a825a.tar.bz2 |
Alternate NTP: Allow custom preview height
The custom NTP page was not being shown on Mac. The problem was that PreviewableContentsController didn't allow the preview height to be customized.
I've updated the Mac implementation to respect the custom height similar to the View's implementation in contents_container.cc.
As a part of this change I've also removed the preview xib file. The xib just contained two nested NSViews. This was fairly simple to recreate in code.
Screenshot: https://code.google.com/p/chromium/issues/detail?id=162695#c4
BUG=162695
Review URL: https://chromiumcodereview.appspot.com/11450018
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@171547 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
8 files changed, 194 insertions, 210 deletions
diff --git a/chrome/app/nibs/PreviewableContents.xib b/chrome/app/nibs/PreviewableContents.xib deleted file mode 100644 index 17caaec..0000000 --- a/chrome/app/nibs/PreviewableContents.xib +++ /dev/null @@ -1,192 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<archive type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="7.10"> - <data> - <int key="IBDocument.SystemTarget">1070</int> - <string key="IBDocument.SystemVersion">12B19</string> - <string key="IBDocument.InterfaceBuilderVersion">2549</string> - <string key="IBDocument.AppKitVersion">1187</string> - <string key="IBDocument.HIToolboxVersion">624.00</string> - <object class="NSMutableDictionary" key="IBDocument.PluginVersions"> - <string key="NS.key.0">com.apple.InterfaceBuilder.CocoaPlugin</string> - <string key="NS.object.0">2549</string> - </object> - <object class="NSArray" key="IBDocument.IntegratedClassDependencies"> - <bool key="EncodedWithXMLCoder">YES</bool> - <string>NSCustomObject</string> - <string>NSCustomView</string> - </object> - <object class="NSArray" key="IBDocument.PluginDependencies"> - <bool key="EncodedWithXMLCoder">YES</bool> - <string>com.apple.InterfaceBuilder.CocoaPlugin</string> - </object> - <object class="NSMutableDictionary" key="IBDocument.Metadata"> - <string key="NS.key.0">PluginDependencyRecalculationVersion</string> - <integer value="1" key="NS.object.0"/> - </object> - <object class="NSMutableArray" key="IBDocument.RootObjects" id="1000"> - <bool key="EncodedWithXMLCoder">YES</bool> - <object class="NSCustomObject" id="1001"> - <string key="NSClassName">PreviewableContentsController</string> - </object> - <object class="NSCustomObject" id="1003"> - <string key="NSClassName">FirstResponder</string> - </object> - <object class="NSCustomObject" id="1004"> - <string key="NSClassName">NSApplication</string> - </object> - <object class="NSCustomView" id="1005"> - <reference key="NSNextResponder"/> - <int key="NSvFlags">274</int> - <object class="NSMutableArray" key="NSSubviews"> - <bool key="EncodedWithXMLCoder">YES</bool> - <object class="NSCustomView" id="115467025"> - <reference key="NSNextResponder" ref="1005"/> - <int key="NSvFlags">274</int> - <string key="NSFrameSize">{480, 360}</string> - <reference key="NSSuperview" ref="1005"/> - <reference key="NSWindow"/> - <string key="NSClassName">NSView</string> - </object> - </object> - <string key="NSFrameSize">{480, 360}</string> - <reference key="NSSuperview"/> - <reference key="NSWindow"/> - <string key="NSClassName">NSView</string> - </object> - </object> - <object class="IBObjectContainer" key="IBDocument.Objects"> - <object class="NSMutableArray" key="connectionRecords"> - <bool key="EncodedWithXMLCoder">YES</bool> - <object class="IBConnectionRecord"> - <object class="IBOutletConnection" key="connection"> - <string key="label">view</string> - <reference key="source" ref="1001"/> - <reference key="destination" ref="1005"/> - </object> - <int key="connectionID">8</int> - </object> - <object class="IBConnectionRecord"> - <object class="IBOutletConnection" key="connection"> - <string key="label">activeContainer_</string> - <reference key="source" ref="1001"/> - <reference key="destination" ref="115467025"/> - </object> - <int key="connectionID">9</int> - </object> - </object> - <object class="IBMutableOrderedSet" key="objectRecords"> - <object class="NSArray" key="orderedObjects"> - <bool key="EncodedWithXMLCoder">YES</bool> - <object class="IBObjectRecord"> - <int key="objectID">0</int> - <object class="NSArray" key="object" id="0"> - <bool key="EncodedWithXMLCoder">YES</bool> - </object> - <reference key="children" ref="1000"/> - <nil key="parent"/> - </object> - <object class="IBObjectRecord"> - <int key="objectID">-2</int> - <reference key="object" ref="1001"/> - <reference key="parent" ref="0"/> - <string key="objectName">File's Owner</string> - </object> - <object class="IBObjectRecord"> - <int key="objectID">-1</int> - <reference key="object" ref="1003"/> - <reference key="parent" ref="0"/> - <string key="objectName">First Responder</string> - </object> - <object class="IBObjectRecord"> - <int key="objectID">-3</int> - <reference key="object" ref="1004"/> - <reference key="parent" ref="0"/> - <string key="objectName">Application</string> - </object> - <object class="IBObjectRecord"> - <int key="objectID">1</int> - <reference key="object" ref="1005"/> - <object class="NSMutableArray" key="children"> - <bool key="EncodedWithXMLCoder">YES</bool> - <reference ref="115467025"/> - </object> - <reference key="parent" ref="0"/> - </object> - <object class="IBObjectRecord"> - <int key="objectID">2</int> - <reference key="object" ref="115467025"/> - <reference key="parent" ref="1005"/> - </object> - </object> - </object> - <object class="NSMutableDictionary" key="flattenedProperties"> - <bool key="EncodedWithXMLCoder">YES</bool> - <object class="NSArray" key="dict.sortedKeys"> - <bool key="EncodedWithXMLCoder">YES</bool> - <string>-1.IBPluginDependency</string> - <string>-2.IBPluginDependency</string> - <string>-3.IBPluginDependency</string> - <string>1.IBPluginDependency</string> - <string>2.IBPluginDependency</string> - </object> - <object class="NSArray" key="dict.values"> - <bool key="EncodedWithXMLCoder">YES</bool> - <string>com.apple.InterfaceBuilder.CocoaPlugin</string> - <string>com.apple.InterfaceBuilder.CocoaPlugin</string> - <string>com.apple.InterfaceBuilder.CocoaPlugin</string> - <string>com.apple.InterfaceBuilder.CocoaPlugin</string> - <string>com.apple.InterfaceBuilder.CocoaPlugin</string> - </object> - </object> - <object class="NSMutableDictionary" key="unlocalizedProperties"> - <bool key="EncodedWithXMLCoder">YES</bool> - <reference key="dict.sortedKeys" ref="0"/> - <reference key="dict.values" ref="0"/> - </object> - <nil key="activeLocalization"/> - <object class="NSMutableDictionary" key="localizations"> - <bool key="EncodedWithXMLCoder">YES</bool> - <reference key="dict.sortedKeys" ref="0"/> - <reference key="dict.values" ref="0"/> - </object> - <nil key="sourceID"/> - <int key="maxID">10</int> - </object> - <object class="IBClassDescriber" key="IBDocument.Classes"> - <object class="NSMutableArray" key="referencedPartialClassDescriptions"> - <bool key="EncodedWithXMLCoder">YES</bool> - <object class="IBPartialClassDescription"> - <string key="className">PreviewableContentsController</string> - <string key="superclassName">NSViewController</string> - <object class="NSMutableDictionary" key="outlets"> - <string key="NS.key.0">activeContainer_</string> - <string key="NS.object.0">NSView</string> - </object> - <object class="NSMutableDictionary" key="toOneOutletInfosByName"> - <string key="NS.key.0">activeContainer_</string> - <object class="IBToOneOutletInfo" key="NS.object.0"> - <string key="name">activeContainer_</string> - <string key="candidateClassName">NSView</string> - </object> - </object> - <object class="IBClassDescriptionSource" key="sourceIdentifier"> - <string key="majorKey">IBProjectSource</string> - <string key="minorKey">./Classes/PreviewableContentsController.h</string> - </object> - </object> - </object> - </object> - <int key="IBDocument.localizationMode">0</int> - <string key="IBDocument.TargetRuntimeIdentifier">IBCocoaFramework</string> - <object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDependencyDefaults"> - <string key="NS.key.0">com.apple.InterfaceBuilder.CocoaPlugin.macosx</string> - <real value="1070" key="NS.object.0"/> - </object> - <object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDevelopmentDependencies"> - <string key="NS.key.0">com.apple.InterfaceBuilder.CocoaPlugin.InterfaceBuilder3</string> - <integer value="3000" key="NS.object.0"/> - </object> - <bool key="IBDocument.PluginDeclaredDependenciesTrackSystemTargetVersion">YES</bool> - <int key="IBDocument.defaultPropertyAccessControl">3</int> - </data> -</archive> diff --git a/chrome/browser/instant/instant_model.cc b/chrome/browser/instant/instant_model.cc index 2a333bd..5c3c9cc 100644 --- a/chrome/browser/instant/instant_model.cc +++ b/chrome/browser/instant/instant_model.cc @@ -49,7 +49,10 @@ void InstantModel::SetPreviewContents(content::WebContents* preview_contents) { } content::WebContents* InstantModel::GetPreviewContents() const { - return controller_->GetPreviewContents(); + // |controller_| maybe NULL durning tests. + if (controller_) + return controller_->GetPreviewContents(); + return preview_contents_; } void InstantModel::AddObserver(InstantModelObserver* observer) const { diff --git a/chrome/browser/ui/cocoa/tab_contents/instant_preview_controller_mac.mm b/chrome/browser/ui/cocoa/tab_contents/instant_preview_controller_mac.mm index d915d63..fb32888 100644 --- a/chrome/browser/ui/cocoa/tab_contents/instant_preview_controller_mac.mm +++ b/chrome/browser/ui/cocoa/tab_contents/instant_preview_controller_mac.mm @@ -22,9 +22,10 @@ InstantPreviewControllerMac::~InstantPreviewControllerMac() { void InstantPreviewControllerMac::PreviewStateChanged( const InstantModel& model) { - if (model.mode().is_search_suggestions()) { - // TODO(dhollowa): Needs height and units implementation on Mac. - [preview_ showPreview:model.GetPreviewContents()]; + if (model.mode().is_ntp() || model.mode().is_search_suggestions()) { + [preview_ showPreview:model.GetPreviewContents() + height:model.height() + heightUnits:model.height_units()]; } else { [preview_ hidePreview]; } diff --git a/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller.h b/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller.h index 1d0da58..de51c95 100644 --- a/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller.h +++ b/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller.h @@ -7,7 +7,9 @@ #import <Cocoa/Cocoa.h> +#include "base/memory/scoped_nsobject.h" #include "base/memory/scoped_ptr.h" +#include "chrome/common/instant_types.h" class Browser; @class BrowserWindowController; @@ -31,13 +33,17 @@ class WebContents; @interface PreviewableContentsController : NSViewController { @private // Container view for the "active" contents. - IBOutlet NSView* activeContainer_; + scoped_nsobject<NSView> activeContainer_; // The preview WebContents. Will be NULL if no preview is currently showing. content::WebContents* previewContents_; // weak // C++ bridge to the Instant model change interface. scoped_ptr<InstantPreviewControllerMac> instantPreviewController_; + + // The desired height of the preview and units. + CGFloat previewHeight_; + InstantSizeUnits previewHeightUnits_; } @property(readonly, nonatomic) NSView* activeContainer; @@ -48,7 +54,9 @@ class WebContents; // Sets the current preview and installs its WebContentsView into the view // hierarchy. Hides the active view. |preview| must not be NULL. -- (void)showPreview:(content::WebContents*)preview; +- (void)showPreview:(content::WebContents*)preview + height:(CGFloat)height + heightUnits:(InstantSizeUnits)heightUnits; // Closes the current preview and shows the active view. - (void)hidePreview; @@ -60,6 +68,8 @@ class WebContents; // Returns YES if the preview contents is currently showing. - (BOOL)isShowingPreview; +- (InstantPreviewControllerMac*)instantPreviewController; + @end #endif // CHROME_BROWSER_UI_COCOA_TAB_CONTENTS_PREVIEWABLE_CONTENTS_CONTROLLER_H_ diff --git a/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller.mm b/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller.mm index b80d159..d8bf39d 100644 --- a/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller.mm +++ b/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller.mm @@ -8,36 +8,63 @@ #include "chrome/browser/ui/cocoa/tab_contents/instant_preview_controller_mac.h" #include "content/public/browser/web_contents.h" -@implementation PreviewableContentsController +@interface PreviewableContentsController() +- (void)viewDidResize:(NSNotification*)note; +- (void)layoutViews; +- (CGFloat)previewHeightInPixels; +@end -@synthesize activeContainer = activeContainer_; +@implementation PreviewableContentsController - (id)initWithBrowser:(Browser*)browser windowController:(BrowserWindowController*)windowController { - if ((self = [super initWithNibName:@"PreviewableContents" - bundle:base::mac::FrameworkBundle()])) { + if ((self = [super init])) { + 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)showPreview:(content::WebContents*)preview { +- (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; + [super dealloc]; +} + +- (void)showPreview:(content::WebContents*)preview + height:(CGFloat)height + heightUnits:(InstantSizeUnits)heightUnits { DCHECK(preview); + if (previewContents_ == preview && + previewHeight_ == height && + previewHeightUnits_ == heightUnits) { + return; + } // Remove any old preview contents before showing the new one. if (previewContents_) [previewContents_->GetNativeView() removeFromSuperview]; previewContents_ = preview; - NSView* previewView = previewContents_->GetNativeView(); - - // Set the autoresizing of the the preview correctly. - [previewView setAutoresizingMask:NSViewHeightSizable|NSViewWidthSizable]; - [previewView setFrame:[[self view] bounds]]; + previewHeight_ = height; + previewHeightUnits_ = heightUnits; // Add the preview contents. - [[self view] addSubview:previewView]; + [[self view] addSubview:previewContents_->GetNativeView()]; + [self layoutViews]; + previewContents_->WasShown(); } @@ -63,4 +90,39 @@ return previewContents_ != nil; } +- (InstantPreviewControllerMac*)instantPreviewController { + return instantPreviewController_.get(); +} + +- (NSView*)activeContainer { + return activeContainer_.get(); +} + +- (void)viewDidResize:(NSNotification*)note { + [self layoutViews]; +} + +- (void)layoutViews { + NSRect bounds = [[self view] bounds]; + + if (previewContents_) { + NSRect frame = bounds; + frame.size.height = [self previewHeightInPixels]; + frame.origin.y = NSMaxY(bounds) - NSHeight(frame); + [previewContents_->GetNativeView() setFrame:frame]; + } + + [activeContainer_ setFrame:bounds]; +} + +- (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 diff --git a/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller_browsertest.mm b/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller_browsertest.mm new file mode 100644 index 0000000..871194a --- /dev/null +++ b/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller_browsertest.mm @@ -0,0 +1,100 @@ +// Copyright 2012 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 "chrome/browser/instant/instant_model.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/browser_window.h" +#include "chrome/browser/ui/cocoa/browser_window_controller.h" +#include "chrome/browser/ui/cocoa/tab_contents/instant_preview_controller_mac.h" +#include "chrome/test/base/in_process_browser_test.h" +#include "content/public/browser/web_contents.h" +#include "ipc/ipc_message.h" +#import "testing/gtest_mac.h" + +class PreviewableContentsControllerTest : public InProcessBrowserTest { + public: + PreviewableContentsControllerTest() : instant_model_(NULL) { + } + + virtual void SetUpOnMainThread() OVERRIDE { + web_contents_.reset(content::WebContents::Create( + browser()->profile(), NULL, MSG_ROUTING_NONE, NULL)); + instant_model_.SetPreviewContents(web_contents_.get()); + + controller_.reset([[PreviewableContentsController alloc] + initWithBrowser:browser() + windowController:nil]); + [[controller_ view] setFrame:NSMakeRect(0, 0, 100, 200)]; + instant_model_.AddObserver([controller_ instantPreviewController]); + } + + virtual void CleanUpOnMainThread() OVERRIDE { + instant_model_.RemoveObserver([controller_ instantPreviewController]); + instant_model_.SetPreviewContents(NULL); + controller_.reset(); + web_contents_.reset(); + } + + void VerifyPreviewFrame(CGFloat expected_height, + InstantSizeUnits units) { + NSRect container_bounds = [[controller_ view] bounds]; + NSRect preview_frame = + [web_contents_->GetNativeView() frame]; + + EXPECT_EQ(NSMinX(container_bounds), NSMinX(preview_frame)); + EXPECT_EQ(NSWidth(container_bounds), NSWidth(preview_frame)); + switch (units) { + case INSTANT_SIZE_PIXELS: + EXPECT_EQ(expected_height, NSHeight(preview_frame)); + EXPECT_EQ(NSMaxY(container_bounds), NSMaxY(preview_frame)); + break; + case INSTANT_SIZE_PERCENT: + EXPECT_EQ((expected_height * NSHeight(container_bounds)) / 100, + NSHeight(preview_frame)); + EXPECT_EQ(NSMaxY(container_bounds), NSMaxY(preview_frame)); + } + } + + protected: + InstantModel instant_model_; + scoped_ptr<content::WebContents> web_contents_; + scoped_nsobject<PreviewableContentsController> controller_; +}; + +// Verify that the view is correctly laid out when size is specified in percent. +IN_PROC_BROWSER_TEST_F(PreviewableContentsControllerTest, SizePerecent) { + chrome::search::Mode mode; + mode.mode = chrome::search::Mode::MODE_NTP; + CGFloat expected_height = 30; + InstantSizeUnits units = INSTANT_SIZE_PERCENT; + instant_model_.SetPreviewState(mode, expected_height, units); + + EXPECT_NSEQ([web_contents_->GetNativeView() superview], + [controller_ view]); + VerifyPreviewFrame(expected_height, units); + + // Resize the view and verify that the preview is also resized. + [[controller_ view] setFrameSize:NSMakeSize(300, 400)]; + VerifyPreviewFrame(expected_height, units); +} + +// Verify that the view is correctly laid out when size is specified in pixels. +IN_PROC_BROWSER_TEST_F(PreviewableContentsControllerTest, SizePixels) { + chrome::search::Mode mode; + mode.mode = chrome::search::Mode::MODE_NTP; + CGFloat expected_height = 30; + InstantSizeUnits units = INSTANT_SIZE_PIXELS; + instant_model_.SetPreviewState(mode, expected_height, units); + + EXPECT_NSEQ([web_contents_->GetNativeView() superview], + [controller_ view]); + VerifyPreviewFrame(expected_height, units); + + // Resize the view and verify that the preview is also resized. + [[controller_ view] setFrameSize:NSMakeSize(300, 400)]; + VerifyPreviewFrame(expected_height, units); +} diff --git a/chrome/chrome_nibs.gypi b/chrome/chrome_nibs.gypi index 647cf65..ec9e56f 100644 --- a/chrome/chrome_nibs.gypi +++ b/chrome/chrome_nibs.gypi @@ -62,7 +62,6 @@ 'app/nibs/InfoBarContainer.xib', 'app/nibs/Notification.xib', 'app/nibs/Panel.xib', - 'app/nibs/PreviewableContents.xib', 'app/nibs/TabView.xib', ], # mac_untranslated_xibs 'mac_all_xibs': [ diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 785d321..8d3636b 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1155,6 +1155,7 @@ 'browser/ui/cocoa/intents/web_intent_inline_service_view_controller_browsertest.mm', 'browser/ui/cocoa/intents/web_intent_picker_view_controller_browsertest.mm', 'browser/ui/cocoa/ssl_client_certificate_selector_cocoa_browsertest.mm', + 'browser/ui/cocoa/tab_contents/previewable_contents_controller_browsertest.mm', 'browser/ui/cocoa/view_id_util_browsertest.mm', 'browser/ui/find_bar/find_bar_host_browsertest.cc', 'browser/ui/fullscreen/fullscreen_controller_browsertest.cc', |