Struct ResourceDictionary

pub struct ResourceDictionary(/* private fields */);
Expand description

A resource dictionary is a container that maps resource IDs to resources. A node can refer to resources by setting resource IDs to resource ID properties. You can set a resource dictionary to the node or to any of its parent nodes to define which resources the node uses. You can use a resource dictionary to group similar resources to logical entity, such as themes or locales, and change a resource dictionary of a node to change multiple resources at the same time.

Use Node::acquireResource to get the resource from the resource dictionary of a node or from the resource dictionaries of its parent nodes.

A resource dictionary can map resource IDs to resources in these ways:

  • Directly to a resource pointer
  • Indirectly to a resource with a resource URL

Use acquire and tryAcquire functions to get a mapped resource regardless of the way how a resource ID is mapped to a resource.

You can store nested resource dictionaries in a resource dictionary. When you are getting a resource from a resource dictionary and the resource dictionary cannot find the resource ID you specify, the resource dictionary looks for the resource ID recursively in the nested resource dictionaries.

In a resource dictionary you can store resources without mapping a resource ID to them. In Kanzi Engine such resources are called anonymous resources. You can get anonymous resources with beginAnonymousResources and endAnonymousResources.

§Examples

To create a resource dictionary:

To add content to a resource dictionary:

To get resources from a resource dictionary:

To change the resource dictionary of a node:

To extend the existing resource dictionary of a node:

Implementations§

§

impl ResourceDictionary

pub fn create( domain: &Domain, name: impl AsRef<KanziStr>, ) -> Result<ResourceDictionary, Error>

Creates a resource dictionary.

§Arguments
  • domain - The domain to which the resource dictionary belongs.
  • name - The name of the resource dictionary.
§Returns

The pointer to the resource dictionary.

§

impl ResourceDictionary

pub fn add_resource_id_url_mapping( &self, key: &ResourceId, url: impl AsRef<KanziStr>, ) -> Result<(), Error>

Adds a resource ID to a resource URL mapping to a resource dictionary.

§Arguments
  • key - The resource ID to add.
  • url - The URL that points to the resource to map to the resource ID.

pub fn add_resource_id_resource_mapping( &self, key: &ResourceId, resource: &Resource, ) -> Result<(), Error>

Adds a resource ID to a resource pointer mapping to a resource dictionary.

§Arguments
  • key - The resource ID to add.
  • resource - The resource pointer to map to the resource ID..

pub fn remove(&self, key: &ResourceId) -> Result<(), Error>

Removes a resource ID mapping from a resource dictionary.

§Arguments
  • key - The resource ID that you want to remove.

pub fn contains(&self, key: &ResourceId) -> Result<bool, Error>

Returns whether a resource dictionary contains a mapping for the resource ID that you pass to the function.

§Arguments
  • key - The resource ID for which you want to check whether there is a mapping in the resource dictionary.
§Returns

If the resource dictionary or a nested resource dictionary contains a mapping for the resource ID that you passed to the function, true, otherwise false.

pub fn find(&self, key: &ResourceId) -> Result<Option<KanziString>, Error>

Resolves the mapping from the resource ID that you pass to the function to the URL of the resource. This function ignores the direct resource ID to resource mappings.

§Arguments
  • key - The resource ID of the resource for which you want to get the URL.
§Returns

Dictionary or a nested resource dictionary contains a resource URL, a resource ID that you passed. If None is returned, no resource URL match provided resource ID.

pub fn iterate_urls(&self) -> Result<UrlsIterator, Error>

Returns an iterator pointing to the first element of the URL mappings container.

§Returns

An iterator pointing to the first element of the container.

pub fn get_urls(&self) -> Result<Vec<(ResourceId, KanziString)>, Error>

Returns a vector containing all URL mappings.

pub fn get_anonymous_resource_iterator( &self, ) -> Result<AnonymousResourceIterator, Error>

Returns an iterator pointing to the first element of the anonymous resources container.

§Returns

