Skip to content

Commit

Permalink
initial image support
Browse files Browse the repository at this point in the history
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Aug 31, 2024
1 parent f9a7d93 commit 407c38f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/timeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ impl TimelineTab {
let selection = 0i32;
let mut list = VirtualList::new();
list.hide_on_resize(None);
list.over_scan(1000.0);
let list = Rc::new(RefCell::new(list));
let notes: Vec<NoteRef> = Vec::with_capacity(cap);

Expand Down Expand Up @@ -327,7 +328,6 @@ impl Timeline {
}
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum MergeKind {
FrontInsert,
Spliced,
Expand Down
69 changes: 48 additions & 21 deletions src/ui/note/contents.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::ui::note::NoteOptions;
use crate::{colors, ui, Damus};
use egui::scroll_area::ScrollBarVisibility;
use egui::{Color32, Hyperlink, Image, RichText};
use nostrdb::{BlockType, Mention, Note, NoteKey, Transaction};
use tracing::warn;
Expand Down Expand Up @@ -111,7 +112,7 @@ fn render_note_contents(
puffin::profile_function!();

let selectable = options.has_selectable_text();
let images: Vec<String> = vec![];
let mut images: Vec<String> = vec![];
let mut inline_note: Option<(&[u8; 32], &str)> = None;

let resp = ui.horizontal_wrapped(|ui| {
Expand Down Expand Up @@ -156,19 +157,17 @@ fn render_note_contents(
}

BlockType::Url => {
/*
let url = block.as_str().to_lowercase();
if url.ends_with("png") || url.ends_with("jpg") {
images.push(url);
let lower_url = block.as_str().to_lowercase();
if lower_url.ends_with("png") || lower_url.ends_with("jpg") {
images.push(block.as_str().to_string());
} else {
*/
#[cfg(feature = "profiling")]
puffin::profile_scope!("url contents");
ui.add(Hyperlink::from_label_and_url(
RichText::new(block.as_str()).color(colors::PURPLE),
block.as_str(),
));
//}
#[cfg(feature = "profiling")]
puffin::profile_scope!("url contents");
ui.add(Hyperlink::from_label_and_url(
RichText::new(block.as_str()).color(colors::PURPLE),
block.as_str(),
));
}
}

BlockType::Text => {
Expand All @@ -188,15 +187,43 @@ fn render_note_contents(
render_note_preview(ui, damus, txn, id, block_str);
}

for image in images {
let img_resp = ui.add(Image::new(image.clone()));
img_resp.context_menu(|ui| {
if ui.button("Copy Link").clicked() {
ui.ctx().copy_text(image);
ui.close_menu();
}
});
if !images.is_empty() && !damus.textmode {
ui.add_space(2.0);
image_carousel(ui, images);
ui.add_space(2.0);
}

resp
}

fn image_carousel(ui: &mut egui::Ui, images: Vec<String>) {
// let's make sure everything is within our area

let height = 360.0;
let width = ui.available_size().x;

ui.add_sized([width, height], |ui: &mut egui::Ui| {
egui::ScrollArea::horizontal()
.scroll_bar_visibility(ScrollBarVisibility::AlwaysVisible)
.show(ui, |ui| {
ui.horizontal(|ui| {
for image in images {
let img_resp = ui.add(
Image::new(image.clone())
.max_height(height)
.rounding(5.0)
.fit_to_original_size(1.0),
);
img_resp.context_menu(|ui| {
if ui.button("Copy Link").clicked() {
ui.ctx().copy_text(image);
ui.close_menu();
}
});
}
})
.response
})
.inner
});
}

0 comments on commit 407c38f

Please sign in to comment.