notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

commit e81cde5374e7fa90aded3409e660978db7c770a7
parent c8b1c1cddae5c295ee1df05949ada1c32d22ce60
Author: kernelkind <kernelkind@gmail.com>
Date:   Fri, 12 Apr 2024 13:02:23 -0400

Add modular custom text styles

Signed-off-by: kernelkind <kernelkind@gmail.com>
Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
MCargo.lock | 33+++++++++++++++++++++++++++++++++
MCargo.toml | 3+++
Msrc/app_style.rs | 61++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
3 files changed, 96 insertions(+), 1 deletion(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -1806,6 +1806,12 @@ dependencies = [ ] [[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + +[[package]] name = "hermit-abi" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2588,6 +2594,8 @@ dependencies = [ "serde", "serde_derive", "serde_json", + "strum", + "strum_macros", "tokio", "tracing", "tracing-subscriber", @@ -3411,6 +3419,12 @@ dependencies = [ ] [[package]] +name = "rustversion" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80af6f9131f277a45a3fba6ce8e2258037bb0477a67e610d3c1fe046ab31de47" + +[[package]] name = "ryu" version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -3762,6 +3776,25 @@ dependencies = [ ] [[package]] +name = "strum" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d8cec3501a5194c432b2b7976db6b7d10ec95c253208b45f83f7136aa985e29" + +[[package]] +name = "strum_macros" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6cf59daf282c0a494ba14fd21610a0325f9f90ec9d1231dea26bcb1d696c946" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "rustversion", + "syn 2.0.48", +] + +[[package]] name = "subtle" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml @@ -36,6 +36,9 @@ nostrdb = "0.3.0" hex = "0.4.3" base32 = "0.4.0" nostr-sdk = "0.29.0" +strum = "0.26" +strum_macros = "0.26" + [features] default = [] diff --git a/src/app_style.rs b/src/app_style.rs @@ -2,8 +2,10 @@ use crate::colors::{dark_color_theme, light_color_theme, ColorTheme, DarkTheme, use egui::{ epaint::Shadow, style::{WidgetVisuals, Widgets}, - Button, Context, Rounding, Stroke, Style, Ui, Visuals, + Button, Context, FontId, Rounding, Stroke, Style, TextStyle, Ui, Visuals, }; +use strum::IntoEnumIterator; +use strum_macros::EnumIter; const WIDGET_ROUNDING: Rounding = Rounding::same(8.0); @@ -34,6 +36,63 @@ pub fn user_requested_visuals_change(cur_darkmode: bool, ui: &mut Ui) -> Option< None } +/// Create custom text sizes for any FontSizes +pub fn create_text_styles(ctx: &Context, font_size: fn(NotedeckTextStyle) -> f32) -> Style { + let mut style = (*ctx.style()).clone(); + + style.text_styles = NotedeckTextStyle::iter().map(|text_style| { + (text_style.text_style(), FontId::new(font_size(text_style), egui::FontFamily::Proportional)) + }).collect(); + + style +} + +pub fn desktop_font_size(text_style: NotedeckTextStyle) -> f32 { + match text_style { + NotedeckTextStyle::Heading => 48.0, + NotedeckTextStyle::Heading2 => 24.0, + NotedeckTextStyle::Heading3 => 20.0, + NotedeckTextStyle::Body => 13.0, + NotedeckTextStyle::Button => 13.0, + NotedeckTextStyle::Small => 12.0, + } +} + +pub fn mobile_font_size(text_style: NotedeckTextStyle) -> f32 { + // TODO: tweak text sizes for optimal mobile viewing + match text_style { + NotedeckTextStyle::Heading => 48.0, + NotedeckTextStyle::Heading2 => 24.0, + NotedeckTextStyle::Heading3 => 20.0, + NotedeckTextStyle::Body => 13.0, + NotedeckTextStyle::Button => 13.0, + NotedeckTextStyle::Small => 12.0, + } +} + +#[derive(EnumIter)] +pub enum NotedeckTextStyle { + Heading, + Heading2, + Heading3, + Body, + Button, + Small, +} + +impl NotedeckTextStyle { + pub fn text_style(&self) -> TextStyle { + match self { + Self::Heading => TextStyle::Heading, + Self::Heading2 => TextStyle::Name("Heading2".into()), + Self::Heading3 => TextStyle::Name("Heading3".into()), + Self::Body => TextStyle::Body, + Self::Button => TextStyle::Button, + Self::Small => TextStyle::Small, + } + } +} + pub fn create_themed_visuals(theme: ColorTheme, default: Visuals) -> Visuals { Visuals { hyperlink_color: theme.hyperlink_color,