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] |
| 218 initWithString:base::SysUTF16ToNSString(decorated_text.text)]); |
| 219 [str beginEditing]; |
| 220 |
| 221 NSValue* const line_style = |
| 222 @(NSUnderlineStyleSingle | NSUnderlinePatternSolid); |
| 223 |
| 224 for (const auto& attribute : decorated_text.attributes) { |
| 225 DCHECK(!attribute.range.is_reversed()); |
| 226 DCHECK_LE(attribute.range.end(), [str length]); |
| 227 |
| 228 NSMutableDictionary* attrs = [NSMutableDictionary dictionary]; |
| 229 NSRange range = attribute.range.ToNSRange(); |
| 230 |
| 231 if (attribute.font.GetNativeFont()) |
| 232 attrs[NSFontAttributeName] = attribute.font.GetNativeFont(); |
| 233 |
| 234 // NSFont does not have underline as an attribute. Hence handle it |
| 235 // separately. |
| 236 const bool underline = attribute.font.GetStyle() & gfx::Font::UNDERLINE; |
| 237 if (underline) |
| 238 attrs[NSUnderlineStyleAttributeName] = line_style; |
| 239 |
| 240 if (attribute.strike) |
| 241 attrs[NSStrikethroughStyleAttributeName] = line_style; |
| 242 |
| 243 [str setAttributes:attrs range:range]; |
| 244 } |
| 245 |
| 246 [str endEditing]; |
| 247 return str.autorelease(); |
| 248 } |
| 249 |
210 } // namespace | 250 } // namespace |
211 | 251 |
212 @interface BridgedContentView () | 252 @interface BridgedContentView () |
213 | 253 |
214 // Returns the active menu controller corresponding to |hostedView_|, | 254 // Returns the active menu controller corresponding to |hostedView_|, |
215 // nil otherwise. | 255 // nil otherwise. |
216 - (MenuController*)activeMenuController; | 256 - (MenuController*)activeMenuController; |
217 | 257 |
218 // Passes |event| to the InputMethod for dispatch. | 258 // Passes |event| to the InputMethod for dispatch. |
219 - (void)handleKeyEvent:(ui::KeyEvent*)event; | 259 - (void)handleKeyEvent:(ui::KeyEvent*)event; |
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
733 } | 773 } |
734 | 774 |
735 - (void)scrollWheel:(NSEvent*)theEvent { | 775 - (void)scrollWheel:(NSEvent*)theEvent { |
736 if (!hostedView_) | 776 if (!hostedView_) |
737 return; | 777 return; |
738 | 778 |
739 ui::ScrollEvent event(theEvent); | 779 ui::ScrollEvent event(theEvent); |
740 hostedView_->GetWidget()->OnScrollEvent(&event); | 780 hostedView_->GetWidget()->OnScrollEvent(&event); |
741 } | 781 } |
742 | 782 |
| 783 - (void)quickLookWithEvent:(NSEvent*)theEvent { |
| 784 if (!hostedView_) |
| 785 return; |
| 786 |
| 787 const gfx::Point locationInContent = ui::EventLocationFromNative(theEvent); |
| 788 views::View* target = hostedView_->GetEventHandlerForPoint(locationInContent); |
| 789 if (!target) |
| 790 return; |
| 791 |
| 792 views::WordLookupClient* wordLookupClient = target->GetWordLookupClient(); |
| 793 if (!wordLookupClient) |
| 794 return; |
| 795 |
| 796 gfx::Point locationInTarget = locationInContent; |
| 797 views::View::ConvertPointToTarget(hostedView_, target, &locationInTarget); |
| 798 gfx::DecoratedText decoratedWord; |
| 799 gfx::Point baselinePoint; |
| 800 if (!wordLookupClient->GetDecoratedWordAtPoint( |
| 801 locationInTarget, &decoratedWord, &baselinePoint)) { |
| 802 return; |
| 803 } |
| 804 |
| 805 // Convert |baselinePoint| to the coordinate system of |hostedView_|. |
| 806 views::View::ConvertPointToTarget(target, hostedView_, &baselinePoint); |
| 807 NSPoint baselinePointAppKit = NSMakePoint( |
| 808 baselinePoint.x(), NSHeight([self frame]) - baselinePoint.y()); |
| 809 [self showDefinitionForAttributedString:GetAttributedString(decoratedWord) |
| 810 atPoint:baselinePointAppKit]; |
| 811 } |
| 812 |
743 //////////////////////////////////////////////////////////////////////////////// | 813 //////////////////////////////////////////////////////////////////////////////// |
744 // NSResponder Action Messages. Keep sorted according NSResponder.h (from the | 814 // NSResponder Action Messages. Keep sorted according NSResponder.h (from the |
745 // 10.9 SDK). The list should eventually be complete. Anything not defined will | 815 // 10.9 SDK). The list should eventually be complete. Anything not defined will |
746 // beep when interpretKeyEvents: would otherwise call it. | 816 // beep when interpretKeyEvents: would otherwise call it. |
747 // TODO(tapted): Make this list complete, except for insert* methods which are | 817 // TODO(tapted): Make this list complete, except for insert* methods which are |
748 // dispatched as regular key events in doCommandBySelector:. | 818 // dispatched as regular key events in doCommandBySelector:. |
749 | 819 |
750 // views::Textfields are single-line only, map Paragraph and Document commands | 820 // views::Textfields are single-line only, map Paragraph and Document commands |
751 // to Line. Also, Up/Down commands correspond to beginning/end of line. | 821 // to Line. Also, Up/Down commands correspond to beginning/end of line. |
752 | 822 |
(...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1356 } | 1426 } |
1357 | 1427 |
1358 return [super accessibilityAttributeValue:attribute]; | 1428 return [super accessibilityAttributeValue:attribute]; |
1359 } | 1429 } |
1360 | 1430 |
1361 - (id)accessibilityHitTest:(NSPoint)point { | 1431 - (id)accessibilityHitTest:(NSPoint)point { |
1362 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; | 1432 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; |
1363 } | 1433 } |
1364 | 1434 |
1365 @end | 1435 @end |
OLD | NEW |