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 Jul 4, 2024
1 parent b5d5044 commit 86d4d21
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/timeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl TimelineView {
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(1000);

Expand Down Expand Up @@ -264,6 +265,7 @@ fn tabs_ui(timeline: &mut Timeline, ui: &mut egui::Ui) {
}

pub fn timeline_view(ui: &mut egui::Ui, app: &mut Damus, timeline: usize) {
//
//padding(4.0, ui, |ui| ui.heading("Notifications"));
/*
let font_id = egui::TextStyle::Body.resolve(ui.style());
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 @@ -109,7 +110,7 @@ fn render_note_contents(
#[cfg(feature = "profiling")]
puffin::profile_function!();

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 @@ -154,19 +155,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 @@ -186,15 +185,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 86d4d21

Please sign in to comment.