ui crate and chrome sidebar

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2025-03-26 12:18:44 -07:00
parent 415a052602
commit 9c9b4199f5
38 changed files with 902 additions and 750 deletions

View File

@@ -3,9 +3,11 @@ use core::f32;
use egui::{vec2, Button, CornerRadius, Layout, Margin, RichText, ScrollArea, TextEdit};
use notedeck::{Images, NotedeckTextStyle};
use crate::{colors, profile_state::ProfileState};
use crate::profile_state::ProfileState;
use super::{banner, unwrap_profile_url, ProfilePic};
use super::banner;
use notedeck_ui::{profile::unwrap_profile_url, ProfilePic};
pub struct EditProfileView<'a> {
state: &'a mut ProfileState,
@@ -34,7 +36,7 @@ impl<'a> EditProfileView<'a> {
crate::ui::padding(padding, ui, |ui| {
ui.with_layout(Layout::right_to_left(egui::Align::Center), |ui| {
if ui
.add(button("Save changes", 119.0).fill(colors::PINK))
.add(button("Save changes", 119.0).fill(notedeck_ui::colors::PINK))
.clicked()
{
save = true;

View File

@@ -1,5 +1,4 @@
pub mod edit;
pub mod picture;
pub mod preview;
pub use edit::EditProfileView;
@@ -7,13 +6,11 @@ use egui::load::TexturePoll;
use egui::{vec2, Color32, CornerRadius, Label, Layout, Rect, RichText, ScrollArea, Sense, Stroke};
use enostr::Pubkey;
use nostrdb::{ProfileRecord, Transaction};
pub use picture::ProfilePic;
pub use preview::ProfilePreview;
use tracing::error;
use crate::{
actionbar::NoteAction,
colors, images,
profile::get_display_name,
timeline::{TimelineCache, TimelineKind},
ui::timeline::{tabs_ui, TimelineTabView},
@@ -21,6 +18,7 @@ use crate::{
};
use notedeck::{Accounts, MuteFun, NotedeckTextStyle, UnknownIds};
use notedeck_ui::{images, profile::get_profile_url, ProfilePic};
use super::note::contents::NoteContext;
use super::note::NoteOptions;
@@ -215,7 +213,7 @@ fn handle_link(ui: &mut egui::Ui, website_url: &str) {
"../../../../../assets/icons/links_4x.png"
));
if ui
.label(RichText::new(website_url).color(colors::PINK))
.label(RichText::new(website_url).color(notedeck_ui::colors::PINK))
.on_hover_cursor(egui::CursorIcon::PointingHand)
.interact(Sense::click())
.clicked()
@@ -231,7 +229,7 @@ fn handle_lud16(ui: &mut egui::Ui, lud16: &str) {
"../../../../../assets/icons/zap_4x.png"
));
let _ = ui.label(RichText::new(lud16).color(colors::PINK));
let _ = ui.label(RichText::new(lud16).color(notedeck_ui::colors::PINK));
}
fn copy_key_widget(pfp_rect: &egui::Rect) -> impl egui::Widget + '_ {
@@ -360,7 +358,7 @@ fn display_name_widget(name: NostrName<'_>, add_placeholder_space: bool) -> impl
Label::new(
RichText::new(format!("@{}", username))
.size(16.0)
.color(colors::MID_GRAY),
.color(notedeck_ui::colors::MID_GRAY),
)
.selectable(false),
)
@@ -371,7 +369,9 @@ fn display_name_widget(name: NostrName<'_>, add_placeholder_space: bool) -> impl
"../../../../../assets/icons/verified_4x.png"
));
ui.add(Label::new(
RichText::new(nip05).size(16.0).color(colors::TEAL),
RichText::new(nip05)
.size(16.0)
.color(notedeck_ui::colors::TEAL),
))
});
@@ -396,18 +396,6 @@ fn display_name_widget(name: NostrName<'_>, add_placeholder_space: bool) -> impl
}
}
pub fn get_profile_url<'a>(profile: Option<&ProfileRecord<'a>>) -> &'a str {
unwrap_profile_url(profile.and_then(|pr| pr.record().profile().and_then(|p| p.picture())))
}
pub fn unwrap_profile_url(maybe_url: Option<&str>) -> &str {
if let Some(url) = maybe_url {
url
} else {
ProfilePic::no_pfp_url()
}
}
fn about_section_widget<'a, 'b>(profile: &'b ProfileRecord<'a>) -> impl egui::Widget + 'b
where
'b: 'a,

