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 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
11 #include "skia/ext/skia_utils_mac.h" | 11 #include "skia/ext/skia_utils_mac.h" |
12 #include "ui/base/cocoa/cocoa_base_utils.h" | 12 #include "ui/base/cocoa/cocoa_base_utils.h" |
13 #include "ui/base/dragdrop/drag_drop_types.h" | 13 #include "ui/base/dragdrop/drag_drop_types.h" |
14 #include "ui/base/dragdrop/os_exchange_data_provider_mac.h" | 14 #include "ui/base/dragdrop/os_exchange_data_provider_mac.h" |
15 #include "ui/base/ime/input_method.h" | 15 #include "ui/base/ime/input_method.h" |
16 #include "ui/base/ime/text_edit_commands.h" | 16 #include "ui/base/ime/text_edit_commands.h" |
17 #include "ui/base/ime/text_input_client.h" | 17 #include "ui/base/ime/text_input_client.h" |
18 #include "ui/compositor/canvas_painter.h" | 18 #include "ui/compositor/canvas_painter.h" |
19 #import "ui/events/cocoa/cocoa_event_utils.h" | 19 #import "ui/events/cocoa/cocoa_event_utils.h" |
20 #include "ui/events/event_utils.h" | |
20 #include "ui/events/keycodes/dom/dom_code.h" | 21 #include "ui/events/keycodes/dom/dom_code.h" |
21 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" | 22 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" |
22 #include "ui/gfx/canvas_paint_mac.h" | 23 #include "ui/gfx/canvas_paint_mac.h" |
24 #include "ui/gfx/decorated_text.h" | |
23 #include "ui/gfx/geometry/rect.h" | 25 #include "ui/gfx/geometry/rect.h" |
24 #import "ui/gfx/mac/coordinate_conversion.h" | 26 #import "ui/gfx/mac/coordinate_conversion.h" |
25 #include "ui/gfx/path.h" | 27 #include "ui/gfx/path.h" |
26 #import "ui/gfx/path_mac.h" | 28 #import "ui/gfx/path_mac.h" |
29 #include "ui/gfx/render_text.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" |
35 | 38 |
36 using views::MenuController; | 39 using views::MenuController; |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 DCHECK([event type] == NSKeyDown || [event type] == NSKeyUp); | 203 DCHECK([event type] == NSKeyDown || [event type] == NSKeyUp); |
201 DCHECK_EQ(1u, [[event characters] length]); | 204 DCHECK_EQ(1u, [[event characters] length]); |
202 | 205 |
203 // [NSEvent characters] already considers the pressed key modifiers. Hence | 206 // [NSEvent characters] already considers the pressed key modifiers. Hence |
204 // send ui::EF_NONE as the key modifier to the KeyEvent constructor. | 207 // send ui::EF_NONE as the key modifier to the KeyEvent constructor. |
205 // E.g. For Alt+S, [NSEvent characters] is 'ß' and not 'S'. | 208 // E.g. For Alt+S, [NSEvent characters] is 'ß' and not 'S'. |
206 return ui::KeyEvent([[event characters] characterAtIndex:0], | 209 return ui::KeyEvent([[event characters] characterAtIndex:0], |
207 ui::KeyboardCodeFromNSEvent(event), ui::EF_NONE); | 210 ui::KeyboardCodeFromNSEvent(event), ui::EF_NONE); |
208 } | 211 } |
209 | 212 |
213 NSAttributedString* GetAttributedString( | |
214 const gfx::DecoratedText& decorated_text) { | |
215 NSMutableAttributedString* str = [[NSMutableAttributedString alloc] init]; | |
216 NSMutableDictionary* attrs = [NSMutableDictionary dictionary]; | |
217 [str beginEditing]; | |
218 [[str mutableString] setString:base::SysUTF16ToNSString(decorated_text.text)]; | |
219 | |
220 for (const auto& attribute : decorated_text.attributes) { | |
221 DCHECK(!attribute.range.is_reversed()); | |
222 DCHECK(attribute.range.end() <= [str length]); | |
223 | |
224 NSRange range = attribute.range.ToNSRange(); | |
225 | |
226 if (attribute.font.GetNativeFont()) { | |
227 [attrs setObject:attribute.font.GetNativeFont() | |
228 forKey:NSFontAttributeName]; | |
229 } else { | |
230 [attrs removeObjectForKey:NSFontAttributeName]; | |
231 } | |
232 | |
233 if (attribute.underline) { | |
karandeepb
2016/09/22 08:17:40
FYI: Cocoa seems to ignore the underline and strik
| |
234 [attrs setObject:@(NSUnderlineStyleSingle | NSUnderlinePatternSolid) | |
235 forKey:NSUnderlineStyleAttributeName]; | |
236 } else { | |
237 [attrs removeObjectForKey:NSUnderlineStyleAttributeName]; | |
238 } | |
239 | |
240 if (attribute.strike) { | |
241 [attrs setObject:@(NSUnderlineStyleSingle | NSUnderlinePatternSolid) | |
242 forKey:NSStrikethroughStyleAttributeName]; | |
243 } else { | |
244 [attrs removeObjectForKey:NSStrikethroughStyleAttributeName]; | |
245 } | |
246 | |
247 [str setAttributes:attrs range:range]; | |
248 | |
249 NSFontTraitMask mask = 0; | |
250 if (attribute.weight >= gfx::Font::Weight::BOLD) | |
251 mask |= NSBoldFontMask; | |
252 if (attribute.italic) | |
253 mask |= NSItalicFontMask; | |
254 | |
255 if (mask) | |
256 [str applyFontTraits:mask range:range]; | |
257 } | |
258 | |
259 [str endEditing]; | |
260 return [str autorelease]; | |
261 } | |
262 | |
210 } // namespace | 263 } // namespace |
211 | 264 |
212 @interface BridgedContentView () | 265 @interface BridgedContentView () |
213 | 266 |
214 // Returns the active menu controller corresponding to |hostedView_|, | 267 // Returns the active menu controller corresponding to |hostedView_|, |
215 // nil otherwise. | 268 // nil otherwise. |
216 - (MenuController*)activeMenuController; | 269 - (MenuController*)activeMenuController; |
217 | 270 |
218 // Passes |event| to the InputMethod for dispatch. | 271 // Passes |event| to the InputMethod for dispatch. |
219 - (void)handleKeyEvent:(ui::KeyEvent*)event; | 272 - (void)handleKeyEvent:(ui::KeyEvent*)event; |
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
733 } | 786 } |
734 | 787 |
735 - (void)scrollWheel:(NSEvent*)theEvent { | 788 - (void)scrollWheel:(NSEvent*)theEvent { |
736 if (!hostedView_) | 789 if (!hostedView_) |
737 return; | 790 return; |
738 | 791 |
739 ui::MouseWheelEvent event(theEvent); | 792 ui::MouseWheelEvent event(theEvent); |
740 hostedView_->GetWidget()->OnMouseEvent(&event); | 793 hostedView_->GetWidget()->OnMouseEvent(&event); |
741 } | 794 } |
742 | 795 |
796 - (void)quickLookWithEvent:(NSEvent*)theEvent { | |
797 if (!hostedView_) | |
798 return; | |
799 | |
800 const gfx::Point locationInContent = ui::EventLocationFromNative(theEvent); | |
801 views::View* target = hostedView_->GetEventHandlerForPoint(locationInContent); | |
802 if (!target) | |
803 return; | |
804 | |
805 gfx::RenderText* renderText = target->GetRenderText(); | |
806 if (!renderText) | |
807 return; | |
808 | |
809 gfx::Point locationInTarget = locationInContent; | |
810 views::View::ConvertPointToTarget(hostedView_, target, &locationInTarget); | |
karandeepb
2016/09/22 08:17:39
Saw https://codereview.chromium.org/2148893003. So
| |
811 gfx::DecoratedText decorated_word; | |
812 gfx::Point baselinePoint; | |
813 if (!renderText->GetDecoratedWordAtPoint(locationInTarget, &decorated_word, | |
814 &baselinePoint)) { | |
815 return; | |
816 } | |
817 | |
818 // Convert |baselinePoint| to the coordinate system of |hostedView_|. | |
819 views::View::ConvertPointToTarget(target, hostedView_, &baselinePoint); | |
820 NSPoint baselinePointAppKit = NSMakePoint( | |
821 baselinePoint.x(), NSHeight([self frame]) - baselinePoint.y()); | |
822 [self showDefinitionForAttributedString:GetAttributedString(decorated_word) | |
823 atPoint:baselinePointAppKit]; | |
824 } | |
825 | |
743 //////////////////////////////////////////////////////////////////////////////// | 826 //////////////////////////////////////////////////////////////////////////////// |
744 // NSResponder Action Messages. Keep sorted according NSResponder.h (from the | 827 // NSResponder Action Messages. Keep sorted according NSResponder.h (from the |
745 // 10.9 SDK). The list should eventually be complete. Anything not defined will | 828 // 10.9 SDK). The list should eventually be complete. Anything not defined will |
746 // beep when interpretKeyEvents: would otherwise call it. | 829 // beep when interpretKeyEvents: would otherwise call it. |
747 // TODO(tapted): Make this list complete, except for insert* methods which are | 830 // TODO(tapted): Make this list complete, except for insert* methods which are |
748 // dispatched as regular key events in doCommandBySelector:. | 831 // dispatched as regular key events in doCommandBySelector:. |
749 | 832 |
750 // views::Textfields are single-line only, map Paragraph and Document commands | 833 // views::Textfields are single-line only, map Paragraph and Document commands |
751 // to Line. Also, Up/Down commands correspond to beginning/end of line. | 834 // to Line. Also, Up/Down commands correspond to beginning/end of line. |
752 | 835 |
(...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1356 } | 1439 } |
1357 | 1440 |
1358 return [super accessibilityAttributeValue:attribute]; | 1441 return [super accessibilityAttributeValue:attribute]; |
1359 } | 1442 } |
1360 | 1443 |
1361 - (id)accessibilityHitTest:(NSPoint)point { | 1444 - (id)accessibilityHitTest:(NSPoint)point { |
1362 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; | 1445 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; |
1363 } | 1446 } |
1364 | 1447 |
1365 @end | 1448 @end |
OLD | NEW |