#![allow(unused_imports)]
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use tairitsu_vdom::Platform;
use super::style::CssProperty;
pub struct ScrollbarRegistry<P: Platform> {
scrollbars: HashMap<String, P::Element>,
}
impl<P: Platform> Default for ScrollbarRegistry<P> {
fn default() -> Self {
Self {
scrollbars: HashMap::new(),
}
}
}
impl<P: Platform> ScrollbarRegistry<P> {
pub fn new() -> Self {
Self::default()
}
pub fn register(&mut self, id: String, track: P::Element) {
self.scrollbars.insert(id, track);
}
pub fn update_width(&mut self, id: &str, width: f64, platform: &Rc<RefCell<P>>) {
if let Some(track_handle) = self.scrollbars.get(id) {
platform.borrow_mut().set_style(
track_handle,
"transition",
"width 300ms cubic-bezier(0.25, 0.1, 0.25, 1)",
);
platform
.borrow_mut()
.set_style(track_handle, "width", &format!("{}px", width));
}
}
pub fn unregister(&mut self, id: &str) {
self.scrollbars.remove(id);
}
}
use std::sync::Mutex;
static LEGACY_REGISTRY: Mutex<Option<HashMap<String, u64>>> = Mutex::new(None);
#[deprecated(note = "Use ScrollbarRegistry instead")]
pub fn register_scrollbar(id: String, track: u64) {
let mut registry = LEGACY_REGISTRY.lock().unwrap();
if registry.is_none() {
*registry = Some(HashMap::new());
}
if let Some(scrollbars) = registry.as_mut() {
scrollbars.insert(id, track);
}
}
#[deprecated(note = "Use ScrollbarRegistry instead")]
pub fn update_scrollbar_width<P: Platform<Element = u64>>(
id: String,
width: f64,
platform: &Rc<RefCell<P>>,
) {
let registry = LEGACY_REGISTRY.lock().unwrap();
if let Some(scrollbars) = registry.as_ref()
&& let Some(&track_handle) = scrollbars.get(&id)
{
platform.borrow_mut().set_style(
&track_handle,
"transition",
"width 300ms cubic-bezier(0.25, 0.1, 0.25, 1)",
);
platform
.borrow_mut()
.set_style(&track_handle, "width", &format!("{}px", width));
}
}
#[deprecated(note = "Use ScrollbarRegistry instead")]
pub fn unregister_scrollbar(id: String) {
let mut registry = LEGACY_REGISTRY.lock().unwrap();
if let Some(scrollbars) = registry.as_mut() {
scrollbars.remove(&id);
}
}