Skip to content

Commit

Permalink
Fix Fill (use new helper context functions) and some smaller fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp-M committed Nov 5, 2023
1 parent af8d58d commit c92231d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 38 deletions.
2 changes: 1 addition & 1 deletion crates/xilem_html/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ mod app;
mod attribute;
mod attribute_value;
mod context;
pub mod svg;
mod diff;
pub mod elements;
pub mod events;
pub mod interfaces;
mod one_of;
mod optional_action;
pub mod svg;
mod vecmap;
mod view;
mod view_ext;
Expand Down
59 changes: 25 additions & 34 deletions crates/xilem_html/src/svg/common_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ 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},
view::{View, ViewMarker},
};

pub struct Fill<T, A, V> {
Expand Down Expand Up @@ -63,18 +62,14 @@ fn brush_to_string(brush: &Brush) -> String {
}
}

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);)*
$(
impl<T, A, E: $crate::interfaces::$dom_interface<T, A>>
$crate::interfaces::$dom_interface<T, A> for $ty<T, A, E>
{
}
)*
}
}

Expand All @@ -95,50 +90,46 @@ 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 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);
element
.as_node_ref()
.dyn_ref::<web_sys::Element>()
.unwrap()
.set_attribute("fill", &brush_to_string(&self.brush))
.unwrap();
(id, child_state, element)
(id, (brush_svg_repr, child_state), element)
}

fn rebuild(
&self,
cx: &mut Cx,
prev: &Self,
id: &mut Id,
state: &mut Self::State,
(brush_svg_repr, child_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);
if self.brush != prev.brush {
*brush_svg_repr = Cow::from(brush_to_string(&self.brush));
}
changed
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],
state: &mut Self::State,
(_, child_state): &mut Self::State,
message: Box<dyn Any>,
app_state: &mut T,
) -> MessageResult<A> {
self.child.message(id_path, state, message, app_state)
self.child.message(id_path, child_state, message, app_state)
}
}

Expand Down
4 changes: 1 addition & 3 deletions crates/xilem_html/src/svg/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ impl<T, A, F: Fn(&mut T, PointerMsg) -> A + Send, V: View<T, A>> View<T, A>
app_state: &mut T,
) -> MessageResult<A> {
match message.downcast() {
Ok(msg) => {
MessageResult::Action((self.callback)(app_state, *msg))
}
Ok(msg) => MessageResult::Action((self.callback)(app_state, *msg)),
Err(message) => self
.child
.message(id_path, &mut state.child_state, message, app_state),
Expand Down

0 comments on commit c92231d

Please sign in to comment.