Draft web platform structure

This commit is contained in:
Héctor Ramón Jiménez 2019-06-25 03:15:34 +02:00
parent eea9530f38
commit c5703eb00a
26 changed files with 1171 additions and 153 deletions

View file

@ -0,0 +1,66 @@
use crate::dpi::LogicalSize;
use crate::error::OsError as RootOE;
use crate::platform_impl::OsError;
use wasm_bindgen::JsCast;
use web_sys::HtmlCanvasElement;
pub struct Canvas {
raw: HtmlCanvasElement,
}
impl Canvas {
pub fn create() -> Result<Self, RootOE> {
let window = web_sys::window().expect("Failed to obtain window");
let document = window.document().expect("Failed to obtain document");
let canvas: HtmlCanvasElement = document
.create_element("canvas")
.map_err(|_| os_error!(OsError("Failed to create canvas element".to_owned())))?
.unchecked_into();
document
.body()
.ok_or_else(|| os_error!(OsError("Failed to find body node".to_owned())))?
.append_child(&canvas)
.map_err(|_| os_error!(OsError("Failed to append canvas".to_owned())))?;
Ok(Canvas { raw: canvas })
}
pub fn set_attribute(&self, attribute: &str, value: &str) {
self.raw
.set_attribute(attribute, value)
.expect(&format!("Set attribute: {}", attribute));
}
pub fn position(&self) -> (f64, f64) {
let bounds = self.raw.get_bounding_client_rect();
(bounds.x(), bounds.y())
}
pub fn width(&self) -> f64 {
self.raw.width() as f64
}
pub fn height(&self) -> f64 {
self.raw.height() as f64
}
pub fn set_size(&self, size: LogicalSize) {
self.raw.set_width(size.width as u32);
self.raw.set_height(size.height as u32);
}
pub fn raw(&self) -> HtmlCanvasElement {
self.raw.clone()
}
pub fn on_mouse_out<F>(&self, f: F) {}
pub fn on_mouse_over<F>(&self, f: F) {}
pub fn on_mouse_up<F>(&self, f: F) {}
pub fn on_mouse_down<F>(&self, f: F) {}
pub fn on_mouse_move<F>(&self, f: F) {}
pub fn on_mouse_scroll<F>(&self, f: F) {}
}

View file

@ -0,0 +1,13 @@
pub struct Document;
impl Document {
pub fn set_title(title: &str) {}
pub fn on_blur<F>(f: F) {}
pub fn on_focus<F>(f: F) {}
pub fn on_key_up<F>(f: F) {}
pub fn on_key_down<F>(f: F) {}
}

View file

@ -0,0 +1,17 @@
mod canvas;
mod document;
mod timeout;
pub use self::canvas::Canvas;
pub use self::document::Document;
pub use self::timeout::Timeout;
pub fn request_animation_frame<F>(f: F)
where
F: Fn(),
{
}
pub fn throw(msg: &str) {
wasm_bindgen::throw_str(msg);
}

View file

@ -0,0 +1,12 @@
use std::time::Duration;
#[derive(Debug, Clone, Copy)]
pub struct Timeout {}
impl Timeout {
pub fn new<F>(f: F, duration: Duration) -> Timeout {
Timeout {}
}
pub fn clear(self) {}
}