View File

@@ -1,265 +0,0 @@
use crate::gif::{handle_repaint, retrieve_latest_texture};
use crate::images::ImageType;
use crate::ui::images::render_images;
use crate::ui::{Preview, PreviewConfig};
use egui::{vec2, Sense, Stroke, TextureHandle};
use nostrdb::{Ndb, Transaction};
use tracing::info;
use notedeck::{supported_mime_hosted_at_url, AppContext, Images};
pub struct ProfilePic<'cache, 'url> {
cache: &'cache mut Images,
url: &'url str,
size: f32,
border: Option<Stroke>,
}
impl egui::Widget for ProfilePic<'_, '_> {
fn ui(self, ui: &mut egui::Ui) -> egui::Response {
render_pfp(ui, self.cache, self.url, self.size, self.border)
}
}
impl<'cache, 'url> ProfilePic<'cache, 'url> {
pub fn new(cache: &'cache mut Images, url: &'url str) -> Self {
let size = Self::default_size() as f32;
ProfilePic {
cache,
url,
size,
border: None,
}
}
pub fn border_stroke(ui: &egui::Ui) -> Stroke {
Stroke::new(4.0, ui.visuals().panel_fill)
}
pub fn from_profile(
cache: &'cache mut Images,
profile: &nostrdb::ProfileRecord<'url>,
) -> Option<Self> {
profile
.record()
.profile()
.and_then(|p| p.picture())
.map(|url| ProfilePic::new(cache, url))
}
#[inline]
pub fn default_size() -> i8 {
38
}
#[inline]
pub fn medium_size() -> i8 {
32
}
#[inline]
pub fn small_size() -> i8 {
24
}
#[inline]
pub fn no_pfp_url() -> &'static str {
"https://damus.io/img/no-profile.svg"
}
#[inline]
pub fn size(mut self, size: f32) -> Self {
self.size = size;
self
}
#[inline]
pub fn border(mut self, stroke: Stroke) -> Self {
self.border = Some(stroke);
self
}
}
#[profiling::function]
fn render_pfp(
ui: &mut egui::Ui,
img_cache: &mut Images,
url: &str,
ui_size: f32,
border: Option<Stroke>,
) -> egui::Response {
// We will want to downsample these so it's not blurry on hi res displays
let img_size = 128u32;
let cache_type = supported_mime_hosted_at_url(&mut img_cache.urls, url)
.unwrap_or(notedeck::MediaCacheType::Image);
render_images(
ui,
img_cache,
url,
ImageType::Profile(img_size),
cache_type,
|ui| {
paint_circle(ui, ui_size, border);
},
|ui, _| {
paint_circle(ui, ui_size, border);
},
|ui, url, renderable_media, gifs| {
let texture_handle =
handle_repaint(ui, retrieve_latest_texture(url, gifs, renderable_media));
pfp_image(ui, texture_handle, ui_size, border);
},
)
}
#[profiling::function]
fn pfp_image(
ui: &mut egui::Ui,
img: &TextureHandle,
size: f32,
border: Option<Stroke>,
) -> egui::Response {
let (rect, response) = ui.allocate_at_least(vec2(size, size), Sense::hover());
if let Some(stroke) = border {
draw_bg_border(ui, rect.center(), size, stroke);
}
ui.put(rect, egui::Image::new(img).max_width(size));
response
}
fn paint_circle(ui: &mut egui::Ui, size: f32, border: Option<Stroke>) -> egui::Response {
let (rect, response) = ui.allocate_at_least(vec2(size, size), Sense::hover());
if let Some(stroke) = border {
draw_bg_border(ui, rect.center(), size, stroke);
}
ui.painter()
.circle_filled(rect.center(), size / 2.0, ui.visuals().weak_text_color());
response
}
fn draw_bg_border(ui: &mut egui::Ui, center: egui::Pos2, size: f32, stroke: Stroke) {
let border_size = size + (stroke.width * 2.0);
ui.painter()
.circle_filled(center, border_size / 2.0, stroke.color);
}
mod preview {
use super::*;
use crate::ui;
use nostrdb::*;
use std::collections::HashSet;
pub struct ProfilePicPreview {
keys: Option<Vec<ProfileKey>>,
}
impl ProfilePicPreview {
fn new() -> Self {
ProfilePicPreview { keys: None }
}
fn show(&mut self, app: &mut AppContext<'_>, ui: &mut egui::Ui) {
egui::ScrollArea::both().show(ui, |ui| {
ui.horizontal_wrapped(|ui| {
let txn = Transaction::new(app.ndb).unwrap();
let keys = if let Some(keys) = &self.keys {
keys
} else {
return;
};
for key in keys {
let profile = app.ndb.get_profile_by_key(&txn, *key).unwrap();
let url = profile
.record()
.profile()
.expect("should have profile")
.picture()
.expect("should have picture");
let expand_size = 10.0;
let anim_speed = 0.05;
let (rect, size, _resp) = ui::anim::hover_expand(
ui,
egui::Id::new(profile.key().unwrap()),
ui::ProfilePic::default_size() as f32,
expand_size,
anim_speed,
);
ui.put(
rect,
ui::ProfilePic::new(app.img_cache, url)
.size(size)
.border(ui::ProfilePic::border_stroke(ui)),
)
.on_hover_ui_at_pointer(|ui| {
ui.set_max_width(300.0);
ui.add(ui::ProfilePreview::new(&profile, app.img_cache));
});
}
});
});
}
fn setup(&mut self, ndb: &Ndb) {
let txn = Transaction::new(ndb).unwrap();
let filters = vec![Filter::new().kinds(vec![0]).build()];
let mut pks = HashSet::new();
let mut keys = HashSet::new();
for query_result in ndb.query(&txn, &filters, 20000).unwrap() {
pks.insert(query_result.note.pubkey());
}
for pk in pks {
let profile = if let Ok(profile) = ndb.get_profile_by_pubkey(&txn, pk) {
profile
} else {
continue;
};
if profile
.record()
.profile()
.and_then(|p| p.picture())
.is_none()
{
continue;
}
keys.insert(profile.key().expect("should not be owned"));
}
let keys: Vec<ProfileKey> = keys.into_iter().collect();
info!("Loaded {} profiles", keys.len());
self.keys = Some(keys);
}
}
impl notedeck::App for ProfilePicPreview {
fn update(&mut self, ctx: &mut AppContext<'_>, ui: &mut egui::Ui) {
if self.keys.is_none() {
self.setup(ctx.ndb);
}
self.show(ctx, ui)
}
}
impl Preview for ProfilePic<'_, '_> {
type Prev = ProfilePicPreview;
fn preview(_cfg: PreviewConfig) -> Self::Prev {
ProfilePicPreview::new()
}
}
}

