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