Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <Cocoa/Cocoa.h> | |
| 6 | |
| 7 #include "ui/base/events.h" | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #import "ui/base/keycodes/keyboard_code_conversion_mac.h" | |
| 11 #include "ui/gfx/point.h" | |
| 12 | |
| 13 namespace ui { | |
| 14 | |
| 15 EventType EventTypeFromNative(const base::NativeEvent& native_event) { | |
| 16 NSEventType native_type = [native_event type]; | |
| 17 switch (native_type) { | |
| 18 case NSLeftMouseDown: | |
| 19 case NSRightMouseDown: | |
| 20 case NSOtherMouseDown: | |
| 21 return ET_MOUSE_PRESSED; | |
| 22 | |
| 23 case NSLeftMouseUp: | |
| 24 case NSRightMouseUp: | |
| 25 case NSOtherMouseUp: | |
| 26 return ET_MOUSE_RELEASED; | |
| 27 | |
| 28 case NSMouseMoved: | |
| 29 return ET_MOUSE_MOVED; | |
| 30 | |
| 31 case NSLeftMouseDragged: | |
| 32 case NSRightMouseDragged: | |
| 33 case NSOtherMouseDragged: | |
| 34 return ET_MOUSE_DRAGGED; | |
| 35 | |
| 36 case NSMouseEntered: | |
| 37 return ET_MOUSE_ENTERED; | |
| 38 | |
| 39 case NSMouseExited: | |
| 40 return ET_MOUSE_EXITED; | |
| 41 | |
| 42 case NSKeyDown: | |
| 43 return ET_KEY_PRESSED; | |
| 44 | |
| 45 case NSKeyUp: | |
| 46 return ET_KEY_RELEASED; | |
| 47 | |
| 48 case NSFlagsChanged: | |
| 49 return ET_KEY_PRESSED; | |
| 50 | |
| 51 case NSScrollWheel: | |
| 52 return ET_MOUSEWHEEL; | |
| 53 | |
| 54 case NSAppKitDefined: | |
| 55 case NSSystemDefined: | |
| 56 case NSApplicationDefined: | |
| 57 case NSPeriodic: | |
| 58 case NSCursorUpdate: | |
| 59 case NSTabletPoint: | |
| 60 case NSTabletProximity: | |
| 61 default: | |
| 62 return ET_UNKNOWN; | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 int EventFlagsFromNative(const base::NativeEvent& native_event) { | |
| 67 int event_flags = 0; | |
| 68 NSUInteger modifiers = [native_event modifierFlags]; | |
| 69 | |
| 70 if (modifiers & NSAlphaShiftKeyMask) | |
| 71 event_flags = event_flags | EF_CAPS_LOCK_DOWN; | |
| 72 | |
| 73 if (modifiers & NSShiftKeyMask) | |
| 74 event_flags = event_flags | EF_SHIFT_DOWN; | |
| 75 | |
| 76 if (modifiers & NSControlKeyMask) | |
| 77 event_flags = event_flags | EF_CONTROL_DOWN; | |
| 78 | |
| 79 if (modifiers & NSAlternateKeyMask) | |
| 80 event_flags = event_flags | EF_ALT_DOWN; | |
| 81 | |
| 82 if (modifiers & NSCommandKeyMask) | |
| 83 event_flags = event_flags | EF_COMMAND_DOWN; | |
| 84 | |
| 85 NSEventType type = [native_event type]; | |
| 86 | |
| 87 if (type == NSLeftMouseDown || | |
| 88 type == NSLeftMouseUp || | |
| 89 type == NSLeftMouseDragged) { | |
| 90 event_flags = event_flags | EF_LEFT_BUTTON_DOWN; | |
|
Nico
2011/12/19 20:54:49
mouse up gets "LEFT_BUTTON_DOWN"? o_O
dhollowa
2011/12/19 23:19:11
I agree this is a bad name for the enum, but is th
| |
| 91 } | |
| 92 | |
| 93 if (type == NSRightMouseDown || | |
| 94 type == NSRightMouseUp || | |
| 95 type == NSRightMouseDragged) { | |
| 96 event_flags = event_flags | EF_RIGHT_BUTTON_DOWN; | |
| 97 } | |
| 98 | |
| 99 if (type == NSOtherMouseDown || | |
| 100 type == NSOtherMouseUp || | |
| 101 type == NSOtherMouseDragged) { | |
| 102 event_flags = event_flags | EF_MIDDLE_BUTTON_DOWN; | |
| 103 } | |
| 104 | |
| 105 return event_flags; | |
| 106 } | |
| 107 | |
| 108 gfx::Point EventLocationFromNative(const base::NativeEvent& native_event) { | |
|
Nico
2011/12/19 20:54:49
Can you (in a different CL) add a comment to this
dhollowa
2011/12/19 23:19:11
I added it in this CL.
On 2011/12/19 20:54:49, Ni
| |
| 109 NSWindow* window = [native_event window]; | |
| 110 NSPoint location = [native_event locationInWindow]; | |
| 111 | |
| 112 // Convert |location| to be relative to coordinate system of |contentView|. | |
| 113 // Note: this assumes that ui::Event coordinates are rooted in the top-level | |
| 114 // view (with flipped coordinates). A more general (but costly) approach | |
| 115 // would be to hit-test the view of the event and use the found view's | |
| 116 // coordinate system. Currently there is no need for this generality, and | |
| 117 // speed is preferred. Flipped views are not suppported. | |
| 118 DCHECK([[window contentView] isFlipped] == NO); | |
| 119 location = [[window contentView] convertPoint:location fromView:nil]; | |
| 120 location.y = [[window contentView] bounds].size.height - location.y; | |
| 121 | |
| 122 return gfx::Point(NSPointToCGPoint(location)); | |
| 123 } | |
| 124 | |
| 125 KeyboardCode KeyboardCodeFromNative(const base::NativeEvent& native_event) { | |
| 126 return ui::KeyboardCodeFromNSEvent(native_event); | |
| 127 } | |
| 128 | |
| 129 bool IsMouseEvent(const base::NativeEvent& native_event) { | |
|
Nico
2011/12/19 20:54:49
Huh, this function is likely the same on all platf
dhollowa
2011/12/19 23:19:11
Other platforms are able to avoid two-stage tests.
Nico
2011/12/19 23:30:28
My hunch is that this is not performance-sensitive
| |
| 130 EventType type = EventTypeFromNative(native_event); | |
| 131 return type == ET_MOUSE_PRESSED || | |
| 132 type == ET_MOUSE_DRAGGED || | |
| 133 type == ET_MOUSE_RELEASED || | |
| 134 type == ET_MOUSE_MOVED || | |
| 135 type == ET_MOUSE_ENTERED || | |
| 136 type == ET_MOUSE_EXITED; | |
| 137 } | |
| 138 | |
| 139 int GetMouseWheelOffset(const base::NativeEvent& native_event) { | |
|
Nico
2011/12/19 20:54:49
blech…can you change this function to return x/y i
dhollowa
2011/12/19 23:19:11
I've added it to my list. This is very much a wor
| |
| 140 // TODO(dhollowa): Come back to this once comparisons can be made with other | |
| 141 // platforms. | |
| 142 return [native_event deltaY]; | |
| 143 } | |
| 144 | |
| 145 int GetTouchId(const base::NativeEvent& native_event) { | |
| 146 // Touch is currently unsupported. | |
| 147 return 0; | |
| 148 } | |
| 149 | |
| 150 float GetTouchRadiusX(const base::NativeEvent& native_event) { | |
| 151 // Touch is currently unsupported. | |
| 152 return 1.0; | |
| 153 } | |
| 154 | |
| 155 float GetTouchRadiusY(const base::NativeEvent& native_event) { | |
| 156 // Touch is currently unsupported. | |
| 157 return 1.0; | |
| 158 } | |
| 159 | |
| 160 float GetTouchAngle(const base::NativeEvent& native_event) { | |
| 161 // Touch is currently unsupported. | |
| 162 return 0.0; | |
| 163 } | |
| 164 | |
| 165 float GetTouchForce(const base::NativeEvent& native_event) { | |
| 166 // Touch is currently unsupported. | |
| 167 return 0.0; | |
| 168 } | |
| 169 | |
| 170 base::NativeEvent CreateNoopEvent() { | |
| 171 return [NSEvent otherEventWithType:NSApplicationDefined | |
| 172 location:NSZeroPoint | |
| 173 modifierFlags:0 | |
| 174 timestamp:[NSDate timeIntervalSinceReferenceDate] | |
| 175 windowNumber:0 | |
| 176 context:nil | |
| 177 subtype:0 | |
| 178 data1:0 | |
| 179 data2:0]; | |
| 180 } | |
| 181 | |
| 182 } // namespace ui | |
| OLD | NEW |