Commit b05afc29 authored by Florian Blasius's avatar Florian Blasius 🤘

Remoe web window crate.

parent 0a8a2dd8
[package]
name = "orbtk-web-window"
version = "0.1.0"
authors = ["Florian Blasius <flovanpt@posteo.de>"]
description = "OrbTk window library for wasm."
repository = "https://gitlab.redox-os.org/redox-os/orbtk"
license = "MIT"
keywords = ["windowing", "wasm"]
edition = "2018"
[dependencies]
stdweb = "0.4.16"
orbtk-structs = { version = "0.1.0", path = "../structs" }
use stdweb::web::event::*;
/// Defines web mouse events.
pub enum Mouse {
Up(MouseUpEvent),
Down(MouseDownEvent),
Move(MouseMoveEvent),
}
/// Defines web key events.
pub enum Key {
Up(KeyUpEvent),
Down(KeyDownEvent),
}
/// Defines a web window event.
pub enum Event {
Mouse(Mouse),
Key(Key),
}
use std::{
rc::Rc,
cell::RefCell,
};
use stdweb::{
self, _js_impl, js,
traits::*,
unstable::TryInto,
web::{
self, document, event,
html_element::{CanvasElement, ImageElement},
window, CanvasRenderingContext2d, FillRule,
},
};
use orbtk_structs::{Position, Size};
pub use events::*;
/// Used to initializes the web engine.
///
/// This method must be called first!
pub fn initialize() {
stdweb::initialize();
}
/// Used to build a new web window.
#[derive(Default, Debug)]
pub struct WebWindowBuilder {
title: String,
size: (f64, f64),
}
impl WebWindowBuilder {
/// Creates a new web window builder with default values.
pub fn new() -> Self {
WebWindowBuilder::default()
}
/// Used to set the `title` of the window.
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = title.into();
self
}
/// Used to set the size of the window.
pub fn size(mut self, width: f64, height: f64) -> Self {
self.size.0 = width;
self.size.1 = height;
self
}
pub fn build(self) -> WebWindow {
document().set_title(&self.title[..]);
let canvas: CanvasElement = document()
.create_element("canvas")
.unwrap()
.try_into()
.unwrap();
canvas.set_width(self.size.0 as u32);
canvas.set_height(self.size.1 as u32);
let events = Rc::new(RefCell::new(vec![]));
let events_c = events.clone();
window().add_event_listener(move |e: event::MouseDownEvent| {
events_c.borrow_mut().push(Event::Mouse(Mouse::Down(e)));
});
let events_c = events.clone();
window().add_event_listener(move |e: event::MouseUpEvent| {
events_c.borrow_mut().push(Event::Mouse(Mouse::Up(e)));
});
let events_c = events.clone();
window().add_event_listener(move |e: event::KeyUpEvent| {
events_c.borrow_mut().push(Event::Key(Key::Up(e)));
});
let events_c = events.clone();
window().add_event_listener(move |e: event::KeyDownEvent| {
events_c.borrow_mut().push(Event::Key(Key::Down(e)));
});
stdweb::event_loop();
WebWindow {
title: self.title,
size: self.size,
canvas,
events,
}
}
}
/// Web implementation of a window. Uses internal a canvas as window.
pub struct WebWindow {
title: String,
canvas: CanvasElement,
size: (f64, f64),
events: Rc<RefCell<Vec<Event>>>,
}
impl WebWindow {
/// Creates a new `WebWindowBuilder` with default values.
pub fn create() -> WebWindowBuilder {
WebWindowBuilder::new()
}
/// Gets the inner canvas.
pub fn canvas(&self) -> CanvasElement {
self.canvas.clone()
}
pub fn events(&self) -> Vec<Event> {
self.events.borrow().clone()
}
}
impl Size for WebWindow {
fn width(&self) -> f64 {
self.canvas.width() as f64
}
fn set_width(&mut self, width: f64) {
self.canvas.set_width(width as u32);
}
fn height(&self) -> f64 {
self.canvas.height() as f64
}
fn set_height(&mut self, height: f64) {
self.canvas.set_height(height as u32)
}
fn size(&self) -> (f64, f64) {
(self.canvas.width() as f64, self.canvas.height() as f64)
}
fn set_size(&mut self, width: f64, height: f64) {
self.set_width(width);
self.set_height(height);
}
}
mod events;
pub mod prelude;
\ No newline at end of file
pub use crate::*;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment