Skip to content

Commit

Permalink
Add from_canvas() to Surface for Wasm (#76)
Browse files Browse the repository at this point in the history
Co-authored-by: Toniman575 <[email protected]>
  • Loading branch information
daxpedda and Toniman575 authored May 8, 2023
1 parent ecfae19 commit fcda747
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,14 @@ jobs:
!contains(matrix.platform.target, 'freebsd') &&
!contains(matrix.platform.target, 'netbsd')
run: cargo clippy --all-targets --target ${{ matrix.platform.target }} $OPTIONS --features $FEATURES -- -Dwarnings

- name: Lint with rustdoc
shell: bash
if: >
(matrix.rust_version == 'stable') &&
!contains(matrix.platform.options, '--no-default-features') &&
!((matrix.platform.os == 'ubuntu-latest') && contains(matrix.platform.target, 'i686')) &&
!contains(matrix.platform.target, 'redox') &&
!contains(matrix.platform.target, 'freebsd') &&
!contains(matrix.platform.target, 'netbsd')
run: cargo doc --no-deps --target ${{ matrix.platform.target }} $OPTIONS --features $FEATURES --document-private-items
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,14 @@ rayon = "1.5.1"
members = [
"run-wasm",
]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
default-target = "x86_64-unknown-linux-gnu"
targets = [
"x86_64-pc-windows-msvc",
"x86_64-apple-darwin",
"x86_64-unknown-linux-gnu",
"wasm32-unknown-unknown",
]
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![doc = include_str!("../README.md")]
#![deny(unsafe_op_in_unsafe_fn)]
#![warn(missing_docs)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

#[cfg(target_os = "macos")]
#[macro_use]
Expand Down Expand Up @@ -35,6 +36,9 @@ use raw_window_handle::{
HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle,
};

#[cfg(target_arch = "wasm32")]
pub use self::web::SurfaceExtWeb;

/// An instance of this struct contains the platform-specific data that must be managed in order to
/// write to a window on that platform.
pub struct Context {
Expand Down
22 changes: 22 additions & 0 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use web_sys::ImageData;
use crate::error::SwResultExt;
use crate::SoftBufferError;
use std::convert::TryInto;
use std::marker::PhantomData;
use std::num::NonZeroU32;

/// Display implementation for the web platform.
Expand Down Expand Up @@ -56,6 +57,10 @@ impl WebImpl {
// We already made sure this was a canvas in `querySelector`.
.unchecked_into();

Self::from_canvas(canvas)
}

fn from_canvas(canvas: HtmlCanvasElement) -> Result<Self, SoftBufferError> {
let ctx = canvas
.get_context("2d")
.ok()
Expand Down Expand Up @@ -96,6 +101,23 @@ impl WebImpl {
}
}

/// Extension methods for the Wasm target on [`Surface`](crate::Surface).
pub trait SurfaceExtWeb: Sized {
/// Creates a new instance of this struct, using the provided [`HtmlCanvasElement`].
fn from_canvas(canvas: HtmlCanvasElement) -> Result<Self, SoftBufferError>;
}

impl SurfaceExtWeb for crate::Surface {
fn from_canvas(canvas: HtmlCanvasElement) -> Result<Self, SoftBufferError> {
let imple = crate::SurfaceDispatch::Web(WebImpl::from_canvas(canvas)?);

Ok(Self {
surface_impl: Box::new(imple),
_marker: PhantomData,
})
}
}

pub struct BufferImpl<'a> {
imp: &'a mut WebImpl,
}
Expand Down

0 comments on commit fcda747

Please sign in to comment.