-
Notifications
You must be signed in to change notification settings - Fork 121
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
738 additions
and
2 deletions.
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,170 @@ | ||
// 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 xilem_core::{Id, MessageResult}; | ||
|
||
use crate::IntoAttributeValue; | ||
use crate::{ | ||
context::{ChangeFlags, Cx}, | ||
view::{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"), | ||
} | ||
} | ||
|
||
crate::interfaces::impl_dom_interfaces_for_ty!(SvgGeometryElement, Fill); | ||
|
||
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 = (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( | ||
&"fill".into(), | ||
&brush_svg_repr.clone().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( | ||
&"fill".into(), | ||
&brush_svg_repr.clone().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) | ||
} | ||
} | ||
|
||
crate::interfaces::impl_dom_interfaces_for_ty!(SvgGeometryElement, Stroke); | ||
|
||
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.