refactor avatar component

This commit is contained in:
2026-09-29 13:18:20 +07:00
parent 8073c97c5a
commit 4d116036b9
8 changed files with 100 additions and 207 deletions
+15 -9
View File
@@ -11,6 +11,7 @@ use gpui::{
};
use nostr_sdk::prelude::*;
use person::PersonRegistry;
use settings::AppSettings;
use state::NostrRegistry;
use theme::ActiveTheme;
use ui::avatar::Avatar;
@@ -21,8 +22,6 @@ use ui::input::{Input, InputEvent, InputState};
use ui::nav::Nav;
use ui::{Disableable, IconName, Sizable, WindowExtension, h_flex, v_flex};
use crate::sidebar::nav_avatar;
/// Maximum number of members a chat can have.
const MAX_MEMBERS: usize = 3;
@@ -349,10 +348,14 @@ impl NewChat {
Nav::new(SharedString::from(format!("contact-{}", start + offset)))
.label(profile.name())
.text_sm()
.when_some(
nav_avatar(Some(profile.avatar_seed()), profile.avatar(), cx),
|this, avatar| this.prefix(avatar),
)
.when(!AppSettings::get_hide_avatar(cx), |this| {
this.prefix(
Avatar::from_source(profile.avatar())
.seed(profile.avatar_seed())
.small()
.flex_shrink_0(),
)
})
.on_click(cx.listener(move |this, _event, _window, cx| {
this.toggle_member(&public_key, cx);
}))
@@ -380,9 +383,12 @@ impl NewChat {
.bg(cx.theme().ghost_element_background_alt)
.text_size(px(10.))
.hover(|this| this.bg(cx.theme().ghost_element_hover))
.when_some(profile.avatar(), |this, avatar| {
this.child(Avatar::from_source(avatar).xsmall().flex_shrink_0())
})
.child(
Avatar::from_source(profile.avatar())
.seed(profile.avatar_seed())
.xsmall()
.flex_shrink_0(),
)
.child(div().max_w_32().truncate().child(profile.name()))
.on_click(cx.listener(move |this, _event, _window, cx| {
this.toggle_member(&public_key, cx);
+20 -10
View File
@@ -13,9 +13,11 @@ use gpui::{
use instant::Duration;
use nostr_sdk::prelude::*;
use person::PersonRegistry;
use settings::AppSettings;
use smallvec::{SmallVec, smallvec};
use state::{FIND_DELAY, NostrRegistry};
use theme::ActiveTheme;
use ui::avatar::Avatar;
use ui::button::{Button, ButtonVariants};
use ui::dock::{DockArea, DockPlacement, Panel, PanelEvent, PanelHandle};
use ui::input::{Input, InputEvent, InputState};
@@ -23,8 +25,6 @@ use ui::nav::Nav;
use ui::notification::Notification;
use ui::{Icon, IconName, Selectable, Sizable, StyledExt, WindowExtension, h_flex, v_flex};
use crate::sidebar::nav_avatar;
const INPUT_PLACEHOLDER: &str = "Find or start a conversation";
pub fn init(dock: WeakEntity<DockArea>, window: &mut Window, cx: &mut App) -> Entity<SearchPanel> {
@@ -337,6 +337,7 @@ impl SearchPanel {
range: Range<usize>,
cx: &Context<Self>,
) -> Vec<impl IntoElement + use<>> {
let hide_avatar = AppSettings::get_hide_avatar(cx);
let persons = PersonRegistry::global(cx);
// Get the results
@@ -365,10 +366,14 @@ impl SearchPanel {
.label(profile.name())
.text_sm()
.font_medium()
.when_some(
nav_avatar(Some(profile.avatar_seed()), profile.avatar(), cx),
|this, avatar| this.prefix(avatar),
)
.when(!hide_avatar, |this| {
this.prefix(
Avatar::from_source(profile.avatar())
.seed(profile.avatar_seed())
.small()
.flex_shrink_0(),
)
})
.on_click(handler)
.selected(selected)
.into_any_element()
@@ -382,6 +387,7 @@ impl SearchPanel {
range: Range<usize>,
cx: &Context<Self>,
) -> Vec<impl IntoElement + use<>> {
let hide_avatar = AppSettings::get_hide_avatar(cx);
let persons = PersonRegistry::global(cx);
// Get the contact list
@@ -410,10 +416,14 @@ impl SearchPanel {
.label(profile.name().trim())
.text_sm()
.font_medium()
.when_some(
nav_avatar(Some(profile.avatar_seed()), profile.avatar(), cx),
|this, avatar| this.prefix(avatar),
)
.when(!hide_avatar, |this| {
this.prefix(
Avatar::from_source(profile.avatar())
.seed(profile.avatar_seed())
.small()
.flex_shrink_0(),
)
})
.on_click(handler)
.selected(selected)
.into_any_element()
+34 -13
View File
@@ -44,7 +44,7 @@ mod tab;
mod utils;
use tab::SidebarTab;
pub(crate) use utils::{nav_avatar, nav_icon, pick_banner};
pub(crate) use utils::{nav_icon, pick_banner};
pub enum SidebarRow {
Room { room: Entity<Room> },
@@ -833,6 +833,8 @@ impl Sidebar {
rows: &[SidebarRow],
cx: &Context<Sidebar>,
) -> Vec<AnyElement> {
let hide_avatar = AppSettings::get_hide_avatar(cx);
rows.get(range.clone())
.into_iter()
.flatten()
@@ -855,8 +857,13 @@ impl Sidebar {
.label(name)
.text_sm()
.font_medium()
.when_some(nav_avatar(Some(seed), picture, cx), |this, avatar| {
this.prefix(avatar)
.when(!hide_avatar, |this| {
this.prefix(
Avatar::from_source(picture)
.seed(seed)
.small()
.flex_shrink_0(),
)
})
.suffix(
div()
@@ -904,10 +911,14 @@ impl Sidebar {
.label(person.name())
.text_sm()
.font_medium()
.when_some(
nav_avatar(Some(person.avatar_seed()), person.avatar(), cx),
|this, avatar| this.prefix(avatar),
)
.when(!hide_avatar, |this| {
this.prefix(
Avatar::from_source(person.avatar())
.seed(person.avatar_seed())
.small()
.flex_shrink_0(),
)
})
.selected(selected)
.on_click(move |_event, _window, cx| {
if let Err(error) = sidebar.update(cx, |this, cx| {
@@ -929,8 +940,13 @@ impl Sidebar {
Nav::new(SharedString::from(format!("com-{index}")))
.label(name)
.text_sm()
.when_some(nav_avatar(Some(seed), picture, cx), |this, avatar| {
this.prefix(avatar)
.when(!hide_avatar, |this| {
this.prefix(
Avatar::from_source(picture)
.seed(seed)
.small()
.flex_shrink_0(),
)
})
.on_click(move |_event, window, cx| {
ui::dock::add_panel_to(
@@ -1021,6 +1037,7 @@ impl Sidebar {
}
fn member_row(&self, public_key: &PublicKey, cx: &App) -> AnyElement {
let hide_avatar = AppSettings::get_hide_avatar(cx);
let persons = PersonRegistry::global(cx);
let person = persons.read(cx).get(public_key, cx);
@@ -1028,10 +1045,14 @@ impl Sidebar {
.label(person.name())
.text_sm()
.font_medium()
.when_some(
nav_avatar(Some(person.avatar_seed()), person.avatar(), cx),
|this, avatar| this.prefix(avatar),
)
.when(!hide_avatar, |this| {
this.prefix(
Avatar::from_source(person.avatar())
.seed(person.avatar_seed())
.small()
.flex_shrink_0(),
)
})
.into_any_element()
}
}
+1 -31
View File
@@ -1,39 +1,9 @@
use std::time::{SystemTime, UNIX_EPOCH};
use gpui::prelude::FluentBuilder as _;
use gpui::{AnyElement, App, ImageSource, IntoElement, ParentElement, SharedString, Styled, px};
use settings::AppSettings;
use gpui::{AnyElement, App, IntoElement, ParentElement, SharedString, Styled, px};
use theme::ActiveTheme;
use ui::avatar::{Avatar, PixelAvatar};
use ui::{Icon, IconName, Sizable, h_flex};
pub(crate) fn nav_avatar(
seed: Option<impl Into<SharedString>>,
picture: Option<impl Into<ImageSource>>,
cx: &App,
) -> Option<AnyElement> {
if AppSettings::get_hide_avatar(cx) {
return None;
}
match (seed.map(Into::into), picture.map(Into::into)) {
(None, None) => None,
(seed, Some(picture)) => Some(
Avatar::from_source(picture)
.when_some(seed, |avatar, seed| avatar.seed(seed))
.small()
.flex_shrink_0()
.into_any_element(),
),
(Some(seed), None) => Some(
PixelAvatar::new(seed)
.small()
.flex_shrink_0()
.into_any_element(),
),
}
}
pub(crate) fn nav_icon(icon: IconName, cx: &App) -> AnyElement {
h_flex()
.flex_shrink_0()