Add modular custom text styles

Signed-off-by: kernelkind <kernelkind@gmail.com>
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
kernelkind
2024-04-12 13:02:23 -04:00
committed by William Casarin
parent c8b1c1cdda
commit e81cde5374
3 changed files with 96 additions and 1 deletions

33
Cargo.lock generated
View File

@@ -1805,6 +1805,12 @@ dependencies = [
"winapi",
]
[[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"
@@ -2588,6 +2594,8 @@ dependencies = [
"serde",
"serde_derive",
"serde_json",
"strum",
"strum_macros",
"tokio",
"tracing",
"tracing-subscriber",
@@ -3410,6 +3418,12 @@ dependencies = [
"untrusted 0.9.0",
]
[[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"
@@ -3761,6 +3775,25 @@ dependencies = [
"float-cmp",
]
[[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"

View File

@@ -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 = []

View File

@@ -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,