summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autocomplete
diff options
context:
space:
mode:
authorrohitrao@chromium.org <rohitrao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-22 17:05:17 +0000
committerrohitrao@chromium.org <rohitrao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-22 17:05:17 +0000
commit868d3022ab0c7ee4abd2387e34fffb0136ca2f49 (patch)
treee2621f02f291ee9ad2303f9080e94fe7efe9ba5b /chrome/browser/autocomplete
parent51587f3778268cd0d04ddbcd75a3631cac4c2037 (diff)
downloadchromium_src-868d3022ab0c7ee4abd2387e34fffb0136ca2f49.zip
chromium_src-868d3022ab0c7ee4abd2387e34fffb0136ca2f49.tar.gz
chromium_src-868d3022ab0c7ee4abd2387e34fffb0136ca2f49.tar.bz2
[Mac] Add the instant opt-in UI to the omnibox dropdown.
BUG=58917, 56385 TEST=Once Instant opt-in is enabled, it should appear in omnibox, not look ugly. Review URL: http://codereview.chromium.org/5161003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@66960 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/autocomplete')
-rw-r--r--chrome/browser/autocomplete/autocomplete_popup_view_mac.h19
-rw-r--r--chrome/browser/autocomplete/autocomplete_popup_view_mac.mm94
2 files changed, 99 insertions, 14 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_popup_view_mac.h b/chrome/browser/autocomplete/autocomplete_popup_view_mac.h
index 20af4f0..32e080c 100644
--- a/chrome/browser/autocomplete/autocomplete_popup_view_mac.h
+++ b/chrome/browser/autocomplete/autocomplete_popup_view_mac.h
@@ -16,12 +16,16 @@
#include "chrome/browser/autocomplete/autocomplete.h"
#include "chrome/browser/autocomplete/autocomplete_match.h"
#include "chrome/browser/autocomplete/autocomplete_popup_view.h"
+#import "chrome/browser/cocoa/location_bar/instant_opt_in_controller.h"
#include "gfx/font.h"
#include "webkit/glue/window_open_disposition.h"
-class AutocompletePopupModel;
+
class AutocompleteEditModel;
class AutocompleteEditViewMac;
+@class AutocompleteMatrix;
+class AutocompletePopupModel;
+@class InstantOptInController;
@class NSImage;
class Profile;
@@ -31,7 +35,8 @@ class Profile;
// TODO(rohitrao): This class is set up in a way that makes testing hard.
// Refactor and write unittests. http://crbug.com/9977
-class AutocompletePopupViewMac : public AutocompletePopupView {
+class AutocompletePopupViewMac : public AutocompletePopupView,
+ public InstantOptInControllerDelegate {
public:
AutocompletePopupViewMac(AutocompleteEditViewMac* edit_view,
AutocompleteEditModel* edit_model,
@@ -39,6 +44,9 @@ class AutocompletePopupViewMac : public AutocompletePopupView {
NSTextField* field);
virtual ~AutocompletePopupViewMac();
+ // Implement the InstantOptInControllerDelegate interface.
+ virtual void UserPressedOptIn(bool opt_in);
+
// Implement the AutocompletePopupView interface.
virtual bool IsOpen() const;
virtual void InvalidateLine(size_t line) {
@@ -111,6 +119,9 @@ class AutocompletePopupViewMac : public AutocompletePopupView {
const float cellWidth);
private:
+ // Returns the AutocompleteMatrix for this popup view.
+ AutocompleteMatrix* GetAutocompleteMatrix();
+
// Create the popup_ instance if needed.
void CreatePopupIfNeeded();
@@ -125,12 +136,16 @@ class AutocompletePopupViewMac : public AutocompletePopupView {
// Returns the NSImage that should be used as an icon for the given match.
NSImage* ImageForMatch(const AutocompleteMatch& match);
+ // Returns whether or not to show the instant opt-in prompt.
+ bool ShouldShowInstantOptIn();
+
scoped_ptr<AutocompletePopupModel> model_;
AutocompleteEditViewMac* edit_view_;
NSTextField* field_; // owned by tab controller
// Child window containing a matrix which implements the popup.
scoped_nsobject<NSWindow> popup_;
+ scoped_nsobject<InstantOptInController> opt_in_controller_;
NSRect targetPopupFrame_;
DISALLOW_COPY_AND_ASSIGN(AutocompletePopupViewMac);
diff --git a/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm b/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm
index 6f26248..0a84fb2 100644
--- a/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm
+++ b/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm
@@ -8,6 +8,7 @@
#include "app/resource_bundle.h"
#include "app/text_elider.h"
+#include "base/mac_util.h"
#include "base/stl_util-inl.h"
#include "base/sys_string_conversions.h"
#include "base/utf_string_conversions.h"
@@ -17,10 +18,17 @@
#include "chrome/browser/autocomplete/autocomplete_popup_model.h"
#include "chrome/browser/cocoa/event_utils.h"
#include "chrome/browser/cocoa/image_utils.h"
+#import "chrome/browser/cocoa/location_bar/instant_opt_in_controller.h"
+#import "chrome/browser/cocoa/location_bar/instant_opt_in_view.h"
+#import "chrome/browser/cocoa/location_bar/omnibox_popup_view.h"
+#include "chrome/browser/instant/instant_confirm_dialog.h"
+#include "chrome/browser/instant/promo_counter.h"
+#include "chrome/browser/profile.h"
#include "gfx/rect.h"
#include "grit/theme_resources.h"
#include "skia/ext/skia_utils_mac.h"
#import "third_party/GTM/AppKit/GTMNSAnimation+Duration.h"
+#import "third_party/GTM/AppKit/GTMNSBezierPath+RoundRect.h"
namespace {
@@ -251,10 +259,16 @@ NSAttributedString* AutocompletePopupViewMac::MatchText(
@interface AutocompleteMatrix : NSMatrix {
@private
+ // If YES, the matrix draws itself with rounded corners at the bottom.
+ // Otherwise, the bottom corners will be square.
+ BOOL bottomCornersRounded_;
+
// Target for click and middle-click.
AutocompletePopupViewMac* popupView_; // weak, owns us.
}
+@property(assign, nonatomic) BOOL bottomCornersRounded;
+
// Create a zero-size matrix initializing |popupView_|.
- initWithPopupView:(AutocompletePopupViewMac*)popupView;
@@ -276,6 +290,7 @@ AutocompletePopupViewMac::AutocompletePopupViewMac(
edit_view_(edit_view),
field_(field),
popup_(nil),
+ opt_in_controller_(nil),
targetPopupFrame_(NSZeroRect) {
DCHECK(edit_view);
DCHECK(edit_model);
@@ -290,11 +305,22 @@ AutocompletePopupViewMac::~AutocompletePopupViewMac() {
// Break references to |this| because the popup may not be
// deallocated immediately.
- AutocompleteMatrix* matrix = [popup_ contentView];
+ AutocompleteMatrix* matrix = GetAutocompleteMatrix();
DCHECK(matrix == nil || [matrix isKindOfClass:[AutocompleteMatrix class]]);
[matrix setPopupView:NULL];
}
+AutocompleteMatrix* AutocompletePopupViewMac::GetAutocompleteMatrix() {
+ // The AutocompleteMatrix will always be the first subview of the popup's
+ // content view.
+ if (popup_ && [[[popup_ contentView] subviews] count]) {
+ NSArray* subviews = [[popup_ contentView] subviews];
+ DCHECK_GE([subviews count], 0U);
+ return (AutocompleteMatrix*)[subviews objectAtIndex:0];
+ }
+ return nil;
+}
+
bool AutocompletePopupViewMac::IsOpen() const {
return [popup_ isVisible] ? true : false;
}
@@ -306,8 +332,7 @@ void AutocompletePopupViewMac::CreatePopupIfNeeded() {
backing:NSBackingStoreBuffered
defer:YES]);
[popup_ setMovableByWindowBackground:NO];
- // The window shape is determined by the content view
- // (AutocompleteMatrix).
+ // The window shape is determined by the content view (OmniboxPopupView).
[popup_ setAlphaValue:1.0];
[popup_ setOpaque:NO];
[popup_ setBackgroundColor:[NSColor clearColor]];
@@ -316,7 +341,11 @@ void AutocompletePopupViewMac::CreatePopupIfNeeded() {
scoped_nsobject<AutocompleteMatrix> matrix(
[[AutocompleteMatrix alloc] initWithPopupView:this]);
- [popup_ setContentView:matrix];
+ scoped_nsobject<OmniboxPopupView> contentView(
+ [[OmniboxPopupView alloc] initWithFrame:NSZeroRect]);
+
+ [contentView addSubview:matrix];
+ [popup_ setContentView:contentView];
}
}
@@ -407,7 +436,7 @@ void AutocompletePopupViewMac::UpdatePopupAppearance() {
// Break references to |this| because the popup may not be
// deallocated immediately.
- AutocompleteMatrix* matrix = [popup_ contentView];
+ AutocompleteMatrix* matrix = GetAutocompleteMatrix();
DCHECK(matrix == nil || [matrix isKindOfClass:[AutocompleteMatrix class]]);
[matrix setPopupView:NULL];
@@ -424,7 +453,7 @@ void AutocompletePopupViewMac::UpdatePopupAppearance() {
gfx::Font resultFont(base::SysNSStringToWide([fieldFont fontName]),
static_cast<int>(resultFontSize));
- AutocompleteMatrix* matrix = [popup_ contentView];
+ AutocompleteMatrix* matrix = GetAutocompleteMatrix();
// Calculate the width of the matrix based on backing out the
// popup's border from the width of the field. Would prefer to use
@@ -454,6 +483,22 @@ void AutocompletePopupViewMac::UpdatePopupAppearance() {
const CGFloat cellHeight = cellSize.height + kCellHeightAdjust;
[matrix setCellSize:NSMakeSize(matrixWidth, cellHeight)];
+ // Add in the instant view if needed and not already present.
+ CGFloat instantHeight = 0;
+ if (ShouldShowInstantOptIn()) {
+ if (!opt_in_controller_.get()) {
+ opt_in_controller_.reset(
+ [[InstantOptInController alloc] initWithDelegate:this]);
+ }
+ [[popup_ contentView] addSubview:[opt_in_controller_ view]];
+ [GetAutocompleteMatrix() setBottomCornersRounded:NO];
+ instantHeight = NSHeight([[opt_in_controller_ view] frame]);
+ } else {
+ [[opt_in_controller_ view] removeFromSuperview];
+ opt_in_controller_.reset(nil);
+ [GetAutocompleteMatrix() setBottomCornersRounded:YES];
+ }
+
// Update the selection before placing (and displaying) the window.
PaintUpdatesNow();
@@ -461,7 +506,8 @@ void AutocompletePopupViewMac::UpdatePopupAppearance() {
// because actually resizing the matrix messed up the popup size
// animation.
DCHECK_EQ([matrix intercellSpacing].height, 0.0);
- PositionPopup(rows * cellHeight);
+ CGFloat matrixHeight = rows * cellHeight;
+ PositionPopup(matrixHeight + instantHeight);
}
gfx::Rect AutocompletePopupViewMac::GetTargetBounds() {
@@ -480,7 +526,7 @@ void AutocompletePopupViewMac::SetSelectedLine(size_t line) {
// This is only called by model in SetSelectedLine() after updating
// everything. Popup should already be visible.
void AutocompletePopupViewMac::PaintUpdatesNow() {
- AutocompleteMatrix* matrix = [popup_ contentView];
+ AutocompleteMatrix* matrix = GetAutocompleteMatrix();
[matrix selectCellAtRow:model_->selected_line() column:0];
}
@@ -509,6 +555,24 @@ void AutocompletePopupViewMac::OpenURLForRow(int row, bool force_background) {
is_keyword_hint ? std::wstring() : keyword);
}
+void AutocompletePopupViewMac::UserPressedOptIn(bool opt_in) {
+ PromoCounter* counter = model_->profile()->GetInstantPromoCounter();
+ DCHECK(counter);
+ counter->Hide();
+ if (opt_in) {
+ browser::ShowInstantConfirmDialogIfNecessary([field_ window],
+ model_->profile());
+ }
+
+ // This call will remove and delete |opt_in_controller_|.
+ UpdatePopupAppearance();
+}
+
+bool AutocompletePopupViewMac::ShouldShowInstantOptIn() {
+ PromoCounter* counter = model_->profile()->GetInstantPromoCounter();
+ return (counter && counter->ShouldShow(base::Time::Now()));
+}
+
@implementation AutocompleteButtonCell
- init {
@@ -569,7 +633,7 @@ void AutocompletePopupViewMac::OpenURLForRow(int row, bool force_background) {
@implementation AutocompleteMatrix
-
+@synthesize bottomCornersRounded = bottomCornersRounded_;
// Remove all tracking areas and initialize the one we want. Removing
// all might be overkill, but it's unclear why there would be others
@@ -752,10 +816,16 @@ void AutocompletePopupViewMac::OpenURLForRow(int row, bool force_background) {
// This handles drawing the decorations of the rounded popup window,
// calling on NSMatrix to draw the actual contents.
- (void)drawRect:(NSRect)rect {
+ CGFloat bottomCornerRadius =
+ (bottomCornersRounded_ ? kPopupRoundingRadius : 0);
+
+ // "Top" really means "bottom" here, since the view is flipped.
NSBezierPath* path =
- [NSBezierPath bezierPathWithRoundedRect:[self bounds]
- xRadius:kPopupRoundingRadius
- yRadius:kPopupRoundingRadius];
+ [NSBezierPath gtm_bezierPathWithRoundRect:[self bounds]
+ topLeftCornerRadius:bottomCornerRadius
+ topRightCornerRadius:bottomCornerRadius
+ bottomLeftCornerRadius:kPopupRoundingRadius
+ bottomRightCornerRadius:kPopupRoundingRadius];
// Draw the matrix clipped to our border.
[NSGraphicsContext saveGraphicsState];