ui: add initial Profile hover previews

The idea with these is that on notedeck you can just hover your cursor
over a profile link to see the profile. I just have a stub for now, but
full design coming soon after.

Also simplify the preview system even further with a macro. In the
future I imagine we can grep every preview in the codebase, and then
include this as a string inside this macro. This is some kind of
template metaprogramming insanity but in theory it could work.

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2024-04-19 22:00:19 -07:00
parent 2d566cc637
commit 05fe164a49
13 changed files with 148 additions and 28 deletions

View File

@@ -1,10 +1,12 @@
pub mod note;
pub mod preview;
pub mod profile;
pub mod relay;
pub mod username;
pub use note::Note;
pub use preview::{Preview, PreviewApp};
pub use profile::ProfilePreview;
pub use relay::RelayView;
pub use username::Username;

View File

@@ -123,16 +123,27 @@ fn render_note_contents(
match block.blocktype() {
BlockType::MentionBech32 => match block.as_mention().unwrap() {
Mention::Pubkey(npub) => {
ui.colored_label(colors::PURPLE, "@");
let profile = damus.ndb.get_profile_by_pubkey(txn, npub.pubkey()).ok();
if let Some(name) = profile
.as_ref()
.and_then(|p| crate::profile::get_profile_name(p))
{
ui.colored_label(colors::PURPLE, name);
} else {
ui.colored_label(colors::PURPLE, "nostrich");
}
ui.horizontal(|ui| {
let profile = damus.ndb.get_profile_by_pubkey(txn, npub.pubkey()).ok();
let name: String = if let Some(name) =
profile.as_ref().and_then(crate::profile::get_profile_name)
{
format!("@{}", name)
} else {
"@nostrich".to_string()
};
let resp = ui.colored_label(colors::PURPLE, &name);
if let Some(rec) = profile.as_ref() {
resp.on_hover_ui_at_pointer(|ui| {
egui::Frame::default().show(ui, |ui| {
ui.add(ui::ProfilePreview::new(rec));
});
});
}
});
}
Mention::Note(note) if options.has_note_previews() => {

View File

@@ -106,7 +106,7 @@ impl<'a> Note<'a> {
match profile
.as_ref()
.ok()
.and_then(|p| p.record.profile()?.picture())
.and_then(|p| p.record().profile()?.picture())
{
// these have different lifetimes and types,
// so the calls must be separate

3
src/ui/profile/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
pub mod preview;
pub use preview::ProfilePreview;

65
src/ui/profile/preview.rs Normal file
View File

@@ -0,0 +1,65 @@
use nostrdb::ProfileRecord;
pub struct ProfilePreview<'a> {
profile: &'a ProfileRecord<'a>,
}
impl<'a> ProfilePreview<'a> {
pub fn new(profile: &'a ProfileRecord<'a>) -> Self {
ProfilePreview { profile }
}
}
impl<'a> egui::Widget for ProfilePreview<'a> {
fn ui(self, ui: &mut egui::Ui) -> egui::Response {
ui.horizontal(|ui| {
ui.label("Profile");
let name = if let Some(name) = crate::profile::get_profile_name(self.profile) {
name
} else {
"nostrich"
};
ui.label(name);
})
.response
}
}
mod previews {
use super::*;
use crate::test_data::test_profile_record;
use crate::ui::{Preview, View};
use egui::Widget;
pub struct ProfilePreviewPreview<'a> {
profile: ProfileRecord<'a>,
}
impl<'a> ProfilePreviewPreview<'a> {
pub fn new() -> Self {
let profile = test_profile_record();
ProfilePreviewPreview { profile }
}
}
impl<'a> Default for ProfilePreviewPreview<'a> {
fn default() -> Self {
ProfilePreviewPreview::new()
}
}
impl<'a> View for ProfilePreviewPreview<'a> {
fn ui(&mut self, ui: &mut egui::Ui) {
ProfilePreview::new(&self.profile).ui(ui);
}
}
impl<'a> Preview for ProfilePreview<'a> {
/// A preview of the profile preview :D
type Prev = ProfilePreviewPreview<'a>;
fn preview() -> Self::Prev {
ProfilePreviewPreview::new()
}
}
}

View File

@@ -44,7 +44,7 @@ impl<'a> Widget for Username<'a> {
};
if let Some(profile) = self.profile {
if let Some(prof) = profile.record.profile() {
if let Some(prof) = profile.record().profile() {
if prof.display_name().is_some() && prof.display_name().unwrap() != "" {
ui_abbreviate_name(ui, prof.display_name().unwrap(), self.abbrev, color);
} else if let Some(name) = prof.name() {