Get to a state where a canvas is spawned

This commit is contained in:
Ryan Goldstein 2019-03-09 21:54:29 -05:00
parent f698d451df
commit 37d354cf7f
3 changed files with 75 additions and 52 deletions

View file

@ -1,4 +1,7 @@
extern crate winit; extern crate winit;
#[macro_use]
extern crate stdweb;
use winit::window::WindowBuilder; use winit::window::WindowBuilder;
use winit::event::{Event, WindowEvent}; use winit::event::{Event, WindowEvent};
use winit::event_loop::{EventLoop, ControlFlow}; use winit::event_loop::{EventLoop, ControlFlow};
@ -10,9 +13,10 @@ fn main() {
.with_title("A fantastic window!") .with_title("A fantastic window!")
.build(&event_loop) .build(&event_loop)
.unwrap(); .unwrap();
console!(log, "Built window!");
event_loop.run(|event, _, control_flow| { event_loop.run(|event, _, control_flow| {
println!("{:?}", event); console!(log, format!("{:?}", event));
match event { match event {
Event::WindowEvent { Event::WindowEvent {

View file

@ -139,7 +139,7 @@ impl<T> EventLoop<T> {
/// ///
/// [`ControlFlow`]: ./enum.ControlFlow.html /// [`ControlFlow`]: ./enum.ControlFlow.html
#[inline] #[inline]
pub fn run<F>(self, event_handler: F) -> ! pub fn run<F>(self, event_handler: F) // TODO: this needs to be !
where F: 'static + FnMut(Event<T>, &EventLoopWindowTarget<T>, &mut ControlFlow) where F: 'static + FnMut(Event<T>, &EventLoopWindowTarget<T>, &mut ControlFlow)
{ {
self.event_loop.run(event_handler) self.event_loop.run(event_handler)

View file

@ -41,7 +41,8 @@ pub struct MonitorHandle;
impl MonitorHandle { impl MonitorHandle {
pub fn get_hidpi_factor(&self) -> f64 { pub fn get_hidpi_factor(&self) -> f64 {
unimplemented!(); // TODO
1.0
} }
pub fn get_position(&self) -> PhysicalPosition { pub fn get_position(&self) -> PhysicalPosition {
@ -71,8 +72,42 @@ pub struct Window {
} }
impl Window { impl Window {
pub fn new<T>(target: &EventLoopWindowTarget<T>, window: WindowAttributes, platform: PlatformSpecificWindowBuilderAttributes) -> Result<Self, CreationError> { pub fn new<T>(target: &EventLoopWindowTarget<T>, attr: WindowAttributes,
unimplemented!(); _: PlatformSpecificWindowBuilderAttributes) -> Result<Self, CreationError> {
let element = document()
.create_element("canvas")
.map_err(|_| CreationError::OsError("Failed to create canvas element".to_owned()))?;
let canvas: CanvasElement = element.try_into()
.map_err(|_| CreationError::OsError("Failed to create canvas element".to_owned()))?;
document().body()
.ok_or_else(|| CreationError::OsError("Failed to find body node".to_owned()))?
.append_child(&canvas);
let window = Window { canvas };
if let Some(dimensions) = attr.dimensions {
window.set_inner_size(dimensions);
} else {
window.set_inner_size(LogicalSize {
width: 1024.0,
height: 768.0,
})
}
// TODO: most of these are no-op, but should they stay here just in case?
window.set_min_dimensions(attr.min_dimensions);
window.set_max_dimensions(attr.max_dimensions);
window.set_resizable(attr.resizable);
window.set_title(&attr.title);
window.set_maximized(attr.maximized);
if attr.visible {
window.show();
} else {
window.hide();
}
//window.set_transparent(attr.transparent);
window.set_decorations(attr.decorations);
window.set_always_on_top(attr.always_on_top);
window.set_window_icon(attr.window_icon);
target.register_window(&window);
Ok(window)
} }
pub fn set_title(&self, title: &str) { pub fn set_title(&self, title: &str) {
@ -88,8 +123,7 @@ impl Window {
} }
pub fn request_redraw(&self) { pub fn request_redraw(&self) {
// TODO: what does this mean // TODO: what does this mean? If it's a 'present'-style call then it's not necessary
unimplemented!();
} }
pub fn get_position(&self) -> Option<LogicalPosition> { pub fn get_position(&self) -> Option<LogicalPosition> {
@ -106,7 +140,6 @@ impl Window {
pub fn set_position(&self, position: LogicalPosition) { pub fn set_position(&self, position: LogicalPosition) {
// TODO: use CSS? // TODO: use CSS?
unimplemented!();
} }
#[inline] #[inline]
@ -148,7 +181,8 @@ impl Window {
#[inline] #[inline]
pub fn get_hidpi_factor(&self) -> f64 { pub fn get_hidpi_factor(&self) -> f64 {
unimplemented!(); // TODO
1.0
} }
#[inline] #[inline]
@ -199,13 +233,13 @@ impl Window {
#[inline] #[inline]
pub fn set_cursor_position(&self, position: LogicalPosition) -> Result<(), String> { pub fn set_cursor_position(&self, position: LogicalPosition) -> Result<(), String> {
// TODO: pointer capture // TODO: pointer capture
unimplemented!(); Ok(())
} }
#[inline] #[inline]
pub fn grab_cursor(&self, grab: bool) -> Result<(), String> { pub fn grab_cursor(&self, grab: bool) -> Result<(), String> {
// TODO: pointer capture // TODO: pointer capture
unimplemented!(); Ok(())
} }
#[inline] #[inline]
@ -217,13 +251,11 @@ impl Window {
#[inline] #[inline]
pub fn set_maximized(&self, maximized: bool) { pub fn set_maximized(&self, maximized: bool) {
// TODO: should there be a maximization / fullscreen API? // TODO: should there be a maximization / fullscreen API?
unimplemented!();
} }
#[inline] #[inline]
pub fn set_fullscreen(&self, monitor: Option<RootMH>) { pub fn set_fullscreen(&self, monitor: Option<RootMH>) {
// TODO: should there be a maximization / fullscreen API? // TODO: should there be a maximization / fullscreen API?
unimplemented!();
} }
#[inline] #[inline]
@ -239,12 +271,11 @@ impl Window {
#[inline] #[inline]
pub fn set_window_icon(&self, window_icon: Option<Icon>) { pub fn set_window_icon(&self, window_icon: Option<Icon>) {
// TODO: should this set the favicon? // TODO: should this set the favicon?
unimplemented!();
} }
#[inline] #[inline]
pub fn set_ime_spot(&self, position: LogicalPosition) { pub fn set_ime_spot(&self, position: LogicalPosition) {
unimplemented!(); // TODO: what is this?
} }
#[inline] #[inline]
@ -271,18 +302,8 @@ impl Window {
} }
} }
fn new_rootelw<T>() -> RootELW<T> {
RootELW {
p: EventLoopWindowTarget {
_phantom: PhantomData
},
_marker: PhantomData
}
}
pub struct EventLoop<T: 'static> { pub struct EventLoop<T: 'static> {
window_target: RootELW<T>, elw: RootELW<T>,
data: Rc<RefCell<EventLoopData<T>>>,
} }
#[derive(Clone)] #[derive(Clone)]
@ -291,9 +312,23 @@ struct EventLoopData<T> {
control: ControlFlow, control: ControlFlow,
} }
pub struct EventLoopWindowTarget<T: 'static> {
data: Rc<RefCell<EventLoopData<T>>>,
}
impl<T> EventLoop<T> { impl<T> EventLoop<T> {
pub fn new() -> Self { pub fn new() -> Self {
unimplemented!(); EventLoop {
elw: RootELW {
p: EventLoopWindowTarget {
data: Rc::new(RefCell::new(EventLoopData {
events: VecDeque::new(),
control: ControlFlow::Poll
}))
},
_marker: PhantomData
}
}
} }
pub fn get_available_monitors(&self) -> VecDequeIter<MonitorHandle> { pub fn get_available_monitors(&self) -> VecDequeIter<MonitorHandle> {
@ -304,7 +339,7 @@ impl<T> EventLoop<T> {
MonitorHandle MonitorHandle
} }
pub fn run<F>(mut self, event_handler: F) -> ! pub fn run<F>(mut self, event_handler: F)
where F: 'static + FnMut(Event<T>, &RootELW<T>, &mut ControlFlow) where F: 'static + FnMut(Event<T>, &RootELW<T>, &mut ControlFlow)
{ {
// TODO: Create event handlers for the JS events // TODO: Create event handlers for the JS events
@ -313,39 +348,26 @@ impl<T> EventLoop<T> {
// TODO: file dropping, PathBuf isn't useful for web // TODO: file dropping, PathBuf isn't useful for web
let document = &document(); let document = &document();
self.add_event(document, |mut data, event: BlurEvent| { self.elw.p.add_event(document, |mut data, event: BlurEvent| {
}); });
self.add_event(document, |mut data, event: FocusEvent| { self.elw.p.add_event(document, |mut data, event: FocusEvent| {
}); });
// TODO: what to do after attaching events stdweb::event_loop(); // TODO: this is only necessary for stdweb emscripten, should it be here?
unimplemented!();
} }
pub fn create_proxy(&self) -> EventLoopProxy<T> { pub fn create_proxy(&self) -> EventLoopProxy<T> {
EventLoopProxy { EventLoopProxy {
data: self.data.clone() data: self.elw.p.data.clone()
} }
} }
pub fn window_target(&self) -> &RootELW<T> { pub fn window_target(&self) -> &RootELW<T> {
&self.window_target &self.elw
} }
// Apply all enqueued events
fn apply_events<F>(&mut self, mut event_handler: F, start: StartCause)
where F: 'static + FnMut(Event<T>, &RootELW<T>, &mut ControlFlow) {
// TODO: how to handle ControlFlow::Exit?
let mut data = self.data.borrow_mut();
let mut control = data.control.clone();
let events = &mut data.events;
event_handler(Event::NewEvents(start), &new_rootelw(), &mut control);
for event in events.drain(..) {
event_handler(event, &new_rootelw(), &mut control);
}
event_handler(Event::EventsCleared, &new_rootelw(), &mut control)
} }
impl<T> EventLoopWindowTarget<T> {
fn register_window(&self, other: &Window) { fn register_window(&self, other: &Window) {
let canvas = &other.canvas; let canvas = &other.canvas;
@ -442,6 +464,7 @@ impl<T> EventLoop<T> {
}); });
} }
fn add_event<E, F>(&self, target: &impl IEventTarget, mut handler: F) fn add_event<E, F>(&self, target: &impl IEventTarget, mut handler: F)
where E: ConcreteEvent, F: FnMut(RefMut<EventLoopData<T>>, E) + 'static { where E: ConcreteEvent, F: FnMut(RefMut<EventLoopData<T>>, E) + 'static {
let data = self.data.clone(); let data = self.data.clone();
@ -664,10 +687,6 @@ impl<T> EventLoopProxy<T> {
} }
} }
pub struct EventLoopWindowTarget<T> {
_phantom: PhantomData<T>
}
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PlatformSpecificWindowBuilderAttributes; pub struct PlatformSpecificWindowBuilderAttributes;