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

Unified Diff: ui/views/cocoa/bridged_content_view.mm

Issue 2348143003: MacViews: Implement Force Touch/Mac dictionary lookup for Textfields. (Closed)
Patch Set: Address review. Created 4 years, 3 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
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];
+ 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]);
+
+ NSRange range = attribute.range.ToNSRange();
+
+ if (attribute.font.GetNativeFont()) {
+ [attrs setObject:attribute.font.GetNativeFont()
+ forKey:NSFontAttributeName];
+ } else {
+ [attrs removeObjectForKey:NSFontAttributeName];
+ }
+
+ if (attribute.underline) {
karandeepb 2016/09/22 08:17:40 FYI: Cocoa seems to ignore the underline and strik
+ [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);
karandeepb 2016/09/22 08:17:39 Saw https://codereview.chromium.org/2148893003. So
+ 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

Powered by Google App Engine
This is Rietveld 408576698