Skip to content

Commit

Permalink
feat(core): 🎸 introduce QueryId for querying types across different b…
Browse files Browse the repository at this point in the history
…inaries
  • Loading branch information
M-Adoo committed Nov 16, 2024
1 parent 284ef55 commit 342c86d
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 127 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he
### Features

- **core**: The `keyframes!` macro has been introduced to manage the intermediate steps of animation states. (#653 @M-Adoo)
- **core**: Added `QueryId` as a replacement for `TypeId` to facilitate querying types by Provider across different binaries. (#pr @M-Adoo)

### Fixed

Expand Down
16 changes: 10 additions & 6 deletions core/src/builtin_widgets/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,25 +320,29 @@ impl RenderProxy for ClassChild {
}

impl Query for OrigChild {
fn query_all<'q>(&'q self, type_id: TypeId, out: &mut smallvec::SmallVec<[QueryHandle<'q>; 1]>) {
fn query_all<'q>(
&'q self, type_id: &QueryId, out: &mut smallvec::SmallVec<[QueryHandle<'q>; 1]>,
) {
self.node().query_all(type_id, out)
}

fn query(&self, type_id: TypeId) -> Option<QueryHandle> { self.node().query(type_id) }
fn query(&self, type_id: &QueryId) -> Option<QueryHandle> { self.node().query(type_id) }

fn query_write(&self, type_id: TypeId) -> Option<QueryHandle> {
fn query_write(&self, type_id: &QueryId) -> Option<QueryHandle> {
self.node_mut().query_write(type_id)
}
}

impl Query for ClassChild {
fn query_all<'q>(&'q self, type_id: TypeId, out: &mut smallvec::SmallVec<[QueryHandle<'q>; 1]>) {
fn query_all<'q>(
&'q self, type_id: &QueryId, out: &mut smallvec::SmallVec<[QueryHandle<'q>; 1]>,
) {
self.inner().child.query_all(type_id, out)
}

fn query(&self, type_id: TypeId) -> Option<QueryHandle> { self.inner().child.query(type_id) }
fn query(&self, type_id: &QueryId) -> Option<QueryHandle> { self.inner().child.query(type_id) }

fn query_write(&self, type_id: TypeId) -> Option<QueryHandle> {
fn query_write(&self, type_id: &QueryId) -> Option<QueryHandle> {
self.inner().child.query_write(type_id)
}
}
Expand Down
16 changes: 8 additions & 8 deletions core/src/builtin_widgets/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl BuildCtx {
.current_providers
.iter()
.rev()
.filter_map(|p| p.query(TypeId::of::<Q>()))
.filter_map(|p| p.query(&QueryId::of::<Q>()))
.filter_map(QueryHandle::into_ref)
}

Expand All @@ -161,7 +161,7 @@ impl BuildCtx {
.current_providers
.iter()
.rev()
.filter_map(|p| p.query_write(TypeId::of::<Q>()))
.filter_map(|p| p.query_write(&QueryId::of::<Q>()))
.filter_map(QueryHandle::into_mut)
}
}
Expand Down Expand Up @@ -207,18 +207,18 @@ impl<T: Deref<Target: WidgetCtxImpl>> ProviderCtx for T {
}

impl<const M: usize> Query for [Box<dyn Query>; M] {
fn query_all<'q>(&'q self, type_id: TypeId, out: &mut SmallVec<[QueryHandle<'q>; 1]>) {
fn query_all<'q>(&'q self, query_id: &QueryId, out: &mut SmallVec<[QueryHandle<'q>; 1]>) {
self
.iter()
.for_each(|q| q.query_all(type_id, out))
.for_each(|q| q.query_all(query_id, out))
}

fn query(&self, type_id: TypeId) -> Option<QueryHandle> {
self.iter().find_map(|q| q.query(type_id))
fn query(&self, query_id: &QueryId) -> Option<QueryHandle> {
self.iter().find_map(|q| q.query(query_id))
}

fn query_write(&self, type_id: TypeId) -> Option<QueryHandle> {
self.iter().find_map(|q| q.query_write(type_id))
fn query_write(&self, query_id: &QueryId) -> Option<QueryHandle> {
self.iter().find_map(|q| q.query_write(query_id))
}

fn queryable(&self) -> bool { true }
Expand Down
40 changes: 20 additions & 20 deletions core/src/builtin_widgets/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,33 +142,33 @@ impl ComposeChild<'static> for Theme {
struct ThemeQuerier<T: StateWriter<Value = Theme>>(T);

impl<T: StateWriter<Value = Theme>> Query for ThemeQuerier<T> {
fn query_all<'q>(&'q self, type_id: TypeId, out: &mut SmallVec<[QueryHandle<'q>; 1]>) {
fn query_all<'q>(&'q self, query_id: &QueryId, out: &mut SmallVec<[QueryHandle<'q>; 1]>) {
// The value of the writer and the writer itself cannot be queried
// at the same time.
if let Some(h) = self.query(type_id) {
if let Some(h) = self.query(query_id) {
out.push(h)
}
}

fn query(&self, type_id: TypeId) -> Option<QueryHandle> {
fn query(&self, query_id: &QueryId) -> Option<QueryHandle> {
ReadRef::filter_map(self.0.read(), |v: &Theme| {
let w: Option<&dyn Any> = if TypeId::of::<Theme>() == type_id {
let w: Option<&dyn QueryAny> = if &QueryId::of::<Theme>() == query_id {
Some(v)
} else if TypeId::of::<Palette>() == type_id {
} else if &QueryId::of::<Palette>() == query_id {
Some(&v.palette)
} else if TypeId::of::<TypographyTheme>() == type_id {
} else if &QueryId::of::<TypographyTheme>() == query_id {
Some(&v.typography_theme)
} else if TypeId::of::<TextStyle>() == type_id {
} else if &QueryId::of::<TextStyle>() == query_id {
Some(&v.typography_theme.body_medium.text)
} else if TypeId::of::<Classes>() == type_id {
} else if &QueryId::of::<Classes>() == query_id {
Some(&v.classes)
} else if TypeId::of::<IconTheme>() == type_id {
} else if &QueryId::of::<IconTheme>() == query_id {
Some(&v.icon_theme)
} else if TypeId::of::<TransitionTheme>() == type_id {
} else if &QueryId::of::<TransitionTheme>() == query_id {
Some(&v.transitions_theme)
} else if TypeId::of::<ComposeDecorators>() == type_id {
} else if &QueryId::of::<ComposeDecorators>() == query_id {
Some(&v.compose_decorators)
} else if TypeId::of::<CustomStyles>() == type_id {
} else if &QueryId::of::<CustomStyles>() == query_id {
Some(&v.custom_styles)
} else {
None
Expand All @@ -179,21 +179,21 @@ impl<T: StateWriter<Value = Theme>> Query for ThemeQuerier<T> {
.map(QueryHandle::from_read_ref)
}

fn query_write(&self, type_id: TypeId) -> Option<QueryHandle> {
fn query_write(&self, query_id: &QueryId) -> Option<QueryHandle> {
WriteRef::filter_map(self.0.write(), |v: &mut Theme| {
let w: Option<&mut dyn Any> = if TypeId::of::<Theme>() == type_id {
let w: Option<&mut dyn QueryAny> = if &QueryId::of::<Theme>() == query_id {
Some(v)
} else if TypeId::of::<Palette>() == type_id {
} else if &QueryId::of::<Palette>() == query_id {
Some(&mut v.palette)
} else if TypeId::of::<TypographyTheme>() == type_id {
} else if &QueryId::of::<TypographyTheme>() == query_id {
Some(&mut v.typography_theme)
} else if TypeId::of::<IconTheme>() == type_id {
} else if &QueryId::of::<IconTheme>() == query_id {
Some(&mut v.icon_theme)
} else if TypeId::of::<TransitionTheme>() == type_id {
} else if &QueryId::of::<TransitionTheme>() == query_id {
Some(&mut v.transitions_theme)
} else if TypeId::of::<ComposeDecorators>() == type_id {
} else if &QueryId::of::<ComposeDecorators>() == query_id {
Some(&mut v.compose_decorators)
} else if TypeId::of::<CustomStyles>() == type_id {
} else if &QueryId::of::<CustomStyles>() == query_id {
Some(&mut v.custom_styles)
} else {
None
Expand Down
28 changes: 15 additions & 13 deletions core/src/data_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,42 @@ impl RenderProxy for DataAttacher {
}

impl Query for DataAttacher {
fn query_all<'q>(&'q self, type_id: TypeId, out: &mut SmallVec<[QueryHandle<'q>; 1]>) {
self.render.query_all(type_id, out);
if let Some(h) = self.data.query(type_id) {
fn query_all<'q>(&'q self, query_id: &QueryId, out: &mut SmallVec<[QueryHandle<'q>; 1]>) {
self.render.query_all(query_id, out);
if let Some(h) = self.data.query(query_id) {
out.push(h)
}
}

fn query(&self, type_id: TypeId) -> Option<QueryHandle> {
fn query(&self, query_id: &QueryId) -> Option<QueryHandle> {
self
.render
.query(type_id)
.or_else(|| self.data.query(type_id))
.query(query_id)
.or_else(|| self.data.query(query_id))
}

fn query_write(&self, type_id: TypeId) -> Option<QueryHandle> {
fn query_write(&self, query_id: &QueryId) -> Option<QueryHandle> {
self
.render
.query_write(type_id)
.or_else(|| self.data.query_write(type_id))
.query_write(query_id)
.or_else(|| self.data.query_write(query_id))
}

fn queryable(&self) -> bool { true }
}

impl Query for AnonymousAttacher {
#[inline]
fn query_all<'q>(&'q self, type_id: TypeId, out: &mut SmallVec<[QueryHandle<'q>; 1]>) {
self.render.query_all(type_id, out)
fn query_all<'q>(&'q self, query_id: &QueryId, out: &mut SmallVec<[QueryHandle<'q>; 1]>) {
self.render.query_all(query_id, out)
}

#[inline]
fn query(&self, type_id: TypeId) -> Option<QueryHandle> { self.render.query(type_id) }
fn query(&self, query_id: &QueryId) -> Option<QueryHandle> { self.render.query(query_id) }

fn query_write(&self, type_id: TypeId) -> Option<QueryHandle> { self.render.query_write(type_id) }
fn query_write(&self, type_id: &QueryId) -> Option<QueryHandle> {
self.render.query_write(type_id)
}

fn queryable(&self) -> bool { self.render.queryable() }
}
Expand Down
16 changes: 8 additions & 8 deletions core/src/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,23 +801,23 @@ fn pipe_priority_value(info: &DynInfo, tree: &WidgetTree) -> i64 {
}

impl Query for PipeNode {
fn query_all<'q>(&'q self, type_id: TypeId, out: &mut SmallVec<[QueryHandle<'q>; 1]>) {
fn query_all<'q>(&'q self, query_id: &QueryId, out: &mut SmallVec<[QueryHandle<'q>; 1]>) {
let p = self.as_ref();
p.data.query_all(type_id, out);
if type_id == TypeId::of::<DynInfo>() {
p.data.query_all(query_id, out);
if query_id == &QueryId::of::<DynInfo>() {
out.push(QueryHandle::new(&p.dyn_info))
}
}

fn query(&self, type_id: TypeId) -> Option<QueryHandle> {
fn query(&self, query_id: &QueryId) -> Option<QueryHandle> {
let p = self.as_ref();
p.data
.query(type_id)
.or_else(|| (type_id == TypeId::of::<DynInfo>()).then(|| QueryHandle::new(&p.dyn_info)))
.query(query_id)
.or_else(|| (query_id == &QueryId::of::<DynInfo>()).then(|| QueryHandle::new(&p.dyn_info)))
}

fn query_write(&self, type_id: TypeId) -> Option<QueryHandle> {
self.as_ref().data.query_write(type_id)
fn query_write(&self, query_id: &QueryId) -> Option<QueryHandle> {
self.as_ref().data.query_write(query_id)
}

fn queryable(&self) -> bool { true }
Expand Down
Loading

0 comments on commit 342c86d

Please sign in to comment.