✨ Añade soporte a temas en la API de extensiones
- Incluye una opción de configuración para definir el tema por defecto. - Añade un tema básico predeterminado.
This commit is contained in:
parent
f4e142a242
commit
86e4c4f110
13 changed files with 148 additions and 5 deletions
31
src/core/theme/all.rs
Normal file
31
src/core/theme/all.rs
Normal file
|
@ -0,0 +1,31 @@
|
|||
use crate::core::theme::ThemeRef;
|
||||
use crate::global;
|
||||
|
||||
use std::sync::{LazyLock, RwLock};
|
||||
|
||||
// TEMAS *******************************************************************************************
|
||||
|
||||
pub static THEMES: LazyLock<RwLock<Vec<ThemeRef>>> = LazyLock::new(|| RwLock::new(Vec::new()));
|
||||
|
||||
// TEMA PREDETERMINADO *****************************************************************************
|
||||
|
||||
pub static DEFAULT_THEME: LazyLock<ThemeRef> =
|
||||
LazyLock::new(|| match theme_by_short_name(&global::SETTINGS.app.theme) {
|
||||
Some(theme) => theme,
|
||||
None => &crate::base::theme::Basic,
|
||||
});
|
||||
|
||||
// TEMA POR NOMBRE *********************************************************************************
|
||||
|
||||
pub fn theme_by_short_name(short_name: impl AsRef<str>) -> Option<ThemeRef> {
|
||||
let short_name = short_name.as_ref().to_lowercase();
|
||||
match THEMES
|
||||
.read()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.find(|t| t.short_name().to_lowercase() == short_name)
|
||||
{
|
||||
Some(theme) => Some(*theme),
|
||||
_ => None,
|
||||
}
|
||||
}
|
30
src/core/theme/definition.rs
Normal file
30
src/core/theme/definition.rs
Normal file
|
@ -0,0 +1,30 @@
|
|||
use crate::core::extension::ExtensionTrait;
|
||||
|
||||
/// Representa una referencia a un tema.
|
||||
///
|
||||
/// Los temas son también extensiones. Por tanto se deben definir igual, es decir, como instancias
|
||||
/// estáticas globales que implementan [`ThemeTrait`], pero también [`ExtensionTrait`].
|
||||
pub type ThemeRef = &'static dyn ThemeTrait;
|
||||
|
||||
/// Interfaz común que debe implementar cualquier tema de `PageTop`.
|
||||
///
|
||||
/// Un tema implementará [`ThemeTrait`] y los métodos que sean necesarios de [`ExtensionTrait`],
|
||||
/// aunque el único obligatorio es [`theme()`](crate::core::extension::ExtensionTrait#method.theme).
|
||||
///
|
||||
/// ```rust
|
||||
/// use pagetop::prelude::*;
|
||||
///
|
||||
/// pub struct MyTheme;
|
||||
///
|
||||
/// impl ExtensionTrait for MyTheme {
|
||||
/// fn name(&self) -> L10n { L10n::n("My theme") }
|
||||
/// fn description(&self) -> L10n { L10n::n("Un tema personal") }
|
||||
///
|
||||
/// fn theme(&self) -> Option<ThemeRef> {
|
||||
/// Some(&Self)
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// impl ThemeTrait for MyTheme {}
|
||||
/// ```
|
||||
pub trait ThemeTrait: ExtensionTrait + Send + Sync {}
|
Loading…
Add table
Add a link
Reference in a new issue