Skip to content

Commit fcda747

Browse files
daxpeddaToniman575
andauthored
Add from_canvas() to Surface for Wasm (#76)
Co-authored-by: Toniman575 <[email protected]>
1 parent ecfae19 commit fcda747

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

.github/workflows/ci.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,14 @@ jobs:
118118
!contains(matrix.platform.target, 'freebsd') &&
119119
!contains(matrix.platform.target, 'netbsd')
120120
run: cargo clippy --all-targets --target ${{ matrix.platform.target }} $OPTIONS --features $FEATURES -- -Dwarnings
121+
122+
- name: Lint with rustdoc
123+
shell: bash
124+
if: >
125+
(matrix.rust_version == 'stable') &&
126+
!contains(matrix.platform.options, '--no-default-features') &&
127+
!((matrix.platform.os == 'ubuntu-latest') && contains(matrix.platform.target, 'i686')) &&
128+
!contains(matrix.platform.target, 'redox') &&
129+
!contains(matrix.platform.target, 'freebsd') &&
130+
!contains(matrix.platform.target, 'netbsd')
131+
run: cargo doc --no-deps --target ${{ matrix.platform.target }} $OPTIONS --features $FEATURES --document-private-items

Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,14 @@ rayon = "1.5.1"
8585
members = [
8686
"run-wasm",
8787
]
88+
89+
[package.metadata.docs.rs]
90+
all-features = true
91+
rustdoc-args = ["--cfg", "docsrs"]
92+
default-target = "x86_64-unknown-linux-gnu"
93+
targets = [
94+
"x86_64-pc-windows-msvc",
95+
"x86_64-apple-darwin",
96+
"x86_64-unknown-linux-gnu",
97+
"wasm32-unknown-unknown",
98+
]

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![doc = include_str!("../README.md")]
22
#![deny(unsafe_op_in_unsafe_fn)]
33
#![warn(missing_docs)]
4+
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
45

56
#[cfg(target_os = "macos")]
67
#[macro_use]
@@ -35,6 +36,9 @@ use raw_window_handle::{
3536
HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle,
3637
};
3738

39+
#[cfg(target_arch = "wasm32")]
40+
pub use self::web::SurfaceExtWeb;
41+
3842
/// An instance of this struct contains the platform-specific data that must be managed in order to
3943
/// write to a window on that platform.
4044
pub struct Context {

src/web.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use web_sys::ImageData;
1111
use crate::error::SwResultExt;
1212
use crate::SoftBufferError;
1313
use std::convert::TryInto;
14+
use std::marker::PhantomData;
1415
use std::num::NonZeroU32;
1516

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

60+
Self::from_canvas(canvas)
61+
}
62+
63+
fn from_canvas(canvas: HtmlCanvasElement) -> Result<Self, SoftBufferError> {
5964
let ctx = canvas
6065
.get_context("2d")
6166
.ok()
@@ -96,6 +101,23 @@ impl WebImpl {
96101
}
97102
}
98103

104+
/// Extension methods for the Wasm target on [`Surface`](crate::Surface).
105+
pub trait SurfaceExtWeb: Sized {
106+
/// Creates a new instance of this struct, using the provided [`HtmlCanvasElement`].
107+
fn from_canvas(canvas: HtmlCanvasElement) -> Result<Self, SoftBufferError>;
108+
}
109+
110+
impl SurfaceExtWeb for crate::Surface {
111+
fn from_canvas(canvas: HtmlCanvasElement) -> Result<Self, SoftBufferError> {
112+
let imple = crate::SurfaceDispatch::Web(WebImpl::from_canvas(canvas)?);
113+
114+
Ok(Self {
115+
surface_impl: Box::new(imple),
116+
_marker: PhantomData,
117+
})
118+
}
119+
}
120+
99121
pub struct BufferImpl<'a> {
100122
imp: &'a mut WebImpl,
101123
}

0 commit comments

Comments
 (0)