An iterator pointing to the first element of the anonymous resources container.

pub fn get_anonymous_resources(&self) -> Result<Vec<Weak<Resource>>, Error>

Returns a vector containing all anonymous resources of the anonymous resource container.

pub fn add_anonymous_resource(&self, resource: &Resource) -> Result<(), Error>

Adds an anonymous resource to a resource dictionary.

§Arguments
  • resource - The anonymous resource that you want to add to the resource dictionary.

pub fn remove_anonymous_resource( &self, resource: &Resource, ) -> Result<(), Error>

Removes an anonymous resource from the resource dictionary. If the resource does not exist in the resource dictionary, the function does not do anything.

§Arguments
  • resource - The anonymous resource you want to remove from the resource dictionary.

pub fn get_nested_dictionaries_reverse_iterator( &self, ) -> Result<NestedDictionariesReverseIterator, Error>

Returns a reverse iterator from the first element of the reversed nested resource dictionary container.

pub fn get_nested_dictionaries_reversed( &self, ) -> Result<Vec<Weak<ResourceDictionary>>, Error>

Returns a vector containing all nested dictionaried of the reversed nested dictionary container.

pub fn add(&self, resource_dictionary: &ResourceDictionary) -> Result<(), Error>

Adds a nested resource dictionary to a resource dictionary. You can add to multiple resource dictionaries the same resource dictionary as a nested resource dictionary. Do not form cycles with the nested resource dictionaries.

§Arguments
  • resource_dictionary - The nested resource dictionary that you want to add to the resource dictionary.

pub fn get_dictionary_count(&self) -> Result<u64, Error>

Gets the number of nested resource dictionaries in a resource dictionary.

§Returns

The number of nested resource dictionaries in the resource dictionary.

pub fn remove_dictionary(&self, index: u64) -> Result<(), Error>

Removes a nested resource dictionary from a resource dictionary.

§Arguments
  • index - The index of the nested resource dictionary that you want to remove.
§Errors

Returns an INDEX_OUT_OF_BOUNDS error if the index is out of bounds.

pub unsafe fn remove_dictionary_unchecked( &self, index: u64, ) -> Result<(), Error>

§Safety

The caller must ensure that index < self.get_dictionary_count().

pub fn find_dictionary( &self, name: impl AsRef<KanziStr>, ) -> Result<Option<Weak<ResourceDictionary>>, Error>

Searches for a nested dictionary with the specified name and returns a pointer to it.

§Arguments
  • name - The name of the dictionary to find.
§Returns

A child dictionary with the specified name. If None is returned, a dictionary with the specified name does not exist.

pub fn find_stored_resource( &self, key: &ResourceId, ) -> Result<Option<Weak<Resource>>, Error>

Finds an explicitly stored resource pointer from a dictionary. Does not try to acquire a resource from the resource manager. To acquire a resource, use ResourceDictionary::acquire.

§Arguments
  • key - The resource ID of the resource that you want to get.
§Returns

The resource to which the resource ID is mapped. If None is returned, no resources were mapped to provided resource ID.

pub fn acquire(&self, key: &ResourceId) -> Result<Resource, Error>

Gets from the resource dictionary the resource with the resource ID that you pass to the function. The function forwards the resource URL mappings to ResourceManager::acquire_resource.

§Arguments
  • key - The resource ID of the resource that you want to get.
§Returns

The resource to which the resource ID is mapped.

  • If the resource ID is mapped to a resource pointer, the function returns the resource pointer.
  • If the resource ID is mapped to a resource URL, the function uses the URL to query the resource from ResourceManager::acquireResource.
  • If the resource dictionary has nested resource dictionaries, the function forwards the query to the nested dictionaries: the last nested resource dictionary is searched first, and the first nested resource dictionary is searched last.
  • If the resource ID is not mapped to anything in this resource dictionary or any of the nested dictionaries, the function returns nullptr.

pub fn acquire_async( &self, key: &ResourceId, closure: impl AcquireFinishedCallback, ) -> Result<Option<ResourceAcquireTask>, Error>

