From d6f81991abf1bc3eeacbabfa94eadcb571f73290 Mon Sep 17 00:00:00 2001 From: kernelkind Date: Fri, 3 Jan 2025 17:30:56 -0500 Subject: [PATCH] refactor banner Signed-off-by: kernelkind --- crates/notedeck_columns/src/ui/profile/mod.rs | 51 ++++++++++--------- .../src/ui/profile/preview.rs | 8 +-- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/crates/notedeck_columns/src/ui/profile/mod.rs b/crates/notedeck_columns/src/ui/profile/mod.rs index 0f2b6a75..beaedf3d 100644 --- a/crates/notedeck_columns/src/ui/profile/mod.rs +++ b/crates/notedeck_columns/src/ui/profile/mod.rs @@ -96,9 +96,11 @@ impl<'a> ProfileView<'a> { fn profile_body(&mut self, ui: &mut egui::Ui, profile: ProfileRecord<'_>) { ui.vertical(|ui| { - ui.add_sized([ui.available_size().x, 120.0], |ui: &mut egui::Ui| { - banner(ui, &profile) - }); + banner( + ui, + profile.record().profile().and_then(|p| p.banner()), + 120.0, + ); let padding = 12.0; crate::ui::padding(padding, ui, |ui| { @@ -343,7 +345,11 @@ fn display_name_widget(name: NostrName<'_>, add_placeholder_space: bool) -> impl } pub fn get_profile_url<'a>(profile: Option<&ProfileRecord<'a>>) -> &'a str { - if let Some(url) = profile.and_then(|pr| pr.record().profile().and_then(|p| p.picture())) { + unwrap_profile_url(profile.and_then(|pr| pr.record().profile().and_then(|p| p.picture()))) +} + +pub fn unwrap_profile_url(maybe_url: Option<&str>) -> &str { + if let Some(url) = maybe_url { url } else { ProfilePic::no_pfp_url() @@ -366,16 +372,11 @@ where } } -fn banner_texture( - ui: &mut egui::Ui, - profile: &ProfileRecord<'_>, -) -> Option { +fn banner_texture(ui: &mut egui::Ui, banner_url: &str) -> Option { // TODO: cache banner - let banner = profile.record().profile().and_then(|p| p.banner()); - - if let Some(banner) = banner { + if !banner_url.is_empty() { let texture_load_res = - egui::Image::new(banner).load_for_size(ui.ctx(), ui.available_size()); + egui::Image::new(banner_url).load_for_size(ui.ctx(), ui.available_size()); if let Ok(texture_poll) = texture_load_res { match texture_poll { TexturePoll::Pending { .. } => {} @@ -387,16 +388,18 @@ fn banner_texture( None } -fn banner(ui: &mut egui::Ui, profile: &ProfileRecord<'_>) -> egui::Response { - if let Some(texture) = banner_texture(ui, profile) { - images::aspect_fill( - ui, - Sense::hover(), - texture.id, - texture.size.x / texture.size.y, - ) - } else { - // TODO: default banner texture - ui.label("") - } +fn banner(ui: &mut egui::Ui, banner_url: Option<&str>, height: f32) -> egui::Response { + ui.add_sized([ui.available_size().x, height], |ui: &mut egui::Ui| { + banner_url + .and_then(|url| banner_texture(ui, url)) + .map(|texture| { + images::aspect_fill( + ui, + Sense::hover(), + texture.id, + texture.size.x / texture.size.y, + ) + }) + .unwrap_or_else(|| ui.label("")) + }) } diff --git a/crates/notedeck_columns/src/ui/profile/preview.rs b/crates/notedeck_columns/src/ui/profile/preview.rs index 0befac3f..c52655d5 100644 --- a/crates/notedeck_columns/src/ui/profile/preview.rs +++ b/crates/notedeck_columns/src/ui/profile/preview.rs @@ -53,9 +53,11 @@ impl<'a, 'cache> ProfilePreview<'a, 'cache> { impl egui::Widget for ProfilePreview<'_, '_> { fn ui(self, ui: &mut egui::Ui) -> egui::Response { ui.vertical(|ui| { - ui.add_sized([ui.available_size().x, 80.0], |ui: &mut egui::Ui| { - banner(ui, self.profile) - }); + banner( + ui, + self.profile.record().profile().and_then(|p| p.banner()), + 80.0, + ); self.body(ui); })