diff options
9 files changed, 187 insertions, 461 deletions
diff --git a/chrome/browser/cocoa/extensions/extension_action_context_menu.mm b/chrome/browser/cocoa/extensions/extension_action_context_menu.mm index 36a7392..797f517 100644 --- a/chrome/browser/cocoa/extensions/extension_action_context_menu.mm +++ b/chrome/browser/cocoa/extensions/extension_action_context_menu.mm @@ -13,7 +13,7 @@ #include "chrome/browser/cocoa/extensions/browser_actions_controller.h" #include "chrome/browser/cocoa/extensions/extension_popup_controller.h" #include "chrome/browser/cocoa/info_bubble_view.h" -#import "chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.h" +#import "chrome/browser/cocoa/location_bar/location_bar_view_mac.h" #include "chrome/browser/cocoa/toolbar_controller.h" #include "chrome/browser/extensions/extension_install_ui.h" #include "chrome/browser/extensions/extensions_service.h" diff --git a/chrome/browser/cocoa/location_bar/autocomplete_text_field.mm b/chrome/browser/cocoa/location_bar/autocomplete_text_field.mm index 46b01d8..041cff8 100644 --- a/chrome/browser/cocoa/location_bar/autocomplete_text_field.mm +++ b/chrome/browser/cocoa/location_bar/autocomplete_text_field.mm @@ -211,13 +211,10 @@ NSRect fieldBounds = [self bounds]; [self addCursorRect:fieldBounds cursor:[NSCursor IBeamCursor]]; - AutocompleteTextFieldCell* cell = [self cell]; - for (AutocompleteTextFieldIcon* icon in [cell layedOutIcons:fieldBounds]) - [self addCursorRect:[icon rect] cursor:[NSCursor arrowCursor]]; - // TODO(shess): This needs to traverse the LocationBarDecorations // and put up a cursor for them, too. Except for the keyword-search // stuff? Sigh. + // http://crbug.com/48867 } // TODO(shess): -resetFieldEditorFrameIfNeeded is the place where @@ -236,6 +233,10 @@ [self removeAllToolTips]; [currentToolTips_ removeAllObjects]; +#if 0 + // TODO(shess): Bring back tooltips. All the wiring is in there, + // just need to hook it up. + // http://crbug.com/49321 AutocompleteTextFieldCell* cell = [self cell]; for (AutocompleteTextFieldIcon* icon in [cell layedOutIcons:[self bounds]]) { NSRect iconRect = [icon rect]; @@ -248,6 +249,7 @@ [currentToolTips_ addObject:tooltip]; [self addToolTipRect:iconRect owner:tooltip userData:nil]; } +#endif } // NOTE(shess): http://crbug.com/19116 describes a weird bug which 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 23b6305..cbb3e63 100644 --- a/chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.h +++ b/chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.h @@ -2,49 +2,17 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include <vector> + #import <Cocoa/Cocoa.h> #import "chrome/browser/cocoa/styled_text_field_cell.h" #include "base/scoped_nsobject.h" -#include "chrome/browser/cocoa/location_bar/location_bar_view_mac.h" +@class AutocompleteTextField; class LocationBarDecoration; -// Holds a |LocationBarImageView| and its current rect. Do not keep references -// to this object, only use it directly after calling |-layedOutIcons:|. -// TODO(shess): This class is basically a helper for laying out the -// icons. Try to refactor it away. If that is not reasonable, at -// least split the image and label cases into subclasses once the -// Omnibox stuff is settled. -@interface AutocompleteTextFieldIcon : NSObject { - // YES to draw the label part of |view_|, otherwise draw the image - // part. - BOOL isLabel_; - - // The frame rect of |view_|. - NSRect rect_; - - // weak, owned by LocationBarViewMac. - LocationBarViewMac::LocationBarImageView* view_; -} - -@property(assign, nonatomic) NSRect rect; -@property(assign, nonatomic) LocationBarViewMac::LocationBarImageView* view; - -- (id)initImageWithView:(LocationBarViewMac::LocationBarImageView*)view; -- (id)initLabelWithView:(LocationBarViewMac::LocationBarImageView*)view; - -// Position |view_| right-justified in |frame|. -- (void)positionInFrame:(NSRect)frame; - -// Draw image or label of |view_| in |rect_| within |controlView|. -// Only call after |-positionInFrame:| has set |rect_| (or after an -// explicit |-setRect:|). -- (void)drawInView:(NSView*)controlView; - -@end - // AutocompleteTextFieldCell extends StyledTextFieldCell to provide support for // certain decorations to be applied to the field. These are the search hint // ("Type to search" on the right-hand side), the keyword hint ("Press [Tab] to @@ -60,9 +28,6 @@ class LocationBarDecoration; // 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_; - - // List of content blocked icons. This may be NULL during testing. - LocationBarViewMac::ContentSettingViews* content_setting_views_; } // Chooses |anImage| only if all pieces won't fit w/in |width|. @@ -92,24 +57,17 @@ class LocationBarDecoration; // The width available after accounting for decorations. - (CGFloat)availableWidthInFrame:(const NSRect)frame; -- (void)setContentSettingViewsList: - (LocationBarViewMac::ContentSettingViews*)views; - -// Returns an array of the visible AutocompleteTextFieldIcon objects. Returns -// only visible icons. -- (NSArray*)layedOutIcons:(NSRect)cellFrame; - // Return the frame for |aDecoration| if the cell is in |cellFrame|. // Returns |NSZeroRect| for decorations which are not currently // visible. - (NSRect)frameForDecoration:(const LocationBarDecoration*)aDecoration inFrame:(NSRect)cellFrame; -// Find the icon under the event. |nil| if |theEvent| is not over -// anything. -- (AutocompleteTextFieldIcon*)iconForEvent:(NSEvent*)theEvent - inRect:(NSRect)cellFrame - ofView:(AutocompleteTextField*)controlView; +// Find the decoration under the event. |NULL| if |theEvent| is not +// over anything. +- (LocationBarDecoration*)decorationForEvent:(NSEvent*)theEvent + inRect:(NSRect)cellFrame + ofView:(AutocompleteTextField*)field; // Return the appropriate menu for any decorations under event. // Returns nil if no menu is present for the decoration, or if the 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 1d5466a..3d62be6 100644 --- a/chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.mm +++ b/chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.mm @@ -213,61 +213,6 @@ void CalculatePositionsInFrame( @end -@implementation AutocompleteTextFieldIcon - -@synthesize rect = rect_; -@synthesize view = view_; - -// Private helper. -- (id)initWithView:(LocationBarViewMac::LocationBarImageView*)view - isLabel:(BOOL)isLabel { - self = [super init]; - if (self) { - isLabel_ = isLabel; - view_ = view; - rect_ = NSZeroRect; - } - return self; -} - -- (id)initImageWithView:(LocationBarViewMac::LocationBarImageView*)view { - return [self initWithView:view isLabel:NO]; -} - -- (id)initLabelWithView:(LocationBarViewMac::LocationBarImageView*)view { - return [self initWithView:view isLabel:YES]; -} - -- (void)positionInFrame:(NSRect)frame { - if (isLabel_) { - NSAttributedString* label = view_->GetLabel(); - DCHECK(label); - const CGFloat labelWidth = ceil([label size].width); - rect_ = NSMakeRect(NSMaxX(frame) - labelWidth, - NSMinY(frame) + kIconLabelYOffset, - labelWidth, NSHeight(frame) - kIconLabelYOffset); - } else { - const NSSize imageSize = view_->GetImageSize(); - const CGFloat yOffset = floor((NSHeight(frame) - imageSize.height) / 2); - rect_ = NSMakeRect(NSMaxX(frame) - imageSize.width, - NSMinY(frame) + yOffset, - imageSize.width, imageSize.height); - } -} - -- (void)drawInView:(NSView*)controlView { - // Make sure someone called |-positionInFrame:|. - DCHECK(!NSIsEmptyRect(rect_)); - if (isLabel_) { - NSAttributedString* label = view_->GetLabel(); - [label drawInRect:rect_]; - } else { - DrawImageInRect(view_->GetImage(), controlView, rect_); - } -} - -@end - @implementation AutocompleteTextFieldCell // @synthesize doesn't seem to compile for this transition. @@ -364,11 +309,6 @@ void CalculatePositionsInFrame( hintString_.reset(); } -- (void)setContentSettingViewsList: - (LocationBarViewMac::ContentSettingViews*)views { - content_setting_views_ = views; -} - - (void)clearDecorations { leftDecorations_.clear(); rightDecorations_.clear(); @@ -432,16 +372,6 @@ void CalculatePositionsInFrame( // NOTE: This function must closely match the logic in // |-drawInteriorWithFrame:inView:|. - // Leave room for items on the right (SSL label, page actions, etc). - // Icons are laid out in |cellFrame| rather than |textFrame| for - // consistency with drawing code. - NSArray* icons = [self layedOutIcons:cellFrame]; - if ([icons count]) { - // Max x for resulting text frame. - const CGFloat maxX = NSMinX([[icons objectAtIndex:0] rect]); - textFrame.size.width = maxX - NSMinX(textFrame); - } - // Keyword string or hint string if they fit. if (hintString_) { const CGFloat hintWidth(WidthForHint(hintString_)); @@ -483,16 +413,6 @@ void CalculatePositionsInFrame( // NOTE: This function must closely match the logic in // |-textFrameForFrame:|. - NSArray* icons = [self layedOutIcons:cellFrame]; - for (AutocompleteTextFieldIcon* icon in icons) { - [icon drawInView:controlView]; - } - if ([icons count]) { - // Max x for resulting text frame. - const CGFloat maxX = NSMinX([[icons objectAtIndex:0] rect]); - workingFrame.size.width = maxX - NSMinX(workingFrame); - } - // Keyword string or hint string if they fit. if (hintString_) { const CGFloat hintWidth(WidthForHint(hintString_)); @@ -509,57 +429,6 @@ void CalculatePositionsInFrame( [super drawInteriorWithFrame:cellFrame inView:controlView]; } -- (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]; - - // Collect the image views for bulk processing. - // TODO(shess): Refactor with LocationBarViewMac to make the - // different types of items more consistent. - std::vector<LocationBarViewMac::LocationBarImageView*> views; - - if (content_setting_views_) { - views.insert(views.end(), - content_setting_views_->begin(), - content_setting_views_->end()); - } - - // Load the visible views into |result|. - for (std::vector<LocationBarViewMac::LocationBarImageView*>::const_iterator - iter = views.begin(); iter != views.end(); ++iter) { - if ((*iter)->IsVisible()) { - scoped_nsobject<AutocompleteTextFieldIcon> icon( - [[AutocompleteTextFieldIcon alloc] initImageWithView:*iter]); - [result addObject:icon]; - } - } - - // 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]) - NSMinX(cellFrame) - kDecorationHorizontalPad; - } - return result; -} - -// Returns the decoration under |theEvent|, or NULL if none. -// Like |-iconForEvent:inRect:ofView:|, but for decorations. - (LocationBarDecoration*)decorationForEvent:(NSEvent*)theEvent inRect:(NSRect)cellFrame ofView:(AutocompleteTextField*)controlView @@ -582,21 +451,6 @@ void CalculatePositionsInFrame( return NULL; } -- (AutocompleteTextFieldIcon*)iconForEvent:(NSEvent*)theEvent - inRect:(NSRect)cellFrame - ofView:(AutocompleteTextField*)controlView { - const BOOL flipped = [controlView isFlipped]; - const NSPoint location = - [controlView convertPoint:[theEvent locationInWindow] fromView:nil]; - - for (AutocompleteTextFieldIcon* icon in [self layedOutIcons:cellFrame]) { - if (NSMouseInRect(location, [icon rect], flipped)) - return icon; - } - - return nil; -} - - (NSMenu*)decorationMenuForEvent:(NSEvent*)theEvent inRect:(NSRect)cellFrame ofView:(AutocompleteTextField*)controlView { @@ -604,11 +458,6 @@ void CalculatePositionsInFrame( [self decorationForEvent:theEvent inRect:cellFrame ofView:controlView]; if (decoration) return decoration->GetMenu(); - - AutocompleteTextFieldIcon* - icon = [self iconForEvent:theEvent inRect:cellFrame ofView:controlView]; - if (icon) - return [icon view]->GetMenu(); return nil; } @@ -617,23 +466,15 @@ void CalculatePositionsInFrame( ofView:(AutocompleteTextField*)controlView { LocationBarDecoration* decoration = [self decorationForEvent:theEvent inRect:cellFrame ofView:controlView]; - AutocompleteTextFieldIcon* icon = - [self iconForEvent:theEvent inRect:cellFrame ofView:controlView]; - if (!decoration && !icon) + if (!decoration) return NO; - NSRect decorationRect = NSZeroRect; - if (icon) { - decorationRect = [icon rect]; - } else if (decoration) { - decorationRect = [self frameForDecoration:decoration inFrame:cellFrame]; - } + NSRect decorationRect = + [self frameForDecoration:decoration inFrame:cellFrame]; - // If the icon is draggable, then initiate a drag if the user drags - // or holds the mouse down for awhile. - if ((icon && [icon view]->IsDraggable()) || - (decoration && decoration->IsDraggable())) { - DCHECK(icon || decoration); + // If the decoration is draggable, then initiate a drag if the user + // drags or holds the mouse down for awhile. + if (decoration->IsDraggable()) { NSDate* timeout = [NSDate dateWithTimeIntervalSinceNow:kLocationIconDragTimeout]; NSEvent* event = [NSApp nextEventMatchingMask:(NSLeftMouseDraggedMask | @@ -642,23 +483,20 @@ void CalculatePositionsInFrame( inMode:NSEventTrackingRunLoopMode dequeue:YES]; if (!event || [event type] == NSLeftMouseDragged) { - NSPasteboard* pboard = nil; - if (icon) pboard = [icon view]->GetDragPasteboard(); - if (decoration) pboard = decoration->GetDragPasteboard(); + NSPasteboard* pboard = decoration->GetDragPasteboard(); DCHECK(pboard); // TODO(shess): My understanding is that the -isFlipped // adjustment should not be necessary. But without it, the - // image is nowhere near the cursor. Perhaps the icon's rect is - // incorrectly calculated? + // image is nowhere near the cursor. Perhaps the decorations's + // rect is incorrectly calculated? // http://crbug.com/40711 NSPoint dragPoint = decorationRect.origin; if ([controlView isFlipped]) dragPoint.y += NSHeight(decorationRect); - NSImage* image = nil; - if (icon) image = [icon view]->GetImage(); - if (decoration) image = decoration->GetDragImage(); + NSImage* image = decoration->GetDragImage(); + DCHECK(image); [controlView dragImage:image at:dragPoint offset:NSZeroSize @@ -673,12 +511,8 @@ void CalculatePositionsInFrame( DCHECK_EQ([event type], NSLeftMouseUp); } - if (icon) { - [icon view]->OnMousePressed(decorationRect); - } else if (decoration) { - if (!decoration->OnMousePressed(decorationRect)) - return NO; - } + if (!decoration->OnMousePressed(decorationRect)) + return NO; return YES; } diff --git a/chrome/browser/cocoa/location_bar/content_setting_decoration.h b/chrome/browser/cocoa/location_bar/content_setting_decoration.h new file mode 100644 index 0000000..9ff6eda --- /dev/null +++ b/chrome/browser/cocoa/location_bar/content_setting_decoration.h @@ -0,0 +1,47 @@ +// 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_CONTENT_SETTING_DECORATION_H_ +#define CHROME_BROWSER_COCOA_LOCATION_BAR_CONTENT_SETTING_DECORATION_H_ + +#include "base/scoped_ptr.h" +#import "chrome/browser/cocoa/location_bar/image_decoration.h" +#include "chrome/common/content_settings_types.h" + +// ContentSettingDecoration is used to display the content settings +// images on the current page. + +class ContentSettingImageModel; +class LocationBarViewMac; +class Profile; +class TabContents; + +class ContentSettingDecoration : public ImageDecoration { + public: + ContentSettingDecoration(ContentSettingsType settings_type, + LocationBarViewMac* owner, + Profile* profile); + virtual ~ContentSettingDecoration(); + + // Updates the image and visibility state based on the supplied TabContents. + void UpdateFromTabContents(const TabContents* tab_contents); + + // Overridden from |LocationBarDecoration| + virtual bool OnMousePressed(NSRect frame); + virtual NSString* GetToolTip(); + + private: + void SetToolTip(NSString* tooltip); + + scoped_ptr<ContentSettingImageModel> content_setting_image_model_; + + LocationBarViewMac* owner_; // weak + Profile* profile_; // weak + + scoped_nsobject<NSString> tooltip_; + + DISALLOW_COPY_AND_ASSIGN(ContentSettingDecoration); +}; + +#endif // CHROME_BROWSER_COCOA_LOCATION_BAR_CONTENT_SETTING_DECORATION_H_ diff --git a/chrome/browser/cocoa/location_bar/content_setting_decoration.mm b/chrome/browser/cocoa/location_bar/content_setting_decoration.mm new file mode 100644 index 0000000..6f2b8a3 --- /dev/null +++ b/chrome/browser/cocoa/location_bar/content_setting_decoration.mm @@ -0,0 +1,88 @@ +// 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/content_setting_decoration.h" + +#include "app/resource_bundle.h" +#include "base/sys_string_conversions.h" +#include "chrome/browser/browser_list.h" +#import "chrome/browser/cocoa/content_blocked_bubble_controller.h" +#import "chrome/browser/cocoa/location_bar/location_bar_view_mac.h" +#include "chrome/browser/content_setting_image_model.h" +#include "chrome/browser/content_setting_bubble_model.h" +#include "chrome/browser/pref_service.h" +#include "chrome/browser/profile.h" +#include "chrome/browser/tab_contents/tab_contents.h" +#include "chrome/common/pref_names.h" +#include "net/base/net_util.h" + +ContentSettingDecoration::ContentSettingDecoration( + ContentSettingsType settings_type, + LocationBarViewMac* owner, + Profile* profile) + : content_setting_image_model_( + ContentSettingImageModel::CreateContentSettingImageModel( + settings_type)), + owner_(owner), + profile_(profile) { +} + +ContentSettingDecoration::~ContentSettingDecoration() { +} + +void ContentSettingDecoration::UpdateFromTabContents( + const TabContents* tab_contents) { + content_setting_image_model_->UpdateFromTabContents(tab_contents); + SetVisible(content_setting_image_model_->is_visible()); + if (IsVisible()) { + // TODO(thakis): We should use pdfs for these icons on OSX. + // http://crbug.com/35847 + ResourceBundle& rb = ResourceBundle::GetSharedInstance(); + SetImage(rb.GetNSImageNamed(content_setting_image_model_->get_icon())); + SetToolTip(base::SysUTF8ToNSString( + content_setting_image_model_->get_tooltip())); + } +} + +bool ContentSettingDecoration::OnMousePressed(NSRect frame) { + // Get host. This should be shared on linux/win/osx medium-term. + TabContents* tabContents = + BrowserList::GetLastActive()->GetSelectedTabContents(); + if (!tabContents) + return true; + + GURL url = tabContents->GetURL(); + std::wstring displayHost; + net::AppendFormattedHost( + url, + UTF8ToWide(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)), + &displayHost, NULL, NULL); + + // Find point for bubble's arrow in screen coordinates. + // TODO(shess): |owner_| is only being used to fetch |field|. + // Consider passing in |control_view|. + AutocompleteTextField* field = owner_->GetAutocompleteTextField(); + frame = GetDrawRectInFrame(frame); + NSPoint anchor = NSMakePoint(NSMidX(frame) + 1, NSMaxY(frame)); + anchor = [field convertPoint:anchor toView:nil]; + anchor = [[field window] convertBaseToScreen:anchor]; + + // Open bubble. + ContentSettingBubbleModel* model = + ContentSettingBubbleModel::CreateContentSettingBubbleModel( + tabContents, profile_, + content_setting_image_model_->get_content_settings_type()); + [ContentBlockedBubbleController showForModel:model + parentWindow:[field window] + anchoredAt:anchor]; + return true; +} + +NSString* ContentSettingDecoration::GetToolTip() { + return tooltip_.get(); +} + +void ContentSettingDecoration::SetToolTip(NSString* tooltip) { + tooltip_.reset([tooltip retain]); +} 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 8d476b6..eb84998 100644 --- a/chrome/browser/cocoa/location_bar/location_bar_view_mac.h +++ b/chrome/browser/cocoa/location_bar/location_bar_view_mac.h @@ -25,6 +25,7 @@ @class AutocompleteTextField; class CommandUpdater; +class ContentSettingDecoration; class ContentSettingImageModel; class EVBubbleDecoration; @class ExtensionPopupController; @@ -135,95 +136,6 @@ class LocationBarViewMac : public AutocompleteEditController, const NotificationSource& source, const NotificationDetails& details); - // Used to display a clickable icon in the location bar. - class LocationBarImageView { - public: - explicit LocationBarImageView() : image_(nil), - label_(nil), - visible_(false) {} - virtual ~LocationBarImageView() {} - - // Sets the image. - void SetImage(NSImage* image); - - // Get the |resource_id| image resource and set the image. - void SetIcon(int resource_id); - - // Sets the label text, font, and color. |text| may be nil; |color| and - // |font| are ignored if |text| is nil. - void SetLabel(NSString* text, NSFont* baseFont, NSColor* color); - - // Sets the visibility. SetImage() should be called with a valid image - // before the visibility is set to |true|. - void SetVisible(bool visible); - - NSImage* GetImage() const { return image_; } - NSAttributedString* GetLabel() const { return label_; } - bool IsVisible() const { return visible_; } - - // Default size when no image is present. - virtual NSSize GetDefaultImageSize() const; - - // Returns the size of the image, else the default size. - NSSize GetImageSize() const; - - // Returns the tooltip for this image view or |nil| if there is none. - virtual NSString* GetToolTip() { return nil; } - - // Used to determinate if the item can act as a drag source. - virtual bool IsDraggable() { return false; } - - // The drag pasteboard to use if a drag is initiated. - virtual NSPasteboard* GetDragPasteboard() { return nil; } - - // Called on mouse down. - virtual void OnMousePressed(NSRect bounds) {} - - // Called to get the icon's context menu. Return |nil| for no menu. - virtual NSMenu* GetMenu() { return nil; } - - private: - scoped_nsobject<NSImage> image_; - - // The label shown next to the icon, or nil if none. - scoped_nsobject<NSAttributedString> label_; - - bool visible_; - - DISALLOW_COPY_AND_ASSIGN(LocationBarImageView); - }; - - // ContentSettingImageView is used to display the content settings images - // on the current page. - class ContentSettingImageView : public LocationBarImageView { - public: - ContentSettingImageView(ContentSettingsType settings_type, - LocationBarViewMac* owner, - Profile* profile); - virtual ~ContentSettingImageView(); - - // Shows a content settings bubble. - void OnMousePressed(NSRect bounds); - - // Updates the image and visibility state based on the supplied TabContents. - void UpdateFromTabContents(const TabContents* tab_contents); - - // Returns the tooltip for this Page Action image or |nil| if there is none. - virtual NSString* GetToolTip(); - - private: - void SetToolTip(NSString* tooltip); - - scoped_ptr<ContentSettingImageModel> content_setting_image_model_; - - LocationBarViewMac* owner_; - Profile* profile_; - scoped_nsobject<NSString> tooltip_; - - DISALLOW_COPY_AND_ASSIGN(ContentSettingImageView); - }; - typedef ScopedVector<ContentSettingImageView> ContentSettingViews; - private: // Posts |notification| to the default notification center. void PostNotification(NSString* notification); @@ -240,7 +152,7 @@ class LocationBarViewMac : public AutocompleteEditController, // Updates visibility of the content settings icons based on the current // tab contents state. - void RefreshContentSettingsViews(); + void RefreshContentSettingsDecorations(); void ShowFirstRunBubbleInternal(FirstRun::BubbleType bubble_type); @@ -274,8 +186,8 @@ class LocationBarViewMac : public AutocompleteEditController, // Any installed Page Actions. ScopedVector<PageActionDecoration> page_action_decorations_; - // The content blocked views. - ContentSettingViews content_setting_views_; + // The content blocked decorations. + ScopedVector<ContentSettingDecoration> content_setting_decorations_; Profile* profile_; 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 949048f..a09d612 100644 --- a/chrome/browser/cocoa/location_bar/location_bar_view_mac.mm +++ b/chrome/browser/cocoa/location_bar/location_bar_view_mac.mm @@ -23,6 +23,7 @@ #import "chrome/browser/cocoa/first_run_bubble_controller.h" #import "chrome/browser/cocoa/location_bar/autocomplete_text_field.h" #import "chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.h" +#import "chrome/browser/cocoa/location_bar/content_setting_decoration.h" #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/page_action_decoration.h" @@ -77,17 +78,13 @@ LocationBarViewMac::LocationBarViewMac( toolbar_model_(toolbar_model), transition_(PageTransition::TYPED), first_run_bubble_(this) { - for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { - ContentSettingImageView* content_setting_view = - new ContentSettingImageView(static_cast<ContentSettingsType>(i), this, - profile_); - content_setting_views_.push_back(content_setting_view); - content_setting_view->SetVisible(false); + for (size_t i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { + DCHECK_EQ(i, content_setting_decorations_.size()); + ContentSettingsType type = static_cast<ContentSettingsType>(i); + content_setting_decorations_.push_back( + new ContentSettingDecoration(type, this, profile_)); } - AutocompleteTextFieldCell* cell = [field_ cell]; - [cell setContentSettingViewsList:&content_setting_views_]; - registrar_.Add(this, NotificationType::EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED, NotificationService::AllSources()); @@ -144,7 +141,7 @@ void LocationBarViewMac::FocusSearch() { } void LocationBarViewMac::UpdateContentSettingsIcons() { - RefreshContentSettingsViews(); + RefreshContentSettingsDecorations(); [field_ updateCursorAndToolTipRects]; [field_ setNeedsDisplay:YES]; } @@ -181,7 +178,7 @@ void LocationBarViewMac::SaveStateToContents(TabContents* contents) { void LocationBarViewMac::Update(const TabContents* contents, bool should_restore_state) { RefreshPageActionDecorations(); - RefreshContentSettingsViews(); + RefreshContentSettingsDecorations(); // AutocompleteEditView restores state if the tab is non-NULL. edit_view_->Update(should_restore_state ? contents : NULL); OnChanged(); @@ -427,132 +424,15 @@ void LocationBarViewMac::PostNotification(NSString* notification) { object:[NSValue valueWithPointer:this]]; } -void LocationBarViewMac::RefreshContentSettingsViews() { - const TabContents* tab_contents = browser_->GetSelectedTabContents(); - for (ContentSettingViews::iterator it(content_setting_views_.begin()); - it != content_setting_views_.end(); - ++it) { - (*it)->UpdateFromTabContents( - toolbar_model_->input_in_progress() ? NULL : tab_contents); - } -} - -// LocationBarImageView--------------------------------------------------------- - -void LocationBarViewMac::LocationBarImageView::SetImage(NSImage* image) { - image_.reset([image retain]); -} - -void LocationBarViewMac::LocationBarImageView::SetIcon(int resource_id) { - ResourceBundle& rb = ResourceBundle::GetSharedInstance(); - SetImage(rb.GetNSImageNamed(resource_id)); -} - -void LocationBarViewMac::LocationBarImageView::SetLabel(NSString* text, - NSFont* baseFont, - NSColor* color) { - // Create an attributed string for the label, if a label was given. - label_.reset(); - if (text) { - DCHECK(color); - DCHECK(baseFont); - NSFont* font = [NSFont fontWithDescriptor:[baseFont fontDescriptor] - size:[baseFont pointSize] - 2.0]; - NSDictionary* attributes = - [NSDictionary dictionaryWithObjectsAndKeys: - color, NSForegroundColorAttributeName, - font, NSFontAttributeName, - NULL]; - NSAttributedString* attrStr = - [[NSAttributedString alloc] initWithString:text attributes:attributes]; - label_.reset(attrStr); +void LocationBarViewMac::RefreshContentSettingsDecorations() { + const bool input_in_progress = toolbar_model_->input_in_progress(); + const TabContents* tab_contents = + input_in_progress ? NULL : browser_->GetSelectedTabContents(); + for (size_t i = 0; i < content_setting_decorations_.size(); ++i) { + content_setting_decorations_[i]->UpdateFromTabContents(tab_contents); } } -void LocationBarViewMac::LocationBarImageView::SetVisible(bool visible) { - visible_ = visible; -} - -NSSize LocationBarViewMac::LocationBarImageView::GetDefaultImageSize() const { - return NSZeroSize; -} - -NSSize LocationBarViewMac::LocationBarImageView::GetImageSize() const { - NSImage* image = GetImage(); - if (image) - return [image size]; - return GetDefaultImageSize(); -} - -// ContentSettingsImageView----------------------------------------------------- -LocationBarViewMac::ContentSettingImageView::ContentSettingImageView( - ContentSettingsType settings_type, - LocationBarViewMac* owner, - Profile* profile) - : content_setting_image_model_( - ContentSettingImageModel::CreateContentSettingImageModel( - settings_type)), - owner_(owner), - profile_(profile) { -} - -LocationBarViewMac::ContentSettingImageView::~ContentSettingImageView() {} - -void LocationBarViewMac::ContentSettingImageView::OnMousePressed(NSRect bounds) -{ - // Get host. This should be shared shared on linux/win/osx medium-term. - TabContents* tabContents = - BrowserList::GetLastActive()->GetSelectedTabContents(); - if (!tabContents) - return; - GURL url = tabContents->GetURL(); - std::wstring displayHost; - net::AppendFormattedHost( - url, - UTF8ToWide(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)), - &displayHost, NULL, NULL); - - // Transform mouse coordinates to screen space. - AutocompleteTextField* textField = owner_->GetAutocompleteTextField(); - NSWindow* window = [textField window]; - bounds = [textField convertRect:bounds toView:nil]; - NSPoint anchor = NSMakePoint(NSMidX(bounds) + 1, NSMinY(bounds)); - anchor = [window convertBaseToScreen:anchor]; - - // Open bubble. - ContentSettingBubbleModel* model = - ContentSettingBubbleModel::CreateContentSettingBubbleModel( - tabContents, profile_, - content_setting_image_model_->get_content_settings_type()); - [ContentBlockedBubbleController showForModel:model - parentWindow:window - anchoredAt:anchor]; -} - -NSString* LocationBarViewMac::ContentSettingImageView::GetToolTip() { - return tooltip_.get(); -} - -void LocationBarViewMac::ContentSettingImageView::UpdateFromTabContents( - const TabContents* tab_contents) { - content_setting_image_model_->UpdateFromTabContents(tab_contents); - if (content_setting_image_model_->is_visible()) { - // TODO(thakis): We should use pdfs for these icons on OSX. - // http://crbug.com/35847 - SetIcon(content_setting_image_model_->get_icon()); - SetToolTip(base::SysUTF8ToNSString( - content_setting_image_model_->get_tooltip())); - SetVisible(true); - } else { - SetVisible(false); - } -} - -void LocationBarViewMac::ContentSettingImageView::SetToolTip(NSString* tooltip) -{ - tooltip_.reset([tooltip retain]); -} - void LocationBarViewMac::DeletePageActionDecorations() { // TODO(shess): Deleting these decorations could result in the cell // refering to them before things are laid out again. Meanwhile, at @@ -616,10 +496,13 @@ void LocationBarViewMac::Layout() { [cell addLeftDecoration:ev_bubble_decoration_.get()]; [cell addRightDecoration:star_decoration_.get()]; - // Display order is right to left. + // Note that display order is right to left. for (size_t i = 0; i < page_action_decorations_.size(); ++i) { [cell addRightDecoration:page_action_decorations_[i]]; } + for (size_t i = 0; i < content_setting_decorations_.size(); ++i) { + [cell addRightDecoration:content_setting_decorations_[i]]; + } // By default only the location icon is visible. location_icon_decoration_->SetVisible(true); diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index bcc6f78..5939339 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -872,6 +872,8 @@ 'browser/cocoa/location_bar/autocomplete_text_field_editor.mm', 'browser/cocoa/location_bar/bubble_decoration.h', 'browser/cocoa/location_bar/bubble_decoration.mm', + 'browser/cocoa/location_bar/content_setting_decoration.h', + 'browser/cocoa/location_bar/content_setting_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', |