Auto merge of #21301 - paulrouget:stickykeys, r=Manishearth

Do not manually keep the modifiers state

Fix #21294

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/21301)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2018-08-01 13:53:27 -04:00 committed by GitHub
commit 88f3c8e158
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 60 deletions

1
Cargo.lock generated
View file

@ -3144,7 +3144,6 @@ name = "servo"
version = "0.0.1"
dependencies = [
"backtrace 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"euclid 0.18.2 (registry+https://github.com/rust-lang/crates.io-index)",
"gdi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"gleam 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -33,7 +33,6 @@ unstable = ["libservo/unstable"]
[target.'cfg(not(target_os = "android"))'.dependencies]
backtrace = "0.3"
bitflags = "1.0"
euclid = "0.18"
gleam = "0.6"
glutin = "0.17"

View file

@ -5,19 +5,6 @@
use servo::msg::constellation_msg::{self, Key, KeyModifiers};
use winit::VirtualKeyCode;
bitflags! {
pub struct WinitKeyModifiers: u8 {
const LEFT_CONTROL = 1;
const RIGHT_CONTROL = 2;
const LEFT_SHIFT = 4;
const RIGHT_SHIFT = 8;
const LEFT_ALT = 16;
const RIGHT_ALT = 32;
const LEFT_SUPER = 64;
const RIGHT_SUPER = 128;
}
}
// Some shortcuts use Cmd on Mac and Control on other systems.
#[cfg(target_os = "macos")]
pub const CMD_OR_CONTROL: KeyModifiers = KeyModifiers::SUPER;
@ -243,23 +230,6 @@ pub fn winit_key_to_script_key(key: VirtualKeyCode) -> Result<constellation_msg:
})
}
pub fn winit_mods_to_script_mods(modifiers: WinitKeyModifiers) -> constellation_msg::KeyModifiers {
let mut result = constellation_msg::KeyModifiers::empty();
if modifiers.intersects(WinitKeyModifiers::LEFT_SHIFT | WinitKeyModifiers::RIGHT_SHIFT) {
result.insert(KeyModifiers::SHIFT);
}
if modifiers.intersects(WinitKeyModifiers::LEFT_CONTROL | WinitKeyModifiers::RIGHT_CONTROL) {
result.insert(KeyModifiers::CONTROL);
}
if modifiers.intersects(WinitKeyModifiers::LEFT_ALT | WinitKeyModifiers::RIGHT_ALT) {
result.insert(KeyModifiers::ALT);
}
if modifiers.intersects(WinitKeyModifiers::LEFT_SUPER | WinitKeyModifiers::RIGHT_SUPER) {
result.insert(KeyModifiers::SUPER);
}
result
}
pub fn is_printable(key_code: VirtualKeyCode) -> bool {
use winit::VirtualKeyCode::*;
match key_code {

View file

@ -14,7 +14,7 @@ use osmesa_sys;
use servo::compositing::windowing::{AnimationState, MouseWindowEvent, WindowEvent};
use servo::compositing::windowing::{EmbedderCoordinates, WindowMethods};
use servo::embedder_traits::EventLoopWaker;
use servo::msg::constellation_msg::{Key, KeyState};
use servo::msg::constellation_msg::{Key, KeyState, KeyModifiers};
use servo::script_traits::TouchEventType;
use servo::servo_config::opts;
use servo::servo_geometry::DeviceIndependentPixel;
@ -31,13 +31,13 @@ use std::rc::Rc;
use std::sync::Arc;
use std::thread;
use std::time;
use super::keyutils::{self, WinitKeyModifiers};
use super::keyutils;
#[cfg(target_os = "windows")]
use user32;
#[cfg(target_os = "windows")]
use winapi;
use winit;
use winit::{ElementState, Event, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode};
use winit::{ElementState, Event, ModifiersState, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode};
use winit::dpi::{LogicalPosition, LogicalSize, PhysicalSize};
#[cfg(target_os = "macos")]
use winit::os::macos::{ActivationPolicy, WindowBuilderExt};
@ -152,7 +152,7 @@ pub struct Window {
mouse_down_point: Cell<TypedPoint2D<i32, DevicePixel>>,
event_queue: RefCell<Vec<WindowEvent>>,
mouse_pos: Cell<TypedPoint2D<i32, DevicePixel>>,
key_modifiers: Cell<WinitKeyModifiers>,
key_modifiers: Cell<KeyModifiers>,
last_pressed_key: Cell<Option<Key>>,
animation_state: Cell<AnimationState>,
fullscreen: Cell<bool>,
@ -284,7 +284,7 @@ impl Window {
mouse_down_point: Cell::new(TypedPoint2D::new(0, 0)),
mouse_pos: Cell::new(TypedPoint2D::new(0, 0)),
key_modifiers: Cell::new(WinitKeyModifiers::empty()),
key_modifiers: Cell::new(KeyModifiers::empty()),
last_pressed_key: Cell::new(None),
gl: gl.clone(),
@ -426,39 +426,32 @@ impl Window {
(last_key, None)
};
let modifiers = keyutils::winit_mods_to_script_mods(self.key_modifiers.get());
let modifiers = self.key_modifiers.get();
let event = WindowEvent::KeyEvent(ch, key, KeyState::Pressed, modifiers);
self.event_queue.borrow_mut().push(event);
}
fn toggle_keyboard_modifiers(&self, virtual_key_code: VirtualKeyCode) {
match virtual_key_code {
VirtualKeyCode::LControl => self.toggle_modifier(WinitKeyModifiers::LEFT_CONTROL),
VirtualKeyCode::RControl => self.toggle_modifier(WinitKeyModifiers::RIGHT_CONTROL),
VirtualKeyCode::LShift => self.toggle_modifier(WinitKeyModifiers::LEFT_SHIFT),
VirtualKeyCode::RShift => self.toggle_modifier(WinitKeyModifiers::RIGHT_SHIFT),
VirtualKeyCode::LAlt => self.toggle_modifier(WinitKeyModifiers::LEFT_ALT),
VirtualKeyCode::RAlt => self.toggle_modifier(WinitKeyModifiers::RIGHT_ALT),
VirtualKeyCode::LWin => self.toggle_modifier(WinitKeyModifiers::LEFT_SUPER),
VirtualKeyCode::RWin => self.toggle_modifier(WinitKeyModifiers::RIGHT_SUPER),
_ => {}
}
fn toggle_keyboard_modifiers(&self, mods: ModifiersState) {
self.toggle_modifier(KeyModifiers::CONTROL, mods.ctrl);
self.toggle_modifier(KeyModifiers::SHIFT, mods.shift);
self.toggle_modifier(KeyModifiers::ALT, mods.alt);
self.toggle_modifier(KeyModifiers::SUPER, mods.logo);
}
fn handle_keyboard_input(&self, element_state: ElementState, virtual_key_code: VirtualKeyCode) {
self.toggle_keyboard_modifiers(virtual_key_code);
fn handle_keyboard_input(&self, element_state: ElementState, code: VirtualKeyCode, mods: ModifiersState) {
self.toggle_keyboard_modifiers(mods);
if let Ok(key) = keyutils::winit_key_to_script_key(virtual_key_code) {
if let Ok(key) = keyutils::winit_key_to_script_key(code) {
let state = match element_state {
ElementState::Pressed => KeyState::Pressed,
ElementState::Released => KeyState::Released,
};
if element_state == ElementState::Pressed && keyutils::is_printable(virtual_key_code) {
if element_state == ElementState::Pressed && keyutils::is_printable(code) {
// If pressed and printable, we expect a ReceivedCharacter event.
self.last_pressed_key.set(Some(key));
} else {
self.last_pressed_key.set(None);
let modifiers = keyutils::winit_mods_to_script_mods(self.key_modifiers.get());
let modifiers = self.key_modifiers.get();
self.event_queue.borrow_mut().push(WindowEvent::KeyEvent(None, key, state, modifiers));
}
}
@ -473,10 +466,10 @@ impl Window {
Event::WindowEvent {
event: winit::WindowEvent::KeyboardInput {
input: winit::KeyboardInput {
state, virtual_keycode: Some(virtual_keycode), ..
state, virtual_keycode: Some(virtual_keycode), modifiers, ..
}, ..
}, ..
} => self.handle_keyboard_input(state, virtual_keycode),
} => self.handle_keyboard_input(state, virtual_keycode, modifiers),
Event::WindowEvent {
event: winit::WindowEvent::MouseInput {
state, button, ..
@ -576,9 +569,13 @@ impl Window {
}
}
fn toggle_modifier(&self, modifier: WinitKeyModifiers) {
fn toggle_modifier(&self, modifier: KeyModifiers, pressed: bool) {
let mut modifiers = self.key_modifiers.get();
modifiers.toggle(modifier);
if pressed {
modifiers.insert(modifier);
} else {
modifiers.remove(modifier);
}
self.key_modifiers.set(modifiers);
}

View file

@ -3,7 +3,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
extern crate backtrace;
#[macro_use] extern crate bitflags;
extern crate euclid;
#[cfg(target_os = "windows")] extern crate gdi32;
extern crate gleam;