Pass all key events to the current constellation frame.

This commit is contained in:
Josh Matthews 2014-10-02 03:11:50 -04:00
parent c5e1b0d32e
commit 1c64dabb15
7 changed files with 304 additions and 9 deletions

View file

@ -19,7 +19,7 @@ use windowing::{MouseWindowEvent, MouseWindowEventClass, MouseWindowMouseDownEve
use windowing::{MouseWindowMouseUpEvent, MouseWindowMoveEventClass, NavigationWindowEvent};
use windowing::{QuitWindowEvent, RefreshWindowEvent, ResizeWindowEvent, ScrollWindowEvent};
use windowing::{WindowEvent, WindowMethods, WindowNavigateMsg, ZoomWindowEvent};
use windowing::{PinchZoomWindowEvent};
use windowing::{PinchZoomWindowEvent, KeyEvent};
use azure::azure_hl;
use std::cmp;
@ -43,7 +43,7 @@ use servo_msg::compositor_msg::{Blank, Epoch, FinishedLoading, IdleRenderState,
use servo_msg::compositor_msg::{ReadyState, RenderingRenderState, RenderState, Scrollable};
use servo_msg::constellation_msg::{ConstellationChan, ExitMsg, LoadUrlMsg};
use servo_msg::constellation_msg::{NavigateMsg, LoadData, PipelineId, ResizedWindowMsg};
use servo_msg::constellation_msg::{WindowSizeData};
use servo_msg::constellation_msg::{WindowSizeData, KeyState, Key};
use servo_msg::constellation_msg;
use servo_util::geometry::{PagePx, ScreenPx, ViewportPx};
use servo_util::memory::MemoryProfilerChan;
@ -707,6 +707,10 @@ impl<Window: WindowMethods> IOCompositor<Window> {
self.on_navigation_window_event(direction);
}
KeyEvent(key, state) => {
self.on_key_event(key, state);
}
FinishedWindowEvent => {
let exit = opts::get().exit_after_load;
if exit {
@ -878,6 +882,11 @@ impl<Window: WindowMethods> IOCompositor<Window> {
chan.send(NavigateMsg(direction))
}
fn on_key_event(&self, key: Key, state: KeyState) {
let ConstellationChan(ref chan) = self.constellation_chan;
chan.send(constellation_msg::KeyEvent(key, state))
}
fn convert_buffer_requests_to_pipeline_requests_map(&self,
requests: Vec<(Rc<Layer<CompositorData>>,
Vec<BufferRequest>)>) ->

View file

@ -13,7 +13,8 @@ use gfx::render_task;
use layers::geometry::DevicePixel;
use layout_traits::{LayoutControlChan, LayoutTaskFactory, ExitNowMsg};
use libc;
use script_traits::{ResizeMsg, ResizeInactiveMsg, ExitPipelineMsg};
use script_traits;
use script_traits::{ResizeMsg, ResizeInactiveMsg, ExitPipelineMsg, SendEventMsg};
use script_traits::{ScriptControlChan, ScriptTaskFactory};
use servo_msg::compositor_msg::LayerId;
use servo_msg::constellation_msg::{ConstellationChan, ExitMsg, FailureMsg, Failure, FrameRectMsg};
@ -21,6 +22,7 @@ use servo_msg::constellation_msg::{IFrameSandboxState, IFrameUnsandboxed, InitLo
use servo_msg::constellation_msg::{LoadCompleteMsg, LoadUrlMsg, LoadData, Msg, NavigateMsg};
use servo_msg::constellation_msg::{NavigationType, PipelineId, RendererReadyMsg, ResizedWindowMsg};
use servo_msg::constellation_msg::{ScriptLoadedURLInIFrameMsg, SubpageId, WindowSizeData};
use servo_msg::constellation_msg::{KeyEvent, Key, KeyState};
use servo_msg::constellation_msg;
use servo_net::image_cache_task::{ImageCacheTask, ImageCacheTaskClient};
use servo_net::resource_task::ResourceTask;
@ -450,6 +452,10 @@ impl<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
debug!("constellation got window resize message");
self.handle_resized_window_msg(new_size);
}
KeyEvent(key, state) => {
debug!("constellation got key event message");
self.handle_key_msg(key, state);
}
}
true
}
@ -761,6 +767,13 @@ impl<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
.any(|current_frame| current_frame.contains(pipeline_id))
}
fn handle_key_msg(&self, key: Key, state: KeyState) {
self.current_frame().as_ref().map(|frame| {
let ScriptControlChan(ref chan) = frame.pipeline.script_chan;
chan.send(SendEventMsg(frame.pipeline.id, script_traits::KeyEvent(key, state)));
});
}
fn handle_renderer_ready_msg(&mut self, pipeline_id: PipelineId) {
debug!("Renderer {} ready to send paint msg", pipeline_id);
// This message could originate from a pipeline in the navigation context or

View file

@ -11,6 +11,7 @@ use geom::scale_factor::ScaleFactor;
use geom::size::TypedSize2D;
use layers::geometry::DevicePixel;
use layers::platform::surface::NativeGraphicsMetadata;
use servo_msg::constellation_msg::{Key, KeyState};
use servo_msg::compositor_msg::{ReadyState, RenderState};
use servo_util::geometry::ScreenPx;
use std::fmt::{FormatError, Formatter, Show};
@ -58,6 +59,8 @@ pub enum WindowEvent {
FinishedWindowEvent,
/// Sent when the user quits the application
QuitWindowEvent,
/// Sent when a key input state changes
KeyEvent(Key, KeyState),
}
impl Show for WindowEvent {
@ -66,6 +69,7 @@ impl Show for WindowEvent {
IdleWindowEvent => write!(f, "Idle"),
RefreshWindowEvent => write!(f, "Refresh"),
ResizeWindowEvent(..) => write!(f, "Resize"),
KeyEvent(..) => write!(f, "Key"),
LoadUrlWindowEvent(..) => write!(f, "LoadUrl"),
MouseWindowEventClass(..) => write!(f, "Mouse"),
MouseWindowMoveEventClass(..) => write!(f, "MouseMove"),

View file

@ -50,6 +50,137 @@ pub struct WindowSizeData {
pub device_pixel_ratio: ScaleFactor<ViewportPx, DevicePixel, f32>,
}
pub enum KeyState {
Pressed,
Released,
Repeated,
}
//N.B. Straight up copied from glfw-rs
pub enum Key {
KeySpace,
KeyApostrophe,
KeyComma,
KeyMinus,
KeyPeriod,
KeySlash,
Key0,
Key1,
Key2,
Key3,
Key4,
Key5,
Key6,
Key7,
Key8,
Key9,
KeySemicolon,
KeyEqual,
KeyA,
KeyB,
KeyC,
KeyD,
KeyE,
KeyF,
KeyG,
KeyH,
KeyI,
KeyJ,
KeyK,
KeyL,
KeyM,
KeyN,
KeyO,
KeyP,
KeyQ,
KeyR,
KeyS,
KeyT,
KeyU,
KeyV,
KeyW,
KeyX,
KeyY,
KeyZ,
KeyLeftBracket,
KeyBackslash,
KeyRightBracket,
KeyGraveAccent,
KeyWorld1,
KeyWorld2,
KeyEscape,
KeyEnter,
KeyTab,
KeyBackspace,
KeyInsert,
KeyDelete,
KeyRight,
KeyLeft,
KeyDown,
KeyUp,
KeyPageUp,
KeyPageDown,
KeyHome,
KeyEnd,
KeyCapsLock,
KeyScrollLock,
KeyNumLock,
KeyPrintScreen,
KeyPause,
KeyF1,
KeyF2,
KeyF3,
KeyF4,
KeyF5,
KeyF6,
KeyF7,
KeyF8,
KeyF9,
KeyF10,
KeyF11,
KeyF12,
KeyF13,
KeyF14,
KeyF15,
KeyF16,
KeyF17,
KeyF18,
KeyF19,
KeyF20,
KeyF21,
KeyF22,
KeyF23,
KeyF24,
KeyF25,
KeyKp0,
KeyKp1,
KeyKp2,
KeyKp3,
KeyKp4,
KeyKp5,
KeyKp6,
KeyKp7,
KeyKp8,
KeyKp9,
KeyKpDecimal,
KeyKpDivide,
KeyKpMultiply,
KeyKpSubtract,
KeyKpAdd,
KeyKpEnter,
KeyKpEqual,
KeyLeftShift,
KeyLeftControl,
KeyLeftAlt,
KeyLeftSuper,
KeyRightShift,
KeyRightControl,
KeyRightAlt,
KeyRightSuper,
KeyMenu,
}
/// Messages from the compositor and script to the constellation.
pub enum Msg {
ExitMsg,
@ -62,6 +193,7 @@ pub enum Msg {
NavigateMsg(NavigationDirection),
RendererReadyMsg(PipelineId),
ResizedWindowMsg(WindowSizeData),
KeyEvent(Key, KeyState),
}
/// Similar to net::resource_task::LoadData

View file

@ -42,7 +42,7 @@ use script_traits::{CompositorEvent, ResizeEvent, ReflowEvent, ClickEvent, Mouse
use script_traits::{MouseMoveEvent, MouseUpEvent, ConstellationControlMsg, ScriptTaskFactory};
use script_traits::{ResizeMsg, AttachLayoutMsg, LoadMsg, ViewportMsg, SendEventMsg};
use script_traits::{ResizeInactiveMsg, ExitPipelineMsg, NewLayoutInfo, OpaqueScriptLayoutChannel};
use script_traits::{ScriptControlChan, ReflowCompleteMsg, UntrustedNodeAddress};
use script_traits::{ScriptControlChan, ReflowCompleteMsg, UntrustedNodeAddress, KeyEvent};
use servo_msg::compositor_msg::{FinishedLoading, LayerId, Loading};
use servo_msg::compositor_msg::{ScriptListener};
use servo_msg::constellation_msg::{ConstellationChan, LoadCompleteMsg, LoadUrlMsg, NavigationDirection};
@ -907,6 +907,10 @@ impl ScriptTask {
MouseMoveEvent(point) => {
self.handle_mouse_move_event(pipeline_id, point);
}
KeyEvent(key, state) => {
println!("key {} is {}", key as int, state as int);
}
}
}
@ -1093,7 +1097,7 @@ impl ScriptTask {
}
None => {}
}
}
}
}

View file

@ -25,7 +25,7 @@ extern crate serialize;
use devtools_traits::DevtoolsControlChan;
use libc::c_void;
use servo_msg::constellation_msg::{ConstellationChan, PipelineId, Failure, WindowSizeData};
use servo_msg::constellation_msg::{LoadData, SubpageId};
use servo_msg::constellation_msg::{LoadData, SubpageId, Key, KeyState};
use servo_msg::compositor_msg::ScriptListener;
use servo_net::image_cache_task::ImageCacheTask;
use servo_net::resource_task::ResourceTask;
@ -74,7 +74,8 @@ pub enum CompositorEvent {
ClickEvent(uint, Point2D<f32>),
MouseDownEvent(uint, Point2D<f32>),
MouseUpEvent(uint, Point2D<f32>),
MouseMoveEvent(Point2D<f32>)
MouseMoveEvent(Point2D<f32>),
KeyEvent(Key, KeyState),
}
/// An opaque wrapper around script<->layout channels to avoid leaking message types into

View file

@ -10,7 +10,7 @@ use alert::{Alert, AlertMethods};
use compositing::compositor_task::{mod, CompositorProxy, CompositorReceiver};
use compositing::windowing::{Forward, Back};
use compositing::windowing::{IdleWindowEvent, ResizeWindowEvent, LoadUrlWindowEvent};
use compositing::windowing::{MouseWindowClickEvent, MouseWindowMouseDownEvent};
use compositing::windowing::{KeyEvent, MouseWindowClickEvent, MouseWindowMouseDownEvent};
use compositing::windowing::{MouseWindowEventClass, MouseWindowMoveEventClass};
use compositing::windowing::{MouseWindowMouseUpEvent, RefreshWindowEvent};
use compositing::windowing::{NavigationWindowEvent, ScrollWindowEvent, ZoomWindowEvent};
@ -25,6 +25,7 @@ use layers::platform::surface::NativeGraphicsMetadata;
use libc::c_int;
use msg::compositor_msg::{FinishedLoading, Blank, Loading, PerformingLayout, ReadyState};
use msg::compositor_msg::{IdleRenderState, RenderState, RenderingRenderState};
use msg::constellation_msg;
use std::cell::{Cell, RefCell};
use std::comm::Receiver;
use std::rc::Rc;
@ -207,8 +208,15 @@ impl Window {
match event {
glfw::KeyEvent(key, _, action, mods) => {
if action == glfw::Press {
self.handle_key(key, mods)
self.handle_key(key, mods);
}
let key = glfw_key_to_script_key(key);
let state = match action {
glfw::Press => constellation_msg::Pressed,
glfw::Release => constellation_msg::Released,
glfw::Repeat => constellation_msg::Repeated,
};
self.event_queue.borrow_mut().push(KeyEvent(key, state));
},
glfw::FramebufferSizeEvent(width, height) => {
self.event_queue.borrow_mut().push(
@ -428,3 +436,127 @@ extern "C" fn on_framebuffer_size(_glfw_window: *mut glfw::ffi::GLFWwindow,
}
}
fn glfw_key_to_script_key(key: glfw::Key) -> constellation_msg::Key {
match key {
glfw::KeySpace => constellation_msg::KeySpace,
glfw::KeyApostrophe => constellation_msg::KeyApostrophe,
glfw::KeyComma => constellation_msg::KeyComma,
glfw::KeyMinus => constellation_msg::KeyMinus,
glfw::KeyPeriod => constellation_msg::KeyPeriod,
glfw::KeySlash => constellation_msg::KeySlash,
glfw::Key0 => constellation_msg::Key0,
glfw::Key1 => constellation_msg::Key1,
glfw::Key2 => constellation_msg::Key2,
glfw::Key3 => constellation_msg::Key3,
glfw::Key4 => constellation_msg::Key4,
glfw::Key5 => constellation_msg::Key5,
glfw::Key6 => constellation_msg::Key6,
glfw::Key7 => constellation_msg::Key7,
glfw::Key8 => constellation_msg::Key8,
glfw::Key9 => constellation_msg::Key9,
glfw::KeySemicolon => constellation_msg::KeySemicolon,
glfw::KeyEqual => constellation_msg::KeyEqual,
glfw::KeyA => constellation_msg::KeyA,
glfw::KeyB => constellation_msg::KeyB,
glfw::KeyC => constellation_msg::KeyC,
glfw::KeyD => constellation_msg::KeyD,
glfw::KeyE => constellation_msg::KeyE,
glfw::KeyF => constellation_msg::KeyF,
glfw::KeyG => constellation_msg::KeyG,
glfw::KeyH => constellation_msg::KeyH,
glfw::KeyI => constellation_msg::KeyI,
glfw::KeyJ => constellation_msg::KeyJ,
glfw::KeyK => constellation_msg::KeyK,
glfw::KeyL => constellation_msg::KeyL,
glfw::KeyM => constellation_msg::KeyM,
glfw::KeyN => constellation_msg::KeyN,
glfw::KeyO => constellation_msg::KeyO,
glfw::KeyP => constellation_msg::KeyP,
glfw::KeyQ => constellation_msg::KeyQ,
glfw::KeyR => constellation_msg::KeyR,
glfw::KeyS => constellation_msg::KeyS,
glfw::KeyT => constellation_msg::KeyT,
glfw::KeyU => constellation_msg::KeyU,
glfw::KeyV => constellation_msg::KeyV,
glfw::KeyW => constellation_msg::KeyW,
glfw::KeyX => constellation_msg::KeyX,
glfw::KeyY => constellation_msg::KeyY,
glfw::KeyZ => constellation_msg::KeyZ,
glfw::KeyLeftBracket => constellation_msg::KeyLeftBracket,
glfw::KeyBackslash => constellation_msg::KeyBackslash,
glfw::KeyRightBracket => constellation_msg::KeyRightBracket,
glfw::KeyGraveAccent => constellation_msg::KeyGraveAccent,
glfw::KeyWorld1 => constellation_msg::KeyWorld1,
glfw::KeyWorld2 => constellation_msg::KeyWorld2,
glfw::KeyEscape => constellation_msg::KeyEscape,
glfw::KeyEnter => constellation_msg::KeyEnter,
glfw::KeyTab => constellation_msg::KeyTab,
glfw::KeyBackspace => constellation_msg::KeyBackspace,
glfw::KeyInsert => constellation_msg::KeyInsert,
glfw::KeyDelete => constellation_msg::KeyDelete,
glfw::KeyRight => constellation_msg::KeyRight,
glfw::KeyLeft => constellation_msg::KeyLeft,
glfw::KeyDown => constellation_msg::KeyDown,
glfw::KeyUp => constellation_msg::KeyUp,
glfw::KeyPageUp => constellation_msg::KeyPageUp,
glfw::KeyPageDown => constellation_msg::KeyPageDown,
glfw::KeyHome => constellation_msg::KeyHome,
glfw::KeyEnd => constellation_msg::KeyEnd,
glfw::KeyCapsLock => constellation_msg::KeyCapsLock,
glfw::KeyScrollLock => constellation_msg::KeyScrollLock,
glfw::KeyNumLock => constellation_msg::KeyNumLock,
glfw::KeyPrintScreen => constellation_msg::KeyPrintScreen,
glfw::KeyPause => constellation_msg::KeyPause,
glfw::KeyF1 => constellation_msg::KeyF1,
glfw::KeyF2 => constellation_msg::KeyF2,
glfw::KeyF3 => constellation_msg::KeyF3,
glfw::KeyF4 => constellation_msg::KeyF4,
glfw::KeyF5 => constellation_msg::KeyF5,
glfw::KeyF6 => constellation_msg::KeyF6,
glfw::KeyF7 => constellation_msg::KeyF7,
glfw::KeyF8 => constellation_msg::KeyF8,
glfw::KeyF9 => constellation_msg::KeyF9,
glfw::KeyF10 => constellation_msg::KeyF10,
glfw::KeyF11 => constellation_msg::KeyF11,
glfw::KeyF12 => constellation_msg::KeyF12,
glfw::KeyF13 => constellation_msg::KeyF13,
glfw::KeyF14 => constellation_msg::KeyF14,
glfw::KeyF15 => constellation_msg::KeyF15,
glfw::KeyF16 => constellation_msg::KeyF16,
glfw::KeyF17 => constellation_msg::KeyF17,
glfw::KeyF18 => constellation_msg::KeyF18,
glfw::KeyF19 => constellation_msg::KeyF19,
glfw::KeyF20 => constellation_msg::KeyF20,
glfw::KeyF21 => constellation_msg::KeyF21,
glfw::KeyF22 => constellation_msg::KeyF22,
glfw::KeyF23 => constellation_msg::KeyF23,
glfw::KeyF24 => constellation_msg::KeyF24,
glfw::KeyF25 => constellation_msg::KeyF25,
glfw::KeyKp0 => constellation_msg::KeyKp0,
glfw::KeyKp1 => constellation_msg::KeyKp1,
glfw::KeyKp2 => constellation_msg::KeyKp2,
glfw::KeyKp3 => constellation_msg::KeyKp3,
glfw::KeyKp4 => constellation_msg::KeyKp4,
glfw::KeyKp5 => constellation_msg::KeyKp5,
glfw::KeyKp6 => constellation_msg::KeyKp6,
glfw::KeyKp7 => constellation_msg::KeyKp7,
glfw::KeyKp8 => constellation_msg::KeyKp8,
glfw::KeyKp9 => constellation_msg::KeyKp9,
glfw::KeyKpDecimal => constellation_msg::KeyKpDecimal,
glfw::KeyKpDivide => constellation_msg::KeyKpDivide,
glfw::KeyKpMultiply => constellation_msg::KeyKpMultiply,
glfw::KeyKpSubtract => constellation_msg::KeyKpSubtract,
glfw::KeyKpAdd => constellation_msg::KeyKpAdd,
glfw::KeyKpEnter => constellation_msg::KeyKpEnter,
glfw::KeyKpEqual => constellation_msg::KeyKpEqual,
glfw::KeyLeftShift => constellation_msg::KeyLeftShift,
glfw::KeyLeftControl => constellation_msg::KeyLeftControl,
glfw::KeyLeftAlt => constellation_msg::KeyLeftAlt,
glfw::KeyLeftSuper => constellation_msg::KeyLeftSuper,
glfw::KeyRightShift => constellation_msg::KeyRightShift,
glfw::KeyRightControl => constellation_msg::KeyRightControl,
glfw::KeyRightAlt => constellation_msg::KeyRightAlt,
glfw::KeyRightSuper => constellation_msg::KeyRightSuper,
glfw::KeyMenu => constellation_msg::KeyMenu,
}
}