Asynchronously acquires a resource. If the function cannot find a resource URL mapping, it does not launch an asynchronous task. Kanzi executes the callback only the next time when it executes callbacks, if there are no asynchronous task to launch.

§Arguments
  • key - The resource ID of the resource that you want to get.
  • func - The callback to execute when loading is complete.
§Returns

The launched asynchronous task.

pub fn merge( &self, preferred_dictionary: &ResourceDictionary, ) -> Result<(), Error>

Copies into this resource dictionary the contents of the resource dictionary that you pass to the function. The function adds to this resource dictionary:

  • Resource ID mappings
  • Pointers to the nested resource directories
  • Anonymous resources from the given resource directory

If there are duplicate values for the resource ID mappings, prefers the values from the resource directory that you pass to the function.

§Arguments
  • preferred_dictionary - The resource dictionary that you want to merge to this resource dictionary.

pub fn duplicate(&self) -> Result<ResourceDictionary, Error>

Duplicates a resource dictionary. When you duplicate a resource dictionary that contains nested resource dictionaries, the function creates a shallow copy: it copies the pointers to the nested resource dictionaries, not the contents of the nested resource dictionaries.

§Returns

The pointer to the duplicated resource dictionary.

Methods from Deref<Target = Resource>§

pub fn get_name(&self) -> Result<KanziString, Error>

Gets the resource name.

pub fn get_url(&self) -> Result<KanziString, Error>

Gets the resource URL. Note that the URL is not necessarily in URL form if resource has been just created.

§Returns

URL string.

pub fn set_keep_alive(&self, keep_alive: bool) -> Result<(), Error>

Sets the keep alive flag. Can be set only before the resource is registered to the resource manager. Normally set by loadFromKZB.

§Arguments
  • keep_alive - Value for the keep alive flag.

Methods from Deref<Target = Object>§

pub fn as_ptr(&self) -> *mut ObjectWrapper

pub fn as_wrapper(&self) -> &ObjectWrapper

pub fn as_object(&self) -> &Object

Represents any type inheriting from Object as &Object. This is useful for comparisions when PartialEq traits failed to compare objects of different types.

let child = screen.get_child(0)?;
let parent = child.get_parent()?.into_error(ErrorKind::ObjectNotFound)?;
// assert_eq!(screen, parent); // <- Fails to compile!
assert_eq!(screen.as_object(), parent.as_object());

pub fn get_native(&self) -> Result<NonNull<c_void>, Error>

Gets a pointer to the backing C++ instance.

pub fn is_stale(&self) -> bool

Checks whether the weak reference has expired.

pub fn get_property<T>( &self, property_type: &PropertyType<T>, ) -> Result<<T as VariantConstraint>::RetArg, Error>

Returns the current value of a property disregarding modifiers.

Base value is affected by the following inputs where the highest entry in the list determines the base value:

  1. Local value set with setProperty or loaded from kzb
  2. Value set by a style affecting the property.
  3. Value defined by class metadata.

Modifiers are not applied, the highest-priority base value is returned.

If no inputs to the property value can be established the system returns the value default value from property type metadata.

§Arguments
  • property_type - The property type identifying the property to retrieve.
§Returns

The evaluated property value.

pub fn get_optional_property<T>( &self, property_type: &PropertyType<T>, ) -> Result<Option<<T as VariantConstraint>::RetArg>, Error>

Returns the current value of a property disregarding modifiers, but does not default to the value in property metadata if there are no inputs to the property value.

If there is no value sources, None is returned.

If no inputs to the property value can be established the system returns the value default value from property type metadata.

§Arguments
  • property_type - The property type identifying the property to retrieve.
§Returns

The evaluated property value.

