Chromium Code Reviews| Index: ui/views/cocoa/bridged_content_view.mm |
| diff --git a/ui/views/cocoa/bridged_content_view.mm b/ui/views/cocoa/bridged_content_view.mm |
| index 1623e32b8ede82ff2b578ad5500010dca09f991c..02fe7ce5718d8686c1ec29e297b872bc3dc134fd 100644 |
| --- a/ui/views/cocoa/bridged_content_view.mm |
| +++ b/ui/views/cocoa/bridged_content_view.mm |
| @@ -17,13 +17,16 @@ |
| #include "ui/base/ime/text_input_client.h" |
| #include "ui/compositor/canvas_painter.h" |
| #import "ui/events/cocoa/cocoa_event_utils.h" |
| +#include "ui/events/event_utils.h" |
| #include "ui/events/keycodes/dom/dom_code.h" |
| #import "ui/events/keycodes/keyboard_code_conversion_mac.h" |
| #include "ui/gfx/canvas_paint_mac.h" |
| +#include "ui/gfx/decorated_text.h" |
| #include "ui/gfx/geometry/rect.h" |
| #import "ui/gfx/mac/coordinate_conversion.h" |
| #include "ui/gfx/path.h" |
| #import "ui/gfx/path_mac.h" |
| +#include "ui/gfx/render_text.h" |
| #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
| #import "ui/views/cocoa/bridged_native_widget.h" |
| #import "ui/views/cocoa/drag_drop_client_mac.h" |
| @@ -207,6 +210,56 @@ bool IsTextRTL(const ui::TextInputClient* client) { |
| ui::KeyboardCodeFromNSEvent(event), ui::EF_NONE); |
| } |
| +NSAttributedString* GetAttributedString( |
| + const gfx::DecoratedText& decorated_text) { |
| + NSMutableAttributedString* str = [[NSMutableAttributedString alloc] init]; |
|
tapted
2016/09/26 02:02:31
Use scoped_nsobject (and str.autorelease() at the
karandeepb
2016/09/26 07:00:08
Done.
|
| + NSMutableDictionary* attrs = [NSMutableDictionary dictionary]; |
| + [str beginEditing]; |
| + [[str mutableString] setString:base::SysUTF16ToNSString(decorated_text.text)]; |
| + |
| + for (const auto& attribute : decorated_text.attributes) { |
| + DCHECK(!attribute.range.is_reversed()); |
| + DCHECK(attribute.range.end() <= [str length]); |
|
tapted
2016/09/26 02:02:31
Is DCHECK_LE a thing?
karandeepb
2016/09/26 07:00:08
Done.
|
| + |
| + NSRange range = attribute.range.ToNSRange(); |
| + |
| + if (attribute.font.GetNativeFont()) { |
| + [attrs setObject:attribute.font.GetNativeFont() |
|
tapted
2016/09/26 02:02:31
Does the Objc2.0 map syntax work -- attrs[NSFontAt
karandeepb
2016/09/26 07:00:08
Done. The map syntax causes compiler warnings thou
|
| + forKey:NSFontAttributeName]; |
| + } else { |
| + [attrs removeObjectForKey:NSFontAttributeName]; |
| + } |
| + |
| + if (attribute.underline) { |
| + [attrs setObject:@(NSUnderlineStyleSingle | NSUnderlinePatternSolid) |
| + forKey:NSUnderlineStyleAttributeName]; |
| + } else { |
| + [attrs removeObjectForKey:NSUnderlineStyleAttributeName]; |
| + } |
| + |
| + if (attribute.strike) { |
| + [attrs setObject:@(NSUnderlineStyleSingle | NSUnderlinePatternSolid) |
| + forKey:NSStrikethroughStyleAttributeName]; |
| + } else { |
| + [attrs removeObjectForKey:NSStrikethroughStyleAttributeName]; |
| + } |
| + |
| + [str setAttributes:attrs range:range]; |
| + |
| + NSFontTraitMask mask = 0; |
| + if (attribute.weight >= gfx::Font::Weight::BOLD) |
| + mask |= NSBoldFontMask; |
| + if (attribute.italic) |
| + mask |= NSItalicFontMask; |
| + |
| + if (mask) |
| + [str applyFontTraits:mask range:range]; |
| + } |
| + |
| + [str endEditing]; |
| + return [str autorelease]; |
| +} |
| + |
| } // namespace |
| @interface BridgedContentView () |
| @@ -740,6 +793,36 @@ - (void)scrollWheel:(NSEvent*)theEvent { |
| hostedView_->GetWidget()->OnMouseEvent(&event); |
| } |
| +- (void)quickLookWithEvent:(NSEvent*)theEvent { |
| + if (!hostedView_) |
| + return; |
| + |
| + const gfx::Point locationInContent = ui::EventLocationFromNative(theEvent); |
| + views::View* target = hostedView_->GetEventHandlerForPoint(locationInContent); |
| + if (!target) |
| + return; |
| + |
| + gfx::RenderText* renderText = target->GetRenderText(); |
| + if (!renderText) |
| + return; |
| + |
| + gfx::Point locationInTarget = locationInContent; |
| + views::View::ConvertPointToTarget(hostedView_, target, &locationInTarget); |
| + gfx::DecoratedText decorated_word; |
| + gfx::Point baselinePoint; |
| + if (!renderText->GetDecoratedWordAtPoint(locationInTarget, &decorated_word, |
| + &baselinePoint)) { |
| + return; |
| + } |
| + |
| + // Convert |baselinePoint| to the coordinate system of |hostedView_|. |
| + views::View::ConvertPointToTarget(target, hostedView_, &baselinePoint); |
| + NSPoint baselinePointAppKit = NSMakePoint( |
| + baselinePoint.x(), NSHeight([self frame]) - baselinePoint.y()); |
| + [self showDefinitionForAttributedString:GetAttributedString(decorated_word) |
| + atPoint:baselinePointAppKit]; |
| +} |
| + |
| //////////////////////////////////////////////////////////////////////////////// |
| // NSResponder Action Messages. Keep sorted according NSResponder.h (from the |
| // 10.9 SDK). The list should eventually be complete. Anything not defined will |