View File

@@ -4,9 +4,10 @@ use egui::{Frame, Label, RichText, Widget};
use egui_extras::Size;
use nostrdb::ProfileRecord;
use notedeck::{Images, NotedeckTextStyle, UserAccount};
use notedeck::{Images, NotedeckTextStyle};
use super::{about_section_widget, banner, display_name_widget, get_display_name, get_profile_url};
use super::{about_section_widget, banner, display_name_widget, get_display_name};
use notedeck_ui::profile::get_profile_url;
pub struct ProfilePreview<'a, 'cache> {
profile: &'a ProfileRecord<'a>,
@@ -152,30 +153,6 @@ mod previews {
}
}
pub fn get_profile_url_owned(profile: Option<ProfileRecord<'_>>) -> &str {
if let Some(url) = profile.and_then(|pr| pr.record().profile().and_then(|p| p.picture())) {
url
} else {
ProfilePic::no_pfp_url()
}
}
pub fn get_account_url<'a>(
txn: &'a nostrdb::Transaction,
ndb: &nostrdb::Ndb,
account: Option<&UserAccount>,
) -> &'a str {
if let Some(selected_account) = account {
if let Ok(profile) = ndb.get_profile_by_pubkey(txn, selected_account.key.pubkey.bytes()) {
get_profile_url_owned(Some(profile))
} else {
get_profile_url_owned(None)
}
} else {
get_profile_url(None)
}
}
pub fn one_line_display_name_widget<'a>(
visuals: &egui::Visuals,
display_name: NostrName<'a>,