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