diff options
author | shess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-15 02:11:12 +0000 |
---|---|---|
committer | shess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-15 02:11:12 +0000 |
commit | a37d5c52ff5212cb7a47687ffd8ac8493c16c0c2 (patch) | |
tree | fa4d788be88771550f19a1194d2d71d3c7773f72 /chrome | |
parent | 284061f83fe4d68e31d890223cd64dcfc34ac1c5 (diff) | |
download | chromium_src-a37d5c52ff5212cb7a47687ffd8ac8493c16c0c2.zip chromium_src-a37d5c52ff5212cb7a47687ffd8ac8493c16c0c2.tar.gz chromium_src-a37d5c52ff5212cb7a47687ffd8ac8493c16c0c2.tar.bz2 |
[Mac] Star as a rhs-decoration in AutocompleteTextFieldCell.
Refactor to convert star decoration to LocationBarDecoration.
BUG=none
TEST=Star icon continues to work.
TEST=Bookmark bubble pops in the right place.
Review URL: http://codereview.chromium.org/2971004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52422 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
14 files changed, 319 insertions, 187 deletions
diff --git a/chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.h b/chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.h index 41e0919..aa58a73 100644 --- a/chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.h +++ b/chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.h @@ -53,18 +53,15 @@ class LocationBarDecoration; // a button-like token on the left-hand side). @interface AutocompleteTextFieldCell : StyledTextFieldCell { @private - // Decorations which live to the left of the text. Owned by - // |LocationBarViewMac|. + // Decorations which live to the left and right of the text, ordered + // from outside in. Decorations are owned by |LocationBarViewMac|. std::vector<LocationBarDecoration*> leftDecorations_; + std::vector<LocationBarDecoration*> rightDecorations_; // Set if there is a string to display as a hint on the right-hand // side of the field. Exclusive WRT |keywordString_|; scoped_nsobject<NSAttributedString> hintString_; - // The star icon sits at the right-hand side of the field when an - // URL is being shown. - LocationBarViewMac::LocationBarImageView* starIconView_; - // List of views showing visible Page Actions. Owned by the location bar. // Display is exclusive WRT the |hintString_| and |keywordString_|. // This may be NULL during testing. @@ -87,17 +84,20 @@ class LocationBarDecoration; availableWidth:(CGFloat)width; - (void)clearHint; -// Clear |leftDecorations_|. +// Clear |leftDecorations_| and |rightDecorations_|. - (void)clearDecorations; // Add a new left-side decoration to the right of the existing // left-side decorations. - (void)addLeftDecoration:(LocationBarDecoration*)decoration; +// Add a new right-side decoration to the left of the existing +// right-side decorations. +- (void)addRightDecoration:(LocationBarDecoration*)decoration; + // The width available after accounting for decorations. - (CGFloat)availableWidthInFrame:(const NSRect)frame; -- (void)setStarIconView:(LocationBarViewMac::LocationBarImageView*)view; - (void)setPageActionViewList:(LocationBarViewMac::PageActionViewList*)list; - (void)setContentSettingViewsList: (LocationBarViewMac::ContentSettingViews*)views; @@ -106,10 +106,6 @@ class LocationBarDecoration; // only visible icons. - (NSArray*)layedOutIcons:(NSRect)cellFrame; -// Return the rectangle the star is being shown in, for purposes of -// positioning the bookmark bubble. -- (NSRect)starIconFrameForFrame:(NSRect)cellFrame; - // Return the frame for |aDecoration| if the cell is in |cellFrame|. // Returns |NSZeroRect| for decorations which are not currently // visible. diff --git a/chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.mm b/chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.mm index 0b637c7..bd37113 100644 --- a/chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.mm +++ b/chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.mm @@ -28,9 +28,6 @@ const CGFloat kBaselineAdjust = 2.0; // Matches the clipping radius of |GradientButtonCell|. const CGFloat kCornerRadius = 4.0; -// Gap to leave between hint and right-hand-side of cell. -const NSInteger kHintXOffset = 4; - // How far to shift bounding box of hint down from top of field. // Assumes -setFlipped:YES. const NSInteger kHintYOffset = 4; @@ -51,8 +48,12 @@ const CGFloat kEditorHorizontalInset = 3.0; // How far to inset the left-hand decorations from the field's bounds. const CGFloat kLeftDecorationXOffset = 3.0; +// How far to inset the right-hand decorations from the field's bounds. +// TODO(shess): Why is this different from |kLeftDecorationXOffset|? +const CGFloat kRightDecorationXOffset = 4.0; + // The amount of padding on either side reserved for drawing decorations. -const NSInteger kDecorationHorizontalPad = 3; +const CGFloat kDecorationHorizontalPad = 3; // How long to wait for mouse-up on the location icon before assuming // that the user wants to drag. @@ -60,7 +61,7 @@ const NSTimeInterval kLocationIconDragTimeout = 0.25; // Conveniences to centralize width+offset calculations. CGFloat WidthForHint(NSAttributedString* hintString) { - return kHintXOffset + ceil([hintString size].width); + return kRightDecorationXOffset + ceil([hintString size].width); } // Convenience to draw |image| in the |rect| portion of |view|. @@ -97,38 +98,42 @@ NSAttributedString* AttributedStringForImage(NSImage* anImage, return [[as copy] autorelease]; } -// Helper function for calculating placement of decorations w/in the -// cell. |frame| is the cell's boundary rectangle. -// |left_decorations| is a set of decorations for the left-hand side -// of the cell, before the text. |decorations| will contain the -// resulting visible decorations, and |decoration_frames| will contain -// their frames in the same coordinates as |frame|. |remaining_frame| -// will contain the frame left over for the field editor. -void CalculatePositionsInFrame( - const NSRect cell_frame, - const std::vector<LocationBarDecoration*>& left_decorations, +// Calculate the positions for a set of decorations. |frame| is the +// overall frame to do layout in, |remaining_frame| will get the +// left-over space. |all_decorations| is the set of decorations to +// lay out, |decorations| will be set to the decorations which are +// visible and which fit, in the same order as |all_decorations|, +// while |decoration_frames| will be the corresponding frames. +// |x_edge| describes the edge to layout the decorations against +// (|NSMinXEdge| or |NSMaxXEdge|). |initial_padding| is the padding +// from the edge of |cell_frame| (|kDecorationHorizontalPad| is used +// between decorations). +void CalculatePositionsHelper( + NSRect frame, + const std::vector<LocationBarDecoration*>& all_decorations, + NSRectEdge x_edge, + CGFloat initial_padding, std::vector<LocationBarDecoration*>* decorations, std::vector<NSRect>* decoration_frames, NSRect* remaining_frame) { - NSRect frame = cell_frame; + DCHECK(x_edge == NSMinXEdge || x_edge == NSMaxXEdge); + DCHECK_EQ(decorations->size(), decoration_frames->size()); - // The left-most decoration will be inset a bit further from the edge. - CGFloat left_padding = kLeftDecorationXOffset; + // The outer-most decoration will be inset a bit further from the + // edge. + CGFloat padding = initial_padding; - decorations->clear(); - decoration_frames->clear(); + for (size_t i = 0; i < all_decorations.size(); ++i) { + if (all_decorations[i]->IsVisible()) { + NSRect padding_rect, available; - for (size_t i = 0; i < left_decorations.size(); ++i) { - if (left_decorations[i]->IsVisible()) { - NSRect padding, available; - - // Peel off the left-side padding. - NSDivideRect(frame, &padding, &available, left_padding, NSMinXEdge); + // Peel off the outside padding. + NSDivideRect(frame, &padding_rect, &available, padding, x_edge); // Find out how large the decoration will be in the remaining // space. const CGFloat used_width = - left_decorations[i]->GetWidthForSpace(NSWidth(available)); + all_decorations[i]->GetWidthForSpace(NSWidth(available)); if (used_width != LocationBarDecoration::kOmittedWidth) { DCHECK_GT(used_width, 0.0); @@ -137,20 +142,62 @@ void CalculatePositionsInFrame( // Peel off the desired width, leaving the remainder in // |frame|. NSDivideRect(available, &decoration_frame, &frame, - used_width, NSMinXEdge); + used_width, x_edge); - decorations->push_back(left_decorations[i]); + decorations->push_back(all_decorations[i]); decoration_frames->push_back(decoration_frame); DCHECK_EQ(decorations->size(), decoration_frames->size()); // Adjust padding for between decorations. - left_padding = kDecorationHorizontalPad; + padding = kDecorationHorizontalPad; } } } + DCHECK_EQ(decorations->size(), decoration_frames->size()); *remaining_frame = frame; +} + +// Helper function for calculating placement of decorations w/in the +// cell. |frame| is the cell's boundary rectangle, |remaining_frame| +// will get any space left after decorations are laid out (for text). +// |left_decorations| is a set of decorations for the left-hand side +// of the cell, |right_decorations| for the right-hand side. +// |decorations| will contain the resulting visible decorations, and +// |decoration_frames| will contain their frames in the same +// coordinates as |frame|. Decorations will be ordered left to right. +void CalculatePositionsInFrame( + NSRect frame, + const std::vector<LocationBarDecoration*>& left_decorations, + const std::vector<LocationBarDecoration*>& right_decorations, + std::vector<LocationBarDecoration*>* decorations, + std::vector<NSRect>* decoration_frames, + NSRect* remaining_frame) { + decorations->clear(); + decoration_frames->clear(); + + // Layout |left_decorations| against the LHS. + CalculatePositionsHelper(frame, left_decorations, + NSMinXEdge, kLeftDecorationXOffset, + decorations, decoration_frames, &frame); + DCHECK_EQ(decorations->size(), decoration_frames->size()); + + // Capture the number of visible left-hand decorations. + const size_t left_count = decorations->size(); + + // Layout |right_decorations| against the RHS. + CalculatePositionsHelper(frame, right_decorations, + NSMaxXEdge, kRightDecorationXOffset, + decorations, decoration_frames, &frame); DCHECK_EQ(decorations->size(), decoration_frames->size()); + + // Reverse the right-hand decorations so that overall everything is + // sorted left to right. + std::reverse(decorations->begin() + left_count, decorations->end()); + std::reverse(decoration_frames->begin() + left_count, + decoration_frames->end()); + + *remaining_frame = frame; } } // namespace @@ -320,10 +367,6 @@ void CalculatePositionsInFrame( page_action_views_ = list; } -- (void)setStarIconView:(LocationBarViewMac::LocationBarImageView*)view { - starIconView_ = view; -} - - (void)setContentSettingViewsList: (LocationBarViewMac::ContentSettingViews*)views { content_setting_views_ = views; @@ -331,17 +374,22 @@ void CalculatePositionsInFrame( - (void)clearDecorations { leftDecorations_.clear(); + rightDecorations_.clear(); } - (void)addLeftDecoration:(LocationBarDecoration*)decoration { leftDecorations_.push_back(decoration); } +- (void)addRightDecoration:(LocationBarDecoration*)decoration { + rightDecorations_.push_back(decoration); +} + - (CGFloat)availableWidthInFrame:(const NSRect)frame { std::vector<LocationBarDecoration*> decorations; std::vector<NSRect> decorationFrames; NSRect textFrame; - CalculatePositionsInFrame(frame, leftDecorations_, + CalculatePositionsInFrame(frame, leftDecorations_, rightDecorations_, &decorations, &decorationFrames, &textFrame); return NSWidth(textFrame); @@ -357,7 +405,7 @@ void CalculatePositionsInFrame( std::vector<LocationBarDecoration*> decorations; std::vector<NSRect> decorationFrames; NSRect textFrame; - CalculatePositionsInFrame(cellFrame, leftDecorations_, + CalculatePositionsInFrame(cellFrame, leftDecorations_, rightDecorations_, &decorations, &decorationFrames, &textFrame); // Find our decoration and return the corresponding frame. @@ -381,7 +429,7 @@ void CalculatePositionsInFrame( std::vector<LocationBarDecoration*> decorations; std::vector<NSRect> decorationFrames; NSRect textFrame = [super textFrameForFrame:cellFrame]; - CalculatePositionsInFrame(textFrame, leftDecorations_, + CalculatePositionsInFrame(textFrame, leftDecorations_, rightDecorations_, &decorations, &decorationFrames, &textFrame); // NOTE: This function must closely match the logic in @@ -411,18 +459,6 @@ void CalculatePositionsInFrame( return textFrame; } -- (NSRect)starIconFrameForFrame:(NSRect)cellFrame { - if (!starIconView_ || !starIconView_->IsVisible()) - return NSZeroRect; - - // The star icon is always at the RHS. - scoped_nsobject<AutocompleteTextFieldIcon> icon( - [[AutocompleteTextFieldIcon alloc] initImageWithView:starIconView_]); - cellFrame.size.width -= kHintXOffset; - [icon positionInFrame:cellFrame]; - return [icon rect]; -} - - (size_t)pageActionCount { // page_action_views_ may be NULL during testing, or if the // containing LocationViewMac object has already been destructed @@ -465,7 +501,7 @@ void CalculatePositionsInFrame( std::vector<LocationBarDecoration*> decorations; std::vector<NSRect> decorationFrames; NSRect workingFrame; - CalculatePositionsInFrame(cellFrame, leftDecorations_, + CalculatePositionsInFrame(cellFrame, leftDecorations_, rightDecorations_, &decorations, &decorationFrames, &workingFrame); // Draw the decorations first. @@ -504,6 +540,14 @@ void CalculatePositionsInFrame( } - (NSArray*)layedOutIcons:(NSRect)cellFrame { + // Trim the decoration area from |cellFrame|. This is duplicate + // work WRT the caller in some cases, but locating this here is + // simpler, and this code will go away soon. + std::vector<LocationBarDecoration*> decorations; + std::vector<NSRect> decorationFrames; + CalculatePositionsInFrame(cellFrame, leftDecorations_, rightDecorations_, + &decorations, &decorationFrames, &cellFrame); + // The set of views to display right-justified in the cell, from // left to right. NSMutableArray* result = [NSMutableArray array]; @@ -527,10 +571,6 @@ void CalculatePositionsInFrame( views.push_back(page_action_views_->ViewAt(i)); } - // The star icon should always come last. - if (starIconView_) - views.push_back(starIconView_); - // Load the visible views into |result|. for (std::vector<LocationBarViewMac::LocationBarImageView*>::const_iterator iter = views.begin(); iter != views.end(); ++iter) { @@ -541,15 +581,17 @@ void CalculatePositionsInFrame( } } - // Leave a boundary at RHS of field. - cellFrame.size.width -= kHintXOffset; + // Padding from right-hand decoration. There should always be at + // least one (the star). + cellFrame.size.width -= kDecorationHorizontalPad; // Position each view within the frame from right to left. for (AutocompleteTextFieldIcon* icon in [result reverseObjectEnumerator]) { [icon positionInFrame:cellFrame]; // Trim the icon's space from the frame. - cellFrame.size.width = NSMinX([icon rect]) - kDecorationHorizontalPad; + cellFrame.size.width = + NSMinX([icon rect]) - NSMinX(cellFrame) - kDecorationHorizontalPad; } return result; } @@ -567,7 +609,7 @@ void CalculatePositionsInFrame( std::vector<LocationBarDecoration*> decorations; std::vector<NSRect> decorationFrames; NSRect textFrame; - CalculatePositionsInFrame(cellFrame, leftDecorations_, + CalculatePositionsInFrame(cellFrame, leftDecorations_, rightDecorations_, &decorations, &decorationFrames, &textFrame); for (size_t i = 0; i < decorations.size(); ++i) { diff --git a/chrome/browser/cocoa/location_bar/autocomplete_text_field_cell_unittest.mm b/chrome/browser/cocoa/location_bar/autocomplete_text_field_cell_unittest.mm index da01dbb..f22de95 100644 --- a/chrome/browser/cocoa/location_bar/autocomplete_text_field_cell_unittest.mm +++ b/chrome/browser/cocoa/location_bar/autocomplete_text_field_cell_unittest.mm @@ -12,6 +12,7 @@ #import "chrome/browser/cocoa/location_bar/location_bar_decoration.h" #import "chrome/browser/cocoa/location_bar/location_icon_decoration.h" #import "chrome/browser/cocoa/location_bar/selected_keyword_decoration.h" +#import "chrome/browser/cocoa/location_bar/star_decoration.h" #include "grit/theme_resources.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -148,6 +149,12 @@ TEST_F(AutocompleteTextFieldCellTest, FocusedDisplay) { EXPECT_NE(ev_bubble_decoration.GetWidthForSpace(kVeryWide), LocationBarDecoration::kOmittedWidth); + StarDecoration star_decoration(NULL); + star_decoration.SetVisible(true); + [cell addRightDecoration:&star_decoration]; + EXPECT_NE(star_decoration.GetWidthForSpace(kVeryWide), + LocationBarDecoration::kOmittedWidth); + // Make sure we're actually calling |DrawInFrame()|. StrictMock<MockDecoration> mock_decoration; mock_decoration.SetVisible(true); diff --git a/chrome/browser/cocoa/location_bar/image_decoration.h b/chrome/browser/cocoa/location_bar/image_decoration.h new file mode 100644 index 0000000..3395307 --- /dev/null +++ b/chrome/browser/cocoa/location_bar/image_decoration.h @@ -0,0 +1,35 @@ +// 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. + +#ifndef CHROME_BROWSER_COCOA_LOCATION_BAR_IMAGE_DECORATION_H_ +#define CHROME_BROWSER_COCOA_LOCATION_BAR_IMAGE_DECORATION_H_ + +#import "base/scoped_nsobject.h" +#include "chrome/browser/cocoa/location_bar/location_bar_decoration.h" + +// |LocationBarDecoration| which sizes and draws itself according to +// an |NSImage|. + +class ImageDecoration : public LocationBarDecoration { + public: + ImageDecoration(); + virtual ~ImageDecoration(); + + NSImage* GetImage(); + void SetImage(NSImage* image); + + // Returns the part of |frame| the image is drawn in. + NSRect GetDrawRectInFrame(NSRect frame); + + // Implement |LocationBarDecoration|. + virtual CGFloat GetWidthForSpace(CGFloat width); + virtual void DrawInFrame(NSRect frame, NSView* control_view); + + private: + scoped_nsobject<NSImage> image_; + + DISALLOW_COPY_AND_ASSIGN(ImageDecoration); +}; + +#endif // CHROME_BROWSER_COCOA_LOCATION_BAR_IMAGE_DECORATION_H_ diff --git a/chrome/browser/cocoa/location_bar/image_decoration.mm b/chrome/browser/cocoa/location_bar/image_decoration.mm new file mode 100644 index 0000000..a5cdc80 --- /dev/null +++ b/chrome/browser/cocoa/location_bar/image_decoration.mm @@ -0,0 +1,51 @@ +// 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. + +#include <cmath> + +#import "chrome/browser/cocoa/location_bar/image_decoration.h" + +#import "chrome/browser/cocoa/image_utils.h" + +ImageDecoration::ImageDecoration() { +} + +ImageDecoration::~ImageDecoration() { +} + +NSImage* ImageDecoration::GetImage() { + return image_; +} + +void ImageDecoration::SetImage(NSImage* image) { + image_.reset([image retain]); +} + +NSRect ImageDecoration::GetDrawRectInFrame(NSRect frame) { + NSImage* image = GetImage(); + if (!image) + return frame; + + const CGFloat delta_height = NSHeight(frame) - [image size].height; + const CGFloat y_inset = std::floor(delta_height / 2.0); + return NSInsetRect(frame, 0.0, y_inset); +} + +CGFloat ImageDecoration::GetWidthForSpace(CGFloat width) { + NSImage* image = GetImage(); + if (image) { + const CGFloat image_width = [image size].width; + if (image_width <= width) + return image_width; + } + return kOmittedWidth; +} + +void ImageDecoration::DrawInFrame(NSRect frame, NSView* control_view) { + [GetImage() drawInRect:GetDrawRectInFrame(frame) + fromRect:NSZeroRect // Entire image + operation:NSCompositeSourceOver + fraction:1.0 + neverFlipped:YES]; +} diff --git a/chrome/browser/cocoa/location_bar/location_icon_decoration_unittest.mm b/chrome/browser/cocoa/location_bar/image_decoration_unittest.mm index 98e1055..29040e0 100644 --- a/chrome/browser/cocoa/location_bar/location_icon_decoration_unittest.mm +++ b/chrome/browser/cocoa/location_bar/image_decoration_unittest.mm @@ -4,23 +4,19 @@ #import <Cocoa/Cocoa.h> -#import "chrome/browser/cocoa/location_bar/location_icon_decoration.h" +#import "chrome/browser/cocoa/location_bar/image_decoration.h" #import "chrome/browser/cocoa/cocoa_test_helper.h" #include "testing/gtest/include/gtest/gtest.h" namespace { -class LocationIconDecorationTest : public CocoaTest { +class ImageDecorationTest : public CocoaTest { public: - LocationIconDecorationTest() - : decoration_(NULL) { - } - - LocationIconDecoration decoration_; + ImageDecoration decoration_; }; -TEST_F(LocationIconDecorationTest, SetGetImage) { +TEST_F(ImageDecorationTest, SetGetImage) { EXPECT_FALSE(decoration_.GetImage()); const NSSize kImageSize = NSMakeSize(20.0, 20.0); @@ -33,7 +29,7 @@ TEST_F(LocationIconDecorationTest, SetGetImage) { EXPECT_FALSE(decoration_.GetImage()); } -TEST_F(LocationIconDecorationTest, GetWidthForSpace) { +TEST_F(ImageDecorationTest, GetWidthForSpace) { const CGFloat kWide = 100.0; const CGFloat kNarrow = 10.0; diff --git a/chrome/browser/cocoa/location_bar/location_bar_view_mac.h b/chrome/browser/cocoa/location_bar/location_bar_view_mac.h index 46d2bc0..8dd1a2e 100644 --- a/chrome/browser/cocoa/location_bar/location_bar_view_mac.h +++ b/chrome/browser/cocoa/location_bar/location_bar_view_mac.h @@ -31,6 +31,7 @@ class EVBubbleDecoration; class LocationIconDecoration; class Profile; class SelectedKeywordDecoration; +class StarDecoration; class ToolbarModel; // A C++ bridge class that represents the location bar UI element to @@ -194,30 +195,6 @@ class LocationBarViewMac : public AutocompleteEditController, DISALLOW_COPY_AND_ASSIGN(LocationBarImageView); }; - // Used to display the bookmark star in the RHS. - class StarIconView : public LocationBarImageView { - public: - explicit StarIconView(CommandUpdater* command_updater); - virtual ~StarIconView() {} - - // Shows the bookmark bubble. - virtual void OnMousePressed(NSRect bounds); - - // Set the image and tooltip based on |starred|. - void SetStarred(bool starred); - - virtual NSString* GetToolTip(); - - private: - // For bringing up bookmark bar. - CommandUpdater* command_updater_; // Weak, owned by Browser. - - // The string to show for a tooltip. - scoped_nsobject<NSString> tooltip_; - - DISALLOW_COPY_AND_ASSIGN(StarIconView); - }; - // PageActionImageView is used to display the icon for a given Page Action // and notify the extension when the icon is clicked. class PageActionImageView : public LocationBarImageView, @@ -421,7 +398,7 @@ class LocationBarViewMac : public AutocompleteEditController, scoped_ptr<EVBubbleDecoration> ev_bubble_decoration_; // Bookmark star right of page actions. - StarIconView star_icon_view_; + scoped_ptr<StarDecoration> star_decoration_; // Any installed Page Actions. PageActionViewList page_action_views_; diff --git a/chrome/browser/cocoa/location_bar/location_bar_view_mac.mm b/chrome/browser/cocoa/location_bar/location_bar_view_mac.mm index b31ce91..275e436 100644 --- a/chrome/browser/cocoa/location_bar/location_bar_view_mac.mm +++ b/chrome/browser/cocoa/location_bar/location_bar_view_mac.mm @@ -26,6 +26,7 @@ #import "chrome/browser/cocoa/location_bar/ev_bubble_decoration.h" #import "chrome/browser/cocoa/location_bar/location_icon_decoration.h" #import "chrome/browser/cocoa/location_bar/selected_keyword_decoration.h" +#import "chrome/browser/cocoa/location_bar/star_decoration.h" #include "chrome/browser/command_updater.h" #include "chrome/browser/content_setting_image_model.h" #include "chrome/browser/content_setting_bubble_model.h" @@ -69,7 +70,7 @@ LocationBarViewMac::LocationBarViewMac( ev_bubble_decoration_( new EVBubbleDecoration(location_icon_decoration_.get(), [field_ font])), - star_icon_view_(command_updater), + star_decoration_(new StarDecoration(command_updater)), page_action_views_(this, profile, toolbar_model), profile_(profile), browser_(browser), @@ -85,7 +86,6 @@ LocationBarViewMac::LocationBarViewMac( } AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; - [cell setStarIconView:&star_icon_view_]; [cell setPageActionViewList:&page_action_views_]; [cell setContentSettingViewsList:&content_setting_views_]; @@ -98,7 +98,7 @@ LocationBarViewMac::~LocationBarViewMac() { // Disconnect from cell in case it outlives us. AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; [cell setPageActionViewList:NULL]; - [cell setStarIconView:NULL]; + [cell clearDecorations]; } void LocationBarViewMac::ShowFirstRunBubble(FirstRun::BubbleType bubble_type) { @@ -353,7 +353,7 @@ void LocationBarViewMac::TestPageActionPressed(size_t index) { void LocationBarViewMac::SetEditable(bool editable) { [field_ setEditable:editable ? YES : NO]; - star_icon_view_.SetVisible(editable); + star_decoration_->SetVisible(editable); UpdatePageActions(); Layout(); } @@ -363,7 +363,7 @@ bool LocationBarViewMac::IsEditable() { } void LocationBarViewMac::SetStarred(bool starred) { - star_icon_view_.SetStarred(starred); + star_decoration_->SetStarred(starred); // TODO(shess): The field-editor frame and cursor rects should not // change, here. @@ -374,12 +374,10 @@ void LocationBarViewMac::SetStarred(bool starred) { NSPoint LocationBarViewMac::GetBookmarkBubblePoint() const { AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; - NSRect frame = [cell starIconFrameForFrame:[field_ bounds]]; - frame = [field_ convertRect:frame toView:nil]; - - // Star is not exactly centered horizontally, and the tip should - // point to the visual bottom of the star, not the middle. - return NSMakePoint(NSMidX(frame), NSMinY(frame) + 4.0); + const NSRect frame = [cell frameForDecoration:star_decoration_.get() + inFrame:[field_ bounds]]; + const NSPoint point = star_decoration_->GetBubblePointInFrame(frame); + return [field_ convertPoint:point toView:nil]; } NSImage* LocationBarViewMac::GetTabButtonImage() { @@ -474,34 +472,6 @@ NSSize LocationBarViewMac::LocationBarImageView::GetImageSize() const { return GetDefaultImageSize(); } -// StarIconView----------------------------------------------------------------- - -LocationBarViewMac::StarIconView::StarIconView(CommandUpdater* command_updater) - : command_updater_(command_updater) { - SetVisible(true); - SetStarred(false); -} - -void LocationBarViewMac::StarIconView::SetStarred(bool starred) { - if (starred) { - SetImage(AutocompleteEditViewMac::ImageForResource(IDR_OMNIBOX_STAR_LIT)); - tooltip_.reset( - [l10n_util::GetNSStringWithFixup(IDS_TOOLTIP_STARRED) retain]); - } else { - SetImage(AutocompleteEditViewMac::ImageForResource(IDR_OMNIBOX_STAR)); - tooltip_.reset( - [l10n_util::GetNSStringWithFixup(IDS_TOOLTIP_STAR) retain]); - } -} - -void LocationBarViewMac::StarIconView::OnMousePressed(NSRect bounds) { - command_updater_->ExecuteCommand(IDC_BOOKMARK_PAGE); -} - -NSString* LocationBarViewMac::StarIconView::GetToolTip() { - return tooltip_.get(); -} - // PageActionImageView---------------------------------------------------------- LocationBarViewMac::PageActionImageView::PageActionImageView( @@ -859,6 +829,7 @@ void LocationBarViewMac::Layout() { [cell addLeftDecoration:location_icon_decoration_.get()]; [cell addLeftDecoration:selected_keyword_decoration_.get()]; [cell addLeftDecoration:ev_bubble_decoration_.get()]; + [cell addRightDecoration:star_decoration_.get()]; // By default only the location icon is visible. location_icon_decoration_->SetVisible(true); diff --git a/chrome/browser/cocoa/location_bar/location_icon_decoration.h b/chrome/browser/cocoa/location_bar/location_icon_decoration.h index 68aaeee..18952d2 100644 --- a/chrome/browser/cocoa/location_bar/location_icon_decoration.h +++ b/chrome/browser/cocoa/location_bar/location_icon_decoration.h @@ -7,27 +7,18 @@ #import <Cocoa/Cocoa.h> -#include "base/scoped_nsobject.h" -#include "chrome/browser/cocoa/location_bar/location_bar_decoration.h" +#include "chrome/browser/cocoa/location_bar/image_decoration.h" class LocationBarViewMac; // LocationIconDecoration is used to display an icon to the left of // the address. -class LocationIconDecoration : public LocationBarDecoration { +class LocationIconDecoration : public ImageDecoration { public: explicit LocationIconDecoration(LocationBarViewMac* owner); virtual ~LocationIconDecoration(); - // The image to display for the location. - NSImage* GetImage(); - void SetImage(NSImage* image); - - // Implement |LocationBarDecoration|. - virtual CGFloat GetWidthForSpace(CGFloat width); - virtual void DrawInFrame(NSRect frame, NSView* control_view); - // Allow dragging the current URL. virtual bool IsDraggable(); virtual NSPasteboard* GetDragPasteboard(); @@ -40,8 +31,6 @@ class LocationIconDecoration : public LocationBarDecoration { // The location bar view that owns us. LocationBarViewMac* owner_; - scoped_nsobject<NSImage> image_; - DISALLOW_COPY_AND_ASSIGN(LocationIconDecoration); }; diff --git a/chrome/browser/cocoa/location_bar/location_icon_decoration.mm b/chrome/browser/cocoa/location_bar/location_icon_decoration.mm index 34cfe78..9ec9794 100644 --- a/chrome/browser/cocoa/location_bar/location_icon_decoration.mm +++ b/chrome/browser/cocoa/location_bar/location_icon_decoration.mm @@ -7,7 +7,6 @@ #import "chrome/browser/cocoa/location_bar/location_icon_decoration.h" #include "base/sys_string_conversions.h" -#import "chrome/browser/cocoa/image_utils.h" #import "chrome/browser/cocoa/location_bar/location_bar_view_mac.h" #include "chrome/browser/tab_contents/tab_contents.h" #import "third_party/mozilla/NSPasteboard+Utils.h" @@ -18,35 +17,6 @@ LocationIconDecoration::LocationIconDecoration(LocationBarViewMac* owner) LocationIconDecoration::~LocationIconDecoration() { } -NSImage* LocationIconDecoration::GetImage() { - return image_; -} - -void LocationIconDecoration::SetImage(NSImage* image) { - image_.reset([image retain]); -} - -CGFloat LocationIconDecoration::GetWidthForSpace(CGFloat width) { - NSImage* image = GetImage(); - if (image) { - const CGFloat image_width = [image size].width; - if (image_width <= width) - return image_width; - } - return kOmittedWidth; -} - -void LocationIconDecoration::DrawInFrame(NSRect frame, NSView* control_view) { - NSImage* image = GetImage(); - const CGFloat delta_height = NSHeight(frame) - [image size].height; - const CGFloat y_inset = std::floor(delta_height / 2.0); - [image drawInRect:NSInsetRect(frame, 0.0, y_inset) - fromRect:NSZeroRect // Entire image - operation:NSCompositeSourceOver - fraction:1.0 - neverFlipped:YES]; -} - bool LocationIconDecoration::IsDraggable() { // Without a tab it will be impossible to get the information needed // to perform a drag. diff --git a/chrome/browser/cocoa/location_bar/star_decoration.h b/chrome/browser/cocoa/location_bar/star_decoration.h new file mode 100644 index 0000000..e39f9a8 --- /dev/null +++ b/chrome/browser/cocoa/location_bar/star_decoration.h @@ -0,0 +1,42 @@ +// 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. + +#ifndef CHROME_BROWSER_COCOA_LOCATION_BAR_STAR_DECORATION_H_ +#define CHROME_BROWSER_COCOA_LOCATION_BAR_STAR_DECORATION_H_ + +#import <Cocoa/Cocoa.h> + +#include "chrome/browser/cocoa/location_bar/image_decoration.h" + +class CommandUpdater; + +// Star icon on the right side of the field. + +class StarDecoration : public ImageDecoration { + public: + explicit StarDecoration(CommandUpdater* command_updater); + virtual ~StarDecoration(); + + // Sets the image and tooltip based on |starred|. + void SetStarred(bool starred); + + // Get the point where the bookmark bubble should point within the + // decoration's frame. + NSPoint GetBubblePointInFrame(NSRect frame); + + // Implement |LocationBarDecoration|. + virtual bool OnMousePressed(NSRect frame); + virtual NSString* GetToolTip(); + + private: + // For bringing up bookmark bar. + CommandUpdater* command_updater_; // Weak, owned by Browser. + + // The string to show for a tooltip. + scoped_nsobject<NSString> tooltip_; + + DISALLOW_COPY_AND_ASSIGN(StarDecoration); +}; + +#endif // CHROME_BROWSER_COCOA_LOCATION_BAR_STAR_DECORATION_H_ diff --git a/chrome/browser/cocoa/location_bar/star_decoration.mm b/chrome/browser/cocoa/location_bar/star_decoration.mm new file mode 100644 index 0000000..1015f58 --- /dev/null +++ b/chrome/browser/cocoa/location_bar/star_decoration.mm @@ -0,0 +1,52 @@ +// 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/location_bar/star_decoration.h" + +#include "app/l10n_util_mac.h" +#include "chrome/app/chrome_dll_resource.h" +#import "chrome/browser/autocomplete/autocomplete_edit_view_mac.h" +#include "chrome/browser/command_updater.h" +#include "grit/generated_resources.h" +#include "grit/theme_resources.h" + +namespace { + +// The info-bubble point should look like it points to the point +// between the star's lower tips. Determined via Pixie.app +// magnification. +const CGFloat kStarPointYOffset = 4.0; + +} // namespace + +StarDecoration::StarDecoration(CommandUpdater* command_updater) + : command_updater_(command_updater) { + SetVisible(true); + SetStarred(false); +} + +StarDecoration::~StarDecoration() { +} + +void StarDecoration::SetStarred(bool starred) { + const int image_id = starred ? IDR_OMNIBOX_STAR_LIT : IDR_OMNIBOX_STAR; + const int tip_id = starred ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR; + SetImage(AutocompleteEditViewMac::ImageForResource(image_id)); + tooltip_.reset([l10n_util::GetNSStringWithFixup(tip_id) retain]); +} + +NSPoint StarDecoration::GetBubblePointInFrame(NSRect frame) { + const NSRect draw_frame = GetDrawRectInFrame(frame); + return NSMakePoint(NSMidX(draw_frame), + NSMaxY(draw_frame) - kStarPointYOffset); +} + +bool StarDecoration::OnMousePressed(NSRect frame) { + command_updater_->ExecuteCommand(IDC_BOOKMARK_PAGE); + return true; +} + +NSString* StarDecoration::GetToolTip() { + return tooltip_.get(); +} diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 6b4a1d1..441d14f 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -860,6 +860,8 @@ 'browser/cocoa/location_bar/bubble_decoration.mm', 'browser/cocoa/location_bar/ev_bubble_decoration.h', 'browser/cocoa/location_bar/ev_bubble_decoration.mm', + 'browser/cocoa/location_bar/image_decoration.h', + 'browser/cocoa/location_bar/image_decoration.mm', 'browser/cocoa/location_bar/location_bar_decoration.h', 'browser/cocoa/location_bar/location_bar_decoration.mm', 'browser/cocoa/location_bar/location_bar_view_mac.h', @@ -868,6 +870,8 @@ 'browser/cocoa/location_bar/location_icon_decoration.mm', 'browser/cocoa/location_bar/selected_keyword_decoration.h', 'browser/cocoa/location_bar/selected_keyword_decoration.mm', + 'browser/cocoa/location_bar/star_decoration.h', + 'browser/cocoa/location_bar/star_decoration.mm', 'browser/cocoa/menu_button.h', 'browser/cocoa/menu_button.mm', 'browser/cocoa/menu_controller.h', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 95de707..ea886ca 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -721,7 +721,7 @@ 'browser/cocoa/location_bar/autocomplete_text_field_editor_unittest.mm', 'browser/cocoa/location_bar/autocomplete_text_field_unittest.mm', 'browser/cocoa/location_bar/autocomplete_text_field_unittest_helper.mm', - 'browser/cocoa/location_bar/location_icon_decoration_unittest.mm', + 'browser/cocoa/location_bar/image_decoration_unittest.mm', 'browser/cocoa/location_bar/selected_keyword_decoration_unittest.mm', 'browser/cocoa/menu_button_unittest.mm', 'browser/cocoa/menu_controller_unittest.mm', |