use std::sync::Arc;
use crate::config::Config;
use crate::events::EventBindings;
use crate::resources::{ResourceError, ResourceRegistry};
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 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 bindings(&self) -> &EventBindings {
&self.bindings
}
pub fn render(&mut self, html: &str, css: &str) {
let bindings = if self.config.events {
Some(std::mem::take(&mut self.bindings))
} else {
None
};
crate::tui::run::run(html, css, bindings, Arc::clone(&self.registry), self.config);
}
}