Skip to content

Commit c570007

Browse files
author
bors-servo
authored
Auto merge of rust-windowing#99 - mrmiywj:fix-return-coordinates-in-MouseInput, r=paulroget
fix return coordinates in MouseInput It is a fix of rust-windowing#97 When working on [servo/#11794](servo/servo#11794). I found the previous PR rust-windowing#97 is wrong. This PR is a fix of that and is right when testing [servo/#11794](servo/servo#11794). Actually, we should return the mouse movement position instead of actual coordination. So the previous PR's return value is wrong. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/glutin/99) <!-- Reviewable:end -->
2 parents 1d3a6fc + a4e687a commit c570007

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

src/api/cocoa/mod.rs

+17-20
Original file line numberDiff line numberDiff line change
@@ -1031,11 +1031,19 @@ impl Clone for IdRef {
10311031

10321032
#[allow(non_snake_case)]
10331033
unsafe fn NSEventToEvent(window: &Window, nsevent: id) -> Option<Event> {
1034-
unsafe fn mousePosition(window: &Window, nsevent: &id) -> (i32, i32) {
1034+
unsafe fn get_mouse_position(window: &Window, nsevent: id) -> (i32, i32) {
10351035
let window_point = nsevent.locationInWindow();
1036+
let cWindow: id = msg_send![nsevent, window];
1037+
let view_point = if cWindow == nil {
1038+
let window_rect = window.window.convertRectFromScreen_(NSRect::new(window_point, NSSize::new(0.0, 0.0)));
1039+
window.view.convertPoint_fromView_(window_rect.origin, nil)
1040+
} else {
1041+
window.view.convertPoint_fromView_(window_point, nil)
1042+
};
1043+
let view_rect = NSView::frame(*window.view);
10361044
let scale_factor = window.hidpi_factor();
1037-
((scale_factor * window_point.x as f32) as i32,
1038-
(scale_factor * window_point.y as f32) as i32)
1045+
((scale_factor * view_point.x as f32) as i32,
1046+
(scale_factor * (view_rect.size.height - view_point.y) as f32) as i32)
10391047
}
10401048

10411049
if nsevent == nil { return None; }
@@ -1049,37 +1057,26 @@ unsafe fn NSEventToEvent(window: &Window, nsevent: id) -> Option<Event> {
10491057
match event_type {
10501058
NSLeftMouseDown => {
10511059
Some(MouseInput(Pressed, MouseButton::Left,
1052-
Some(mousePosition(window, &nsevent))))
1060+
Some(get_mouse_position(window, nsevent))))
10531061
},
10541062
NSLeftMouseUp => {
10551063
Some(MouseInput(Released, MouseButton::Left,
1056-
Some(mousePosition(window, &nsevent))))
1064+
Some(get_mouse_position(window, nsevent))))
10571065
},
10581066
NSRightMouseDown => {
10591067
Some(MouseInput(Pressed, MouseButton::Right,
1060-
Some(mousePosition(window, &nsevent))))
1068+
Some(get_mouse_position(window, nsevent))))
10611069
},
10621070
NSRightMouseUp => {
10631071
Some(MouseInput(Released, MouseButton::Right,
1064-
Some(mousePosition(window, &nsevent))))
1072+
Some(get_mouse_position(window, nsevent))))
10651073
},
10661074
NSMouseMoved |
10671075
NSLeftMouseDragged |
10681076
NSOtherMouseDragged |
10691077
NSRightMouseDragged => {
1070-
let window_point = nsevent.locationInWindow();
1071-
let cWindow: id = msg_send![nsevent, window];
1072-
let view_point = if cWindow == nil {
1073-
let window_rect = window.window.convertRectFromScreen_(NSRect::new(window_point, NSSize::new(0.0, 0.0)));
1074-
window.view.convertPoint_fromView_(window_rect.origin, nil)
1075-
} else {
1076-
window.view.convertPoint_fromView_(window_point, nil)
1077-
};
1078-
let view_rect = NSView::frame(*window.view);
1079-
let scale_factor = window.hidpi_factor();
1080-
1081-
Some(MouseMoved((scale_factor * view_point.x as f32) as i32,
1082-
(scale_factor * (view_rect.size.height - view_point.y) as f32) as i32))
1078+
let (x, y) = get_mouse_position(window, nsevent);
1079+
Some(MouseMoved(x, y))
10831080
},
10841081
NSKeyDown => {
10851082
let mut events = VecDeque::new();

0 commit comments

Comments
 (0)