notedeck

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

top_buttons.rs (3177B)


      1 /// Top buttons UI for the Dave chat interface.
      2 ///
      3 /// Contains the profile picture button, settings button, and session list toggle
      4 /// that appear at the top of the chat view.
      5 use super::DaveAction;
      6 use nostrdb::{Ndb, Transaction};
      7 use notedeck::{Accounts, AppContext, Images, MediaJobSender};
      8 use notedeck_ui::{app_images, ProfilePic};
      9 
     10 /// Render the top buttons UI (profile pic, settings, session list toggle)
     11 pub fn top_buttons_ui(app_ctx: &mut AppContext, ui: &mut egui::Ui) -> Option<DaveAction> {
     12     let mut action: Option<DaveAction> = None;
     13     let mut rect = ui.available_rect_before_wrap();
     14     rect = rect.translate(egui::vec2(20.0, 20.0));
     15     rect.set_height(32.0);
     16     rect.set_width(32.0);
     17 
     18     // Show session list button on mobile/narrow screens
     19     if notedeck::ui::is_narrow(ui.ctx()) {
     20         let r = ui
     21             .put(rect, egui::Button::new("\u{2630}").frame(false))
     22             .on_hover_text("Show chats")
     23             .on_hover_cursor(egui::CursorIcon::PointingHand);
     24 
     25         if r.clicked() {
     26             action = Some(DaveAction::ShowSessionList);
     27         }
     28 
     29         rect = rect.translate(egui::vec2(30.0, 0.0));
     30     }
     31 
     32     let txn = Transaction::new(app_ctx.ndb).unwrap();
     33     let r = ui
     34         .put(
     35             rect,
     36             &mut pfp_button(
     37                 &txn,
     38                 app_ctx.accounts,
     39                 app_ctx.img_cache,
     40                 app_ctx.ndb,
     41                 app_ctx.media_jobs.sender(),
     42             ),
     43         )
     44         .on_hover_cursor(egui::CursorIcon::PointingHand);
     45 
     46     if r.clicked() {
     47         action = Some(DaveAction::ToggleChrome);
     48     }
     49 
     50     // Settings button
     51     rect = rect.translate(egui::vec2(30.0, 0.0));
     52     let dark_mode = ui.visuals().dark_mode;
     53     let r = ui
     54         .put(rect, settings_button(dark_mode))
     55         .on_hover_cursor(egui::CursorIcon::PointingHand);
     56 
     57     if r.clicked() {
     58         action = Some(DaveAction::OpenSettings);
     59     }
     60 
     61     action
     62 }
     63 
     64 fn settings_button(dark_mode: bool) -> impl egui::Widget {
     65     move |ui: &mut egui::Ui| {
     66         let img_size = 24.0;
     67         let max_size = 32.0;
     68 
     69         let img = if dark_mode {
     70             app_images::settings_dark_image()
     71         } else {
     72             app_images::settings_light_image()
     73         }
     74         .max_width(img_size);
     75 
     76         let helper = notedeck_ui::anim::AnimationHelper::new(
     77             ui,
     78             "settings-button",
     79             egui::vec2(max_size, max_size),
     80         );
     81 
     82         let cur_img_size = helper.scale_1d_pos(img_size);
     83         img.paint_at(
     84             ui,
     85             helper
     86                 .get_animation_rect()
     87                 .shrink((max_size - cur_img_size) / 2.0),
     88         );
     89 
     90         helper.take_animation_response()
     91     }
     92 }
     93 
     94 fn pfp_button<'me, 'a>(
     95     txn: &'a Transaction,
     96     accounts: &Accounts,
     97     img_cache: &'me mut Images,
     98     ndb: &Ndb,
     99     jobs: &'me MediaJobSender,
    100 ) -> ProfilePic<'me, 'a> {
    101     let account = accounts.get_selected_account();
    102     let profile = ndb
    103         .get_profile_by_pubkey(txn, account.key.pubkey.bytes())
    104         .ok();
    105 
    106     ProfilePic::from_profile_or_default(img_cache, jobs, profile.as_ref())
    107         .size(24.0)
    108         .sense(egui::Sense::click())
    109 }