pub struct ScopeID<'a> { /* private fields */ }
Expand description
Represents a scope with a particular ID assigned to it. Used to generate new
IDs for anything created inside this scope, with this scope’s ID as parents,
or to generate a new ScopeID
with this scope as its parent. Includes
ScopeID::iter
and ScopeID::cond
to make it easier to generate stable
IDs across control flow boundaries. It can be used in conjunction with
gen_id
to generate child IDs with source and line numbers.
§Examples
use feather_ui::layout::fixed;
use feather_ui::component::{shape, region::Region};
use feather_ui::{ScopeID, gen_id, DRect, DAbsPoint, color::sRGB, FILL_DRECT, children };
fn foobar(mut id: ScopeID<'_>) {
let rect = shape::round_rect::<DRect>(
gen_id!(id),
FILL_DRECT,
0.0,
0.0,
wide::f32x4::splat(10.0),
sRGB::new(0.2, 0.7, 0.4, 1.0),
sRGB::transparent(),
DAbsPoint::zero(),
);
let region = Region::<DRect>::new(
id.create(),
FILL_DRECT,
children![fixed::Prop, rect],
);
}
Implementations§
Source§impl<'a> ScopeID<'a>
impl<'a> ScopeID<'a>
Sourcepub fn id(&mut self) -> &Arc<SourceID>
pub fn id(&mut self) -> &Arc<SourceID>
Gets the underlying ID for this scope. This is sometimes useful when creating custom scopes that belong to a specific component.
Sourcepub fn create(&mut self) -> Arc<SourceID>
pub fn create(&mut self) -> Arc<SourceID>
Creates a new unique SourceID
using an internal counter with this
scope as it’s parent.
§Examples
use feather_ui::component::shape;
use feather_ui::{ScopeID, DRect, DAbsPoint, color::sRGB, FILL_DRECT };
fn foobar(mut id: ScopeID<'_>) {
let rect = shape::round_rect::<DRect>(
id.create(),
FILL_DRECT,
0.0,
0.0,
wide::f32x4::splat(10.0),
sRGB::new(0.2, 0.7, 0.4, 1.0),
sRGB::transparent(),
DAbsPoint::zero(),
);
}
Sourcepub fn scope(&mut self) -> ScopeID<'_>
pub fn scope(&mut self) -> ScopeID<'_>
Creates a new scoped ID with this scope as it’s parent that can then be passed into a function.
Sourcepub fn child(&mut self, id: DataID) -> Arc<SourceID>
pub fn child(&mut self, id: DataID) -> Arc<SourceID>
Creates a new unique SourceID using the provided DataID, which bypasses
the internal counter. This can be used to either manually create a
new SourceID from a custom DataID by the user, or by calling
gen_id
, which calls this function internally.
Sourcepub fn iter<U: IntoIterator>(
&mut self,
other: U,
) -> ScopeIterID<'_, U::IntoIter> ⓘ
pub fn iter<U: IntoIterator>( &mut self, other: U, ) -> ScopeIterID<'_, U::IntoIter> ⓘ
Wraps another iterator and returns a pair of both a unique ID and the result of the iterator. Required for outline functions that use a for loop when iterating through data.
§Examples
use feather_ui::component::shape;
use feather_ui::{ScopeID, DRect, DAbsPoint, color::sRGB, FILL_DRECT };
fn foobar(count: usize, mut scope: ScopeID<'_>) {
for (i, id) in scope.iter(0..count) {
let _ = shape::round_rect::<DRect>(
id,
FILL_DRECT,
i as f32,
0.0,
wide::f32x4::splat(4.0),
sRGB::transparent(),
sRGB::transparent(),
DAbsPoint::zero(),
);
}
}
Sourcepub fn cond<R>(
&mut self,
condition: bool,
tvalue: impl FnOnce(ScopeID<'_>) -> R,
fvalue: impl FnOnce(ScopeID<'_>) -> R,
) -> R
pub fn cond<R>( &mut self, condition: bool, tvalue: impl FnOnce(ScopeID<'_>) -> R, fvalue: impl FnOnce(ScopeID<'_>) -> R, ) -> R
Wraps the true and false branches of a condition, ensuring the IDs for both are maintained separately regardless of which branch is picked.
§Examples
use feather_ui::component::{shape, ComponentWrap, text::Text};
use feather_ui::{ScopeID, DRect, DAbsPoint, color::sRGB, FILL_DRECT };
fn foobar(cond: bool, mut scope: ScopeID<'_>) {
let _ = scope.cond::<Box<dyn ComponentWrap<dyn feather_ui::layout::base::Empty>>>(
cond,
|mut id: ScopeID<'_>| Box::new(shape::round_rect::<DRect>(
id.create(),
FILL_DRECT,
0.0,
0.0,
wide::f32x4::splat(4.0),
sRGB::transparent(),
sRGB::transparent(),
DAbsPoint::zero(),
)),
|mut id: ScopeID<'_>| {
Box::new(Text::<DRect> {
id: id.create(),
props: FILL_DRECT.into(),
text: "Foobar".to_string(),
font_size: 40.0,
line_height: 56.0,
..Default::default()
})
});
}
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for ScopeID<'a>
impl<'a> !RefUnwindSafe for ScopeID<'a>
impl<'a> Send for ScopeID<'a>
impl<'a> Sync for ScopeID<'a>
impl<'a> Unpin for ScopeID<'a>
impl<'a> !UnwindSafe for ScopeID<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more