cosmic_applet_avestruz/views/components/
article.rs1use cosmic::Element;
20use cosmic::iced::widget::text::{Span, Wrapping};
21use cosmic::iced::widget::{rich_text, span};
22use cosmic::iced::{Background, Color, ContentFit, Length, border, mouse};
23use cosmic::widget::{self, container, text};
24
25use crate::fl;
26use crate::helpers::relative_time;
27use crate::images::{ImageShape, ImageState, STRIP_ASPECT};
28use crate::messages::Message;
29use crate::nostr::article::{Article, ArticleLink};
30use crate::nostr::models::{Note, display_name};
31use crate::nostr::outbox::Whereabouts;
32use crate::views::components::link_preview::{Page, link_preview_height, page_card};
33use crate::views::components::note_row::{NOTE_SPACING, RowContext, RowSizes, line_height};
34use crate::views::components::{CARD_RADIUS, MediaPlaceholder};
35
36const TITLE_SCALE: f32 = 1.5;
39
40const QUIET_ALPHA: f32 = 0.65;
43
44const SUMMARY_BAR_WIDTH: f32 = 3.0;
47const SUMMARY_BAR_ALPHA: f32 = 0.35;
48const SUMMARY_GAP: f32 = 10.0;
49
50const CHAR_WIDTH_PER_SIZE: f32 = 0.48;
53
54fn title(article: &Article) -> String {
56 if article.title.is_empty() { fl!("article-untitled") } else { article.title.clone() }
57}
58
59pub fn article_card_height(sizes: RowSizes) -> f32 {
62 link_preview_height(sizes)
63}
64
65pub fn article_card<'a>(note: &'a Note, article: &'a Article, pressed: bool, context: &RowContext<'a>) -> Element<'a, Message> {
70 let over = if pressed {
73 let name = display_name(context.profiles, ¬e.author);
74 fl!("article-card-by", name = name.as_ref(), minutes = article.minutes)
75 } else {
76 fl!("article-card", minutes = article.minutes)
77 };
78 let card = page_card(
79 Page {
80 over: over.into(),
81 title: if article.title.is_empty() { fl!("article-untitled").into() } else { article.title.as_str().into() },
82 description: article.summary.as_deref(),
83 image: article.image.as_deref(),
84 },
85 context,
86 );
87 if !pressed || context.under_a_menu() {
88 return card;
89 }
90 widget::mouse_area(card)
91 .on_press(Message::OpenThread(note.id, Whereabouts::default()))
92 .interaction(mouse::Interaction::Pointer)
93 .into()
94}
95
96pub fn article_link<'a>(link: &'a ArticleLink, context: &RowContext<'a>) -> Element<'a, Message> {
99 let spans: Vec<Span<'a, ArticleLink>> = vec![span(link.label.as_str()).color(context.accent).link(link.clone())];
100 rich_text(spans)
101 .size(context.sizes.text())
102 .wrapping(Wrapping::WordOrGlyph)
103 .width(Length::Fill)
104 .on_link_click(Message::OpenArticle)
105 .into()
106}
107
108fn title_size(sizes: RowSizes) -> f32 {
110 (f32::from(sizes.text()) * TITLE_SCALE).round()
111}
112
113fn lines(chars: usize, size: f32, width: f32) -> f32 {
116 let per_line = ((width / (size * CHAR_WIDTH_PER_SIZE)) as usize).max(1);
117 chars.div_ceil(per_line).max(1) as f32
118}
119
120pub fn article_head_height(article: &Article, sizes: RowSizes, width: f32) -> f32 {
124 let spacing = f32::from(NOTE_SPACING);
125 let size = title_size(sizes);
126 let heading = lines(title(article).chars().count(), size, width) * size * 1.4;
127 let small = spacing + line_height(sizes.time());
128 let summary = article.summary.as_ref().map_or(0.0, |summary| {
129 let room = (width - SUMMARY_BAR_WIDTH - SUMMARY_GAP).max(1.0);
130 let wrapped: f32 = summary.split('\n').map(|line| lines(line.chars().count(), f32::from(sizes.text()), room)).sum();
131 spacing + wrapped * line_height(sizes.text())
132 });
133 let picture = article.image.as_ref().map_or(0.0, |_| spacing + width / STRIP_ASPECT as f32);
134 heading + small + summary + picture
135}
136
137pub fn article_head<'a>(note: &'a Note, article: &'a Article, context: &RowContext<'a>) -> Element<'a, Message> {
141 let RowContext { images, now, sizes, .. } = *context;
142 let on_background: Color = cosmic::theme::active().cosmic().on_bg_color().into();
143 let quiet = Color { a: QUIET_ALPHA, ..on_background };
144
145 let heading = text(title(article))
146 .size(title_size(sizes))
147 .font(cosmic::font::bold())
148 .wrapping(Wrapping::WordOrGlyph)
149 .width(Length::Fill);
150 let when = relative_time(now, note.created_at);
151 let small = text(fl!("article-read", minutes = article.minutes, when = when.as_str()))
152 .size(sizes.time())
153 .wrapping(Wrapping::None)
154 .class(cosmic::theme::Text::Color(quiet));
155 let small = container(small).width(Length::Fill).height(line_height(sizes.time())).clip(true);
156
157 let summary = article.summary.as_deref().map(|summary| {
158 let bar = container(widget::Space::new()).width(Length::Fixed(SUMMARY_BAR_WIDTH)).height(Length::Fill).class(
159 cosmic::theme::Container::custom(move |_| container::Style {
160 background: Some(Background::Color(Color { a: SUMMARY_BAR_ALPHA, ..on_background })),
161 border: border::rounded(SUMMARY_BAR_WIDTH / 2.0),
162 ..container::Style::default()
163 }),
164 );
165 let said = text(summary)
166 .size(sizes.text())
167 .wrapping(Wrapping::WordOrGlyph)
168 .width(Length::Fill)
169 .class(cosmic::theme::Text::Color(quiet));
170 widget::Row::new().push(bar).push(said).spacing(SUMMARY_GAP).height(Length::Shrink)
173 });
174
175 let picture: Option<Element<'a, Message>> =
178 article.image.as_deref().and_then(|image| match images.get(image, ImageShape::Strip) {
179 ImageState::Ready(loaded) => Some(
180 widget::image(loaded.handle.clone())
181 .width(Length::Fill)
182 .height(Length::Shrink)
183 .content_fit(ContentFit::Contain)
184 .border_radius([CARD_RADIUS; 4])
185 .into(),
186 ),
187 ImageState::Loading => {
188 Some(MediaPlaceholder::new(Some((STRIP_ASPECT * 1_000, 1_000)), 0.0).radius(CARD_RADIUS).into())
189 }
190 ImageState::Failed => None,
191 });
192
193 widget::Column::new()
194 .push(heading)
195 .push(small)
196 .push_maybe(summary)
197 .push_maybe(picture)
198 .spacing(NOTE_SPACING)
199 .width(Length::Fill)
200 .into()
201}
202
203pub fn article_topics_height(article: &Article, sizes: RowSizes) -> f32 {
205 if article.topics.is_empty() { 0.0 } else { line_height(sizes.time()) }
206}
207
208pub fn article_topics<'a>(article: &Article, context: &RowContext<'a>) -> Option<Element<'a, Message>> {
211 if article.topics.is_empty() {
212 return None;
213 }
214 let small = context.sizes.time();
215 let topics = article.topics.iter().map(|topic| format!("#{topic}")).collect::<Vec<_>>().join(" ");
216 let line = text(topics).size(small).wrapping(Wrapping::None).class(cosmic::theme::Text::Color(context.accent));
217 Some(container(line).width(Length::Fill).height(line_height(small)).clip(true).into())
218}