pub struct Env(_);
Expand description

An environment passed down through all widget traversals.

All widget methods have access to an environment, and it is passed downwards during traversals.

A widget can retrieve theme parameters (colors, dimensions, etc.). In addition, it can pass custom data down to all descendants. An important example of the latter is setting a value for enabled/disabled status so that an entire subtree can be disabled (“grayed out”) with one setting.

EnvScope can be used to override parts of Env for its descendants.

Important

It is the programmer’s responsibility to ensure that the environment is used correctly. See Key for an example.

  • Keys should be consts with unique names
  • Keys must always be set before they are used.
  • Values can only be overwritten by values of the same type.

Implementations§

§

impl Env

pub const DEBUG_WIDGET: Key<bool> = Key::new("org.linebender.druid.built-in.debug-widget")

A key used to tell widgets to print additional debug information.

This does nothing by default; however you can check this key while debugging a widget to limit println spam.

For convenience, this key can be set with the WidgetExt::debug_widget method.

Examples
if env.get(Env::DEBUG_WIDGET) {
    eprintln!("widget {:?} bounds: {:?}", widget_id, my_rect);
}

pub fn get<V>(&self, key: impl Borrow<Key<V>>) -> Vwhere V: ValueType,

Gets a value from the environment, expecting it to be present.

Note that the return value is a reference for “expensive” types such as strings, but an ordinary value for “cheap” types such as numbers and colors.

Panics

Panics if the key is not found, or if it is present with the wrong type.

pub fn try_get<V>(&self, key: impl Borrow<Key<V>>) -> Result<V, MissingKeyError>where V: ValueType,

Tries to get a value from the environment.

If the value is not found, the raw key is returned as the error.

Panics

Panics if the value for the key is found, but has the wrong type.

pub fn get_untyped<V>(&self, key: impl Borrow<Key<V>>) -> &Value

Gets a value from the environment, in its encapsulated Value form, expecting the key to be present.

WARNING: This is not intended for general use, but only for inspecting an Env e.g. for debugging, theme editing, and theme loading.

Panics

Panics if the key is not found.

pub fn try_get_untyped<V>( &self, key: impl Borrow<Key<V>> ) -> Result<&Value, MissingKeyError>

Gets a value from the environment, in its encapsulated Value form, returning None if a value isn’t found.

Note

This is not intended for general use, but only for inspecting an Env e.g. for debugging, theme editing, and theme loading.

pub fn get_all(&self) -> impl ExactSizeIterator

Gets the entire contents of the Env, in key-value pairs.

WARNING: This is not intended for general use, but only for inspecting an Env e.g. for debugging, theme editing, and theme loading.

pub fn adding<V>(self, key: Key<V>, value: impl Into<V>) -> Envwhere V: ValueType,

Adds a key/value, acting like a builder.

pub fn set<V>(&mut self, key: Key<V>, value: impl Into<V>)where V: ValueType,

Sets a value in an environment.

Panics

Panics if the environment already has a value for the key, but it is of a different type.

pub fn try_set_raw<V>( &mut self, key: Key<V>, raw: Value ) -> Result<(), ValueTypeError>where V: ValueType,

Try to set a resolved Value for this key.

This will return a ValueTypeError if the value’s inner type differs from the type of the key.

§

impl Env

pub fn empty() -> Env

Returns an empty Env.

This is useful for creating a set of overrides.

Trait Implementations§

§

impl Clone for Env

§

fn clone(&self) -> Env

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Data for Env

§

fn same(&self, other: &Env) -> bool

Determine whether two values are the same. Read more
§

impl Debug for Env

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Env

§

impl !Send for Env

§

impl !Sync for Env

§

impl Unpin for Env

§

impl !UnwindSafe for Env

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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 Twhere U: From<T>,

const: unstable · 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

impl<T> RoundFrom<T> for T

§

fn round_from(x: T) -> T

Performs the conversion.
§

impl<T, U> RoundInto<U> for Twhere U: RoundFrom<T>,

§

fn round_into(self) -> U

Performs the conversion.
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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