diff options
9 files changed, 99 insertions, 15 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_mac.h b/chrome/browser/autocomplete/autocomplete_edit_view_mac.h index 56d7cee..c1ff0c0 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_mac.h +++ b/chrome/browser/autocomplete/autocomplete_edit_view_mac.h @@ -96,6 +96,7 @@ class AutocompleteEditViewMac : public AutocompleteEditView, virtual bool CanPasteAndGo(); virtual int GetPasteActionStringId(); virtual void OnPasteAndGo(); + virtual void OnSecurityIconClicked(); // Helper functions for use from AutocompleteEditHelper Objective-C // class. diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm b/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm index 76fbe8a..c736c8f 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm +++ b/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm @@ -16,8 +16,10 @@ #include "chrome/browser/autocomplete/autocomplete_edit.h" #include "chrome/browser/autocomplete/autocomplete_popup_model.h" #include "chrome/browser/autocomplete/autocomplete_popup_view_mac.h" +#include "chrome/browser/browser_list.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/cocoa/event_utils.h" +#include "chrome/browser/tab_contents/navigation_entry.h" #include "chrome/browser/tab_contents/tab_contents.h" #include "grit/generated_resources.h" @@ -630,6 +632,16 @@ bool AutocompleteEditViewMac::CanPasteAndGo() { model_->CanPasteAndGo(GetClipboardText(g_browser_process->clipboard())); } +void AutocompleteEditViewMac::OnSecurityIconClicked() { + TabContents* tab = BrowserList::GetLastActive()->GetSelectedTabContents(); + NavigationEntry* nav_entry = tab->controller().GetActiveEntry(); + if (!nav_entry) { + NOTREACHED(); + return; + } + tab->ShowPageInfo(nav_entry->url(), nav_entry->ssl(), true); +} + int AutocompleteEditViewMac::GetPasteActionStringId() { DCHECK(CanPasteAndGo()); diff --git a/chrome/browser/cocoa/autocomplete_text_field.h b/chrome/browser/cocoa/autocomplete_text_field.h index f850a27..be88561 100644 --- a/chrome/browser/cocoa/autocomplete_text_field.h +++ b/chrome/browser/cocoa/autocomplete_text_field.h @@ -49,6 +49,10 @@ class AutocompleteTextFieldObserver { // Called when the user initiates a "paste and go" or "paste and // search" into |field_|. virtual void OnPasteAndGo() = 0; + + // Called when the user clicks the hint icon (i.e. the security icon) in the + // location bar. + virtual void OnSecurityIconClicked() = 0; }; @interface AutocompleteTextField : NSTextField { diff --git a/chrome/browser/cocoa/autocomplete_text_field.mm b/chrome/browser/cocoa/autocomplete_text_field.mm index 1f2c23f4..60a7a56 100644 --- a/chrome/browser/cocoa/autocomplete_text_field.mm +++ b/chrome/browser/cocoa/autocomplete_text_field.mm @@ -94,7 +94,8 @@ const NSPoint locationInWindow = [theEvent locationInWindow]; const NSPoint location = [self convertPoint:locationInWindow fromView:nil]; - const NSRect textFrame = [[self cell] textFrameForFrame:[self bounds]]; + AutocompleteTextFieldCell* cell = [self cell]; + const NSRect textFrame([cell textFrameForFrame:[self bounds]]); // A version of the textFrame which extends across the field's // entire width. @@ -132,6 +133,14 @@ return; } + // Check to see if the user clicked the hint icon in the cell. If so, we need + // to display the page info window. + const NSRect hintIconFrame = [cell hintImageFrameForFrame:[self bounds]]; + if (NSMouseInRect(location, hintIconFrame, [self isFlipped])) { + observer_->OnSecurityIconClicked(); + return; + } + NSText* editor = [self currentEditor]; // We should only be here if we accepted first-responder status and diff --git a/chrome/browser/cocoa/autocomplete_text_field_cell.h b/chrome/browser/cocoa/autocomplete_text_field_cell.h index b8d931f..47fbb52 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_cell.h +++ b/chrome/browser/cocoa/autocomplete_text_field_cell.h @@ -68,6 +68,9 @@ // corresponds to the frame with our added decorations sliced off. - (NSRect)textFrameForFrame:(NSRect)cellFrame; +// Return the portion of the cell to use for displaing the |hintIcon_|. +- (NSRect)hintImageFrameForFrame:(NSRect)cellFrame; + @end // Internal methods here exposed for unit testing. diff --git a/chrome/browser/cocoa/autocomplete_text_field_cell.mm b/chrome/browser/cocoa/autocomplete_text_field_cell.mm index 585a1dd..10ae6f4 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_cell.mm +++ b/chrome/browser/cocoa/autocomplete_text_field_cell.mm @@ -254,6 +254,25 @@ const NSInteger kHintIconHorizontalPad = 5; return [super drawingRectForBounds:[self textFrameForFrame:theRect]]; } +- (NSRect)hintImageFrameForFrame:(NSRect)cellFrame { + // We'll draw the entire image + const NSSize imageRect([hintIcon_ size]); + + // Move the rect that we're drawing into to the far right + cellFrame.origin.x += cellFrame.size.width - imageRect.width; + // Add back the padding + cellFrame.origin.x -= kHintIconHorizontalPad; + + // Center the image vertically in the frame + cellFrame.origin.y += + floor((cellFrame.size.height - imageRect.height) / 2); + + // Set the drawing size to the image size + cellFrame.size = imageRect; + + return cellFrame; +} + - (void)drawHintWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { DCHECK(hintString_); @@ -296,25 +315,14 @@ const NSInteger kHintIconHorizontalPad = 5; } - (void)drawHintIconWithFrame:(NSRect)cellFrame - inView:(NSView*)controlView { + inView:(NSView*)controlView { // We'll draw the entire image NSRect imageRect = NSZeroRect; imageRect.size = [hintIcon_ size]; - - // Move the rect that we're drawing into to the far right - cellFrame.origin.x += cellFrame.size.width - imageRect.size.width; - // Add back the padding - cellFrame.origin.x -= kHintIconHorizontalPad; - - // Center the image vertically in the frame - cellFrame.origin.y += - floor((cellFrame.size.height - imageRect.size.height) / 2); - - // Set the drawing size to the image size - cellFrame.size = imageRect.size; + const NSRect hintFrame([self hintImageFrameForFrame:cellFrame]); [hintIcon_ setFlipped:[controlView isFlipped]]; - [hintIcon_ drawInRect:cellFrame + [hintIcon_ drawInRect:hintFrame fromRect:imageRect operation:NSCompositeSourceOver fraction:1.0]; diff --git a/chrome/browser/cocoa/autocomplete_text_field_cell_unittest.mm b/chrome/browser/cocoa/autocomplete_text_field_cell_unittest.mm index 076789f..ef8d415 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_cell_unittest.mm +++ b/chrome/browser/cocoa/autocomplete_text_field_cell_unittest.mm @@ -355,4 +355,38 @@ TEST_F(AutocompleteTextFieldCellTest, DrawingRectForBounds) { EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); } +// Test that the security icon is at the right side of the cell. +TEST_F(AutocompleteTextFieldCellTest, HintImageFrame) { + AutocompleteTextFieldCell* cell = + static_cast<AutocompleteTextFieldCell*>([view_ cell]); + const NSRect bounds([view_ bounds]); + scoped_nsobject<NSImage> hintIcon( + [[NSImage alloc] initWithSize:NSMakeSize(20, 20)]); + + NSRect iconRect = [cell hintImageFrameForFrame:bounds]; + EXPECT_TRUE(NSIsEmptyRect(iconRect)); + + // Save the starting frame for after clear. + const NSRect originalIconRect(iconRect); + + [cell setHintIcon:hintIcon]; + iconRect = [cell hintImageFrameForFrame:bounds]; + EXPECT_FALSE(NSIsEmptyRect(iconRect)); + EXPECT_TRUE(NSContainsRect(bounds, iconRect)); + + // Make sure we are right of the |drawingRect|. + NSRect drawingRect = [cell drawingRectForBounds:bounds]; + EXPECT_LE(NSMaxX(drawingRect), NSMinX(iconRect)); + + // Make sure we're right of the |textFrame|. + NSRect textFrame = [cell textFrameForFrame:bounds]; + EXPECT_LE(NSMaxX(textFrame), NSMinX(iconRect)); + + // Make sure we clear correctly. + [cell setHintIcon:nil]; + iconRect = [cell hintImageFrameForFrame:bounds]; + EXPECT_TRUE(NSEqualRects(iconRect, originalIconRect)); + EXPECT_TRUE(NSIsEmptyRect(iconRect)); +} + } // namespace diff --git a/chrome/browser/cocoa/autocomplete_text_field_unittest.mm b/chrome/browser/cocoa/autocomplete_text_field_unittest.mm index 41cbe21..d5d1b9f0 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_unittest.mm +++ b/chrome/browser/cocoa/autocomplete_text_field_unittest.mm @@ -517,6 +517,18 @@ TEST_F(AutocompleteTextFieldTest, TripleClickSelectsAll) { EXPECT_EQ(selectedRange.length, [[field_ stringValue] length]); } +TEST_F(AutocompleteTextFieldTest, SecurityIconMouseDown) { + AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; + scoped_nsobject<NSImage> hintIcon( + [[NSImage alloc] initWithSize:NSMakeSize(20, 20)]); + [cell setHintIcon:hintIcon.get()]; + NSRect iconFrame([cell hintImageFrameForFrame:[field_ bounds]]); + NSPoint location(NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame))); + NSEvent* event(Event(field_, location, NSLeftMouseDown, 1)); + EXPECT_CALL(field_observer_, OnSecurityIconClicked()); + [field_ mouseDown:event]; +} + } // namespace @implementation AutocompleteTextFieldTestDelegate diff --git a/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h b/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h index 9aef620..c3a617e 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h +++ b/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h @@ -37,6 +37,7 @@ class MockAutocompleteTextFieldObserver : public AutocompleteTextFieldObserver { MOCK_METHOD0(CanPasteAndGo, bool()); MOCK_METHOD0(GetPasteActionStringId, int()); MOCK_METHOD0(OnPasteAndGo, void()); + MOCK_METHOD0(OnSecurityIconClicked, void()); }; } // namespace |