Skip to content

Commit

Permalink
wip: builds now
Browse files Browse the repository at this point in the history
  • Loading branch information
tangmi committed Dec 27, 2019
1 parent 741253f commit f6716be
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 62 deletions.
18 changes: 10 additions & 8 deletions src/platform_impl/web/event_loop/window_target.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{backend, device, proxy::Proxy, runner, window};
use crate::dpi::LogicalSize;
use crate::dpi::PhysicalSize;
use crate::event::{DeviceId, ElementState, Event, KeyboardInput, TouchPhase, WindowEvent};
use crate::event_loop::ControlFlow;
use crate::window::WindowId;
Expand Down Expand Up @@ -168,20 +168,22 @@ impl<T> WindowTarget<T> {

let runner = self.runner.clone();
let raw = canvas.raw().clone();
let mut intended_size = LogicalSize {
width: raw.width() as f64,
height: raw.height() as f64,
// todo
let mut intended_size = PhysicalSize {
width: raw.width() as u32,
height: raw.height() as u32,
};
canvas.on_fullscreen_change(move || {
// If the canvas is marked as fullscreen, it is moving *into* fullscreen
// If it is not, it is moving *out of* fullscreen
let new_size = if backend::is_fullscreen(&raw) {
intended_size = LogicalSize {
width: raw.width() as f64,
height: raw.height() as f64,
// todo
intended_size = PhysicalSize {
width: raw.width() as u32,
height: raw.height() as u32,
};

backend::window_size()
backend::window_size().to_physical(backend::hidpi_factor())
} else {
intended_size
};
Expand Down
6 changes: 3 additions & 3 deletions src/platform_impl/web/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl Handle {
}

pub fn position(&self) -> PhysicalPosition<i32> {
PhysicalPosition { x: 0.0, y: 0.0 }
PhysicalPosition { x: 0, y: 0 }
}

pub fn name(&self) -> Option<String> {
Expand All @@ -19,8 +19,8 @@ impl Handle {

pub fn size(&self) -> PhysicalSize<u32> {
PhysicalSize {
width: 0.0,
height: 0.0,
width: 0,
height: 0,
}
}

Expand Down
39 changes: 23 additions & 16 deletions src/platform_impl/web/stdweb/canvas.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::event;
use crate::dpi::{LogicalPosition, LogicalSize};
use crate::dpi::{LogicalPosition, PhysicalPosition, PhysicalSize, Size};
use crate::error::OsError as RootOE;
use crate::event::{ModifiersState, MouseButton, MouseScrollDelta, ScanCode, VirtualKeyCode};
use crate::platform_impl::OsError;
Expand Down Expand Up @@ -82,30 +82,36 @@ impl Canvas {
.expect(&format!("Set attribute: {}", attribute));
}

pub fn position(&self) -> (f64, f64) {
pub fn position(&self) -> LogicalPosition<f64> {
let bounds = self.raw.get_bounding_client_rect();

(bounds.get_x(), bounds.get_y())
LogicalPosition {
x: bounds.get_x(),
y: bounds.get_y(),
}
}

pub fn size(&self) -> LogicalSize<f64> {
LogicalSize {
width: self.raw.width() as f64,
height: self.raw.height() as f64,
pub fn size(&self) -> PhysicalSize<u32> {
PhysicalSize {
width: self.raw.width() as u32,
height: self.raw.height() as u32,
}
}

pub fn set_size(&self, size: LogicalSize<f64>) {
pub fn set_size(&self, size: Size) {
use stdweb::*;

let physical_size = size.to_physical(super::hidpi_factor());
let dpi_factor = super::hidpi_factor();

let physical_size = size.to_physical::<u32>(dpi_factor);
let logical_size = size.to_logical::<f64>(dpi_factor);

self.raw.set_width(physical_size.width as u32);
self.raw.set_height(physical_size.height as u32);
self.raw.set_width(physical_size.width);
self.raw.set_height(physical_size.height);

js! {
@{self.raw.as_ref()}.style.width = @{size.width} + "px";
@{self.raw.as_ref()}.style.height = @{size.height} + "px";
@{self.raw.as_ref()}.style.width = @{logical_size.width} + "px";
@{self.raw.as_ref()}.style.height = @{logical_size.height} + "px";
}
}

Expand Down Expand Up @@ -196,7 +202,7 @@ impl Canvas {
self.on_mouse_release = Some(self.add_user_event(move |event: PointerUpEvent| {
handler(
event.pointer_id(),
event::mouse_button(&event),
event::mouse_button(&event), // todo convert to physical
event::mouse_modifiers(&event),
);
}));
Expand All @@ -217,12 +223,13 @@ impl Canvas {

pub fn on_cursor_move<F>(&mut self, mut handler: F)
where
F: 'static + FnMut(i32, LogicalPosition, ModifiersState),
F: 'static + FnMut(i32, PhysicalPosition<i32>, ModifiersState),
{
// todo
self.on_cursor_move = Some(self.add_event(move |event: PointerMoveEvent| {
handler(
event.pointer_id(),
event::mouse_position(&event),
event::mouse_position(&event).to_physical(super::hidpi_factor()),
event::mouse_modifiers(&event),
);
}));
Expand Down
40 changes: 25 additions & 15 deletions src/platform_impl/web/web_sys/canvas.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::event;
use crate::dpi::{LogicalPosition, LogicalSize};
use crate::dpi::{LogicalPosition, PhysicalPosition, PhysicalSize, Size};
use crate::error::OsError as RootOE;
use crate::event::{ModifiersState, MouseButton, MouseScrollDelta, ScanCode, VirtualKeyCode};
use crate::platform_impl::OsError;
Expand Down Expand Up @@ -80,28 +80,38 @@ impl Canvas {
.expect(&format!("Set attribute: {}", attribute));
}

pub fn position(&self) -> (f64, f64) {
pub fn position(&self) -> LogicalPosition<f64> {
let bounds = self.raw.get_bounding_client_rect();

(bounds.x(), bounds.y())
LogicalPosition {
x: bounds.x(),
y: bounds.y(),
}
}

pub fn size(&self) -> LogicalSize {
LogicalSize {
width: self.raw.width() as f64,
height: self.raw.height() as f64,
pub fn size(&self) -> PhysicalSize<u32> {
PhysicalSize {
width: self.raw.width(),
height: self.raw.height(),
}
}

pub fn set_size(&self, size: LogicalSize<f64>) {
let physical_size = size.to_physical(super::hidpi_factor());
pub fn set_size(&self, size: Size) {
let dpi_factor = super::hidpi_factor();

let physical_size = size.to_physical::<u32>(dpi_factor);
let logical_size = size.to_logical::<f64>(dpi_factor);

self.raw.set_width(physical_size.width as u32);
self.raw.set_height(physical_size.height as u32);
self.raw.set_width(physical_size.width);
self.raw.set_height(physical_size.height);

let style = self.raw.style();
let _todo = style.set_property("width", &format!("{}px", size.width));
let _todo = style.set_property("height", &format!("{}px", size.height));
style
.set_property("width", &format!("{}px", logical_size.width))
.unwrap();
style
.set_property("height", &format!("{}px", logical_size.height))
.unwrap();
}

pub fn raw(&self) -> &HtmlCanvasElement {
Expand Down Expand Up @@ -223,12 +233,12 @@ impl Canvas {

pub fn on_cursor_move<F>(&mut self, mut handler: F)
where
F: 'static + FnMut(i32, LogicalPosition, ModifiersState),
F: 'static + FnMut(i32, PhysicalPosition<i32>, ModifiersState),
{
self.on_cursor_move = Some(self.add_event("pointermove", move |event: PointerEvent| {
handler(
event.pointer_id(),
event::mouse_position(&event),
event::mouse_position(&event).to_physical(super::hidpi_factor()),
event::mouse_modifiers(&event),
);
}));
Expand Down
43 changes: 23 additions & 20 deletions src/platform_impl/web/window.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::dpi::{LogicalPosition, LogicalSize};
use crate::dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size};
use crate::error::{ExternalError, NotSupportedError, OsError as RootOE};
use crate::icon::Icon;
use crate::monitor::MonitorHandle as RootMH;
Expand Down Expand Up @@ -44,10 +44,10 @@ impl Window {
register_redraw_request,
};

window.set_inner_size(attr.inner_size.unwrap_or(LogicalSize {
window.set_inner_size(attr.inner_size.unwrap_or(Size::Logical(LogicalSize {
width: 1024.0,
height: 768.0,
}));
})));
window.set_title(&attr.title);
window.set_maximized(attr.maximized);
window.set_visible(attr.visible);
Expand All @@ -72,17 +72,21 @@ impl Window {
(self.register_redraw_request)();
}

pub fn outer_position(&self) -> Result<LogicalPosition<f64>, NotSupportedError> {
let (x, y) = self.canvas.position();

Ok(LogicalPosition { x, y })
pub fn outer_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
// todo: not supported?
// Ok(self.canvas.position().to_physical(self.hidpi_factor))
Err(NotSupportedError::new())
}

pub fn inner_position(&self) -> Result<LogicalPosition<f64>, NotSupportedError> {
Ok(*self.position.borrow())
pub fn inner_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
// todo: not supported?
// Ok(*self.position.borrow())
Err(NotSupportedError::new())
}

pub fn set_outer_position(&self, position: LogicalPosition<f64>) {
pub fn set_outer_position(&self, position: Position) {
let position = position.to_logical(self.hidpi_factor());

*self.position.borrow_mut() = position;

self.canvas.set_attribute("position", "fixed");
Expand All @@ -91,27 +95,29 @@ impl Window {
}

#[inline]
pub fn inner_size(&self) -> LogicalSize<f64> {
pub fn inner_size(&self) -> PhysicalSize<u32> {
// TODO
self.canvas.size()
}

#[inline]
pub fn outer_size(&self) -> LogicalSize<f64> {
pub fn outer_size(&self) -> PhysicalSize<u32> {
// TODO
self.canvas.size()
}

#[inline]
pub fn set_inner_size(&self, size: LogicalSize<f64>) {
pub fn set_inner_size(&self, size: Size) {
self.canvas.set_size(size);
}

#[inline]
pub fn set_min_inner_size(&self, _dimensions: Option<LogicalSize<f64>>) {
pub fn set_min_inner_size(&self, _dimensions: Option<Size>) {
// Intentionally a no-op: users can't resize canvas elements
}

#[inline]
pub fn set_max_inner_size(&self, _dimensions: Option<LogicalSize<f64>>) {
pub fn set_max_inner_size(&self, _dimensions: Option<Size>) {
// Intentionally a no-op: users can't resize canvas elements
}

Expand Down Expand Up @@ -172,10 +178,7 @@ impl Window {
}

#[inline]
pub fn set_cursor_position(
&self,
_position: LogicalPosition<f64>,
) -> Result<(), ExternalError> {
pub fn set_cursor_position(&self, _position: Position) -> Result<(), ExternalError> {
// Intentionally a no-op, as the web does not support setting cursor positions
Ok(())
}
Expand Down Expand Up @@ -235,7 +238,7 @@ impl Window {
}

#[inline]
pub fn set_ime_position(&self, _position: LogicalPosition<f64>) {
pub fn set_ime_position(&self, _position: Position) {
// Currently a no-op as it does not seem there is good support for this on web
}

Expand Down
2 changes: 2 additions & 0 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ impl Window {
///
/// - **iOS:** Can only be called on the main thread. Returns the top left coordinates of the
/// window in the screen space coordinate system.
/// - **Web:** Returns the top-left coordinates relative to the viewport. or, TODO: not supported?
#[inline]
pub fn outer_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
self.window.outer_position()
Expand All @@ -454,6 +455,7 @@ impl Window {
///
/// - **iOS:** Can only be called on the main thread. Sets the top left coordinates of the
/// window in the screen space coordinate system.
/// - **Web:** Sets the top-left coordinates relative to the viewport. or, TODO: not supported?
#[inline]
pub fn set_outer_position<P: Into<Position>>(&self, position: P) {
self.window.set_outer_position(position.into())
Expand Down

0 comments on commit f6716be

Please sign in to comment.