Compare commits

...

2 Commits

Author SHA1 Message Date
William Casarin df56cb9fcd Initial note block renderer
Still need mentions, soon! For now we at least color hashtags and links
2023-12-30 22:54:20 -08:00
William Casarin 26885f854b update to latest nostrdb 2023-12-30 22:54:20 -08:00
3 changed files with 124 additions and 36 deletions

40
Cargo.lock generated
View File

@ -189,6 +189,29 @@ dependencies = [
"which",
]
[[package]]
name = "bindgen"
version = "0.69.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ffcebc3849946a7170a05992aac39da343a90676ab392c51a4280981d6379c2"
dependencies = [
"bitflags 2.4.1",
"cexpr",
"clang-sys",
"lazy_static",
"lazycell",
"log",
"peeking_take_while",
"prettyplease",
"proc-macro2",
"quote",
"regex",
"rustc-hash",
"shlex",
"syn 2.0.41",
"which",
]
[[package]]
name = "bip39"
version = "2.0.0"
@ -1437,9 +1460,10 @@ dependencies = [
[[package]]
name = "nostrdb"
version = "0.1.5"
source = "git+https://github.com/damus-io/nostrdb-rs.git?rev=21d0002f5ff62e51c4114e3d15a2ffa4e99b17e8#21d0002f5ff62e51c4114e3d15a2ffa4e99b17e8"
version = "0.1.6"
source = "git+https://github.com/damus-io/nostrdb-rs.git?rev=71d7ce19d64e15f364d9908e5e34f0409395054c#71d7ce19d64e15f364d9908e5e34f0409395054c"
dependencies = [
"bindgen 0.69.1",
"cc",
"env_logger",
"flatbuffers",
@ -1637,6 +1661,16 @@ version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "prettyplease"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae005bd773ab59b4725093fd7df83fd7892f7d8eafb48dbd7de6e024e4215f9d"
dependencies = [
"proc-macro2",
"syn 2.0.41",
]
[[package]]
name = "proc-macro2"
version = "1.0.70"
@ -2061,7 +2095,7 @@ version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "404d98ffda633acd7a3ea68e0e5f160814ee7ae8942c89d60c53f7e0fae99373"
dependencies = [
"bindgen",
"bindgen 0.63.0",
"cc",
"flate2",
"heck",

View File

@ -13,8 +13,9 @@ hyper-util = { version = "0.1.1", features = ["full"] }
http-body-util = "0.1"
log = "0.4.20"
env_logger = "0.10.1"
nostrdb = { git = "https://github.com/damus-io/nostrdb-rs.git", rev = "21d0002f5ff62e51c4114e3d15a2ffa4e99b17e8" }
nostrdb = { git = "https://github.com/damus-io/nostrdb-rs.git", rev = "71d7ce19d64e15f364d9908e5e34f0409395054c" }
#nostrdb = { path = "/home/jb55/src/rust/nostrdb-rs" }
#nostrdb = "0.1.6"
#nostr-sdk = { git = "https://github.com/damus-io/nostr-sdk.git", rev = "fc0dc7b38f5060f171228b976b9700c0135245d3" }
nostr-sdk = "0.26.0"
hex = "0.4.3"

View File

@ -2,12 +2,15 @@ use crate::{fonts, Error, Notecrumbs};
use egui::emath::Rot2;
use egui::epaint::Shadow;
use egui::{
pos2, Color32, FontId, Mesh, Rect, RichText, Rounding, Shape, TextureHandle, Vec2, Visuals,
pos2,
text::{LayoutJob, TextFormat},
Color32, FontFamily, FontId, Mesh, Rect, RichText, Rounding, Shape, TextureHandle, Vec2,
Visuals,
};
use log::{debug, info, warn};
use nostr_sdk::nips::nip19::Nip19;
use nostr_sdk::prelude::*;
use nostrdb::{Note, Transaction};
use nostrdb::{BlockType, Blocks, Note, Transaction};
use std::f32::consts::PI;
impl ProfileRenderData {
@ -23,6 +26,7 @@ impl ProfileRenderData {
#[derive(Debug, Clone)]
pub struct NoteData {
pub id: Option<[u8; 32]>,
pub content: String,
}
@ -90,7 +94,7 @@ impl From<EventId> for EventSource {
impl NoteData {
fn default() -> Self {
let content = "".to_string();
NoteData { content }
NoteData { content, id: None }
}
}
@ -192,12 +196,16 @@ fn get_profile_render_data(
fn ndb_note_to_data(note: &Note) -> NoteData {
let content = note.content().to_string();
NoteData { content }
let id = Some(*note.id());
NoteData { content, id }
}
fn sdk_note_to_note_data(note: &Event) -> NoteData {
let content = note.content.clone();
NoteData { content }
NoteData {
content,
id: Some(note.id.to_bytes()),
}
}
fn get_note_render_data(
@ -290,7 +298,59 @@ fn setup_visuals(font_data: &egui::FontData, ctx: &egui::Context) {
fonts::setup_fonts(font_data, ctx);
}
fn wrapped_body(ui: &mut egui::Ui, text: &str) {
fn wrapped_body_blocks(ui: &mut egui::Ui, note: &Note, blocks: &Blocks) {
let size = 50.0;
let mut job = LayoutJob::default();
job.justify = false;
job.halign = egui::Align::LEFT;
job.wrap = egui::text::TextWrapping {
max_rows: 5,
break_anywhere: false,
overflow_character: Some('…'),
..Default::default()
};
let purple = Color32::from_rgb(0xcc, 0x43, 0xc5);
for block in blocks.iter(note) {
match block.blocktype() {
BlockType::Url => job.append(
block.as_str(),
0.0,
TextFormat {
font_id: FontId::new(size, FontFamily::Proportional),
color: purple,
..Default::default()
},
),
BlockType::Hashtag => job.append(
&format!("#{}", block.as_str()),
0.0,
TextFormat {
font_id: FontId::new(size, FontFamily::Proportional),
color: purple,
..Default::default()
},
),
_ => job.append(
block.as_str(),
0.0,
TextFormat {
font_id: FontId::new(size, FontFamily::Proportional),
color: Color32::WHITE,
..Default::default()
},
),
};
}
ui.label(job);
}
fn wrapped_body_text(ui: &mut egui::Ui, text: &str) {
use egui::text::{LayoutJob, TextFormat};
let format = TextFormat {
@ -303,15 +363,6 @@ fn wrapped_body(ui: &mut egui::Ui, text: &str) {
let mut job = LayoutJob::single_section(text.to_owned(), format);
job.justify = false;
job.halign = egui::Align::LEFT;
job.wrap = egui::text::TextWrapping {
max_rows: 4,
break_anywhere: false,
overflow_character: Some('…'),
..Default::default()
};
ui.label(job);
}
@ -354,14 +405,6 @@ fn note_ui(app: &Notecrumbs, ctx: &egui::Context, note: &NoteRenderData) {
let pfp = ctx.load_texture("pfp", note.profile.pfp.clone(), Default::default());
let bg = ctx.load_texture("background", app.background.clone(), Default::default());
/*
let desired_height = canvas_height - total_margin * 2.0;
let desired_width = canvas_width - total_margin * 2.0;
let desired_size = Vec2::new(desired_width, desired_height);
ui.set_min_size(desired_size);
ui.set_max_size(desired_size);
*/
egui::CentralPanel::default()
.frame(
egui::Frame::default()
@ -389,18 +432,28 @@ fn note_ui(app: &Notecrumbs, ctx: &egui::Context, note: &NoteRenderData) {
//egui::ScrollArea::vertical().show(ui, |ui| {
ui.spacing_mut().item_spacing = Vec2::new(10.0, 50.0);
ui.horizontal(|ui| {
ui.with_layout(right_aligned(), |ui| {
ui.label(RichText::new("damus.io").size(30.0));
});
});
ui.vertical(|ui| {
let desired = Vec2::new(desired_width, desired_height / 2.2);
let desired = Vec2::new(desired_width, desired_height / 1.5);
ui.set_max_size(desired);
ui.set_min_size(desired);
// only one widget is allowed in here
wrapped_body(ui, &note.note.content);
let mut rendered = false;
let ok = (|| -> Result<(), nostrdb::Error> {
let txn = Transaction::new(&app.ndb)?;
let note_id = note.note.id.ok_or(nostrdb::Error::NotFound)?;
let note = app.ndb.get_note_by_id(&txn, &note_id)?;
let blocks =
app.ndb.get_blocks_by_key(&txn, note.key().unwrap())?;
wrapped_body_blocks(ui, &note, &blocks);
Ok(())
})();
if let Err(_) = ok {
wrapped_body_text(ui, &note.note.content);
}
});
ui.horizontal(|ui| {