browser_video_capture/
macros.rs1#[macro_export]
2macro_rules! js_set {
3 ($target:expr, $field:literal, $value:expr) => {
4 web_sys::js_sys::Reflect::set(
5 $target.as_ref(),
6 &web_sys::wasm_bindgen::JsValue::from($field),
7 &$value.into(),
8 )
9 .unwrap()
10 .then_some(())
11 .unwrap();
12 };
13}
14
15#[macro_export]
16macro_rules! impl_canvas_capture_area {
17 ($name:ty) => {
18 impl crate::CaptureArea for $name {
19 fn capture_width(&self) -> u32 {
20 self.canvas.width()
21 }
22
23 fn capture_height(&self) -> u32 {
24 self.canvas.height()
25 }
26
27 fn set_capture_width(&self, width: u32) {
28 self.canvas.set_width(width);
29 }
30
31 fn set_capture_height(&self, height: u32) {
32 self.canvas.set_height(height);
33 }
34 }
35 };
36}
37
38#[macro_export]
39macro_rules! get_context {
40 ("2d" $name:literal, $canvas:expr, $options:expr) => {
41 $canvas.get_context_with_context_options("2d", &$options.into())
42 };
43 ("webgl" $name:literal, $canvas:expr, $options:expr) => {
44 $canvas.get_context_with_context_options(
45 $options
46 .experimental
47 .then_some(concat!($name, "-experimental"))
48 .unwrap_or($name),
49 &$options.into(),
50 )
51 };
52 ("webgl2" $name:literal, $canvas:expr, $options:expr) => {
53 $canvas.get_context_with_context_options(
54 $options
55 .experimental
56 .then_some(concat!($name, "-experimental"))
57 .unwrap_or($name),
58 &$options.into(),
59 )
60 };
61}
62
63#[macro_export]
64macro_rules! impl_capture_from_canvas {
65 ($id:tt, $capture:ty, $canvas:ty, $context:ty, $option:ty) => {
66 impl $capture {
67 pub fn from_canvas(canvas: $canvas, color: crate::CaptureColor) -> Result<Option<Self>, js_sys::Error> {
68 canvas
69 .get_context($id)
70 .map(|value| {
71 value
72 .map(|obj| obj.dyn_into::<$context>().unwrap())
73 .map(|context| Self::new(canvas, context, color))
74 })
75 .map_err(|value| value.into())
76 }
77
78 pub fn from_canvas_with_options(
79 canvas: $canvas,
80 color: crate::CaptureColor,
81 options: $option,
82 ) -> Result<Option<Self>, js_sys::Error> {
83 get_context!($id $id, canvas, options)
84 .map(|value| {
85 value
86 .map(|obj| obj.dyn_into::<$context>().unwrap())
87 .map(|context| Self::new(canvas, context, color))
88 })
89 .map_err(|value| value.into())
90 }
91 }
92 };
93}
94
95#[macro_export]
96macro_rules! options_field {
97 ("" $obj:expr, $alias:literal, $value:expr) => {
98 };
99
100 ($field:tt $obj:expr, $alias:literal, $value:expr) => {
101 js_set!($obj, $alias, $value);
102 };
103}
104
105#[macro_export]
106macro_rules! impl_context_options {
107 ($name:tt $($alias:literal $field:tt: $typ:ty),+) => {
108 #[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
109 pub struct $name {
110 $(pub $field: $typ,)+
111 }
112
113 impl $name {
114 $(pub fn $field(mut self, value: $typ) -> Self {
115 self.$field = value;
116 self
117 })+
118 }
119
120 impl Into<JsValue> for $name {
121 fn into(self) -> JsValue {
122 let options = js_sys::Object::new();
123 $(options_field!($alias options, $alias, self.$field);)+
124 options.into()
125 }
126 }
127 };
128}