-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
767 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
// Copyright 2023 the Druid Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::borrow::Cow; | ||
use std::{any::Any, marker::PhantomData}; | ||
|
||
use peniko::Brush; | ||
use wasm_bindgen::JsCast; | ||
use xilem_core::{Id, MessageResult}; | ||
|
||
use crate::IntoAttributeValue; | ||
use crate::{ | ||
context::{ChangeFlags, Cx}, | ||
view::{DomNode, View, ViewMarker}, | ||
}; | ||
|
||
pub struct Fill<T, A, V> { | ||
child: V, | ||
// This could reasonably be static Cow also, but keep things simple | ||
brush: Brush, | ||
phantom: PhantomData<fn() -> (T, A)>, | ||
} | ||
|
||
pub struct Stroke<T, A, V> { | ||
child: V, | ||
// This could reasonably be static Cow also, but keep things simple | ||
brush: Brush, | ||
style: peniko::kurbo::Stroke, | ||
phantom: PhantomData<fn() -> (T, A)>, | ||
} | ||
|
||
pub fn fill<T, A, V>(child: V, brush: impl Into<Brush>) -> Fill<T, A, V> { | ||
Fill { | ||
child, | ||
brush: brush.into(), | ||
phantom: Default::default(), | ||
} | ||
} | ||
|
||
pub fn stroke<T, A, V>( | ||
child: V, | ||
brush: impl Into<Brush>, | ||
style: peniko::kurbo::Stroke, | ||
) -> Stroke<T, A, V> { | ||
Stroke { | ||
child, | ||
brush: brush.into(), | ||
style, | ||
phantom: Default::default(), | ||
} | ||
} | ||
|
||
fn brush_to_string(brush: &Brush) -> String { | ||
match brush { | ||
Brush::Solid(color) => { | ||
if color.a == 0 { | ||
"none".into() | ||
} else { | ||
format!("#{:02x}{:02x}{:02x}", color.r, color.g, color.b) | ||
} | ||
} | ||
_ => todo!("gradients not implemented"), | ||
} | ||
} | ||
|
||
macro_rules! impl_dom_interface_for_ty { | ||
($dom_interface:ident, $ty:ident) => { | ||
impl<T, A, E: $crate::interfaces::$dom_interface<T, A>> | ||
$crate::interfaces::$dom_interface<T, A> for $ty<T, A, E> | ||
{ | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! impl_dom_interfaces_for_ty { | ||
($ty:ident: $($dom_interface:ident,)*) => { | ||
$(impl_dom_interface_for_ty!($dom_interface, $ty);)* | ||
} | ||
} | ||
|
||
// TODO it would be great to have a macro that does automatically derive all child interfaces of some DOM interface... | ||
impl_dom_interfaces_for_ty!(Fill: | ||
Element, | ||
SvgElement, | ||
SvgGraphicsElement, | ||
SvgGeometryElement, | ||
SvgRectElement, | ||
SvgPathElement, | ||
SvgCircleElement, | ||
SvgLineElement, | ||
); | ||
|
||
impl<T, A, V> ViewMarker for Fill<T, A, V> {} | ||
impl<T, A, V> crate::interfaces::sealed::Sealed for Fill<T, A, V> {} | ||
|
||
// TODO: make generic over A (probably requires Phantom) | ||
impl<T, A, V: View<T, A>> View<T, A> for Fill<T, A, V> { | ||
type State = V::State; | ||
type Element = V::Element; | ||
|
||
fn build(&self, cx: &mut Cx) -> (Id, Self::State, Self::Element) { | ||
let (id, child_state, element) = self.child.build(cx); | ||
element | ||
.as_node_ref() | ||
.dyn_ref::<web_sys::Element>() | ||
.unwrap() | ||
.set_attribute("fill", &brush_to_string(&self.brush)) | ||
.unwrap(); | ||
(id, child_state, element) | ||
} | ||
|
||
fn rebuild( | ||
&self, | ||
cx: &mut Cx, | ||
prev: &Self, | ||
id: &mut Id, | ||
state: &mut Self::State, | ||
element: &mut V::Element, | ||
) -> ChangeFlags { | ||
let prev_id = *id; | ||
let mut changed = self.child.rebuild(cx, &prev.child, id, state, element); | ||
if self.brush != prev.brush || prev_id != *id { | ||
element | ||
.as_node_ref() | ||
.dyn_ref::<web_sys::Element>() | ||
.unwrap() | ||
.set_attribute("fill", &brush_to_string(&self.brush)) | ||
.unwrap(); | ||
changed.insert(ChangeFlags::OTHER_CHANGE); | ||
} | ||
changed | ||
} | ||
|
||
fn message( | ||
&self, | ||
id_path: &[Id], | ||
state: &mut Self::State, | ||
message: Box<dyn Any>, | ||
app_state: &mut T, | ||
) -> MessageResult<A> { | ||
self.child.message(id_path, state, message, app_state) | ||
} | ||
} | ||
|
||
impl_dom_interfaces_for_ty!(Stroke: | ||
Element, | ||
SvgElement, | ||
SvgGraphicsElement, | ||
SvgGeometryElement, | ||
SvgRectElement, | ||
SvgPathElement, | ||
SvgCircleElement, | ||
SvgLineElement, | ||
); | ||
|
||
impl<T, A, V> ViewMarker for Stroke<T, A, V> {} | ||
impl<T, A, V> crate::interfaces::sealed::Sealed for Stroke<T, A, V> {} | ||
|
||
impl<T, A, V: View<T, A>> View<T, A> for Stroke<T, A, V> { | ||
type State = (Cow<'static, str>, V::State); | ||
type Element = V::Element; | ||
|
||
fn build(&self, cx: &mut Cx) -> (Id, Self::State, Self::Element) { | ||
let brush_svg_repr = Cow::from(brush_to_string(&self.brush)); | ||
cx.add_new_attribute_to_current_element( | ||
&"stroke".into(), | ||
&brush_svg_repr.clone().into_attribute_value(), | ||
); | ||
cx.add_new_attribute_to_current_element( | ||
&"stroke-width".into(), | ||
&self.style.width.into_attribute_value(), | ||
); | ||
let (id, child_state, element) = self.child.build(cx); | ||
(id, (brush_svg_repr, child_state), element) | ||
} | ||
|
||
fn rebuild( | ||
&self, | ||
cx: &mut Cx, | ||
prev: &Self, | ||
id: &mut Id, | ||
(brush_svg_repr, child_state): &mut Self::State, | ||
element: &mut V::Element, | ||
) -> ChangeFlags { | ||
if self.brush != prev.brush { | ||
*brush_svg_repr = Cow::from(brush_to_string(&self.brush)); | ||
} | ||
cx.add_new_attribute_to_current_element( | ||
&"stroke".into(), | ||
&brush_svg_repr.clone().into_attribute_value(), | ||
); | ||
cx.add_new_attribute_to_current_element( | ||
&"stroke-width".into(), | ||
&self.style.width.into_attribute_value(), | ||
); | ||
self.child | ||
.rebuild(cx, &prev.child, id, child_state, element) | ||
} | ||
|
||
fn message( | ||
&self, | ||
id_path: &[Id], | ||
(_, child_state): &mut Self::State, | ||
message: Box<dyn Any>, | ||
app_state: &mut T, | ||
) -> MessageResult<A> { | ||
self.child.message(id_path, child_state, message, app_state) | ||
} | ||
} |
Oops, something went wrong.