From ce4a97b18f5e5690fa39ad9fdb011906ad3ef03f Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Fri, 3 Feb 2023 14:31:18 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20[pagetop]=20Mejor=20uso=20?= =?UTF-8?q?de=20traits=20en=20m=C3=B3dulos/temas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop/src/core/module/all.rs | 2 +- pagetop/src/core/theme.rs | 2 +- pagetop/src/core/theme/all.rs | 17 +++++++++-------- pagetop/src/core/theme/definition.rs | 14 ++------------ 4 files changed, 13 insertions(+), 22 deletions(-) diff --git a/pagetop/src/core/module/all.rs b/pagetop/src/core/module/all.rs index 50bee917..8bed3f28 100644 --- a/pagetop/src/core/module/all.rs +++ b/pagetop/src/core/module/all.rs @@ -83,7 +83,7 @@ fn add_to_enabled(list: &mut Vec, module: ModuleStaticRef) { pub fn register_themes() { for m in ENABLED_MODULES.read().unwrap().iter() { - theme::all::register_theme(m.handle(), m.theme()); + theme::all::register_theme(m.theme()); } } diff --git a/pagetop/src/core/theme.rs b/pagetop/src/core/theme.rs index 6e008d80..67902dcb 100644 --- a/pagetop/src/core/theme.rs +++ b/pagetop/src/core/theme.rs @@ -1,4 +1,4 @@ mod definition; -pub use definition::{BaseTheme, ThemeStaticRef, ThemeTrait}; +pub use definition::{ThemeStaticRef, ThemeTrait}; pub(crate) mod all; diff --git a/pagetop/src/core/theme/all.rs b/pagetop/src/core/theme/all.rs index 2bbc81a7..9b876356 100644 --- a/pagetop/src/core/theme/all.rs +++ b/pagetop/src/core/theme/all.rs @@ -1,31 +1,32 @@ use super::ThemeStaticRef; -use crate::util::Handle; use crate::{trace, LazyStatic}; use std::sync::RwLock; // Temas registrados. -static THEMES: LazyStatic>> = +static THEMES: LazyStatic>> = LazyStatic::new(|| RwLock::new(Vec::new())); -pub fn register_theme(handle: Handle, theme: Option) { +pub fn register_theme(theme: Option) { if let Some(theme) = theme { + let handle = theme.handle(); let mut registered_themes = THEMES.write().unwrap(); - if !registered_themes.iter().any(|t| t.0 == handle) { + if !registered_themes.iter().any(|t| t.handle() == handle) { trace::debug!("Registering theme \"{}\"", theme.single_name()); - registered_themes.push((handle, theme)); + registered_themes.push(theme); } } } pub fn theme_by_single_name(single_name: &str) -> Option { + let single_name = single_name.to_lowercase(); match THEMES - .write() + .read() .unwrap() .iter() - .find(|t| t.1.single_name().to_lowercase() == single_name.to_lowercase()) + .find(|t| t.single_name().to_lowercase() == single_name) { - Some((_, theme)) => Some(*theme), + Some(theme) => Some(*theme), _ => None, } } diff --git a/pagetop/src/core/theme/definition.rs b/pagetop/src/core/theme/definition.rs index ddec7246..6c130878 100644 --- a/pagetop/src/core/theme/definition.rs +++ b/pagetop/src/core/theme/definition.rs @@ -1,18 +1,14 @@ use crate::base::component::{Container, Html}; use crate::core::component::{ComponentTrait, RenderContext}; +use crate::core::module::ModuleTrait; use crate::html::{html, Favicon, Markup}; use crate::response::page::Page; -use crate::util::single_type_name; use crate::{concat_string, config}; pub type ThemeStaticRef = &'static dyn ThemeTrait; -pub trait BaseTheme { - fn single_name(&self) -> &'static str; -} - /// Los temas deben implementar este "trait". -pub trait ThemeTrait: BaseTheme + Send + Sync { +pub trait ThemeTrait: ModuleTrait + Send + Sync { #[allow(unused_variables)] fn before_render_page(&self, page: &mut Page) { if page.favicon().is_none() { @@ -139,9 +135,3 @@ pub trait ThemeTrait: BaseTheme + Send + Sync { })) } } - -impl BaseTheme for T { - fn single_name(&self) -> &'static str { - single_type_name::() - } -}