pub fn set_property<T>( &self, property_type: &PropertyType<T>, value: <T as VariantConstraint>::DataArg<'_>, ) -> Result<(), Error>

Sets the local value of a property type.

pub fn has_value<T>( &self, property_type: &PropertyType<T>, ) -> Result<bool, Error>

Evaluates whether there are any inputs into the property value. Both value sources and modifiers are taken into account.

§Returns

Returns true if there are inputs into the property value, false otherwise.

pub fn remove_local_value<T>( &self, property_type: &PropertyType<T>, ) -> Result<(), Error>

Removes the local value associated with the property.

pub fn get_metaclass(&self) -> Result<Metaclass, Error>

Returns the metaclass of the dynamic type of the object.

pub fn get_domain(&self) -> Result<Domain, Error>

Returns the domain the object belongs to.

pub fn set_flag_keep_during_patching<T>( &self, property_type: &PropertyType<T>, ) -> Result<(), Error>

Sets the flag to indicate that the property was loaded from KZB.

pub fn debug_string(&self) -> Result<String, Error>

Builds a string representation of the object intended for debugging purposes.

Trait Implementations§

§

impl Clone for ResourceDictionary

§

fn clone(&self) -> ResourceDictionary

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
§

impl ConcreteObjectConstraint for ResourceDictionary

§

fn create_instance( domain: &Domain, name: impl AsRef<KanziStr>, ) -> Result<Self, Error>

§

impl Debug for ResourceDictionary

§

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

Formats the value using the given formatter. Read more
§

impl Deref for ResourceDictionary

§

type Target = Resource

The resulting type after dereferencing.
§

fn deref(&self) -> &<ResourceDictionary as Deref>::Target

Dereferences the value.
§

impl Inheritable for ResourceDictionary

§

unsafe fn downcast_unchecked<T>(self) -> T
where T: Inherits<Self>,

Downcast the object to a more specific type. Read more
§

unsafe fn downcast_unchecked_ref<T>(&self) -> &T
where T: Inherits<Self>,

Downcast the object reference to a more specific type. Read more
§

impl Inherits<Object> for ResourceDictionary

§

fn upcast(self) -> Base

§

fn upcast_ref(&self) -> &Base

§

impl Inherits<Resource> for ResourceDictionary

§

fn upcast(self) -> Base

§

fn upcast_ref(&self) -> &Base

§

impl Inherits<ResourceDictionary> for ResourceDictionarySelector

§

fn upcast(self) -> Base

§

fn upcast_ref(&self) -> &Base

§

impl MetaclassConstraint for ResourceDictionary

§

fn get_static_metaclass() -> &'static Metaclass

Gets metaclass associated with a given type.
§

impl ObjectConstraint for ResourceDictionary

§

fn downcast<T>(self) -> Result<Option<T>, Error>
where T: MetaInherits<Self>,

Casts metaclass to a more specific type by value.
§

fn downcast_ref<T>(&self) -> Result<Option<&T>, Error>
where T: MetaInherits<Self>,

Casts metaclass to a more specific type by reference.
§

fn is_a<T>(&self) -> Result<bool, Error>
where T: MetaInherits<Self>,

Determines whether the class this metaclass describes derives from a class described by specified metaclass.
§

fn downgrade(self) -> Weak<Self>

§

fn downgrade_ref(&self) -> Weak<Self>

§

fn lock(self) -> ThreadObject<Self>

§

fn lock_ref(&self) -> ThreadObject<Self>

§

impl<T> PartialEq<T> for ResourceDictionary

§

fn eq(&self, rhs: &T) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl<T> PartialEq<Weak<T>> for ResourceDictionary

§

fn eq(&self, rhs: &Weak<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl Eq for ResourceDictionary

Auto Trait Implementations§

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
§

impl<T> AsAny for T
where T: 'static,

§

fn as_any(&self) -> &(dyn Any + 'static)

§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Inherits<T> for T

§

fn upcast(self) -> Base

§

fn upcast_ref(&self) -> &Base

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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 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.
§

impl<Base, T> MetaInherits<Base> for T
where Base: ObjectConstraint, T: Inherits<Base> + ObjectConstraint,