notedeck

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

context.rs (4726B)


      1 use egui::{Rect, Vec2};
      2 use nostrdb::NoteKey;
      3 use notedeck::{tr, BroadcastContext, Localization, NoteContextSelection};
      4 
      5 use crate::context_menu::{context_button, stationary_arbitrary_menu_button};
      6 
      7 pub struct NoteContextButton {
      8     put_at: Option<Rect>,
      9     note_key: NoteKey,
     10 }
     11 
     12 impl egui::Widget for NoteContextButton {
     13     fn ui(self, ui: &mut egui::Ui) -> egui::Response {
     14         let r = if let Some(r) = self.put_at {
     15             r
     16         } else {
     17             let mut place = ui.available_rect_before_wrap();
     18             let size = Self::max_width();
     19             place.set_width(size);
     20             place.set_height(size);
     21             place
     22         };
     23 
     24         Self::show(ui, self.note_key, r)
     25     }
     26 }
     27 
     28 impl NoteContextButton {
     29     pub fn new(note_key: NoteKey) -> Self {
     30         let put_at: Option<Rect> = None;
     31         NoteContextButton { note_key, put_at }
     32     }
     33 
     34     pub fn place_at(mut self, rect: Rect) -> Self {
     35         self.put_at = Some(rect);
     36         self
     37     }
     38 
     39     pub fn max_width() -> f32 {
     40         Self::max_radius() * 3.0 + Self::max_distance_between_circles() * 2.0
     41     }
     42 
     43     pub fn size() -> Vec2 {
     44         let width = Self::max_width();
     45         egui::vec2(width, width)
     46     }
     47 
     48     fn max_radius() -> f32 {
     49         4.0
     50     }
     51 
     52     fn max_distance_between_circles() -> f32 {
     53         2.0
     54     }
     55 
     56     #[profiling::function]
     57     pub fn show(ui: &mut egui::Ui, note_key: NoteKey, put_at: Rect) -> egui::Response {
     58         let id = ui.id().with(("more_options_anim", note_key));
     59 
     60         context_button(ui, id, put_at)
     61     }
     62 
     63     #[profiling::function]
     64     pub fn menu(
     65         ui: &mut egui::Ui,
     66         i18n: &mut Localization,
     67         button_response: egui::Response,
     68     ) -> Option<NoteContextSelection> {
     69         let mut context_selection: Option<NoteContextSelection> = None;
     70 
     71         stationary_arbitrary_menu_button(ui, button_response, |ui| {
     72             ui.set_max_width(200.0);
     73 
     74             if ui
     75                 .button(tr!(
     76                     i18n,
     77                     "Copy Note Link",
     78                     "Copy the damus.io note link for this note to clipboard"
     79                 ))
     80                 .clicked()
     81             {
     82                 context_selection = Some(NoteContextSelection::CopyNeventLink);
     83                 ui.close_menu();
     84             }
     85 
     86             // Debug: Check what the tr! macro returns
     87             let copy_text = tr!(
     88                 i18n,
     89                 "Copy Text",
     90                 "Copy the text content of the note to clipboard"
     91             );
     92             tracing::debug!("Copy Text translation: '{}'", copy_text);
     93 
     94             if ui.button(copy_text).clicked() {
     95                 context_selection = Some(NoteContextSelection::CopyText);
     96                 ui.close_menu();
     97             }
     98             if ui
     99                 .button(tr!(
    100                     i18n,
    101                     "Copy Pubkey",
    102                     "Copy the author's public key to clipboard"
    103                 ))
    104                 .clicked()
    105             {
    106                 context_selection = Some(NoteContextSelection::CopyPubkey);
    107                 ui.close_menu();
    108             }
    109             if ui
    110                 .button(tr!(
    111                     i18n,
    112                     "Copy Note ID",
    113                     "Copy the note identifier to clipboard"
    114                 ))
    115                 .clicked()
    116             {
    117                 context_selection = Some(NoteContextSelection::CopyNevent);
    118                 ui.close_menu();
    119             }
    120             if ui
    121                 .button(tr!(
    122                     i18n,
    123                     "Copy Note JSON",
    124                     "Copy the raw note data in JSON format to clipboard"
    125                 ))
    126                 .clicked()
    127             {
    128                 context_selection = Some(NoteContextSelection::CopyNoteJSON);
    129                 ui.close_menu();
    130             }
    131             if ui
    132                 .button(tr!(
    133                     i18n,
    134                     "Broadcast",
    135                     "Broadcast the note to all connected relays"
    136                 ))
    137                 .clicked()
    138             {
    139                 context_selection = Some(NoteContextSelection::Broadcast(
    140                     BroadcastContext::Everywhere,
    141                 ));
    142                 ui.close_menu();
    143             }
    144             if ui
    145                 .button(tr!(
    146                     i18n,
    147                     "Broadcast Local",
    148                     "Broadcast the note only to local network relays"
    149                 ))
    150                 .clicked()
    151             {
    152                 context_selection = Some(NoteContextSelection::Broadcast(
    153                     BroadcastContext::LocalNetwork,
    154                 ));
    155                 ui.close_menu();
    156             }
    157         });
    158 
    159         context_selection
    160     }
    161 }