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

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

Issue 2348143003: MacViews: Implement Force Touch/Mac dictionary lookup for Textfields. (Closed)
Patch Set: Address nits. 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
« no previous file with comments | « ui/views/BUILD.gn ('k') | ui/views/controls/textfield/textfield.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..0bc0dc879e0813c1f108fdca06135deb56ca1558 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,42 @@ 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]
+ initWithString:base::SysUTF16ToNSString(decorated_text.text)]);
+ [str beginEditing];
+
+ NSValue* const line_style =
+ @(NSUnderlineStyleSingle | NSUnderlinePatternSolid);
+
+ 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] = line_style;
+
+ if (attribute.strike)
+ attrs[NSStrikethroughStyleAttributeName] = line_style;
+
+ [str setAttributes:attrs range:range];
+ }
+
+ [str endEditing];
+ return str.autorelease();
+}
+
} // namespace
@interface BridgedContentView ()
@@ -740,6 +780,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* wordLookupClient = target->GetWordLookupClient();
+ if (!wordLookupClient)
+ return;
+
+ gfx::Point locationInTarget = locationInContent;
+ views::View::ConvertPointToTarget(hostedView_, target, &locationInTarget);
+ gfx::DecoratedText decoratedWord;
+ gfx::Point baselinePoint;
+ if (!wordLookupClient->GetDecoratedWordAtPoint(
+ locationInTarget, &decoratedWord, &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(decoratedWord)
+ 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
« no previous file with comments | « ui/views/BUILD.gn ('k') | ui/views/controls/textfield/textfield.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698