notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

preview.rs (4057B)


      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.profile.record().profile().and_then(|p| p.banner()),
     69                 80.0,
     70             );
     71 
     72             self.body(ui);
     73         })
     74         .response
     75     }
     76 }
     77 
     78 pub struct SimpleProfilePreview<'a, 'cache> {
     79     profile: Option<&'a ProfileRecord<'a>>,
     80     pub i18n: &'cache mut Localization,
     81     cache: &'cache mut Images,
     82     jobs: &'cache MediaJobSender,
     83     is_nsec: bool,
     84 }
     85 
     86 impl<'a, 'cache> SimpleProfilePreview<'a, 'cache> {
     87     pub fn new(
     88         profile: Option<&'a ProfileRecord<'a>>,
     89         cache: &'cache mut Images,
     90         jobs: &'cache MediaJobSender,
     91         i18n: &'cache mut Localization,
     92         is_nsec: bool,
     93     ) -> Self {
     94         SimpleProfilePreview {
     95             profile,
     96             cache,
     97             is_nsec,
     98             i18n,
     99             jobs,
    100         }
    101     }
    102 }
    103 
    104 impl egui::Widget for SimpleProfilePreview<'_, '_> {
    105     fn ui(self, ui: &mut egui::Ui) -> egui::Response {
    106         Frame::new()
    107             .show(ui, |ui| {
    108                 ui.add(
    109                     &mut ProfilePic::new(self.cache, self.jobs, get_profile_url(self.profile))
    110                         .size(48.0),
    111                 );
    112                 ui.vertical(|ui| {
    113                     ui.add(display_name_widget(&get_display_name(self.profile), true));
    114                     if !self.is_nsec {
    115                         ui.add(
    116                             Label::new(
    117                                 RichText::new(tr!(
    118                                     self.i18n,
    119                                     "Read only",
    120                                     "Label for read-only profile mode"
    121                                 ))
    122                                 .size(notedeck::fonts::get_font_size(
    123                                     ui.ctx(),
    124                                     &NotedeckTextStyle::Tiny,
    125                                 ))
    126                                 .color(ui.visuals().warn_fg_color),
    127                             )
    128                             .selectable(false),
    129                         );
    130                     }
    131                 });
    132             })
    133             .response
    134     }
    135 }