From c45556d8e38f97cc6a083565717a145745bc5eb6 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Fri, 1 Sep 2023 07:19:32 +0200 Subject: [PATCH] feat(widget): add typography functions to text module --- src/widget/mod.rs | 2 +- src/widget/text.rs | 81 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 75 insertions(+), 8 deletions(-) diff --git a/src/widget/mod.rs b/src/widget/mod.rs index de0660b9..df62e544 100644 --- a/src/widget/mod.rs +++ b/src/widget/mod.rs @@ -60,7 +60,7 @@ pub mod settings; pub mod spin_button; pub use spin_button::{spin_button, SpinButton}; -mod text; +pub mod text; pub use text::{text, Text}; mod toggler; diff --git a/src/widget/text.rs b/src/widget/text.rs index d2357081..6712d84c 100644 --- a/src/widget/text.rs +++ b/src/widget/text.rs @@ -1,14 +1,81 @@ -use std::borrow::Cow; - +use crate::Renderer; pub use iced::widget::Text; +use std::borrow::Cow; /// Creates a new [`Text`] widget with the provided content. /// /// [`Text`]: widget::Text -pub fn text<'a, Renderer>(text: impl Into>) -> Text<'a, Renderer> -where - Renderer: iced_core::text::Renderer, - Renderer::Theme: iced::widget::text::StyleSheet, -{ +pub fn text<'a>(text: impl Into> + 'a) -> Text<'a, Renderer> { Text::new(text) } + +/// Available presets for text typography +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +pub enum Typography { + Body, + Caption, + CaptionHeading, + Heading, + Monotext, + Title1, + Title2, + Title3, + Title4, +} + +/// [`Text`] widget with the Title 1 typography preset. +pub fn title1<'a>(text: impl Into> + 'a) -> Text<'a, Renderer> { + Text::new(text) + .size(32.0) + .line_height(44.0) + .font(crate::font::FONT_LIGHT) +} + +/// [`Text`] widget with the Title 2 typography preset. +pub fn title2<'a>(text: impl Into> + 'a) -> Text<'a, Renderer> { + Text::new(text).size(28.0).line_height(36.0) +} + +/// [`Text`] widget with the Title 3 typography preset. +pub fn title3<'a>(text: impl Into> + 'a) -> Text<'a, Renderer> { + Text::new(text).size(24.0).line_height(32.0) +} + +/// [`Text`] widget with the Title 4 typography preset. +pub fn title4<'a>(text: impl Into> + 'a) -> Text<'a, Renderer> { + Text::new(text).size(20.0).line_height(28.0) +} + +/// [`Text`] widget with the Heading typography preset. +pub fn heading<'a>(text: impl Into> + 'a) -> Text<'a, Renderer> { + Text::new(text) + .size(14.0) + .line_height(20.0) + .font(crate::font::FONT_SEMIBOLD) +} + +/// [`Text`] widget with the Caption Heading typography preset. +pub fn caption_heading<'a>(text: impl Into> + 'a) -> Text<'a, Renderer> { + Text::new(text) + .size(10.0) + .line_height(14.0) + .font(crate::font::FONT_SEMIBOLD) +} + +/// [`Text`] widget with the Body typography preset. +pub fn body<'a>(text: impl Into> + 'a) -> Text<'a, Renderer> { + Text::new(text).size(14.0).line_height(20.0) +} + +/// [`Text`] widget with the Caption typography preset. +pub fn caption<'a>(text: impl Into> + 'a) -> Text<'a, Renderer> { + Text::new(text).size(10.0).line_height(14.0) +} + +/// [`Text`] widget with the Monotext typography preset. +pub fn monotext<'a>(text: impl Into> + 'a) -> Text<'a, Renderer> { + Text::new(text) + .size(14.0) + .line_height(20.0) + .font(crate::font::FONT_MONO_REGULAR) +}