ScopeID

Struct ScopeID 

Source
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>

Source

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.

Source

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(),
);
}
Source

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.

Source

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.

Source

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(),
    );
}
}
Source

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§

Source§

impl<AppData: Clone + FromLua + IntoLua> FnPersist2<&AppData, ScopeID<'static>, HashMap<Arc<SourceID>, Option<Window>>> for LuaPersist<AppData>

Source§

fn init(&self) -> Self::Store

Source§

fn call( &mut self, _: Self::Store, appdata: &AppData, id: ScopeID<'static>, ) -> (Self::Store, HashMap<Arc<SourceID>, Option<Window>>)

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &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
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> MaybeSend for T

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,