media: render images on posts

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2024-02-11 13:58:38 -08:00
parent 4ae3a701b8
commit 72a53c0192

View File

@@ -428,6 +428,8 @@ impl Damus {
cc.egui_ctx cc.egui_ctx
.set_pixels_per_point(cc.egui_ctx.pixels_per_point() + 0.2); .set_pixels_per_point(cc.egui_ctx.pixels_per_point() + 0.2);
egui_extras::install_image_loaders(&cc.egui_ctx);
let mut config = Config::new(); let mut config = Config::new();
config.set_ingester_threads(2); config.set_ingester_threads(2);
Self { Self {
@@ -605,6 +607,10 @@ fn render_note_contents(
puffin::profile_function!(); puffin::profile_function!();
let blocks = damus.ndb.get_blocks_by_key(txn, note_key)?; let blocks = damus.ndb.get_blocks_by_key(txn, note_key)?;
let mut images: Vec<String> = vec![];
ui.horizontal_wrapped(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
for block in blocks.iter(note) { for block in blocks.iter(note) {
match block.blocktype() { match block.blocktype() {
@@ -631,11 +637,16 @@ fn render_note_contents(
} }
BlockType::Url => { BlockType::Url => {
let url = block.as_str().to_lowercase();
if url.ends_with("png") || url.ends_with("jpg") {
images.push(url);
} else {
ui.add(Hyperlink::from_label_and_url( ui.add(Hyperlink::from_label_and_url(
RichText::new(block.as_str()).color(PURPLE), RichText::new(block.as_str()).color(PURPLE),
block.as_str(), block.as_str(),
)); ));
} }
}
BlockType::Text => { BlockType::Text => {
ui.weak(block.as_str()); ui.weak(block.as_str());
@@ -646,6 +657,11 @@ fn render_note_contents(
} }
} }
} }
});
for image in images {
ui.image(image);
}
Ok(()) Ok(())
} }
@@ -672,12 +688,9 @@ fn render_note(ui: &mut egui::Ui, damus: &mut Damus, note_key: NoteKey) -> Resul
ui.with_layout(egui::Layout::top_down(egui::Align::LEFT), |ui| { ui.with_layout(egui::Layout::top_down(egui::Align::LEFT), |ui| {
render_username(ui, profile.as_ref().ok(), note.pubkey()); render_username(ui, profile.as_ref().ok(), note.pubkey());
ui.horizontal_wrapped(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
if let Err(_err) = render_note_contents(ui, damus, &txn, &note, note_key) { if let Err(_err) = render_note_contents(ui, damus, &txn, &note, note_key) {
warn!("could not render note contents for note {:?}", note_key) warn!("could not render note contents for note {:?}", note_key)
} }
});
}) })
}); });
}); });