🏷️ Simplifica nombres de traits esenciales
Los traits `ExtensionTrait`, `ThemeTrait` y `ComponentTrait` pasan a ser `Extension`, `Theme`y `Component`, respectivamente,
This commit is contained in:
parent
ac0889cb8c
commit
bf3ea43b53
23 changed files with 78 additions and 79 deletions
|
@ -25,7 +25,7 @@ pub use all::dispatch_actions;
|
|||
/// ```rust,ignore
|
||||
/// use pagetop::prelude::*;
|
||||
///
|
||||
/// impl ExtensionTrait for MyTheme {
|
||||
/// impl Extension for MyTheme {
|
||||
/// fn actions(&self) -> Vec<ActionBox> {
|
||||
/// actions_boxed![
|
||||
/// action::theme::BeforeRender::<Button>::new(&Self, before_render_button),
|
||||
|
@ -34,7 +34,7 @@ pub use all::dispatch_actions;
|
|||
/// }
|
||||
/// }
|
||||
///
|
||||
/// impl ThemeTrait for MyTheme {}
|
||||
/// impl Theme for MyTheme {}
|
||||
///
|
||||
/// fn before_render_button(c: &mut Button, cx: &mut Context) { todo!() }
|
||||
/// fn render_error404(c: &Error404, cx: &mut Context) -> PrepareMarkup { todo!() }
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
//! API para construir nuevos componentes.
|
||||
|
||||
mod definition;
|
||||
pub use definition::{ComponentRender, ComponentTrait};
|
||||
pub use definition::{Component, ComponentRender};
|
||||
|
||||
mod children;
|
||||
pub use children::Children;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::core::component::ComponentTrait;
|
||||
use crate::core::component::Component;
|
||||
use crate::html::{html, Context, Markup};
|
||||
use crate::{builder_fn, UniqueId};
|
||||
|
||||
|
@ -9,14 +9,14 @@ use std::vec::IntoIter;
|
|||
|
||||
/// Representa un componente encapsulado de forma segura y compartida.
|
||||
///
|
||||
/// Esta estructura permite manipular y renderizar cualquier tipo que implemente [`ComponentTrait`],
|
||||
/// Esta estructura permite manipular y renderizar cualquier tipo que implemente [`Component`],
|
||||
/// garantizando acceso concurrente a través de [`Arc<RwLock<_>>`].
|
||||
#[derive(Clone)]
|
||||
pub struct Child(Arc<RwLock<dyn ComponentTrait>>);
|
||||
pub struct Child(Arc<RwLock<dyn Component>>);
|
||||
|
||||
impl Child {
|
||||
/// Crea un nuevo [`Child`] a partir de un componente.
|
||||
pub fn with(component: impl ComponentTrait) -> Self {
|
||||
pub fn with(component: impl Component) -> Self {
|
||||
Child(Arc::new(RwLock::new(component)))
|
||||
}
|
||||
|
||||
|
@ -47,15 +47,15 @@ impl Child {
|
|||
/// Variante tipada de [`Child`] para evitar conversiones durante el uso.
|
||||
///
|
||||
/// Facilita el acceso a componentes del mismo tipo sin necesidad de hacer `downcast`.
|
||||
pub struct Typed<C: ComponentTrait>(Arc<RwLock<C>>);
|
||||
pub struct Typed<C: Component>(Arc<RwLock<C>>);
|
||||
|
||||
impl<C: ComponentTrait> Clone for Typed<C> {
|
||||
impl<C: Component> Clone for Typed<C> {
|
||||
fn clone(&self) -> Self {
|
||||
Self(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: ComponentTrait> Typed<C> {
|
||||
impl<C: Component> Typed<C> {
|
||||
/// Crea un nuevo [`Typed`] a partir de un componente.
|
||||
pub fn with(component: C) -> Self {
|
||||
Typed(Arc::new(RwLock::new(component)))
|
||||
|
@ -97,7 +97,7 @@ pub enum ChildOp {
|
|||
}
|
||||
|
||||
/// Operaciones con un componente hijo tipado [`Typed<C>`] en una lista [`Children`].
|
||||
pub enum TypedOp<C: ComponentTrait> {
|
||||
pub enum TypedOp<C: Component> {
|
||||
Add(Typed<C>),
|
||||
InsertAfterId(&'static str, Typed<C>),
|
||||
InsertBeforeId(&'static str, Typed<C>),
|
||||
|
@ -153,7 +153,7 @@ impl Children {
|
|||
|
||||
/// Ejecuta una operación con [`TypedOp`] en la lista.
|
||||
#[builder_fn]
|
||||
pub fn with_typed<C: ComponentTrait + Default>(mut self, op: TypedOp<C>) -> Self {
|
||||
pub fn with_typed<C: Component + Default>(mut self, op: TypedOp<C>) -> Self {
|
||||
match op {
|
||||
TypedOp::Add(typed) => self.add(typed.into_child()),
|
||||
TypedOp::InsertAfterId(id, typed) => self.insert_after_id(id, typed.into_child()),
|
||||
|
|
|
@ -5,7 +5,7 @@ use crate::html::{html, Context, Markup, PrepareMarkup, Render};
|
|||
/// Define la función de renderizado para todos los componentes.
|
||||
///
|
||||
/// Este *trait* se implementa automáticamente en cualquier tipo (componente) que implemente
|
||||
/// [`ComponentTrait`], por lo que no requiere ninguna codificación manual.
|
||||
/// [`Component`], por lo que no requiere ninguna codificación manual.
|
||||
pub trait ComponentRender {
|
||||
/// Renderiza el componente usando el contexto proporcionado.
|
||||
fn render(&mut self, cx: &mut Context) -> Markup;
|
||||
|
@ -86,7 +86,7 @@ pub trait ComponentTrait: AnyInfo + ComponentRender + Send + Sync {
|
|||
/// 7. Despacha [`action::component::AfterRender<C>`](crate::base::action::component::AfterRender)
|
||||
/// para que otras extensiones puedan hacer sus últimos ajustes.
|
||||
/// 8. Finalmente devuelve un [`Markup`] del renderizado preparado en el paso 5.
|
||||
impl<C: ComponentTrait> ComponentRender for C {
|
||||
impl<C: Component> ComponentRender for C {
|
||||
fn render(&mut self, cx: &mut Context) -> Markup {
|
||||
// Si no es renderizable, devuelve un bloque HTML vacío.
|
||||
if !action::component::IsRenderable::dispatch(self, cx) {
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
//! API para añadir nuevas funcionalidades usando extensiones.
|
||||
//!
|
||||
//! Cada funcionalidad adicional que quiera incorporarse a una aplicación `PageTop` se debe modelar
|
||||
//! como una **extensión**. Todas comparten la misma interfaz declarada en [`ExtensionTrait`].
|
||||
//! como una **extensión**. Todas comparten la misma interfaz declarada en [`Extension`].
|
||||
|
||||
mod definition;
|
||||
pub use definition::{ExtensionRef, ExtensionTrait};
|
||||
pub use definition::{Extension, ExtensionRef};
|
||||
|
||||
pub(crate) mod all;
|
||||
|
|
|
@ -8,7 +8,7 @@ use crate::{actions_boxed, service};
|
|||
///
|
||||
/// Las extensiones se definen como instancias estáticas globales para poder acceder a ellas desde
|
||||
/// cualquier hilo de la ejecución sin necesidad de sincronización adicional.
|
||||
pub type ExtensionRef = &'static dyn ExtensionTrait;
|
||||
pub type ExtensionRef = &'static dyn Extension;
|
||||
|
||||
/// Interfaz común que debe implementar cualquier extensión de `PageTop`.
|
||||
///
|
||||
|
@ -20,12 +20,12 @@ pub type ExtensionRef = &'static dyn ExtensionTrait;
|
|||
///
|
||||
/// pub struct Blog;
|
||||
///
|
||||
/// impl ExtensionTrait for Blog {
|
||||
/// impl Extension for Blog {
|
||||
/// fn name(&self) -> L10n { L10n::n("Blog") }
|
||||
/// fn description(&self) -> L10n { L10n::n("Sistema de blogs") }
|
||||
/// }
|
||||
/// ```
|
||||
pub trait ExtensionTrait: AnyInfo + Send + Sync {
|
||||
pub trait Extension: AnyInfo + Send + Sync {
|
||||
/// Nombre legible para el usuario.
|
||||
///
|
||||
/// Predeterminado por el [`short_name`](AnyInfo::short_name) del tipo asociado a la extensión.
|
||||
|
@ -38,8 +38,8 @@ pub trait ExtensionTrait: AnyInfo + Send + Sync {
|
|||
L10n::default()
|
||||
}
|
||||
|
||||
/// Los temas son extensiones que implementan [`ExtensionTrait`] y también
|
||||
/// [`ThemeTrait`](crate::core::theme::ThemeTrait).
|
||||
/// Los temas son extensiones que implementan [`Extension`] y también
|
||||
/// [`Theme`](crate::core::theme::Theme).
|
||||
///
|
||||
/// Si la extensión no es un tema, este método devuelve `None` por defecto.
|
||||
///
|
||||
|
@ -51,13 +51,13 @@ pub trait ExtensionTrait: AnyInfo + Send + Sync {
|
|||
///
|
||||
/// pub struct MyTheme;
|
||||
///
|
||||
/// impl ExtensionTrait for MyTheme {
|
||||
/// impl Extension for MyTheme {
|
||||
/// fn theme(&self) -> Option<ThemeRef> {
|
||||
/// Some(&Self)
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// impl ThemeTrait for MyTheme {}
|
||||
/// impl Theme for MyTheme {}
|
||||
/// ```
|
||||
fn theme(&self) -> Option<ThemeRef> {
|
||||
None
|
||||
|
@ -94,7 +94,7 @@ pub trait ExtensionTrait: AnyInfo + Send + Sync {
|
|||
///
|
||||
/// pub struct ExtensionSample;
|
||||
///
|
||||
/// impl ExtensionTrait for ExtensionSample {
|
||||
/// impl Extension for ExtensionSample {
|
||||
/// fn configure_service(&self, scfg: &mut service::web::ServiceConfig) {
|
||||
/// scfg.route("/sample", web::get().to(route_sample));
|
||||
/// }
|
||||
|
|
|
@ -9,13 +9,12 @@
|
|||
//! tipografías, espaciados y cualquier otro detalle visual o de comportamiento (como animaciones,
|
||||
//! *scripts* de interfaz, etc.).
|
||||
//!
|
||||
//! Es una extensión más (implementando [`ExtensionTrait`](crate::core::extension::ExtensionTrait)).
|
||||
//! Se instala, activa y declara dependencias igual que el resto de extensiones; y se señala a sí
|
||||
//! misma como tema (implementando [`theme()`](crate::core::extension::ExtensionTrait::theme)
|
||||
//! y [`ThemeTrait`]).
|
||||
//! Es una extensión más (implementando [`Extension`](crate::core::extension::Extension)). Se
|
||||
//! instala, activa y declara dependencias igual que el resto de extensiones; y se señala a sí misma
|
||||
//! como tema (implementando [`theme()`](crate::core::extension::Extension::theme) y [`Theme`]).
|
||||
|
||||
mod definition;
|
||||
pub use definition::{ThemeRef, ThemeTrait};
|
||||
pub use definition::{Theme, ThemeRef};
|
||||
|
||||
mod regions;
|
||||
pub(crate) use regions::ChildrenInRegions;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::core::extension::ExtensionTrait;
|
||||
use crate::core::extension::Extension;
|
||||
use crate::core::theme::CONTENT_REGION_NAME;
|
||||
use crate::global;
|
||||
use crate::html::{html, Markup};
|
||||
|
@ -8,20 +8,20 @@ use crate::response::page::Page;
|
|||
/// 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;
|
||||
/// estáticas globales que implementan [`Theme`], pero también [`Extension`].
|
||||
pub type ThemeRef = &'static dyn Theme;
|
||||
|
||||
/// 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()`](ExtensionTrait::theme).
|
||||
/// Un tema implementará [`Theme`] y los métodos que sean necesarios de [`Extension`], aunque el
|
||||
/// único obligatorio es [`theme()`](Extension::theme).
|
||||
///
|
||||
/// ```rust
|
||||
/// use pagetop::prelude::*;
|
||||
///
|
||||
/// pub struct MyTheme;
|
||||
///
|
||||
/// impl ExtensionTrait for MyTheme {
|
||||
/// impl Extension for MyTheme {
|
||||
/// fn name(&self) -> L10n { L10n::n("My theme") }
|
||||
/// fn description(&self) -> L10n { L10n::n("Un tema personal") }
|
||||
///
|
||||
|
@ -30,9 +30,9 @@ pub type ThemeRef = &'static dyn ThemeTrait;
|
|||
/// }
|
||||
/// }
|
||||
///
|
||||
/// impl ThemeTrait for MyTheme {}
|
||||
/// impl Theme for MyTheme {}
|
||||
/// ```
|
||||
pub trait ThemeTrait: ExtensionTrait + Send + Sync {
|
||||
pub trait Theme: Extension + Send + Sync {
|
||||
fn regions(&self) -> Vec<(&'static str, L10n)> {
|
||||
vec![(CONTENT_REGION_NAME, L10n::l("content"))]
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue