Skip to main content

ferroui_base/platform/
asset_loader.rs

1use super::{AssetAssembly, AssetStream, IAssetLoader};
2use crate::utilities::Uri;
3use crate::{FerroLocator, LocatorExtensions};
4use std::rc::Rc;
5
6/// The scheme of the URIs that identify assets registered by a crate:
7/// `<scheme>://<crate name>/<path>`.
8pub const ASSET_SCHEME: &str = "ferres";
9
10/// Provides access to the assets compiled into the application binary,
11/// through the registered [`IAssetLoader`].
12pub struct AssetLoader;
13
14impl AssetLoader {
15    fn get_asset_loader() -> Rc<dyn IAssetLoader> {
16        FerroLocator::current().get_required_service::<dyn IAssetLoader>()
17    }
18
19    /// Sets the default assembly from which to load embedded-resource
20    /// (`resm:`) assets for which no assembly is specified.
21    pub fn set_default_assembly(assembly: &AssetAssembly) {
22        Self::get_asset_loader().set_default_assembly(assembly)
23    }
24
25    /// Checks if an asset with the specified URI exists.
26    pub fn exists(uri: &Uri, base_uri: Option<&Uri>) -> bool {
27        Self::get_asset_loader().exists(uri, base_uri)
28    }
29
30    /// Opens the asset with the requested URI.
31    pub fn open(uri: &Uri, base_uri: Option<&Uri>) -> std::io::Result<Box<dyn AssetStream>> {
32        Self::get_asset_loader().open(uri, base_uri)
33    }
34
35    /// Opens the asset with the requested URI and returns the asset stream
36    /// along with the assembly containing the asset.
37    pub fn open_and_get_assembly(
38        uri: &Uri,
39        base_uri: Option<&Uri>,
40    ) -> std::io::Result<(Box<dyn AssetStream>, AssetAssembly)> {
41        Self::get_asset_loader().open_and_get_assembly(uri, base_uri)
42    }
43
44    /// Extracts the assembly information from a URI.
45    pub fn get_assembly(uri: &Uri, base_uri: Option<&Uri>) -> Option<AssetAssembly> {
46        Self::get_asset_loader().get_assembly(uri, base_uri)
47    }
48
49    /// Gets all assets of a folder and its subfolders that match the
50    /// specified URI.
51    pub fn get_assets(uri: &Uri, base_uri: Option<&Uri>) -> Vec<Uri> {
52        Self::get_asset_loader().get_assets(uri, base_uri)
53    }
54
55    /// Removes the cached descriptor of the assembly with the given name.
56    pub fn invalidate_assembly_cache(name: &str) {
57        Self::get_asset_loader().invalidate_assembly_cache(name)
58    }
59
60    /// Removes all cached assembly descriptors.
61    pub fn invalidate_assembly_cache_all() {
62        Self::get_asset_loader().invalidate_assembly_cache_all()
63    }
64}