From 84bd5f631afb58a9d844ff106435e2df6ad1e6bb Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Sat, 22 Jul 2023 01:47:43 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20components=20to=20theme-less?= =?UTF-8?q?=20regions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop/src/core/component/pack.rs | 16 +++--- pagetop/src/core/module/all.rs | 4 +- pagetop/src/core/theme.rs | 6 +-- pagetop/src/core/theme/all.rs | 2 +- .../src/core/theme/{basic.rs => default.rs} | 12 ++--- pagetop/src/core/theme/regions.rs | 53 +++++++++++++------ pagetop/src/response/page.rs | 4 +- 7 files changed, 56 insertions(+), 41 deletions(-) rename pagetop/src/core/theme/{basic.rs => default.rs} (83%) diff --git a/pagetop/src/core/component/pack.rs b/pagetop/src/core/component/pack.rs index 2072c5c7..9fe79e40 100644 --- a/pagetop/src/core/component/pack.rs +++ b/pagetop/src/core/component/pack.rs @@ -53,18 +53,14 @@ impl PackComponents { pack } - pub(crate) fn merge(one: Option<&PackComponents>, other: Option<&PackComponents>) -> Self { - if let Some(one) = one { - let mut components = one.0.clone(); - if let Some(other) = other { - components.append(&mut other.0.clone()); + pub(crate) fn merge(packs: &[Option<&PackComponents>]) -> Self { + let mut pack = PackComponents::default(); + for p in packs { + if let Some(p) = p { + pack.0.append(&mut p.0.clone()); } - PackComponents(components) - } else if let Some(other) = other { - PackComponents(other.0.clone()) - } else { - PackComponents::default() } + pack } // PackComponents BUILDER. diff --git a/pagetop/src/core/module/all.rs b/pagetop/src/core/module/all.rs index b50f8dfd..4bd8b530 100644 --- a/pagetop/src/core/module/all.rs +++ b/pagetop/src/core/module/all.rs @@ -27,8 +27,8 @@ pub fn register_modules(app: ModuleRef) { // List of modules to enable. let mut list: Vec = Vec::new(); - // Enable basic theme. - add_to_enabled(&mut list, &crate::core::theme::Basic); + // Enable default theme. + add_to_enabled(&mut list, &crate::core::theme::DefaultTheme); // Enable application modules. add_to_enabled(&mut list, app); diff --git a/pagetop/src/core/theme.rs b/pagetop/src/core/theme.rs index 35a6ff2f..7a59d60e 100644 --- a/pagetop/src/core/theme.rs +++ b/pagetop/src/core/theme.rs @@ -2,10 +2,10 @@ mod definition; pub use definition::{ThemeRef, ThemeTrait}; mod regions; -pub use regions::add_component_to; pub(crate) use regions::ComponentsRegions; +pub use regions::{add_component_in, Region}; -mod basic; -pub(crate) use basic::Basic; +mod default; +pub use default::DefaultTheme; pub(crate) mod all; diff --git a/pagetop/src/core/theme/all.rs b/pagetop/src/core/theme/all.rs index 2b6d56b1..f8214b05 100644 --- a/pagetop/src/core/theme/all.rs +++ b/pagetop/src/core/theme/all.rs @@ -13,7 +13,7 @@ pub static THEMES: LazyStatic>> = LazyStatic::new(|| RwLock pub static THEME: LazyStatic = LazyStatic::new(|| match theme_by_single_name(&config::SETTINGS.app.theme) { Some(theme) => theme, - None => &crate::core::theme::Basic, + None => &crate::core::theme::DefaultTheme, }); // THEME BY NAME *********************************************************************************** diff --git a/pagetop/src/core/theme/basic.rs b/pagetop/src/core/theme/default.rs similarity index 83% rename from pagetop/src/core/theme/basic.rs rename to pagetop/src/core/theme/default.rs index 2217fbd9..4444f27a 100644 --- a/pagetop/src/core/theme/basic.rs +++ b/pagetop/src/core/theme/default.rs @@ -6,19 +6,19 @@ use crate::response::page::Page; use crate::service; use crate::{create_handle, serve_static_files, static_files, Handle}; -create_handle!(THEME_BASIC); +create_handle!(THEME_DEFAULT); static_files!(theme); -pub struct Basic; +pub struct DefaultTheme; -impl ModuleTrait for Basic { +impl ModuleTrait for DefaultTheme { fn handle(&self) -> Handle { - THEME_BASIC + THEME_DEFAULT } fn theme(&self) -> Option { - Some(&Basic) + Some(&DefaultTheme) } fn configure_service(&self, cfg: &mut service::web::ServiceConfig) { @@ -26,7 +26,7 @@ impl ModuleTrait for Basic { } } -impl ThemeTrait for Basic { +impl ThemeTrait for DefaultTheme { fn before_prepare_body(&self, page: &mut Page) { page.alter_favicon(Some(Favicon::new().with_icon("/theme/favicon.ico"))) .alter_context(ContextOp::AddStyleSheet( diff --git a/pagetop/src/core/theme/regions.rs b/pagetop/src/core/theme/regions.rs index dd28e88e..ba40d975 100644 --- a/pagetop/src/core/theme/regions.rs +++ b/pagetop/src/core/theme/regions.rs @@ -1,12 +1,16 @@ use crate::core::component::{ComponentRef, PackComponents, PackOp}; -use crate::LazyStatic; +use crate::core::theme::ThemeRef; +use crate::{Handle, LazyStatic}; use std::collections::HashMap; use std::sync::RwLock; -static THEME_REGIONS: LazyStatic>> = +static THEME_REGIONS: LazyStatic>> = LazyStatic::new(|| RwLock::new(HashMap::new())); +static COMMON_REGIONS: LazyStatic> = + LazyStatic::new(|| RwLock::new(ComponentsRegions::new())); + #[derive(Default)] pub struct ComponentsRegions(HashMap<&'static str, PackComponents>); @@ -15,7 +19,13 @@ impl ComponentsRegions { ComponentsRegions::default() } - pub fn add_to(&mut self, region: &'static str, cref: ComponentRef) { + pub fn new_with(region: &'static str, cref: ComponentRef) -> Self { + let mut regions = ComponentsRegions::new(); + regions.add_in(region, cref); + regions + } + + pub fn add_in(&mut self, region: &'static str, cref: ComponentRef) { if let Some(region) = self.0.get_mut(region) { region.alter(PackOp::Add, cref); } else { @@ -23,24 +33,33 @@ impl ComponentsRegions { } } - pub fn get_extended_pack(&self, theme: &str, region: &str) -> PackComponents { - if let Some(hm_theme) = THEME_REGIONS.read().unwrap().get(theme) { - PackComponents::merge(self.0.get(region), hm_theme.0.get(region)) + pub fn get_pack(&self, theme: ThemeRef, region: &str) -> PackComponents { + let common = COMMON_REGIONS.read().unwrap(); + if let Some(hm) = THEME_REGIONS.read().unwrap().get(&theme.handle()) { + PackComponents::merge(&[common.0.get(region), self.0.get(region), hm.0.get(region)]) } else { - PackComponents::merge(self.0.get(region), None) + PackComponents::merge(&[common.0.get(region), self.0.get(region)]) } } } -pub fn add_component_to(theme: &'static str, region: &'static str, cref: ComponentRef) { - let mut hm = THEME_REGIONS.write().unwrap(); - if let Some(hm_theme) = hm.get_mut(theme) { - hm_theme.add_to(region, cref); - } else { - hm.insert(theme, { - let mut regions = ComponentsRegions::new(); - regions.add_to(region, cref); - regions - }); +pub enum Region { + Named(&'static str), + OfTheme(ThemeRef, &'static str), +} + +pub fn add_component_in(region: Region, cref: ComponentRef) { + match region { + Region::Named(name) => { + COMMON_REGIONS.write().unwrap().add_in(name, cref); + } + Region::OfTheme(theme, region) => { + let mut regions = THEME_REGIONS.write().unwrap(); + if let Some(hm) = regions.get_mut(&theme.handle()) { + hm.add_in(region, cref); + } else { + regions.insert(theme.handle(), ComponentsRegions::new_with(region, cref)); + } + } } } diff --git a/pagetop/src/response/page.rs b/pagetop/src/response/page.rs index 0a96768f..c2d9434c 100644 --- a/pagetop/src/response/page.rs +++ b/pagetop/src/response/page.rs @@ -90,7 +90,7 @@ impl Page { #[fn_builder] pub fn alter_in(&mut self, region: &'static str, component: impl ComponentTrait) -> &mut Self { - self.regions.add_to(region, ComponentRef::to(component)); + self.regions.add_in(region, ComponentRef::to(component)); self } @@ -173,7 +173,7 @@ impl Page { pub fn prepare_region(&mut self, region: &str) -> Option { let render = self .regions - .get_extended_pack(self.context.theme().single_name(), region) + .get_pack(self.context.theme(), region) .prepare(self.context()); if render.is_empty() { None