preview.rs (3748B)
1 use crate::ProfilePic; 2 use egui::{Frame, Label, RichText}; 3 use egui_extras::Size; 4 use nostrdb::ProfileRecord; 5 6 use notedeck::{ 7 name::get_display_name, profile::get_profile_url, tr, Images, Localization, NotedeckTextStyle, 8 }; 9 10 use super::{about_section_widget, banner, display_name_widget}; 11 12 pub struct ProfilePreview<'a, 'cache> { 13 profile: &'a ProfileRecord<'a>, 14 cache: &'cache mut Images, 15 banner_height: Size, 16 } 17 18 impl<'a, 'cache> ProfilePreview<'a, 'cache> { 19 pub fn new(profile: &'a ProfileRecord<'a>, cache: &'cache mut Images) -> Self { 20 let banner_height = Size::exact(80.0); 21 ProfilePreview { 22 profile, 23 cache, 24 banner_height, 25 } 26 } 27 28 pub fn banner_height(&mut self, size: Size) { 29 self.banner_height = size; 30 } 31 32 fn body(self, ui: &mut egui::Ui) { 33 let padding = 12.0; 34 crate::padding(padding, ui, |ui| { 35 let mut pfp_rect = ui.available_rect_before_wrap(); 36 let size = 80.0; 37 pfp_rect.set_width(size); 38 pfp_rect.set_height(size); 39 let pfp_rect = pfp_rect.translate(egui::vec2(0.0, -(padding + 2.0 + (size / 2.0)))); 40 41 ui.put( 42 pfp_rect, 43 &mut ProfilePic::new(self.cache, get_profile_url(Some(self.profile))) 44 .size(size) 45 .border(ProfilePic::border_stroke(ui)), 46 ); 47 ui.add(display_name_widget( 48 &get_display_name(Some(self.profile)), 49 false, 50 )); 51 ui.add(about_section_widget(Some(self.profile))); 52 }); 53 } 54 } 55 56 impl egui::Widget for ProfilePreview<'_, '_> { 57 fn ui(self, ui: &mut egui::Ui) -> egui::Response { 58 ui.vertical(|ui| { 59 banner( 60 ui, 61 self.profile.record().profile().and_then(|p| p.banner()), 62 80.0, 63 ); 64 65 self.body(ui); 66 }) 67 .response 68 } 69 } 70 71 pub struct SimpleProfilePreview<'a, 'cache> { 72 profile: Option<&'a ProfileRecord<'a>>, 73 pub i18n: &'cache mut Localization, 74 cache: &'cache mut Images, 75 is_nsec: bool, 76 } 77 78 impl<'a, 'cache> SimpleProfilePreview<'a, 'cache> { 79 pub fn new( 80 profile: Option<&'a ProfileRecord<'a>>, 81 cache: &'cache mut Images, 82 i18n: &'cache mut Localization, 83 is_nsec: bool, 84 ) -> Self { 85 SimpleProfilePreview { 86 profile, 87 cache, 88 is_nsec, 89 i18n, 90 } 91 } 92 } 93 94 impl egui::Widget for SimpleProfilePreview<'_, '_> { 95 fn ui(self, ui: &mut egui::Ui) -> egui::Response { 96 Frame::new() 97 .show(ui, |ui| { 98 ui.add(&mut ProfilePic::new(self.cache, get_profile_url(self.profile)).size(48.0)); 99 ui.vertical(|ui| { 100 ui.add(display_name_widget(&get_display_name(self.profile), true)); 101 if !self.is_nsec { 102 ui.add( 103 Label::new( 104 RichText::new(tr!( 105 self.i18n, 106 "Read only", 107 "Label for read-only profile mode" 108 )) 109 .size(notedeck::fonts::get_font_size( 110 ui.ctx(), 111 &NotedeckTextStyle::Tiny, 112 )) 113 .color(ui.visuals().warn_fg_color), 114 ) 115 .selectable(false), 116 ); 117 } 118 }); 119 }) 120 .response 121 } 122 }