From 8fbd6326d80cb9960bb0f14938a1486f40990bc4 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 17 May 2013 10:56:10 -0700 Subject: [PATCH] Load purple.com when hitting ctrl+L. This is a precursor to popping up an alert box. --- src/components/servo/compositing/mod.rs | 8 ++-- .../servo/platform/common/glut_windowing.rs | 37 +++++++++++++++++-- .../platform/common/shared_gl_windowing.rs | 5 ++- src/components/servo/scripting/script_task.rs | 12 +++++- src/components/servo/windowing.rs | 7 ++++ 5 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/components/servo/compositing/mod.rs b/src/components/servo/compositing/mod.rs index 5e9bf617c00..602b1e501d5 100644 --- a/src/components/servo/compositing/mod.rs +++ b/src/components/servo/compositing/mod.rs @@ -17,8 +17,9 @@ use geom::rect::Rect; use geom::size::Size2D; use gfx::compositor::{Compositor, LayerBuffer, LayerBufferSet}; use gfx::opts::Opts; +use layers::layers::{Image, ImageData}; use layers; -use servo_util::time; +use servo_util::{time, url}; mod resize_rate_limiter; @@ -97,7 +98,7 @@ fn mainloop(po: Port, script_chan: SharedChan, opts: &Opts) { 0, layers::layers::RGB24Format, ~[]); - let image = @mut layers::layers::Image::new(image_data as @layers::layers::ImageData); + let image = @mut Image::new(image_data as @ImageData); let image_layer = @mut layers::layers::ImageLayer(image); original_layer_transform = image_layer.common.transform; image_layer.common.set_transform(original_layer_transform.scale(800.0, 600.0, 1.0)); @@ -141,7 +142,7 @@ fn mainloop(po: Port, script_chan: SharedChan, opts: &Opts) { data_source_surface: buffer.draw_target.snapshot().get_data_surface(), size: Size2D(width, height) }; - let image = @mut layers::layers::Image::new(image_data as @layers::layers::ImageData); + let image = @mut Image::new(image_data as @ImageData); // Find or create an image layer. let image_layer; @@ -192,6 +193,7 @@ fn mainloop(po: Port, script_chan: SharedChan, opts: &Opts) { window.present(); } + // Hook the windowing system's resize callback up to the resize rate limiter. do window.set_resize_callback |width, height| { debug!("osmain: window resized to %ux%u", width, height); resize_rate_limiter.window_resized(width, height); diff --git a/src/components/servo/platform/common/glut_windowing.rs b/src/components/servo/platform/common/glut_windowing.rs index b4ab82ac5d8..167eae2dcf0 100644 --- a/src/components/servo/platform/common/glut_windowing.rs +++ b/src/components/servo/platform/common/glut_windowing.rs @@ -7,7 +7,8 @@ /// GLUT is a very old and bare-bones toolkit. However, it has good cross-platform support, at /// least on desktops. It is designed for testing Servo without the need of a UI. -use windowing::{ApplicationMethods, CompositeCallback, ResizeCallback, WindowMethods}; +use windowing::{ApplicationMethods, CompositeCallback, LoadUrlCallback, ResizeCallback}; +use windowing::{WindowMethods}; use geom::size::Size2D; use glut::glut::{DOUBLE, WindowHeight, WindowWidth}; @@ -27,8 +28,10 @@ impl ApplicationMethods for Application { /// The type of a window. pub struct Window { glut_window: glut::Window, + composite_callback: Option, resize_callback: Option, + load_url_callback: Option, } impl WindowMethods for Window { @@ -41,8 +44,10 @@ impl WindowMethods for Window { // Create our window object. let window = @mut Window { glut_window: glut_window, + composite_callback: None, resize_callback: None, + load_url_callback: None, }; // Register event handlers. @@ -51,14 +56,17 @@ impl WindowMethods for Window { None => {} Some(callback) => callback(width as uint, height as uint), } - }; + } do glut::display_func { // FIXME(pcwalton): This will not work with multiple windows. match window.composite_callback { None => {} Some(callback) => callback(), } - }; + } + do glut::keyboard_func |key, _, _| { + window.handle_key(key) + } window } @@ -84,9 +92,32 @@ impl WindowMethods for Window { self.resize_callback = Some(new_resize_callback) } + /// Registers a callback to be run when a new URL is to be loaded. + pub fn set_load_url_callback(&mut self, new_load_url_callback: LoadUrlCallback) { + self.load_url_callback = Some(new_load_url_callback) + } + /// Spins the event loop. pub fn check_loop(@mut self) { glut::check_loop() } } +impl Window { + /// Helper function to handle keyboard events. + fn handle_key(&self, key: u8) { + debug!("got key: %d", key as int); + if key == 12 { // ^L + self.load_url() + } + } + + /// Helper function to pop up an alert box prompting the user to load a URL. + fn load_url(&self) { + match self.load_url_callback { + None => error!("no URL callback registered, doing nothing"), + Some(callback) => callback("http://purple.com/"), + } + } +} + diff --git a/src/components/servo/platform/common/shared_gl_windowing.rs b/src/components/servo/platform/common/shared_gl_windowing.rs index a859696a75e..aa4a84b12cd 100644 --- a/src/components/servo/platform/common/shared_gl_windowing.rs +++ b/src/components/servo/platform/common/shared_gl_windowing.rs @@ -11,7 +11,7 @@ /// along with the Servo process or trusted. If the OpenGL driver itself is untrusted, then this /// windowing implementation is not appropriate. -use windowing::{CompositeCallback, ResizeCallback}; +use windowing::{CompositeCallback, LoadUrlCallback, ResizeCallback}; use geom::size::Size2D; use sharegl::base::ShareContext; @@ -53,6 +53,9 @@ impl WindowingMethods for Window { /// Registers a callback to run when a resize event occurs. pub fn set_resize_callback(&mut self, _: ResizeCallback) {} + /// Registers a callback to run when a new URL is to be loaded. + pub fn set_load_url_callback(&mut self, _: LoadUrlCallback) {} + /// Returns the next event. pub fn check_loop(@mut self) {} } diff --git a/src/components/servo/scripting/script_task.rs b/src/components/servo/scripting/script_task.rs index a89c2cdd654..035873e76f4 100644 --- a/src/components/servo/scripting/script_task.rs +++ b/src/components/servo/scripting/script_task.rs @@ -121,8 +121,11 @@ pub struct ScriptContext { js_context: @Cx, /// The JavaScript compartment. js_compartment: @mut Compartment, + /// Global static data related to the DOM. dom_static: GlobalStaticData, + /// Whether the JS bindings have been initialized. + bindings_initialized: bool, /// The outermost frame. This frame contains the document, window, and page URL. root_frame: Option, @@ -189,7 +192,9 @@ impl ScriptContext { js_runtime: js_runtime, js_context: js_context, js_compartment: compartment, + dom_static: GlobalStaticData(), + bindings_initialized: false, root_frame: None, @@ -287,7 +292,12 @@ impl ScriptContext { /// objects, parses HTML and CSS, and kicks off initial layout. fn load(&mut self, url: Url) { // Define the script DOM bindings. - define_bindings(self.js_compartment); + // + // FIXME: Can this be done earlier, to save the flag? + if !self.bindings_initialized { + define_bindings(self.js_compartment); + self.bindings_initialized = true + } // Parse HTML. // diff --git a/src/components/servo/windowing.rs b/src/components/servo/windowing.rs index bd7f0ce510f..8c642727a7f 100644 --- a/src/components/servo/windowing.rs +++ b/src/components/servo/windowing.rs @@ -12,6 +12,9 @@ pub type CompositeCallback = @fn(); /// Type of the function that is called when the window is resized. pub type ResizeCallback = @fn(uint, uint); +/// Type of the function that is called when a new URL is to be loaded. +pub type LoadUrlCallback = @fn(&str); + /// Methods for an abstract Application. pub trait ApplicationMethods { fn new() -> Self; @@ -24,10 +27,14 @@ pub trait WindowMethods { pub fn size(&self) -> Size2D; /// Presents the window to the screen (perhaps by page flipping). pub fn present(&mut self); + /// Registers a callback to run when a composite event occurs. pub fn set_composite_callback(&mut self, new_composite_callback: CompositeCallback); /// Registers a callback to run when a resize event occurs. pub fn set_resize_callback(&mut self, new_resize_callback: ResizeCallback); + /// Registers a callback to run when a new URL is to be loaded. + pub fn set_load_url_callback(&mut self, new_load_url_callback: LoadUrlCallback); + /// Spins the event loop. pub fn check_loop(@mut self); }