Skip to content

Commit 93800e0

Browse files
kernelkindjb55
authored andcommitted
Add SimpleProfilePreview
Preview that only contains the pfp, display name, and username Signed-off-by: kernelkind <[email protected]> Signed-off-by: William Casarin <[email protected]>
1 parent 59818ed commit 93800e0

File tree

1 file changed

+90
-41
lines changed

1 file changed

+90
-41
lines changed

src/ui/profile/preview.rs

Lines changed: 90 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::imgcache::ImageCache;
33
use crate::ui::ProfilePic;
44
use crate::{colors, images, DisplayName};
55
use egui::load::TexturePoll;
6-
use egui::{RichText, Sense};
6+
use egui::{Frame, Layout, RichText, Sense, Vec2, Widget};
77
use egui_extras::Size;
88
use nostrdb::ProfileRecord;
99

@@ -63,46 +63,10 @@ impl<'a, 'cache> ProfilePreview<'a, 'cache> {
6363
}
6464

6565
fn body(self, ui: &mut egui::Ui) {
66-
let name = if let Some(name) = crate::profile::get_profile_name(self.profile) {
67-
name
68-
} else {
69-
DisplayName::One("??")
70-
};
71-
7266
crate::ui::padding(12.0, ui, |ui| {
73-
let url = if let Some(url) = self.profile.record().profile().and_then(|p| p.picture()) {
74-
url
75-
} else {
76-
ProfilePic::no_pfp_url()
77-
};
78-
79-
ui.add(ProfilePic::new(self.cache, url).size(80.0));
80-
81-
match name {
82-
DisplayName::One(n) => {
83-
ui.label(RichText::new(n).text_style(NotedeckTextStyle::Heading3.text_style()));
84-
}
85-
86-
DisplayName::Both {
87-
display_name,
88-
username,
89-
} => {
90-
ui.label(
91-
RichText::new(display_name)
92-
.text_style(NotedeckTextStyle::Heading3.text_style()),
93-
);
94-
95-
ui.label(
96-
RichText::new(format!("@{}", username))
97-
.size(12.0)
98-
.color(colors::MID_GRAY),
99-
);
100-
}
101-
}
102-
103-
if let Some(about) = self.profile.record().profile().and_then(|p| p.about()) {
104-
ui.label(about);
105-
}
67+
ui.add(ProfilePic::new(self.cache, get_profile_url(self.profile)).size(80.0));
68+
ui.add(display_name_widget(get_display_name(self.profile), false));
69+
ui.add(about_section_widget(self.profile));
10670
});
10771
}
10872
}
@@ -120,11 +84,38 @@ impl<'a, 'cache> egui::Widget for ProfilePreview<'a, 'cache> {
12084
}
12185
}
12286

87+
pub struct SimpleProfilePreview<'a, 'cache> {
88+
profile: &'a ProfileRecord<'a>,
89+
cache: &'cache mut ImageCache,
90+
}
91+
92+
impl<'a, 'cache> SimpleProfilePreview<'a, 'cache> {
93+
pub fn new(profile: &'a ProfileRecord<'a>, cache: &'cache mut ImageCache) -> Self {
94+
SimpleProfilePreview { profile, cache }
95+
}
96+
97+
pub fn dimensions(&self) -> Vec2 {
98+
Vec2::new(120.0, 150.0)
99+
}
100+
}
101+
102+
impl<'a, 'cache> egui::Widget for SimpleProfilePreview<'a, 'cache> {
103+
fn ui(self, ui: &mut egui::Ui) -> egui::Response {
104+
Frame::none()
105+
.show(ui, |ui| {
106+
ui.add(ProfilePic::new(self.cache, get_profile_url(self.profile)).size(48.0));
107+
ui.vertical(|ui| {
108+
ui.add(display_name_widget(get_display_name(self.profile), true));
109+
});
110+
})
111+
.response
112+
}
113+
}
114+
123115
mod previews {
124116
use super::*;
125117
use crate::test_data::test_profile_record;
126118
use crate::ui::{Preview, View};
127-
use egui::Widget;
128119

129120
pub struct ProfilePreviewPreview<'a> {
130121
profile: ProfileRecord<'a>,
@@ -160,3 +151,61 @@ mod previews {
160151
}
161152
}
162153
}
154+
155+
fn get_display_name<'a>(profile: &'a ProfileRecord<'a>) -> DisplayName<'a> {
156+
if let Some(name) = crate::profile::get_profile_name(profile) {
157+
name
158+
} else {
159+
DisplayName::One("??")
160+
}
161+
}
162+
163+
fn get_profile_url<'a>(profile: &'a ProfileRecord<'a>) -> &'a str {
164+
if let Some(url) = profile.record().profile().and_then(|p| p.picture()) {
165+
url
166+
} else {
167+
ProfilePic::no_pfp_url()
168+
}
169+
}
170+
171+
fn display_name_widget(
172+
display_name: DisplayName<'_>,
173+
add_placeholder_space: bool,
174+
) -> impl egui::Widget + '_ {
175+
move |ui: &mut egui::Ui| match display_name {
176+
DisplayName::One(n) => {
177+
let name_response =
178+
ui.label(RichText::new(n).text_style(NotedeckTextStyle::Heading3.text_style()));
179+
if add_placeholder_space {
180+
ui.add_space(16.0);
181+
}
182+
name_response
183+
}
184+
185+
DisplayName::Both {
186+
display_name,
187+
username,
188+
} => {
189+
ui.label(
190+
RichText::new(display_name).text_style(NotedeckTextStyle::Heading3.text_style()),
191+
);
192+
193+
ui.label(
194+
RichText::new(format!("@{}", username))
195+
.size(12.0)
196+
.color(colors::MID_GRAY),
197+
)
198+
}
199+
}
200+
}
201+
202+
fn about_section_widget<'a>(profile: &'a ProfileRecord<'a>) -> impl egui::Widget + 'a {
203+
|ui: &mut egui::Ui| {
204+
if let Some(about) = profile.record().profile().and_then(|p| p.about()) {
205+
ui.label(about)
206+
} else {
207+
// need any Response so we dont need an Option
208+
ui.allocate_response(egui::Vec2::ZERO, egui::Sense::hover())
209+
}
210+
}
211+
}

0 commit comments

Comments
 (0)