Skip to content

Commit

Permalink
Merge pull request #16 from Zoxc/any-view
Browse files Browse the repository at this point in the history
Uncomment AnyView and update it to use ChangeFlags
  • Loading branch information
Zoxc authored Dec 2, 2022
2 parents 3d99fdf + c713974 commit 13b65e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

// pub mod adapt;
// pub mod any_view;
pub mod any_view;
// pub mod async_list;
pub mod button;
// pub mod layout_observer;
Expand Down
20 changes: 13 additions & 7 deletions src/view/any_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ use std::{
ops::{Deref, DerefMut},
};

use crate::{event::EventResult, id::Id, widget::AnyWidget};
use crate::{
event::EventResult,
id::Id,
widget::{AnyWidget, ChangeFlags},
};

use super::{Cx, View};

Expand All @@ -41,7 +45,7 @@ pub trait AnyView<T, A = ()> {
id: &mut Id,
state: &mut Box<dyn Any + Send>,
element: &mut Box<dyn AnyWidget>,
) -> bool;
) -> ChangeFlags;

fn dyn_event(
&self,
Expand Down Expand Up @@ -73,25 +77,27 @@ where
id: &mut Id,
state: &mut Box<dyn Any + Send>,
element: &mut Box<dyn AnyWidget>,
) -> bool {
) -> ChangeFlags {
if let Some(prev) = prev.as_any().downcast_ref() {
if let Some(state) = state.downcast_mut() {
if let Some(element) = element.deref_mut().as_any_mut().downcast_mut() {
self.rebuild(cx, prev, id, state, element)
} else {
println!("downcast of element failed in dyn_rebuild");
false
ChangeFlags::empty()
}
} else {
println!("downcast of state failed in dyn_rebuild");
false
ChangeFlags::empty()
}
} else {
let (new_id, new_state, new_element) = self.build(cx);
*id = new_id;
*state = Box::new(new_state);
*element = Box::new(new_element);
true

// Everything about the new view could be different, so return all the flags
ChangeFlags::all()
}
}

Expand Down Expand Up @@ -127,7 +133,7 @@ impl<T, A> View<T, A> for Box<dyn AnyView<T, A> + Send> {
id: &mut Id,
state: &mut Self::State,
element: &mut Self::Element,
) -> bool {
) -> ChangeFlags {
self.deref()
.dyn_rebuild(cx, prev.deref(), id, state, element)
}
Expand Down

0 comments on commit 13b65e2

Please sign in to comment.