OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ui/views/cocoa/bridged_content_view.h" | 5 #import "ui/views/cocoa/bridged_content_view.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #import "base/mac/mac_util.h" | 8 #import "base/mac/mac_util.h" |
9 #import "base/mac/scoped_nsobject.h" | 9 #import "base/mac/scoped_nsobject.h" |
10 #import "base/mac/sdk_forward_declarations.h" | |
10 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
11 #include "skia/ext/skia_utils_mac.h" | 12 #include "skia/ext/skia_utils_mac.h" |
12 #include "ui/base/cocoa/cocoa_base_utils.h" | 13 #include "ui/base/cocoa/cocoa_base_utils.h" |
13 #include "ui/base/dragdrop/drag_drop_types.h" | 14 #include "ui/base/dragdrop/drag_drop_types.h" |
14 #include "ui/base/dragdrop/os_exchange_data_provider_mac.h" | 15 #include "ui/base/dragdrop/os_exchange_data_provider_mac.h" |
15 #include "ui/base/ime/input_method.h" | 16 #include "ui/base/ime/input_method.h" |
16 #include "ui/base/ime/text_edit_commands.h" | 17 #include "ui/base/ime/text_edit_commands.h" |
17 #include "ui/base/ime/text_input_client.h" | 18 #include "ui/base/ime/text_input_client.h" |
18 #include "ui/compositor/canvas_painter.h" | 19 #include "ui/compositor/canvas_painter.h" |
19 #import "ui/events/cocoa/cocoa_event_utils.h" | 20 #import "ui/events/cocoa/cocoa_event_utils.h" |
21 #include "ui/events/event_utils.h" | |
20 #include "ui/events/keycodes/dom/dom_code.h" | 22 #include "ui/events/keycodes/dom/dom_code.h" |
21 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" | 23 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" |
22 #include "ui/gfx/canvas_paint_mac.h" | 24 #include "ui/gfx/canvas_paint_mac.h" |
25 #include "ui/gfx/decorated_text.h" | |
23 #include "ui/gfx/geometry/rect.h" | 26 #include "ui/gfx/geometry/rect.h" |
24 #import "ui/gfx/mac/coordinate_conversion.h" | 27 #import "ui/gfx/mac/coordinate_conversion.h" |
25 #include "ui/gfx/path.h" | 28 #include "ui/gfx/path.h" |
26 #import "ui/gfx/path_mac.h" | 29 #import "ui/gfx/path_mac.h" |
27 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 30 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
28 #import "ui/views/cocoa/bridged_native_widget.h" | 31 #import "ui/views/cocoa/bridged_native_widget.h" |
29 #import "ui/views/cocoa/drag_drop_client_mac.h" | 32 #import "ui/views/cocoa/drag_drop_client_mac.h" |
30 #include "ui/views/controls/menu/menu_config.h" | 33 #include "ui/views/controls/menu/menu_config.h" |
31 #include "ui/views/controls/menu/menu_controller.h" | 34 #include "ui/views/controls/menu/menu_controller.h" |
32 #include "ui/views/view.h" | 35 #include "ui/views/view.h" |
33 #include "ui/views/widget/native_widget_mac.h" | 36 #include "ui/views/widget/native_widget_mac.h" |
34 #include "ui/views/widget/widget.h" | 37 #include "ui/views/widget/widget.h" |
38 #include "ui/views/word_lookup_client.h" | |
35 | 39 |
36 using views::MenuController; | 40 using views::MenuController; |
37 | 41 |
38 namespace { | 42 namespace { |
39 | 43 |
40 NSString* const kFullKeyboardAccessChangedNotification = | 44 NSString* const kFullKeyboardAccessChangedNotification = |
41 @"com.apple.KeyboardUIModeDidChange"; | 45 @"com.apple.KeyboardUIModeDidChange"; |
42 | 46 |
43 // Returns true if all four corners of |rect| are contained inside |path|. | 47 // Returns true if all four corners of |rect| are contained inside |path|. |
44 bool IsRectInsidePath(NSRect rect, NSBezierPath* path) { | 48 bool IsRectInsidePath(NSRect rect, NSBezierPath* path) { |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 DCHECK([event type] == NSKeyDown || [event type] == NSKeyUp); | 204 DCHECK([event type] == NSKeyDown || [event type] == NSKeyUp); |
201 DCHECK_EQ(1u, [[event characters] length]); | 205 DCHECK_EQ(1u, [[event characters] length]); |
202 | 206 |
203 // [NSEvent characters] already considers the pressed key modifiers. Hence | 207 // [NSEvent characters] already considers the pressed key modifiers. Hence |
204 // send ui::EF_NONE as the key modifier to the KeyEvent constructor. | 208 // send ui::EF_NONE as the key modifier to the KeyEvent constructor. |
205 // E.g. For Alt+S, [NSEvent characters] is 'ß' and not 'S'. | 209 // E.g. For Alt+S, [NSEvent characters] is 'ß' and not 'S'. |
206 return ui::KeyEvent([[event characters] characterAtIndex:0], | 210 return ui::KeyEvent([[event characters] characterAtIndex:0], |
207 ui::KeyboardCodeFromNSEvent(event), ui::EF_NONE); | 211 ui::KeyboardCodeFromNSEvent(event), ui::EF_NONE); |
208 } | 212 } |
209 | 213 |
214 NSAttributedString* GetAttributedString( | |
215 const gfx::DecoratedText& decorated_text) { | |
216 base::scoped_nsobject<NSMutableAttributedString> str( | |
217 [[NSMutableAttributedString alloc] init]); | |
218 [str beginEditing]; | |
219 [[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.
| |
220 | |
221 for (const auto& attribute : decorated_text.attributes) { | |
222 DCHECK(!attribute.range.is_reversed()); | |
223 DCHECK_LE(attribute.range.end(), [str length]); | |
224 | |
225 NSMutableDictionary* attrs = [NSMutableDictionary dictionary]; | |
226 NSRange range = attribute.range.ToNSRange(); | |
227 | |
228 if (attribute.font.GetNativeFont()) | |
229 attrs[NSFontAttributeName] = attribute.font.GetNativeFont(); | |
230 | |
231 // NSFont does not have underline as an attribute. Hence handle it | |
232 // separately. | |
233 const bool underline = attribute.font.GetStyle() & gfx::Font::UNDERLINE; | |
234 if (underline) { | |
235 attrs[NSUnderlineStyleAttributeName] = | |
236 @(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.
| |
237 } | |
238 | |
239 if (attribute.strike) { | |
240 attrs[NSStrikethroughStyleAttributeName] = | |
241 @(NSUnderlineStyleSingle | NSUnderlinePatternSolid); | |
242 } | |
243 | |
244 [str setAttributes:attrs range:range]; | |
245 } | |
246 | |
247 [str endEditing]; | |
248 return str.autorelease(); | |
249 } | |
250 | |
210 } // namespace | 251 } // namespace |
211 | 252 |
212 @interface BridgedContentView () | 253 @interface BridgedContentView () |
213 | 254 |
214 // Returns the active menu controller corresponding to |hostedView_|, | 255 // Returns the active menu controller corresponding to |hostedView_|, |
215 // nil otherwise. | 256 // nil otherwise. |
216 - (MenuController*)activeMenuController; | 257 - (MenuController*)activeMenuController; |
217 | 258 |
218 // Passes |event| to the InputMethod for dispatch. | 259 // Passes |event| to the InputMethod for dispatch. |
219 - (void)handleKeyEvent:(ui::KeyEvent*)event; | 260 - (void)handleKeyEvent:(ui::KeyEvent*)event; |
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
733 } | 774 } |
734 | 775 |
735 - (void)scrollWheel:(NSEvent*)theEvent { | 776 - (void)scrollWheel:(NSEvent*)theEvent { |
736 if (!hostedView_) | 777 if (!hostedView_) |
737 return; | 778 return; |
738 | 779 |
739 ui::ScrollEvent event(theEvent); | 780 ui::ScrollEvent event(theEvent); |
740 hostedView_->GetWidget()->OnScrollEvent(&event); | 781 hostedView_->GetWidget()->OnScrollEvent(&event); |
741 } | 782 } |
742 | 783 |
784 - (void)quickLookWithEvent:(NSEvent*)theEvent { | |
785 if (!hostedView_) | |
786 return; | |
787 | |
788 const gfx::Point locationInContent = ui::EventLocationFromNative(theEvent); | |
789 views::View* target = hostedView_->GetEventHandlerForPoint(locationInContent); | |
790 if (!target) | |
791 return; | |
792 | |
793 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.
| |
794 if (!word_lookup_client) | |
795 return; | |
796 | |
797 gfx::Point locationInTarget = locationInContent; | |
798 views::View::ConvertPointToTarget(hostedView_, target, &locationInTarget); | |
799 gfx::DecoratedText decorated_word; | |
tapted
2016/10/05 06:20:55
decorated_word -> decoratedWord
karandeepb
2016/10/06 01:07:05
Done.
| |
800 gfx::Point baselinePoint; | |
801 if (!word_lookup_client->GetDecoratedWordAtPoint( | |
802 locationInTarget, &decorated_word, &baselinePoint)) { | |
803 return; | |
804 } | |
805 | |
806 // Convert |baselinePoint| to the coordinate system of |hostedView_|. | |
807 views::View::ConvertPointToTarget(target, hostedView_, &baselinePoint); | |
808 NSPoint baselinePointAppKit = NSMakePoint( | |
809 baselinePoint.x(), NSHeight([self frame]) - baselinePoint.y()); | |
810 [self showDefinitionForAttributedString:GetAttributedString(decorated_word) | |
811 atPoint:baselinePointAppKit]; | |
812 } | |
813 | |
743 //////////////////////////////////////////////////////////////////////////////// | 814 //////////////////////////////////////////////////////////////////////////////// |
744 // NSResponder Action Messages. Keep sorted according NSResponder.h (from the | 815 // NSResponder Action Messages. Keep sorted according NSResponder.h (from the |
745 // 10.9 SDK). The list should eventually be complete. Anything not defined will | 816 // 10.9 SDK). The list should eventually be complete. Anything not defined will |
746 // beep when interpretKeyEvents: would otherwise call it. | 817 // beep when interpretKeyEvents: would otherwise call it. |
747 // TODO(tapted): Make this list complete, except for insert* methods which are | 818 // TODO(tapted): Make this list complete, except for insert* methods which are |
748 // dispatched as regular key events in doCommandBySelector:. | 819 // dispatched as regular key events in doCommandBySelector:. |
749 | 820 |
750 // views::Textfields are single-line only, map Paragraph and Document commands | 821 // views::Textfields are single-line only, map Paragraph and Document commands |
751 // to Line. Also, Up/Down commands correspond to beginning/end of line. | 822 // to Line. Also, Up/Down commands correspond to beginning/end of line. |
752 | 823 |
(...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1356 } | 1427 } |
1357 | 1428 |
1358 return [super accessibilityAttributeValue:attribute]; | 1429 return [super accessibilityAttributeValue:attribute]; |
1359 } | 1430 } |
1360 | 1431 |
1361 - (id)accessibilityHitTest:(NSPoint)point { | 1432 - (id)accessibilityHitTest:(NSPoint)point { |
1362 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; | 1433 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; |
1363 } | 1434 } |
1364 | 1435 |
1365 @end | 1436 @end |
OLD | NEW |