attempt at fixing font loading (doesn't work yet)

This commit is contained in:
Daniel Bulant 2024-11-22 13:49:55 +01:00
parent 9e47c89a7d
commit 394fcf01e8
No known key found for this signature in database
2 changed files with 30 additions and 11 deletions

View file

@ -1,7 +1,7 @@
use std::sync::{LazyLock, OnceLock}; use std::sync::{LazyLock, OnceLock};
use cushy::{ use cushy::{
fonts::LoadedFont, fonts::{FontCollection, LoadedFont},
styles::{ styles::{
components::{FontFamily, FontWeight, LineHeight}, components::{FontFamily, FontWeight, LineHeight},
DynamicComponent, FamilyOwned, FontFamilyList, Weight, DynamicComponent, FamilyOwned, FontFamilyList, Weight,
@ -12,13 +12,18 @@ use cushy::{
static FONT: OnceLock<LoadedFont> = OnceLock::new(); static FONT: OnceLock<LoadedFont> = OnceLock::new();
pub fn load_fonts(cushy: &Cushy) { pub fn load_fonts(/*cushy: &Cushy*/) -> FontCollection {
let font = cushy.fonts().push_unloadable( 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/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"); println!("loaded");
FONT.set(font).map_err(|_| ()).unwrap(); FONT.set(font).map_err(|_| ()).unwrap();
fonts
} }
fn font_component() -> DynamicComponent { fn font_component() -> DynamicComponent {
@ -26,7 +31,9 @@ fn font_component() -> DynamicComponent {
move |context| { move |context| {
let font = &FONT.get().unwrap(); let font = &FONT.get().unwrap();
// dbg!(font); // 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); dbg!(&face);
Some(cushy::styles::Component::custom(FontFamilyList::from( Some(cushy::styles::Component::custom(FontFamilyList::from(
vec![FamilyOwned::Name(face.families[0].0.clone())], 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<DynamicComponent> = LazyLock::new(font_component); static FONT_FAMILY_COMPONENT: LazyLock<DynamicComponent> = LazyLock::new(font_component);
static FONT_WEIGHT_COMPONENT: LazyLock<DynamicComponent> = LazyLock::new(font_weight_component);
pub fn icon(str: &str) -> impl MakeWidget { pub fn icon(str: &str) -> impl MakeWidget {
str.with_dynamic(&FontFamily, FONT_FAMILY_COMPONENT.clone()) 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}"; pub const SHUFFLE: &str = "\u{e043}";

View file

@ -40,7 +40,6 @@ mod widgets;
fn main() -> cushy::Result { fn main() -> cushy::Result {
let args = Args::parse(); let args = Args::parse();
let app = PendingApp::new(TokioRuntime::default()); let app = PendingApp::new(TokioRuntime::default());
load_fonts(app.cushy());
let token = get_token().unwrap(); let token = get_token().unwrap();
@ -95,16 +94,17 @@ fn main() -> cushy::Result {
let selected_page = Dynamic::new(ActivePage::default()); 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()) .and(LikedSongsPage::new(context.clone()).into_widget())
.into_columns() .into_columns()
.expand() .expand()
.and(bar(dynplayer)) .and(bar(dynplayer))
.into_rows() .into_rows()
.expand() .expand()
.make_window() .into_window();
.open(&mut app) let fonts = load_fonts(/*app.cushy()*/);
.unwrap(); win.fonts = fonts;
win.open(&mut app).unwrap();
}); });
}); });