wgui: Separate user and wgui assets, topmost widgets (poc)
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
use std::{
|
||||
cell::{RefCell, RefMut},
|
||||
io::Read,
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
use crate::{assets::AssetProvider, drawing, i18n::I18n};
|
||||
use crate::{
|
||||
assets::{AssetPath, AssetProvider},
|
||||
assets_internal, drawing,
|
||||
i18n::I18n,
|
||||
};
|
||||
|
||||
pub struct Defaults {
|
||||
pub dark_mode: bool,
|
||||
@@ -22,8 +27,9 @@ impl Default for Defaults {
|
||||
}
|
||||
|
||||
pub struct Globals {
|
||||
pub assets: Box<dyn AssetProvider>,
|
||||
pub i18n: I18n,
|
||||
pub assets_internal: Box<dyn AssetProvider>,
|
||||
pub assets_builtin: Box<dyn AssetProvider>,
|
||||
pub i18n_builtin: I18n,
|
||||
pub defaults: Defaults,
|
||||
}
|
||||
|
||||
@@ -31,10 +37,33 @@ pub struct Globals {
|
||||
pub struct WguiGlobals(Rc<RefCell<Globals>>);
|
||||
|
||||
impl WguiGlobals {
|
||||
pub fn new(mut assets: Box<dyn AssetProvider>, defaults: Defaults) -> anyhow::Result<Self> {
|
||||
let i18n = I18n::new(&mut assets)?;
|
||||
pub fn new(mut assets_builtin: Box<dyn AssetProvider>, defaults: Defaults) -> anyhow::Result<Self> {
|
||||
let i18n_builtin = I18n::new(&mut assets_builtin)?;
|
||||
let assets_internal = Box::new(assets_internal::AssetInternal {});
|
||||
|
||||
Ok(Self(Rc::new(RefCell::new(Globals { assets, i18n, defaults }))))
|
||||
Ok(Self(Rc::new(RefCell::new(Globals {
|
||||
assets_internal,
|
||||
assets_builtin,
|
||||
i18n_builtin,
|
||||
defaults,
|
||||
}))))
|
||||
}
|
||||
|
||||
pub fn get_asset(&self, asset_path: AssetPath) -> anyhow::Result<Vec<u8>> {
|
||||
match asset_path {
|
||||
AssetPath::WguiInternal(path) => self.assets_internal().load_from_path(path),
|
||||
AssetPath::BuiltIn(path) => self.assets_builtin().load_from_path(path),
|
||||
AssetPath::Filesystem(path) => {
|
||||
let mut file = std::fs::File::open(path)?;
|
||||
/* 16 MiB safeguard */
|
||||
if file.metadata()?.len() > 16 * 1024 * 1024 {
|
||||
anyhow::bail!("Too large file size");
|
||||
}
|
||||
let mut data = Vec::new();
|
||||
file.read_to_end(&mut data)?;
|
||||
Ok(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self) -> RefMut<'_, Globals> {
|
||||
@@ -42,10 +71,14 @@ impl WguiGlobals {
|
||||
}
|
||||
|
||||
pub fn i18n(&self) -> RefMut<'_, I18n> {
|
||||
RefMut::map(self.0.borrow_mut(), |x| &mut x.i18n)
|
||||
RefMut::map(self.0.borrow_mut(), |x| &mut x.i18n_builtin)
|
||||
}
|
||||
|
||||
pub fn assets(&self) -> RefMut<'_, Box<dyn AssetProvider>> {
|
||||
RefMut::map(self.0.borrow_mut(), |x| &mut x.assets)
|
||||
pub fn assets_internal(&self) -> RefMut<'_, Box<dyn AssetProvider>> {
|
||||
RefMut::map(self.0.borrow_mut(), |x| &mut x.assets_internal)
|
||||
}
|
||||
|
||||
pub fn assets_builtin(&self) -> RefMut<'_, Box<dyn AssetProvider>> {
|
||||
RefMut::map(self.0.borrow_mut(), |x| &mut x.assets_builtin)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user