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

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

Issue 2348143003: MacViews: Implement Force Touch/Mac dictionary lookup for Textfields. (Closed)
Patch Set: Call EnsureLayout in GetDecoratedTextForRange. Created 4 years, 2 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 e29ea9ec01c90fadc6ff333da92cdd99d04b00e4..137108f8971a54d512959bc93b8c0399bb06e5ce 100644
--- a/ui/views/cocoa/bridged_content_view.mm
+++ b/ui/views/cocoa/bridged_content_view.mm
@@ -7,6 +7,7 @@
#include "base/logging.h"
#import "base/mac/mac_util.h"
#import "base/mac/scoped_nsobject.h"
+#import "base/mac/sdk_forward_declarations.h"
#include "base/strings/sys_string_conversions.h"
#include "skia/ext/skia_utils_mac.h"
#include "ui/base/cocoa/cocoa_base_utils.h"
@@ -17,9 +18,11 @@
#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"
@@ -32,6 +35,7 @@
#include "ui/views/view.h"
#include "ui/views/widget/native_widget_mac.h"
#include "ui/views/widget/widget.h"
+#include "ui/views/word_lookup_client.h"
using views::MenuController;
@@ -207,6 +211,43 @@ bool IsTextRTL(const ui::TextInputClient* client) {
ui::KeyboardCodeFromNSEvent(event), ui::EF_NONE);
}
+NSAttributedString* GetAttributedString(
+ const gfx::DecoratedText& decorated_text) {
+ base::scoped_nsobject<NSMutableAttributedString> str(
+ [[NSMutableAttributedString alloc] init]);
+ [str beginEditing];
+ [[str mutableString] setString:base::SysUTF16ToNSString(decorated_text.text)];
tapted 2016/10/05 06:20:54 I haven't seen this approach in Chrome before - I
karandeepb 2016/10/06 01:07:05 Done.
+
+ for (const auto& attribute : decorated_text.attributes) {
+ DCHECK(!attribute.range.is_reversed());
+ DCHECK_LE(attribute.range.end(), [str length]);
+
+ NSMutableDictionary* attrs = [NSMutableDictionary dictionary];
+ NSRange range = attribute.range.ToNSRange();
+
+ if (attribute.font.GetNativeFont())
+ attrs[NSFontAttributeName] = attribute.font.GetNativeFont();
+
+ // NSFont does not have underline as an attribute. Hence handle it
+ // separately.
+ const bool underline = attribute.font.GetStyle() & gfx::Font::UNDERLINE;
+ if (underline) {
+ attrs[NSUnderlineStyleAttributeName] =
+ @(NSUnderlineStyleSingle | NSUnderlinePatternSolid);
tapted 2016/10/05 06:20:55 How about, at the top of the method, NSValue* c
karandeepb 2016/10/06 01:07:05 Done.
+ }
+
+ if (attribute.strike) {
+ attrs[NSStrikethroughStyleAttributeName] =
+ @(NSUnderlineStyleSingle | NSUnderlinePatternSolid);
+ }
+
+ [str setAttributes:attrs range:range];
+ }
+
+ [str endEditing];
+ return str.autorelease();
+}
+
} // namespace
@interface BridgedContentView ()
@@ -740,6 +781,36 @@ - (void)scrollWheel:(NSEvent*)theEvent {
hostedView_->GetWidget()->OnScrollEvent(&event);
}
+- (void)quickLookWithEvent:(NSEvent*)theEvent {
+ if (!hostedView_)
+ return;
+
+ const gfx::Point locationInContent = ui::EventLocationFromNative(theEvent);
+ views::View* target = hostedView_->GetEventHandlerForPoint(locationInContent);
+ if (!target)
+ return;
+
+ views::WordLookupClient* word_lookup_client = target->GetWordLookupClient();
tapted 2016/10/05 06:20:54 word_lookup_client -> wordLookupClient
karandeepb 2016/10/06 01:07:05 Done.
+ if (!word_lookup_client)
+ return;
+
+ gfx::Point locationInTarget = locationInContent;
+ views::View::ConvertPointToTarget(hostedView_, target, &locationInTarget);
+ gfx::DecoratedText decorated_word;
tapted 2016/10/05 06:20:55 decorated_word -> decoratedWord
karandeepb 2016/10/06 01:07:05 Done.
+ gfx::Point baselinePoint;
+ if (!word_lookup_client->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