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.
Implementations§
§impl Env
impl Env
pub const DEBUG_WIDGET: Key<bool> = Key::new("org.linebender.druid.built-in.debug-widget")
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,
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,
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
pub fn get_untyped<V>(&self, key: impl Borrow<Key<V>>) -> &Value
pub fn try_get_untyped<V>(
&self,
key: impl Borrow<Key<V>>
) -> Result<&Value, MissingKeyError>
pub fn try_get_untyped<V>( &self, key: impl Borrow<Key<V>> ) -> Result<&Value, MissingKeyError>
pub fn get_all(&self) -> impl ExactSizeIterator
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,
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,
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,
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.