From 394fcf01e801366c5c52431c41c1fcc0fddd352d Mon Sep 17 00:00:00 2001 From: Daniel Bulant Date: Fri, 22 Nov 2024 13:49:55 +0100 Subject: [PATCH] attempt at fixing font loading (doesn't work yet) --- src/icons.rs | 31 +++++++++++++++++++++++++------ src/main.rs | 10 +++++----- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/icons.rs b/src/icons.rs index 90aa23c..a016e9f 100644 --- a/src/icons.rs +++ b/src/icons.rs @@ -1,7 +1,7 @@ use std::sync::{LazyLock, OnceLock}; use cushy::{ - fonts::LoadedFont, + fonts::{FontCollection, LoadedFont}, styles::{ components::{FontFamily, FontWeight, LineHeight}, DynamicComponent, FamilyOwned, FontFamilyList, Weight, @@ -12,13 +12,18 @@ use cushy::{ static FONT: OnceLock = OnceLock::new(); -pub fn load_fonts(cushy: &Cushy) { - let font = cushy.fonts().push_unloadable( +pub fn load_fonts(/*cushy: &Cushy*/) -> FontCollection { + let fonts = FontCollection::default(); + let font_data = include_bytes!("../fonts/MaterialIcons-Regular.ttf").to_vec(); + dbg!(font_data.len()); + let font = fonts.push_unloadable( // include_bytes!("../fonts/MaterialSymbolsOutlined[FILL,GRAD,opsz,wght].ttf").into(), - include_bytes!("../fonts/MaterialIcons-Regular.ttf").into(), + font_data, ); + // /home/dan/projects/despot/fonts/MaterialIcons-Regular.ttf println!("loaded"); FONT.set(font).map_err(|_| ()).unwrap(); + fonts } fn font_component() -> DynamicComponent { @@ -26,7 +31,9 @@ fn font_component() -> DynamicComponent { move |context| { let font = &FONT.get().unwrap(); // dbg!(font); - let face = context.loaded_font_faces(font).first()?; + let faces = context.loaded_font_faces(font); + dbg!(&faces); + let face = faces.first()?; dbg!(&face); Some(cushy::styles::Component::custom(FontFamilyList::from( vec![FamilyOwned::Name(face.families[0].0.clone())], @@ -35,11 +42,23 @@ fn font_component() -> DynamicComponent { }) } +fn font_weight_component() -> DynamicComponent { + DynamicComponent::new({ + move |context| { + let font = &FONT.get().unwrap(); + let faces = context.loaded_font_faces(font); + let face = faces.first()?; + Some(cushy::styles::Component::FontWeight(face.weight)) + } + }) +} + static FONT_FAMILY_COMPONENT: LazyLock = LazyLock::new(font_component); +static FONT_WEIGHT_COMPONENT: LazyLock = LazyLock::new(font_weight_component); pub fn icon(str: &str) -> impl MakeWidget { str.with_dynamic(&FontFamily, FONT_FAMILY_COMPONENT.clone()) - .with(&FontWeight, Weight::NORMAL) + .with_dynamic(&FontWeight, FONT_WEIGHT_COMPONENT.clone()) } pub const SHUFFLE: &str = "\u{e043}"; diff --git a/src/main.rs b/src/main.rs index fba1ea3..365fa2c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,7 +40,6 @@ mod widgets; fn main() -> cushy::Result { let args = Args::parse(); let app = PendingApp::new(TokioRuntime::default()); - load_fonts(app.cushy()); let token = get_token().unwrap(); @@ -95,16 +94,17 @@ fn main() -> cushy::Result { let selected_page = Dynamic::new(ActivePage::default()); - playlists_widget(playlists.items, selected_page) + let mut win = playlists_widget(playlists.items, selected_page) .and(LikedSongsPage::new(context.clone()).into_widget()) .into_columns() .expand() .and(bar(dynplayer)) .into_rows() .expand() - .make_window() - .open(&mut app) - .unwrap(); + .into_window(); + let fonts = load_fonts(/*app.cushy()*/); + win.fonts = fonts; + win.open(&mut app).unwrap(); }); });