cosmic_applet_avestruz/views/
profile.rs1use cosmic::Element;
10use cosmic::iced::widget::text::Wrapping;
11use cosmic::iced::{Alignment, ContentFit, Length};
12use cosmic::widget::{self, button, container, text};
13
14use crate::fl;
15use crate::images::{ImageShape, ImageState, ImageStore, LARGEST_AVATAR, STRIP_ASPECT};
16use crate::messages::Message;
17use crate::nostr::models::{display_name, short_npub};
18use crate::nostr::nip05;
19use crate::state::{AppModel, FollowProblem, Nip05State, ProfileState};
20use crate::views::components::{MediaPlaceholder, POPUP_MAX_WIDTH, PlaceInScroller, avatar, link_line, list_scroller, zap_icon};
21
22const PROFILE_AVATAR_SIZE: f32 = LARGEST_AVATAR;
25
26const PROFILE_BANNER_RADIUS: f32 = 6.0;
30const PROFILE_BANNER_MAX_HEIGHT: f32 = POPUP_MAX_WIDTH / STRIP_ASPECT as f32;
31
32const PROFILE_NAME_SIZE: u16 = 18;
35const PROFILE_TEXT_SIZE: u16 = 13;
36const PROFILE_SMALL_SIZE: u16 = 11;
37
38fn banner<'a>(images: &'a ImageStore, url: &str) -> Option<Element<'a, Message>> {
42 match images.get(url, ImageShape::Strip) {
43 ImageState::Ready(loaded) => Some(
44 cosmic::widget::image(loaded.handle.clone())
45 .width(Length::Fill)
46 .height(Length::Shrink)
47 .content_fit(ContentFit::Contain)
48 .border_radius([PROFILE_BANNER_RADIUS; 4])
49 .into(),
50 ),
51 ImageState::Loading => Some(
53 MediaPlaceholder::new(Some((STRIP_ASPECT * 1_000, 1_000)), PROFILE_BANNER_MAX_HEIGHT)
54 .radius(PROFILE_BANNER_RADIUS)
55 .into(),
56 ),
57 ImageState::Failed => None,
58 }
59}
60
61fn small_print<'a>(message: String) -> Element<'a, Message> {
63 text(message).size(PROFILE_SMALL_SIZE).wrapping(Wrapping::WordOrGlyph).width(Length::Fill).into()
64}
65
66impl AppModel {
67 fn view_profile_header(&self) -> Element<'_, Message> {
69 let back = button::icon(widget::icon::from_name("go-previous-symbolic"))
70 .tooltip(fl!("profile-back"))
71 .on_press(Message::ClosePage);
72 widget::Row::new()
73 .push(back)
74 .push(text(fl!("profile-title")).size(16).font(cosmic::font::bold()))
75 .spacing(8)
76 .align_y(Alignment::Center)
77 .padding([4, 12])
78 .into()
79 }
80
81 fn view_profile_key(&self, state: &ProfileState) -> Element<'_, Message> {
84 let copy = button::icon(widget::icon::from_name("edit-copy-symbolic").size(12))
85 .tooltip(if state.npub_copied { fl!("profile-npub-copied") } else { fl!("profile-copy-npub") })
86 .on_press(Message::CopyNpub(state.public_key))
87 .padding(2);
88 widget::Row::new()
89 .push(text(short_npub(&state.public_key)).size(PROFILE_SMALL_SIZE).wrapping(Wrapping::None))
90 .push(copy)
91 .spacing(4)
92 .align_y(Alignment::Center)
93 .into()
94 }
95
96 fn view_profile_nip05(&self, state: &ProfileState) -> Option<Element<'_, Message>> {
99 let check = self.nip05.get(&state.public_key).filter(|check| check.state == Nip05State::Verified)?;
100 let verified = widget::Row::new()
101 .push(widget::icon::from_name("emblem-ok-symbolic").size(PROFILE_SMALL_SIZE + 1))
102 .push(
103 text(nip05::shown(&check.name))
104 .size(PROFILE_SMALL_SIZE)
105 .class(cosmic::theme::Text::Accent)
106 .wrapping(Wrapping::None),
107 )
108 .spacing(4)
109 .align_y(Alignment::Center);
110 Some(widget::tooltip(verified, text(fl!("profile-verified")), widget::tooltip::Position::Bottom).into())
111 }
112
113 fn view_follow_button(&self, state: &ProfileState) -> Option<Element<'_, Message>> {
117 self.account.as_ref().filter(|account| account.can_sign && account.public_key != state.public_key)?;
118 let following = self.follows.as_ref()?.contains(&state.public_key);
119 let first_list = state.follow_problem == Some(FollowProblem::NoList);
122 let follow = Message::Follow { person: state.public_key, follow: !following, first_list };
123 let button = match (following, first_list) {
124 (true, _) => button::standard(fl!("profile-unfollow")),
125 (false, true) => button::suggested(fl!("profile-follow-anyway")),
126 (false, false) => button::suggested(fl!("profile-follow")),
127 };
128 Some(button.on_press_maybe(self.follow_pending.is_none().then_some(follow)).into())
129 }
130
131 fn view_profile_person<'a>(&'a self, state: &'a ProfileState) -> Element<'a, Message> {
136 let profile = self.profiles.get(&state.public_key);
137 let name = display_name(&self.profiles, &state.public_key);
138 let picture = profile.and_then(|profile| profile.picture.as_deref());
139 let picture = avatar(&self.images, picture, &state.public_key, &name, PROFILE_AVATAR_SIZE);
140
141 let mut who = widget::Column::new()
143 .push(
144 container(text(name.clone()).size(PROFILE_NAME_SIZE).font(cosmic::font::bold()).wrapping(Wrapping::None))
145 .clip(true),
146 )
147 .push(self.view_profile_key(state))
148 .spacing(2)
149 .width(Length::Fill);
150 if let Some(verified) = self.view_profile_nip05(state) {
151 who = who.push(verified);
152 }
153 let mut identity = widget::Row::new().push(picture).push(who).spacing(12).align_y(Alignment::Center);
154 if let Some(follow) = self.view_follow_button(state) {
155 identity = identity.push(follow);
156 }
157
158 let mut person = widget::Column::new().spacing(8).padding([12, 0]).width(Length::Fill);
159 if let Some(banner) = profile.and_then(|profile| profile.banner.as_deref()).and_then(|url| banner(&self.images, url)) {
160 person = person.push(banner);
161 }
162 person = person.push(identity);
163 match &state.follow_problem {
164 Some(FollowProblem::NoList) => person = person.push(small_print(fl!("profile-follow-no-list", name = name.as_ref()))),
165 Some(FollowProblem::Failed(error)) => {
166 person = person.push(
167 text(error.as_str())
168 .size(PROFILE_SMALL_SIZE + 1)
169 .class(cosmic::theme::Text::Accent)
170 .wrapping(Wrapping::WordOrGlyph),
171 );
172 }
173 None => {}
174 }
175
176 if let Some(about) = profile.and_then(|profile| profile.about.as_deref()) {
177 person = person.push(text(about).size(PROFILE_TEXT_SIZE).wrapping(Wrapping::WordOrGlyph).width(Length::Fill));
178 }
179 if let Some(website) = profile.and_then(|profile| profile.website.as_ref()) {
180 let accent = cosmic::theme::active().cosmic().accent_text_color().into();
181 person = person.push(link_line(&website.url, &website.label, accent, PROFILE_TEXT_SIZE));
182 }
183 if let Some(lightning) = profile.and_then(|profile| profile.lightning.as_deref()) {
184 let address = widget::Row::new()
185 .push(widget::icon::icon(zap_icon()).size(PROFILE_SMALL_SIZE + 1))
186 .push(text(lightning).size(PROFILE_SMALL_SIZE).wrapping(Wrapping::WordOrGlyph))
187 .spacing(4)
188 .align_y(Alignment::Center);
189 person = person.push(address);
190 }
191 person.into()
192 }
193
194 pub fn view_profile<'a>(&'a self, state: &'a ProfileState) -> Element<'a, Message> {
196 let nothing = if state.notes.refreshing { fl!("profile-loading") } else { fl!("profile-no-notes") };
197 let modes = container(self.view_feed_modes()).width(Length::Fill).align_x(Alignment::Center).padding([8, 0]);
200 let content = widget::Column::new()
201 .push(self.view_profile_person(state))
202 .push(widget::divider::horizontal::default())
203 .push(modes)
204 .push(widget::divider::horizontal::default())
205 .push(PlaceInScroller::new(&state.notes.position, &state.notes_top))
208 .push(self.view_notes(&state.notes, nothing));
209
210 widget::Column::new()
211 .push(self.view_profile_header())
212 .push(widget::divider::horizontal::default())
213 .push(self.with_new_notes(list_scroller(&state.notes.position, content), &state.notes))
214 .into()
215 }
216}