✏️ [pagetop] Mejor uso de traits en módulos/temas
This commit is contained in:
parent
9372edb526
commit
ce4a97b18f
4 changed files with 13 additions and 22 deletions
|
|
@ -83,7 +83,7 @@ fn add_to_enabled(list: &mut Vec<ModuleStaticRef>, module: ModuleStaticRef) {
|
||||||
|
|
||||||
pub fn register_themes() {
|
pub fn register_themes() {
|
||||||
for m in ENABLED_MODULES.read().unwrap().iter() {
|
for m in ENABLED_MODULES.read().unwrap().iter() {
|
||||||
theme::all::register_theme(m.handle(), m.theme());
|
theme::all::register_theme(m.theme());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
mod definition;
|
mod definition;
|
||||||
pub use definition::{BaseTheme, ThemeStaticRef, ThemeTrait};
|
pub use definition::{ThemeStaticRef, ThemeTrait};
|
||||||
|
|
||||||
pub(crate) mod all;
|
pub(crate) mod all;
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,32 @@
|
||||||
use super::ThemeStaticRef;
|
use super::ThemeStaticRef;
|
||||||
use crate::util::Handle;
|
|
||||||
use crate::{trace, LazyStatic};
|
use crate::{trace, LazyStatic};
|
||||||
|
|
||||||
use std::sync::RwLock;
|
use std::sync::RwLock;
|
||||||
|
|
||||||
// Temas registrados.
|
// Temas registrados.
|
||||||
static THEMES: LazyStatic<RwLock<Vec<(Handle, ThemeStaticRef)>>> =
|
static THEMES: LazyStatic<RwLock<Vec<ThemeStaticRef>>> =
|
||||||
LazyStatic::new(|| RwLock::new(Vec::new()));
|
LazyStatic::new(|| RwLock::new(Vec::new()));
|
||||||
|
|
||||||
pub fn register_theme(handle: Handle, theme: Option<ThemeStaticRef>) {
|
pub fn register_theme(theme: Option<ThemeStaticRef>) {
|
||||||
if let Some(theme) = theme {
|
if let Some(theme) = theme {
|
||||||
|
let handle = theme.handle();
|
||||||
let mut registered_themes = THEMES.write().unwrap();
|
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());
|
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<ThemeStaticRef> {
|
pub fn theme_by_single_name(single_name: &str) -> Option<ThemeStaticRef> {
|
||||||
|
let single_name = single_name.to_lowercase();
|
||||||
match THEMES
|
match THEMES
|
||||||
.write()
|
.read()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.iter()
|
.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,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,14 @@
|
||||||
use crate::base::component::{Container, Html};
|
use crate::base::component::{Container, Html};
|
||||||
use crate::core::component::{ComponentTrait, RenderContext};
|
use crate::core::component::{ComponentTrait, RenderContext};
|
||||||
|
use crate::core::module::ModuleTrait;
|
||||||
use crate::html::{html, Favicon, Markup};
|
use crate::html::{html, Favicon, Markup};
|
||||||
use crate::response::page::Page;
|
use crate::response::page::Page;
|
||||||
use crate::util::single_type_name;
|
|
||||||
use crate::{concat_string, config};
|
use crate::{concat_string, config};
|
||||||
|
|
||||||
pub type ThemeStaticRef = &'static dyn ThemeTrait;
|
pub type ThemeStaticRef = &'static dyn ThemeTrait;
|
||||||
|
|
||||||
pub trait BaseTheme {
|
|
||||||
fn single_name(&self) -> &'static str;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Los temas deben implementar este "trait".
|
/// Los temas deben implementar este "trait".
|
||||||
pub trait ThemeTrait: BaseTheme + Send + Sync {
|
pub trait ThemeTrait: ModuleTrait + Send + Sync {
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
fn before_render_page(&self, page: &mut Page) {
|
fn before_render_page(&self, page: &mut Page) {
|
||||||
if page.favicon().is_none() {
|
if page.favicon().is_none() {
|
||||||
|
|
@ -139,9 +135,3 @@ pub trait ThemeTrait: BaseTheme + Send + Sync {
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: ?Sized + ThemeTrait> BaseTheme for T {
|
|
||||||
fn single_name(&self) -> &'static str {
|
|
||||||
single_type_name::<Self>()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue