Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1462)

Unified Diff: chrome/browser/cocoa/infobar_controller.mm

Issue 1127001: Implement the confirm infobar with link for mac.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/cocoa/infobar_controller.h ('k') | chrome/browser/cocoa/infobar_controller_unittest.mm » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/cocoa/infobar_controller.mm
===================================================================
--- chrome/browser/cocoa/infobar_controller.mm (revision 41951)
+++ chrome/browser/cocoa/infobar_controller.mm (working copy)
@@ -36,6 +36,12 @@
// Removes the ok and cancel buttons, and resizes the textfield to use the
// space.
- (void)removeButtons;
+
+// Sets the info bar message to the specified |message|, with a hypertext
+// style link. |link| will be inserted into message at |link_offset|.
+-(void)setLabelToMessage:(NSString*)message
+ withLink:(NSString*)link
+ atOffset:(NSUInteger)link_offset;
@end
@implementation InfoBarController
@@ -179,6 +185,54 @@
[self cleanUpAfterAnimation:YES];
}
+-(void)setLabelToMessage:(NSString*)message
+ withLink:(NSString*)link
+ atOffset:(NSUInteger)link_offset {
+ // Create an attributes dictionary for the entire message. We have
+ // to expicitly set the font the control's font. We also override
+ // the cursor to give us the normal cursor rather than the text
+ // insertion cursor.
+ NSMutableDictionary* linkAttributes =
+ [NSMutableDictionary dictionaryWithObject:[NSCursor arrowCursor]
+ forKey:NSCursorAttributeName];
+ [linkAttributes setObject:[label_ font]
+ forKey:NSFontAttributeName];
+
+ // Create the attributed string for the main message text.
+ NSMutableAttributedString* infoText =
+ [[[NSMutableAttributedString alloc]
+ initWithString:message] autorelease];
+ [infoText addAttributes:linkAttributes
+ range:NSMakeRange(0, [infoText length])];
+
+ // Add additional attributes to style the link text appropriately as
+ // well as linkify it. We use an empty string for the NSLink
+ // attribute because the actual object we pass doesn't matter, but
+ // it cannot be nil.
+ [linkAttributes setObject:[NSColor blueColor]
+ forKey:NSForegroundColorAttributeName];
+ [linkAttributes setObject:[NSNumber numberWithBool:YES]
+ forKey:NSUnderlineStyleAttributeName];
+ [linkAttributes setObject:[NSCursor pointingHandCursor]
+ forKey:NSCursorAttributeName];
+ [linkAttributes setObject:[NSString string] // dummy value
+ forKey:NSLinkAttributeName];
+
+ // Insert the link text into the string at the appropriate offset.
+ NSAttributedString* attributed_string =
+ [[[NSAttributedString alloc] initWithString:link
+ attributes:linkAttributes] autorelease];
+ [infoText insertAttributedString:attributed_string
+ atIndex:link_offset];
+
+ // Update the label view with the new text. The view must be
+ // selectable and allow editing text attributes for the
+ // linkification to work correctly.
+ [label_ setAllowsEditingTextAttributes:YES];
+ [label_ setSelectable:YES];
+ [label_ setAttributedStringValue:infoText];
+}
+
@end
@@ -224,50 +278,9 @@
DCHECK(delegate);
size_t offset = std::wstring::npos;
std::wstring message = delegate->GetMessageTextWithOffset(&offset);
-
- // Create an attributes dictionary for the entire message. We have
- // to expicitly set the font the control's font. We also override
- // the cursor to give us the normal cursor rather than the text
- // insertion cursor.
- NSMutableDictionary* linkAttributes =
- [NSMutableDictionary dictionaryWithObject:[NSCursor arrowCursor]
- forKey:NSCursorAttributeName];
- [linkAttributes setObject:[label_ font]
- forKey:NSFontAttributeName];
-
- // Create the attributed string for the main message text.
- NSMutableAttributedString* infoText =
- [[[NSMutableAttributedString alloc]
- initWithString:base::SysWideToNSString(message)] autorelease];
- [infoText addAttributes:linkAttributes
- range:NSMakeRange(0, [infoText length])];
-
- // Add additional attributes to style the link text appropriately as
- // well as linkify it. We use an empty string for the NSLink
- // attribute because the actual object we pass doesn't matter, but
- // it cannot be nil.
- [linkAttributes setObject:[NSColor blueColor]
- forKey:NSForegroundColorAttributeName];
- [linkAttributes setObject:[NSNumber numberWithBool:YES]
- forKey:NSUnderlineStyleAttributeName];
- [linkAttributes setObject:[NSCursor pointingHandCursor]
- forKey:NSCursorAttributeName];
- [linkAttributes setObject:[NSString string] // dummy value
- forKey:NSLinkAttributeName];
-
- // Insert the link text into the string at the appropriate offset.
- [infoText insertAttributedString:
- [[[NSAttributedString alloc]
- initWithString:base::SysWideToNSString(delegate->GetLinkText())
- attributes:linkAttributes] autorelease]
- atIndex:offset];
-
- // Update the label view with the new text. The view must be
- // selectable and allow editing text attributes for the
- // linkification to work correctly.
- [label_ setAllowsEditingTextAttributes:YES];
- [label_ setSelectable:YES];
- [label_ setAttributedStringValue:infoText];
+ [self setLabelToMessage:base::SysWideToNSString(message)
+ withLink:base::SysWideToNSString(delegate->GetLinkText())
+ atOffset:offset];
}
// Called when someone clicks on the link in the infobar. This method
@@ -372,10 +385,36 @@
frame.size.width = rightEdge - NSMinX(frame);
[label_ setFrame:frame];
- // Set the text.
- [label_ setStringValue:base::SysWideToNSString(delegate->GetMessageText())];
+ // Set the text and link.
+ NSString* message = base::SysWideToNSString(delegate->GetMessageText());
+ std::wstring link = delegate->GetLinkText();
+ if (link.empty()) {
+ // Simple case: no link, so just set the message directly.
+ [label_ setStringValue:message];
+ } else {
+ // Inserting the link unintentionally causes the text to behave slightly
+ // different result to the simple case above: text is truncated on word
+ // boundaries if needed, rather than elided with ellipses.
+
+ // Add spacing between the label and the link.
+ message = [message stringByAppendingString:@" "];
+ [self setLabelToMessage:message
+ withLink:base::SysWideToNSString(link)
+ atOffset:[message length]];
+ }
}
+// Called when someone clicks on the link in the infobar. This method
+// is called by the InfobarTextField on its delegate (the
+// LinkInfoBarController).
+- (void)linkClicked {
+ WindowOpenDisposition disposition =
+ event_utils::WindowOpenDispositionFromNSEvent([NSApp currentEvent]);
+ if (delegate_ &&
+ delegate_->AsConfirmInfoBarDelegate()->LinkClicked(disposition))
+ [self removeInfoBar];
+}
+
@end
« no previous file with comments | « chrome/browser/cocoa/infobar_controller.h ('k') | chrome/browser/cocoa/infobar_controller_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698