Account switcher

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2024-05-24 16:46:37 -04:00
committed by William Casarin
parent f489ed3b9e
commit c0b1a01b5d
8 changed files with 257 additions and 95 deletions

View File

@@ -3,7 +3,7 @@ use crate::imgcache::ImageCache;
use crate::ui::ProfilePic;
use crate::{colors, images, DisplayName};
use egui::load::TexturePoll;
use egui::{Frame, RichText, Sense, Vec2, Widget};
use egui::{Frame, RichText, Sense, Widget};
use egui_extras::Size;
use nostrdb::ProfileRecord;
@@ -93,10 +93,6 @@ impl<'a, 'cache> SimpleProfilePreview<'a, 'cache> {
pub fn new(profile: &'a ProfileRecord<'a>, cache: &'cache mut ImageCache) -> Self {
SimpleProfilePreview { profile, cache }
}
pub fn dimensions(&self) -> Vec2 {
Vec2::new(120.0, 150.0)
}
}
impl<'a, 'cache> egui::Widget for SimpleProfilePreview<'a, 'cache> {
@@ -152,7 +148,7 @@ mod previews {
}
}
fn get_display_name<'a>(profile: &'a ProfileRecord<'a>) -> DisplayName<'a> {
pub fn get_display_name<'a>(profile: &'a ProfileRecord<'a>) -> DisplayName<'a> {
if let Some(name) = crate::profile::get_profile_name(profile) {
name
} else {

View File

@@ -1,8 +1,9 @@
use enostr::Pubkey;
use nostrdb::{Ndb, Transaction};
use crate::{account_manager::AccountManager, imgcache::ImageCache};
use crate::{account_manager::AccountManager, imgcache::ImageCache, DisplayName};
use super::preview::SimpleProfilePreview;
use super::preview::{get_display_name, SimpleProfilePreview};
pub struct SimpleProfilePreviewController<'a> {
ndb: &'a Ndb,
@@ -45,13 +46,12 @@ impl<'a> SimpleProfilePreviewController<'a> {
if let Ok(profile) = profile {
let preview = SimpleProfilePreview::new(&profile, self.img_cache);
let is_selected = if let Some(selected) =
account_manager.get_currently_selected_account()
{
i == selected
} else {
false
};
let is_selected =
if let Some(selected) = account_manager.get_selected_account_index() {
i == selected
} else {
false
};
if let Some(op) = add_preview_ui(ui, preview, width, is_selected) {
match op {
@@ -74,11 +74,17 @@ impl<'a> SimpleProfilePreviewController<'a> {
pub fn view_profile_previews(
&mut self,
account_manager: &'a AccountManager,
account_manager: &mut AccountManager,
ui: &mut egui::Ui,
add_preview_ui: fn(ui: &mut egui::Ui, preview: SimpleProfilePreview, index: usize) -> bool,
) -> Option<usize> {
let mut clicked_at: Option<usize> = None;
add_preview_ui: fn(
ui: &mut egui::Ui,
preview: SimpleProfilePreview,
width: f32,
is_selected: bool,
index: usize,
) -> bool,
) {
let width = ui.available_width();
for i in 0..account_manager.num_accounts() {
if let Some(account) = account_manager.get_account(i) {
@@ -90,14 +96,35 @@ impl<'a> SimpleProfilePreviewController<'a> {
if let Ok(profile) = profile {
let preview = SimpleProfilePreview::new(&profile, self.img_cache);
if add_preview_ui(ui, preview, i) {
clicked_at = Some(i)
let is_selected =
if let Some(selected) = account_manager.get_selected_account_index() {
i == selected
} else {
false
};
if add_preview_ui(ui, preview, width, is_selected, i) {
account_manager.select_account(i);
}
}
}
}
}
}
clicked_at
pub fn show_with_nickname(
&'a self,
ui: &mut egui::Ui,
key: &Pubkey,
ui_element: fn(ui: &mut egui::Ui, username: &DisplayName) -> egui::Response,
) -> Option<egui::Response> {
if let Ok(txn) = Transaction::new(self.ndb) {
let profile = self.ndb.get_profile_by_pubkey(&txn, key.bytes());
if let Ok(profile) = profile {
return Some(ui_element(ui, &get_display_name(&profile)));
}
}
None
}
}