Elimina la asignación de márgenes/espaciado

This commit is contained in:
Manuel Cillero 2022-07-14 22:44:51 +02:00
parent c593fc5801
commit e011c9269a
18 changed files with 142 additions and 436 deletions

View file

@ -34,7 +34,7 @@ pub async fn summary() -> app::Result<Markup> {
Page::new()
.using_theme("Bootsier")
.with_context(InContextOp::SetTheme("Bootsier"))
.with_title("Admin")

View file

@ -23,7 +23,6 @@ pub struct Anchor {
weight : isize,
id : IdentifierValue,
classes : Classes,
layout : InlineLayout,
anchor_type: AnchorType,
href : AttributeValue,
html : Markup,
@ -40,7 +39,6 @@ impl ComponentTrait for Anchor {
weight : 0,
id : IdentifierValue::new(),
classes : Classes::new(),
layout : InlineLayout::new(),
anchor_type: AnchorType::Link,
href : AttributeValue::new(),
html : html! {},
@ -75,7 +73,6 @@ impl ComponentTrait for Anchor {
a
id=[self.id().get()]
class=[self.classes().get()]
style=[self.layout().get()]
href=[self.href().get()]
target=[target]
{
@ -130,11 +127,6 @@ impl Anchor {
self
}
pub fn with_layout(mut self, layout: &[LayoutSet]) -> Self {
self.alter_layout(layout);
self
}
pub fn with_type(mut self, anchor_type: AnchorType) -> Self {
self.alter_type(anchor_type);
self
@ -192,11 +184,6 @@ impl Anchor {
self
}
pub fn alter_layout(&mut self, layout: &[LayoutSet]) -> &mut Self {
self.layout.set(layout);
self
}
pub fn alter_type(&mut self, anchor_type: AnchorType) -> &mut Self {
self.anchor_type = anchor_type;
self.classes.alter(ClassesOp::SetDefault, match self.anchor_type {
@ -248,10 +235,6 @@ impl Anchor {
&self.classes
}
pub fn layout(&self) -> &InlineLayout {
&self.layout
}
pub fn anchor_type(&self) -> &AnchorType {
&self.anchor_type
}

View file

@ -22,7 +22,6 @@ pub struct Column {
weight : isize,
id : IdentifierValue,
classes : Classes,
layout : InlineLayout,
size : ColumnSize,
components: ComponentsBundle,
template : String,
@ -35,7 +34,6 @@ impl ComponentTrait for Column {
weight : 0,
id : IdentifierValue::new(),
classes : Classes::new(),
layout : InlineLayout::new(),
size : ColumnSize::Default,
components: ComponentsBundle::new(),
template : "default".to_owned(),
@ -74,7 +72,7 @@ impl ComponentTrait for Column {
fn default_render(&self, context: &mut InContext) -> Markup {
html! {
div id=[self.id().get()] class=[self.classes().get()] style=[self.layout().get()] {
div id=[self.id().get()] class=[self.classes().get()] {
(self.components().render(context))
}
}
@ -113,11 +111,6 @@ impl Column {
self
}
pub fn with_layout(mut self, layout: &[LayoutSet]) -> Self {
self.alter_layout(layout);
self
}
pub fn with_size(mut self, size: ColumnSize) -> Self {
self.alter_size(size);
self
@ -155,11 +148,6 @@ impl Column {
self
}
pub fn alter_layout(&mut self, layout: &[LayoutSet]) -> &mut Self {
self.layout.set(layout);
self
}
pub fn alter_size(&mut self, size: ColumnSize) -> &mut Self {
self.size = size;
self
@ -184,10 +172,6 @@ impl Column {
&self.classes
}
pub fn layout(&self) -> &InlineLayout {
&self.layout
}
pub fn size(&self) -> &ColumnSize {
&self.size
}

View file

@ -7,7 +7,6 @@ pub struct Row {
weight : isize,
id : IdentifierValue,
classes : Classes,
layout : InlineLayout,
columns : ComponentsBundle,
template : String,
}
@ -19,7 +18,6 @@ impl ComponentTrait for Row {
weight : 0,
id : IdentifierValue::new(),
classes : Classes::new_with_default("row"),
layout : InlineLayout::new(),
columns : ComponentsBundle::new(),
template : "default".to_owned(),
}
@ -39,7 +37,7 @@ impl ComponentTrait for Row {
fn default_render(&self, context: &mut InContext) -> Markup {
html! {
div id=[self.id().get()] class=[self.classes().get()] style=[self.layout().get()] {
div id=[self.id().get()] class=[self.classes().get()] {
(self.columns().render(context))
}
}
@ -78,11 +76,6 @@ impl Row {
self
}
pub fn with_layout(mut self, layout: &[LayoutSet]) -> Self {
self.alter_layout(layout);
self
}
pub fn with_column(mut self, column: grid::Column) -> Self {
self.alter_column(column);
self
@ -115,11 +108,6 @@ impl Row {
self
}
pub fn alter_layout(&mut self, layout: &[LayoutSet]) -> &mut Self {
self.layout.set(layout);
self
}
pub fn alter_column(&mut self, column: grid::Column) -> &mut Self {
self.columns.add(column);
self
@ -140,10 +128,6 @@ impl Row {
&self.classes
}
pub fn layout(&self) -> &InlineLayout {
&self.layout
}
pub fn columns(&self) -> &ComponentsBundle {
&self.columns
}

View file

@ -7,7 +7,6 @@ pub struct Icon {
weight : isize,
icon_name : String,
classes : Classes,
layout : InlineLayout,
}
impl ComponentTrait for Icon {
@ -17,7 +16,6 @@ impl ComponentTrait for Icon {
weight : 0,
icon_name : "question-circle-fill".to_owned(),
classes : Classes::new(),
layout : InlineLayout::new(),
}
}
@ -35,16 +33,16 @@ impl ComponentTrait for Icon {
fn before_render(&mut self, context: &mut InContext) {
context
.with_stylesheet(AssetsOp::Add(
.alter(InContextOp::StyleSheet(AssetsOp::Add(
StyleSheet::located("/theme/icons/bootstrap-icons.css")
.with_version("1.8.2")
));
)));
self.alter_classes(ClassesOp::SetDefault, concat_string!("bi-", self.icon_name()).as_str());
}
fn default_render(&self, _context: &mut InContext) -> Markup {
html! { i class=[self.classes().get()] style=[self.layout().get()] {}; }
html! { i class=[self.classes().get()] {}; }
}
fn as_ref_any(&self) -> &dyn AnyComponent {
@ -83,11 +81,6 @@ impl Icon {
self
}
pub fn with_layout(mut self, layout: &[LayoutSet]) -> Self {
self.alter_layout(layout);
self
}
// Icon ALTER.
pub fn alter_renderable(&mut self, renderable: fn() -> bool) -> &mut Self {
@ -110,11 +103,6 @@ impl Icon {
self
}
pub fn alter_layout(&mut self, layout: &[LayoutSet]) -> &mut Self {
self.layout.set(layout);
self
}
// Icon GETTERS.
pub fn icon_name(&self) -> &str {
@ -124,8 +112,4 @@ impl Icon {
pub fn classes(&self) -> &Classes {
&self.classes
}
pub fn layout(&self) -> &InlineLayout {
&self.layout
}
}

View file

@ -208,19 +208,19 @@ impl ComponentTrait for Menu {
fn before_render(&mut self, context: &mut InContext) {
context
.with_stylesheet(AssetsOp::Add(
.alter(InContextOp::StyleSheet(AssetsOp::Add(
StyleSheet::located("/theme/menu/css/menu.css")
.with_version("1.1.1")
))
.with_stylesheet(AssetsOp::Add(
)))
.alter(InContextOp::StyleSheet(AssetsOp::Add(
StyleSheet::located("/theme/menu/css/menu-clean.css")
.with_version("1.1.1")
))
.with_javascript(AssetsOp::Add(
)))
.alter(InContextOp::JavaScript(AssetsOp::Add(
JavaScript::located("/theme/menu/js/menu.min.js")
.with_version("1.1.1")
))
.add_jquery();
)))
.alter(InContextOp::AddJQuery);
}
fn default_render(&self, context: &mut InContext) -> Markup {

View file

@ -39,16 +39,7 @@ fn hello_world() -> Container {
Container::header()
.with_id("hello-world")
.with_component(grid::Row::new()
.with_layout(&[
LayoutSet::PaddingTop(UnitValue::RelEm(2.0)),
LayoutSet::PaddingBottom(UnitValue::RelEm(2.0)),
LayoutSet::PaddingLeft(UnitValue::RelPct(2.0)),
])
.with_column(grid::Column::new()
.with_layout(&[
LayoutSet::PaddingTop(UnitValue::RelEm(2.0)),
LayoutSet::PaddingBottom(UnitValue::RelEm(2.0)),
])
.with_size(grid::ColumnSize::Is4of12)
.with_component(Heading::h1(html! {
(l("page_title"))
@ -72,10 +63,6 @@ fn hello_world() -> Container {
("Offered services")
})
.with_left_icon(Icon::with("card-checklist"))
.with_layout(&[
LayoutSet::PaddingSide(UnitValue::UnSet, UnitValue::RelEm(1.5)),
LayoutSet::RadiusAll(UnitValue::RelEm(1.5)),
])
)
.with_component(Anchor::button("#", html! {
("Get quote")
@ -93,19 +80,11 @@ fn welcome() -> Container {
Container::new()
.with_id("visiting")
.with_component(grid::Row::new()
.with_layout(&[
LayoutSet::PaddingSide(UnitValue::RelEm(1.0), UnitValue::RelPct(5.0))
])
.with_column(grid::Column::new()
.with_layout(&[LayoutSet::PaddingAll(UnitValue::RelPct(2.0))])
.with_size(grid::ColumnSize::Is5of12)
.with_component(Image::image("/theme/images/demo-visiting.svg"))
)
.with_column(grid::Column::new()
.with_layout(&[
LayoutSet::PaddingTop(UnitValue::RelPct(2.5)),
LayoutSet::PaddingLeft(UnitValue::RelPct(5.0)),
])
.with_component(Heading::h2(html! {
(t("welcome_to", &args!["app" => SETTINGS.app.name.as_str()]))
}))
@ -128,14 +107,7 @@ fn about_pagetop() -> Container {
Container::new()
.with_id("pagetop")
.with_component(grid::Row::new()
.with_layout(&[
LayoutSet::PaddingSide(UnitValue::RelEm(1.0), UnitValue::RelPct(5.0))
])
.with_column(grid::Column::new()
.with_layout(&[
LayoutSet::PaddingTop(UnitValue::RelPct(2.5)),
LayoutSet::PaddingLeft(UnitValue::RelPct(5.0)),
])
.with_size(grid::ColumnSize::Is7of12)
.with_component(Heading::h2(html! {
(l("pagetop_title"))
@ -156,7 +128,6 @@ fn about_pagetop() -> Container {
)
)
.with_column(grid::Column::new()
.with_layout(&[LayoutSet::PaddingAll(UnitValue::RelPct(2.0))])
.with_component(Image::image("/theme/images/demo-pagetop.svg"))
)
)
@ -166,19 +137,11 @@ fn promo_pagetop() -> Container {
Container::new()
.with_id("promo")
.with_component(grid::Row::new()
.with_layout(&[
LayoutSet::PaddingSide(UnitValue::RelEm(1.0), UnitValue::RelPct(5.0))
])
.with_column(grid::Column::new()
.with_layout(&[LayoutSet::PaddingAll(UnitValue::RelPct(2.0))])
.with_size(grid::ColumnSize::Is5of12)
.with_component(Image::image("/theme/images/demo-pagetop.svg"))
)
.with_column(grid::Column::new()
.with_layout(&[
LayoutSet::PaddingTop(UnitValue::RelPct(2.5)),
LayoutSet::PaddingLeft(UnitValue::RelPct(5.0)),
])
.with_component(Heading::h2(html! {
(l("pagetop_promo_title"))
})
@ -198,14 +161,7 @@ fn reporting_problems() -> Container {
Container::new()
.with_id("reporting")
.with_component(grid::Row::new()
.with_layout(&[
LayoutSet::PaddingSide(UnitValue::RelEm(1.0), UnitValue::RelPct(5.0))
])
.with_column(grid::Column::new()
.with_layout(&[
LayoutSet::PaddingTop(UnitValue::RelPct(2.5)),
LayoutSet::PaddingLeft(UnitValue::RelPct(5.0)),
])
.with_size(grid::ColumnSize::Is7of12)
.with_component(Heading::h2(html! {
(l("report_problems_title"))
@ -222,7 +178,6 @@ fn reporting_problems() -> Container {
)
)
.with_column(grid::Column::new()
.with_layout(&[LayoutSet::PaddingAll(UnitValue::RelPct(2.0))])
.with_component(Image::image("/theme/images/demo-pagetop.svg"))
)
)

View file

@ -16,15 +16,15 @@ impl ThemeTrait for Aliner {
}
fn before_render_page(&self, page: &mut Page) {
page.context()
.with_favicon(Some(Favicon::new()
page
.alter_context(InContextOp::Favicon(Some(Favicon::new()
.with_icon("/theme/favicon.png")
))
.with_stylesheet(AssetsOp::Add(
)))
.alter_context(InContextOp::StyleSheet(AssetsOp::Add(
StyleSheet::located(
"/aliner/css/styles.css"
)
.with_weight(-99)
));
)));
}
}

View file

@ -18,21 +18,21 @@ impl ThemeTrait for Bootsier {
}
fn before_render_page(&self, page: &mut Page) {
page.context()
.with_favicon(Some(Favicon::new()
page
.alter_context(InContextOp::Favicon(Some(Favicon::new()
.with_icon("/theme/favicon.png")
))
.with_stylesheet(AssetsOp::Add(
)))
.alter_context(InContextOp::StyleSheet(AssetsOp::Add(
StyleSheet::located("/bootsier/css/bootstrap.min.css")
.with_version("5.1.3")
.with_weight(-99)
))
.with_javascript(AssetsOp::Add(
)))
.alter_context(InContextOp::JavaScript(AssetsOp::Add(
JavaScript::located("/bootsier/js/bootstrap.bundle.min.js")
.with_version("5.1.3")
.with_weight(-99)
))
.add_jquery();
)))
.alter_context(InContextOp::AddJQuery);
}
fn render_error_page(&self, mut s: app::http::StatusCode) -> app::Result<Markup> {

View file

@ -16,16 +16,16 @@ impl ThemeTrait for Bulmix {
}
fn before_render_page(&self, page: &mut Page) {
page.context()
.with_favicon(Some(Favicon::new()
page
.alter_context(InContextOp::Favicon(Some(Favicon::new()
.with_icon("/theme/favicon.png")
))
.with_stylesheet(AssetsOp::Add(
)))
.alter_context(InContextOp::StyleSheet(AssetsOp::Add(
StyleSheet::located("/bulmix/css/bulma.min.css")
.with_version("0.9.4")
.with_weight(-99)
))
.add_jquery();
)))
.alter_context(InContextOp::AddJQuery);
}
fn before_render_component(
@ -100,7 +100,7 @@ impl ThemeTrait for Bulmix {
let icon = component_ref::<Icon>(component);
Some(html! {
span class="icon" {
i class=[icon.classes().get()] style=[icon.layout().get()] {};
i class=[icon.classes().get()] {};
}
})
},

View file

@ -5,7 +5,7 @@ pub use hook::{
};
mod context;
pub use context::InContext;
pub use context::{InContext, InContextOp};
mod definition;
pub use definition::{

View file

@ -11,6 +11,15 @@ static DEFAULT_THEME: Lazy<&dyn ThemeTrait> = Lazy::new(|| {
}
});
pub enum InContextOp {
AddMetadata(&'static str, &'static str),
Favicon(Option<Favicon>),
SetTheme(&'static str),
StyleSheet(AssetsOp<StyleSheet>),
JavaScript(AssetsOp<JavaScript>),
AddJQuery,
}
pub struct InContext {
theme : &'static dyn ThemeTrait,
favicon : Option<Favicon>,
@ -34,40 +43,32 @@ impl InContext {
}
}
pub fn using_theme(&mut self, theme_name: &str) -> &mut Self {
self.theme = theme_by_single_name(theme_name).unwrap_or(*DEFAULT_THEME);
self
}
pub fn with_favicon(&mut self, favicon: Option<Favicon>) -> &mut Self {
self.favicon = favicon;
self
}
pub fn add_metadata(&mut self, name: String, content: String) -> &mut Self {
self.metadata.push((name, content));
self
}
pub fn with_stylesheet(&mut self, css: AssetsOp<StyleSheet>) -> &mut Self {
self.stylesheets.alter(css);
self
}
pub fn with_javascript(&mut self, js: AssetsOp<JavaScript>) -> &mut Self {
self.javascripts.alter(js);
self
}
pub fn add_jquery(&mut self) -> &mut Self {
if !self.with_jquery {
self.with_javascript(AssetsOp::Add(
JavaScript::located("/theme/js/jquery.min.js")
.with_version("3.6.0")
.with_weight(isize::MIN)
.with_mode(JSMode::Normal)
));
self.with_jquery = true;
pub fn alter(&mut self, op: InContextOp) -> &mut Self {
match op {
InContextOp::AddMetadata(name, content) => {
self.metadata.push((name.to_owned(), content.to_owned()));
},
InContextOp::Favicon(favicon) => {
self.favicon = favicon;
},
InContextOp::SetTheme(theme_name) => {
self.theme = theme_by_single_name(theme_name).unwrap_or(*DEFAULT_THEME);
},
InContextOp::StyleSheet(css) => {
self.stylesheets.alter(css);
},
InContextOp::JavaScript(js) => {
self.javascripts.alter(js);
},
InContextOp::AddJQuery => if !self.with_jquery {
self.javascripts.alter(AssetsOp::Add(
JavaScript::located("/theme/js/jquery.min.js")
.with_version("3.6.0")
.with_weight(isize::MIN)
.with_mode(JSMode::Normal)
));
self.with_jquery = true;
},
}
self
}

View file

@ -1,7 +1,7 @@
use crate::{app, concat_string, util};
use crate::config::SETTINGS;
use crate::html::{Favicon, Markup, html};
use crate::core::component::{ComponentTrait, InContext};
use crate::core::component::{ComponentTrait, InContext, InContextOp};
use crate::response::page::Page;
use crate::base::component::Chunck;
@ -27,10 +27,10 @@ pub trait ThemeTrait: BaseTheme + Send + Sync {
#[allow(unused_variables)]
fn before_render_page(&self, page: &mut Page) {
page.context()
.with_favicon(Some(Favicon::new()
page
.alter_context(InContextOp::Favicon(Some(Favicon::new()
.with_icon("/theme/favicon.png")
));
)));
}
fn render_page_head(&self, page: &mut Page) -> Markup {

View file

@ -16,9 +16,3 @@ pub use identifier::IdentifierValue;
mod classes;
pub use classes::{Classes, ClassesOp, ClassValue};
mod unit;
pub use unit::UnitValue;
mod layout;
pub use layout::{InlineLayout, LayoutSet};

View file

@ -33,7 +33,7 @@ impl Classes {
classes
}
pub fn alter(&mut self, op: ClassesOp, classes: &str) -> &Self {
pub fn alter(&mut self, op: ClassesOp, classes: &str) -> &mut Self {
let classes = classes.trim();
match op {
ClassesOp::Add => {

View file

@ -1,165 +0,0 @@
use crate::concat_string;
use super::unit::UnitValue;
const RADIUS_BOTTOM_LEFT: &str = "border-bottom-left-radius";
const RADIUS_BOTTOM_RIGHT: &str = "border-bottom-right-radius";
const RADIUS_TOP_LEFT: &str = "border-top-left-radius";
const RADIUS_TOP_RIGHT: &str = "border-top-right-radius";
const MARGIN_BOTTOM: &str = "margin-bottom";
const MARGIN_LEFT: &str = "margin-left";
const MARGIN_RIGHT: &str = "margin-right";
const MARGIN_TOP: &str = "margin-top";
const PADDING_BOTTOM: &str = "padding-bottom";
const PADDING_LEFT: &str = "padding-left";
const PADDING_RIGHT: &str = "padding-right";
const PADDING_TOP: &str = "padding-top";
pub enum LayoutSet {
Margin(UnitValue, UnitValue, UnitValue, UnitValue),
MarginAll(UnitValue),
MarginSide(UnitValue, UnitValue),
MarginBottom(UnitValue),
MarginLeft(UnitValue),
MarginRight(UnitValue),
MarginTop(UnitValue),
Padding(UnitValue, UnitValue, UnitValue, UnitValue),
PaddingAll(UnitValue),
PaddingSide(UnitValue, UnitValue),
PaddingBottom(UnitValue),
PaddingLeft(UnitValue),
PaddingRight(UnitValue),
PaddingTop(UnitValue),
Radius(UnitValue, UnitValue, UnitValue, UnitValue),
RadiusAll(UnitValue),
RadiusBottomLeft(UnitValue),
RadiusBottomRight(UnitValue),
RadiusTopLeft(UnitValue),
RadiusTopRight(UnitValue),
}
impl LayoutSet {
fn set(&self, into_spaces: &mut InlineLayout) {
match self {
// MARGIN LAYOUT.
LayoutSet::Margin(top, right, bottom, left) => {
self.add(MARGIN_TOP, top, into_spaces);
self.add(MARGIN_RIGHT, right, into_spaces);
self.add(MARGIN_BOTTOM, bottom, into_spaces);
self.add(MARGIN_LEFT, left, into_spaces);
},
LayoutSet::MarginAll(val) => {
self.add(MARGIN_TOP, val, into_spaces);
self.add(MARGIN_RIGHT, val, into_spaces);
self.add(MARGIN_BOTTOM, val, into_spaces);
self.add(MARGIN_LEFT, val, into_spaces);
},
LayoutSet::MarginSide(top_bottom, right_left) => {
self.add(MARGIN_TOP, top_bottom, into_spaces);
self.add(MARGIN_RIGHT, right_left, into_spaces);
self.add(MARGIN_BOTTOM, top_bottom, into_spaces);
self.add(MARGIN_LEFT, right_left, into_spaces);
},
LayoutSet::MarginTop(val) => self.add(MARGIN_TOP, val, into_spaces),
LayoutSet::MarginRight(val) => self.add(MARGIN_RIGHT, val, into_spaces),
LayoutSet::MarginBottom(val) => self.add(MARGIN_BOTTOM, val, into_spaces),
LayoutSet::MarginLeft(val) => self.add(MARGIN_LEFT, val, into_spaces),
// PADDING LAYOUT.
LayoutSet::Padding(top, right, bottom, left) => {
self.add(PADDING_TOP, top, into_spaces);
self.add(PADDING_RIGHT, right, into_spaces);
self.add(PADDING_BOTTOM, bottom, into_spaces);
self.add(PADDING_LEFT, left, into_spaces);
},
LayoutSet::PaddingAll(val) => {
self.add(PADDING_TOP, val, into_spaces);
self.add(PADDING_RIGHT, val, into_spaces);
self.add(PADDING_BOTTOM, val, into_spaces);
self.add(PADDING_LEFT, val, into_spaces);
},
LayoutSet::PaddingSide(top_bottom, right_left) => {
self.add(PADDING_TOP, top_bottom, into_spaces);
self.add(PADDING_RIGHT, right_left, into_spaces);
self.add(PADDING_BOTTOM, top_bottom, into_spaces);
self.add(PADDING_LEFT, right_left, into_spaces);
},
LayoutSet::PaddingTop(val) => self.add(PADDING_TOP, val, into_spaces),
LayoutSet::PaddingRight(val) => self.add(PADDING_RIGHT, val, into_spaces),
LayoutSet::PaddingBottom(val) => self.add(PADDING_BOTTOM, val, into_spaces),
LayoutSet::PaddingLeft(val) => self.add(PADDING_LEFT, val, into_spaces),
// BORDER RADIUS LAYOUT.
LayoutSet::Radius(top_left, top_right, bottom_right, bottom_left) => {
self.add(RADIUS_TOP_LEFT, top_left, into_spaces);
self.add(RADIUS_TOP_RIGHT, top_right, into_spaces);
self.add(RADIUS_BOTTOM_RIGHT, bottom_right, into_spaces);
self.add(RADIUS_BOTTOM_LEFT, bottom_left, into_spaces);
},
LayoutSet::RadiusAll(val) => {
self.add(RADIUS_TOP_LEFT, val, into_spaces);
self.add(RADIUS_TOP_RIGHT, val, into_spaces);
self.add(RADIUS_BOTTOM_RIGHT, val, into_spaces);
self.add(RADIUS_BOTTOM_LEFT, val, into_spaces);
},
LayoutSet::RadiusTopLeft(val) => self.add(RADIUS_TOP_LEFT, val, into_spaces),
LayoutSet::RadiusTopRight(val) => self.add(RADIUS_TOP_RIGHT, val, into_spaces),
LayoutSet::RadiusBottomRight(val) => self.add(RADIUS_BOTTOM_RIGHT, val, into_spaces),
LayoutSet::RadiusBottomLeft(val) => self.add(RADIUS_BOTTOM_LEFT, val, into_spaces),
}
}
fn add(&self, property: &str, value: &UnitValue, into_spaces: &mut InlineLayout) {
let val = value.to_string();
let style = InlineProperty {
property: property.to_owned(),
inline : concat_string!(property, ":", val, ";"),
};
match into_spaces.0.iter().position(|s| s.property.eq(&style.property)) {
Some(pos) => {
into_spaces.0.remove(pos);
if !val.is_empty() {
into_spaces.0.insert(pos, style);
}
},
_ => if !val.is_empty() {
into_spaces.0.push(style)
}
}
}
}
struct InlineProperty {
property: String,
inline : String,
}
pub struct InlineLayout(Vec<InlineProperty>);
impl InlineLayout {
pub fn new() -> Self {
InlineLayout(Vec::new())
}
pub fn set(&mut self, layout: &[LayoutSet]) -> &Self {
for i in 0..layout.len() {
layout[i].set(self);
}
self
}
pub fn get(&self) -> Option<String> {
if self.0.len() == 0 {
None
} else {
let mut inline = "".to_owned();
self.0.iter().for_each(|s| inline.push_str(s.inline.as_str()));
Some(inline)
}
}
}

View file

@ -1,52 +0,0 @@
use crate::concat_string;
// About pixels: Pixels (px) are relative to the viewing device. For low-dpi
// devices, 1px is one device pixel (dot) of the display. For printers and high
// resolution screens 1px implies multiple device pixels.
// About em: 2em means 2 times the size of the current font. The em and rem
// units are practical in creating perfectly scalable layout!
// About viewport: If the browser window size is 50cm wide, 1vw = 0.5cm.
pub enum UnitValue {
Auto,
Cm(isize), // Centimeters.
In(isize), // Inches (1in = 96px = 2.54cm).
Mm(isize), // Millimeters.
Pc(isize), // Picas (1pc = 12pt).
Pt(isize), // Points (1pt = 1/72 of 1in).
Px(isize), // Pixels (1px = 1/96th of 1in).
RelEm(f32), // Relative to the font-size of the element.
RelPct(f32), // Percentage relative to the parent element.
RelRem(f32), // Relative to font-size of the root element.
RelVh(f32), // Relative to 1% of the height of the viewport.
RelVw(f32), // Relative to 1% of the value of the viewport.
UnSet,
}
impl ToString for UnitValue {
fn to_string(&self) -> String {
match self {
UnitValue::Auto => "auto".to_owned(),
// Absolute value.
UnitValue::Cm(aw) => concat_string!(aw.to_string(), "cm"),
UnitValue::In(aw) => concat_string!(aw.to_string(), "in"),
UnitValue::Mm(aw) => concat_string!(aw.to_string(), "mm"),
UnitValue::Pc(aw) => concat_string!(aw.to_string(), "pc"),
UnitValue::Pt(aw) => concat_string!(aw.to_string(), "pt"),
UnitValue::Px(aw) => concat_string!(aw.to_string(), "px"),
// Relative value.
UnitValue::RelEm(rw) => concat_string!(rw.to_string(), "em"),
UnitValue::RelPct(rw) => concat_string!(rw.to_string(), "%"),
UnitValue::RelRem(rw) => concat_string!(rw.to_string(), "rem"),
UnitValue::RelVh(rw) => concat_string!(rw.to_string(), "vh"),
UnitValue::RelVw(rw) => concat_string!(rw.to_string(), "vw"),
_ => "".to_owned(),
}
}
}

View file

@ -36,21 +36,22 @@ static DEFAULT_DIRECTION: Lazy<Option<String>> = Lazy::new(|| {
pub enum TextDirection { Auto, LeftToRight, RightToLeft }
pub struct Page<'a> {
pub struct Page {
context : InContext,
language : AttributeValue,
direction : AttributeValue,
title : AttributeValue,
description : AttributeValue,
context : InContext,
regions : HashMap<&'a str, ComponentsBundle>,
body_classes: Classes,
regions : HashMap<&'static str, ComponentsBundle>,
template : String,
}
impl<'a> Page<'a> {
impl Page {
pub fn new() -> Self {
Page {
context : InContext::new(),
language : match &*DEFAULT_LANGUAGE {
Some(language) => AttributeValue::new_with_value(language),
_ => AttributeValue::new(),
@ -61,44 +62,49 @@ impl<'a> Page<'a> {
},
title : AttributeValue::new(),
description : AttributeValue::new(),
context : InContext::new(),
regions : common_components(),
body_classes: Classes::new_with_default("body"),
regions : common_components(),
template : "default".to_owned(),
}
}
// Page BUILDER.
pub fn with_language(&mut self, language: &str) -> &mut Self {
self.language.with_value(language);
pub fn with_context(mut self, op: InContextOp) -> Self {
self.alter_context(op);
self
}
pub fn with_direction(&mut self, dir: TextDirection) -> &mut Self {
self.direction.with_value(match dir {
TextDirection::Auto => "auto",
TextDirection::LeftToRight => "ltr",
TextDirection::RightToLeft => "rtl",
});
pub fn with_language(mut self, language: &str) -> Self {
self.alter_language(language);
self
}
pub fn with_title(&mut self, title: &str) -> &mut Self {
self.title.with_value(title);
pub fn with_direction(mut self, dir: TextDirection) -> Self {
self.alter_direction(dir);
self
}
pub fn with_description(&mut self, description: &str) -> &mut Self {
self.description.with_value(description);
pub fn with_title(mut self, title: &str) -> Self {
self.alter_title(title);
self
}
pub fn with_description(mut self, description: &str) -> Self {
self.alter_description(description);
self
}
pub fn with_body_classes(mut self, op: ClassesOp, classes: &str) -> Self {
self.alter_body_classes(op, classes);
self
}
pub fn add_to(
&mut self,
region: &'a str,
mut self,
region: &'static str,
component: impl ComponentTrait
) -> &mut Self {
) -> Self {
if let Some(regions) = self.regions.get_mut(region) {
regions.add(component);
} else {
@ -107,18 +113,58 @@ impl<'a> Page<'a> {
self
}
pub fn using_template(mut self, template: &str) -> Self {
self.alter_template(template);
self
}
// Page ALTER.
pub fn alter_context(&mut self, op: InContextOp) -> &mut Self {
self.context.alter(op);
self
}
pub fn alter_language(&mut self, language: &str) -> &mut Self {
self.language.with_value(language);
self
}
pub fn alter_direction(&mut self, dir: TextDirection) -> &mut Self {
self.direction.with_value(match dir {
TextDirection::Auto => "auto",
TextDirection::LeftToRight => "ltr",
TextDirection::RightToLeft => "rtl",
});
self
}
pub fn alter_title(&mut self, title: &str) -> &mut Self {
self.title.with_value(title);
self
}
pub fn alter_description(&mut self, description: &str) -> &mut Self {
self.description.with_value(description);
self
}
pub fn alter_body_classes(&mut self, op: ClassesOp, classes: &str) -> &mut Self {
self.body_classes.alter(op, classes);
self
}
pub fn using_template(&mut self, template: &str) -> &mut Self {
pub fn alter_template(&mut self, template: &str) -> &mut Self {
self.template = template.to_owned();
self
}
// Page GETTERS.
pub fn context(&mut self) -> &mut InContext {
&mut self.context
}
pub fn language(&self) -> &AttributeValue {
&self.language
}
@ -135,10 +181,6 @@ impl<'a> Page<'a> {
&self.description
}
pub fn context(&mut self) -> &mut InContext {
&mut self.context
}
pub fn body_classes(&self) -> &Classes {
&self.body_classes
}
@ -184,8 +226,4 @@ impl<'a> Page<'a> {
// Page EXTRAS.
pub fn using_theme(&mut self, theme_name: &str) -> &mut Self {
self.context.using_theme(theme_name);
self
}
}