use crate::config::Config;
use crate::events::EventBindings;
use crate::resources::{ResourceError, ResourceRegistry};
use std::sync::Arc;
pub struct Session {
registry: Arc<ResourceRegistry>,
config: Config,
bindings: EventBindings,
}
impl Default for Session {
fn default() -> Self {
Self::new()
}
}
impl Session {
pub fn new() -> Self {
Self {
registry: Arc::new(ResourceRegistry::new()),
config: Config::default(),
bindings: EventBindings::default(),
}
}
pub fn register(&self, path: impl Into<String>, bytes: Vec<u8>) -> Result<(), ResourceError> {
self.registry.register(path, bytes)
}
pub fn unregister(&self, path: &str) -> bool {
self.registry.unregister(path)
}
pub fn load(&self, path: &str) -> Result<Arc<Vec<u8>>, ResourceError> {
self.registry.load(path)
}
pub fn registry(&self) -> &ResourceRegistry {
&self.registry
}
pub(crate) fn registry_handle(&self) -> Arc<ResourceRegistry> {
Arc::clone(&self.registry)
}
pub(crate) fn take_bindings(&mut self) -> EventBindings {
std::mem::take(&mut self.bindings)
}
pub fn with_registry<R>(&self, f: impl FnOnce() -> R) -> R {
crate::resources::with_registry(Arc::clone(&self.registry), f)
}
pub fn config(&self) -> &Config {
&self.config
}
pub fn config_mut(&mut self) -> &mut Config {
&mut self.config
}
pub fn set_config(&mut self, config: Config) {
self.config = config;
}
pub fn set_zoom(&mut self, zoom: f32) {
self.config.zoom = zoom;
}
pub fn zoom(&self) -> f32 {
self.config.zoom
}
pub fn set_bindings(&mut self, bindings: EventBindings) {
self.bindings = bindings;
}
pub fn render(
&mut self,
html: &crate::HtmlSource,
css: &crate::CssSource,
js: &crate::JsSource,
) {
#[cfg(debug_assertions)]
let paths = crate::tui::run::HotReloadPaths {
html: html.path(),
css: css.path(),
js: js.path(),
};
#[cfg(debug_assertions)]
if paths.any() && self.config.events {
crate::tui::run::run_hot(paths, html.as_str(), css.as_str(), js.as_str(), self);
return;
}
#[cfg(debug_assertions)]
if paths.any() && !self.config.events {
log::warn!("Hot reload requires Config.events; rendering statically");
}
crate::tui::run::run(html.as_str(), css.as_str(), js.as_str(), self);
}
}