diff --git a/crates/voelin-gpui/src/shell/widgets.rs b/crates/voelin-gpui/src/shell/widgets.rs index a0fb266..18829d4 100644 --- a/crates/voelin-gpui/src/shell/widgets.rs +++ b/crates/voelin-gpui/src/shell/widgets.rs @@ -11,6 +11,7 @@ use gpui_kit::{ }; use gpui_kit::{ElementId, ObjectFit, StyledImage as _}; use voelin_ui_vm::Rgb; +use voelin_ui_vm::avatar::first_letter; use voelin_ui_vm::image::ImageRef; use crate::pictures::{PictureKey, picture}; @@ -90,13 +91,28 @@ pub fn banner_scrim(theme: &VoelinTheme) -> Background { ) } +/// Under this size (pixels) an avatar shows one letter, larger (`avatar.slint`). +const SMALL_AVATAR: f32 = 28.0; + +/// What an avatar of `size` pixels writes on its colour, and how large: +/// the initials at 0.38 of the size, but only the first letter at half of it +/// under 28 pixels, where two letters at 0.38 are a few pixels high and cramped +/// (`avatar.slint`'s `small`, with `Images.first-letter`). +fn initials_for(initials: &str, size: f32) -> (SharedString, f32) { + if size < SMALL_AVATAR { + (first_letter(initials).into(), size * 0.5) + } else { + (initials.to_owned().into(), size * 0.38) + } +} + /// An avatar: the picture when there is one (a file of the engine's cache), -/// else the initials on `tint`, square with rounded corners for a server -/// and round for a person, with a presence dot and a green ring while the -/// person's voice goes out. The picture is keyed by the file and its stamp -/// ([`crate::pictures`]), so a picture the engine writes again shows. Pictures -/// other than files of the cache are drawn as initials until the slices that -/// decode them (the chat's). +/// else the initials on `tint` (one letter under 28 pixels), square with +/// rounded corners for a server and round for a person, with a presence dot and +/// a green ring while the person's voice goes out. The picture is keyed by the +/// file and its stamp ([`crate::pictures`]), so a picture the engine writes +/// again shows. Pictures other than files of the cache are drawn as initials +/// until the slices that decode them (the chat's). #[derive(IntoElement)] pub struct Avatar { id: ElementId, @@ -166,13 +182,16 @@ impl RenderOnce for Avatar { .bg(hsla(if key.is_some() { c.surface_3 } else { tint(self.tint) })); let face = match key { Some(key) => face.child(picture(key).size_full().object_fit(ObjectFit::Cover)), - None => face.child( - div() - .text_size(px(self.size * 0.38)) - .font_weight(FontWeight::SEMIBOLD) - .text_color(hsla(Rgba(0xffff_ffff))) - .child(self.initials), - ), + None => { + let (letters, size) = initials_for(&self.initials, self.size); + face.child( + div() + .text_size(px(size)) + .font_weight(FontWeight::SEMIBOLD) + .text_color(hsla(Rgba(0xffff_ffff))) + .child(letters), + ) + } }; let mut avatar = div().id(self.id).relative().size(px(self.size)).flex_none(); if self.speaking { @@ -469,6 +488,20 @@ mod tests { use super::*; use crate::theme::Choice; + #[test] + fn two_letters_are_written_large_enough_and_one_letter_under_28_pixels() { + // The tree's 22 pixel avatars: one letter, half the size. + assert_eq!(initials_for("DE", 22.0), (SharedString::from("D"), 11.0)); + assert_eq!(initials_for("GP", 27.9).0, SharedString::from("G")); + // 28 pixels and up: the initials at 0.38 of the size. + let (letters, size) = initials_for("NS", 28.0); + assert_eq!((letters, size), (SharedString::from("NS"), 28.0 * 0.38)); + assert_eq!(initials_for("NS", 46.0).0, SharedString::from("NS")); + // What the engine gave when there is none, and a letter with a mark. + assert_eq!(initials_for("", 22.0).0, SharedString::from("")); + assert_eq!(initials_for("A\u{308}B", 22.0).0, SharedString::from("A\u{308}")); + } + #[test] fn the_banner_veil_keeps_the_text_readable_over_any_picture() { for choice in [Choice::Dark, Choice::Light] {