Compare commits

...

4 Commits

Author SHA1 Message Date
William Casarin e688f74e49 update nostrdb 2024-01-01 09:42:53 -08:00
William Casarin 0e7b8d7c98 render mentions in previews 2024-01-01 09:06:02 -08:00
William Casarin 83e63394e8 cleanup some warnings 2024-01-01 09:05:50 -08:00
William Casarin 1c1835d56b update nostrdb 2024-01-01 09:05:24 -08:00
4 changed files with 123 additions and 49 deletions

2
Cargo.lock generated
View File

@ -1461,7 +1461,7 @@ dependencies = [
[[package]]
name = "nostrdb"
version = "0.1.6"
source = "git+https://github.com/damus-io/nostrdb-rs.git?rev=71d7ce19d64e15f364d9908e5e34f0409395054c#71d7ce19d64e15f364d9908e5e34f0409395054c"
source = "git+https://github.com/damus-io/nostrdb-rs.git?rev=351906121f79d71b2a5a999944a8a22c6b8dde4e#351906121f79d71b2a5a999944a8a22c6b8dde4e"
dependencies = [
"bindgen 0.69.1",
"cc",

View File

@ -13,7 +13,7 @@ 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 = "71d7ce19d64e15f364d9908e5e34f0409395054c" }
nostrdb = { git = "https://github.com/damus-io/nostrdb-rs.git", rev = "351906121f79d71b2a5a999944a8a22c6b8dde4e" }
#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" }

View File

@ -271,13 +271,14 @@ fn get_env_timeout() -> Duration {
}
fn get_gradient() -> egui::ColorImage {
use egui::{pos2, Color32, ColorImage};
use egui::{Color32, ColorImage};
//use egui::pos2;
use gradient::Gradient;
//let gradient = Gradient::linear(Color32::LIGHT_GRAY, Color32::DARK_GRAY);
let size = pfp::PFP_SIZE as usize;
let radius = (pfp::PFP_SIZE as f32) / 2.0;
let center = pos2(radius, radius);
//let size = pfp::PFP_SIZE as usize;
//let radius = (pfp::PFP_SIZE as f32) / 2.0;
//let center = pos2(radius, radius);
let scol = [0x1C, 0x55, 0xFF];
//let ecol = [0xFA, 0x0D, 0xD4];

View File

@ -1,5 +1,4 @@
use crate::{fonts, Error, Notecrumbs};
use egui::emath::Rot2;
use egui::epaint::Shadow;
use egui::{
pos2,
@ -9,9 +8,13 @@ use egui::{
};
use log::{debug, info, warn};
use nostr_sdk::nips::nip19::Nip19;
use nostr_sdk::prelude::*;
use nostrdb::{BlockType, Blocks, Note, Transaction};
use std::f32::consts::PI;
use nostr_sdk::prelude::{json, Event, EventId, Nip19Event, XOnlyPublicKey};
use nostrdb::{Block, BlockType, Blocks, Mention, Ndb, Note, Transaction};
const PURPLE: Color32 = Color32::from_rgb(0xcc, 0x43, 0xc5);
//use egui::emath::Rot2;
//use std::f32::consts::PI;
impl ProfileRenderData {
pub fn default(pfp: egui::ImageData) -> Self {
@ -298,9 +301,77 @@ fn setup_visuals(font_data: &egui::FontData, ctx: &egui::Context) {
fonts::setup_fonts(font_data, ctx);
}
fn wrapped_body_blocks(ui: &mut egui::Ui, note: &Note, blocks: &Blocks) {
let size = 50.0;
fn push_job_text(job: &mut LayoutJob, s: &str, color: Color32) {
job.append(
s,
0.0,
TextFormat {
font_id: FontId::new(50.0, FontFamily::Proportional),
color,
..Default::default()
},
)
}
#[inline]
pub fn floor_char_boundary(s: &str, index: usize) -> usize {
if index >= s.len() {
s.len()
} else {
let lower_bound = index.saturating_sub(3);
let new_index = s.as_bytes()[lower_bound..=index]
.iter()
.rposition(|b| is_utf8_char_boundary(*b));
// SAFETY: we know that the character boundary will be within four bytes
unsafe { lower_bound + new_index.unwrap_unchecked() }
}
}
#[inline]
fn is_utf8_char_boundary(c: u8) -> bool {
// This is bit magic equivalent to: b < 128 || b >= 192
(c as i8) >= -0x40
}
const ABBREV_SIZE: usize = 10;
fn abbrev_str(name: &str) -> String {
if name.len() > ABBREV_SIZE {
let closest = floor_char_boundary(name, ABBREV_SIZE);
format!("{}...", &name[..closest])
} else {
name.to_owned()
}
}
fn push_job_user_mention(
job: &mut LayoutJob,
ndb: &Ndb,
block: &Block,
txn: &Transaction,
pk: &[u8; 32],
) {
let record = ndb.get_profile_by_pubkey(&txn, pk);
if let Ok(record) = record {
let profile = record.record.profile().unwrap();
push_job_text(
job,
&format!("@{}", &abbrev_str(profile.name().unwrap_or("nostrich"))),
PURPLE,
);
} else {
push_job_text(job, &format!("@{}", &abbrev_str(block.as_str())), PURPLE);
}
}
fn wrapped_body_blocks(
ui: &mut egui::Ui,
ndb: &Ndb,
note: &Note,
blocks: &Blocks,
txn: &Transaction,
) {
let mut job = LayoutJob::default();
job.justify = false;
job.halign = egui::Align::LEFT;
@ -311,39 +382,46 @@ fn wrapped_body_blocks(ui: &mut egui::Ui, note: &Note, blocks: &Blocks) {
..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::Url => push_job_text(&mut job, block.as_str(), PURPLE),
BlockType::Hashtag => job.append(
&format!("#{}", block.as_str()),
0.0,
TextFormat {
font_id: FontId::new(size, FontFamily::Proportional),
color: purple,
..Default::default()
},
),
BlockType::Hashtag => {
push_job_text(&mut job, "#", PURPLE);
push_job_text(&mut job, block.as_str(), PURPLE);
}
_ => job.append(
block.as_str(),
0.0,
TextFormat {
font_id: FontId::new(size, FontFamily::Proportional),
color: Color32::WHITE,
..Default::default()
},
),
BlockType::MentionBech32 => {
let pk = match block.as_mention().unwrap() {
Mention::Event(ev) => push_job_text(
&mut job,
&format!("@{}", &abbrev_str(block.as_str())),
PURPLE,
),
Mention::Note(ev) => {
push_job_text(
&mut job,
&format!("@{}", &abbrev_str(block.as_str())),
PURPLE,
);
}
Mention::Profile(nprofile) => {
push_job_user_mention(&mut job, ndb, &block, &txn, nprofile.pubkey())
}
Mention::Pubkey(npub) => {
push_job_user_mention(&mut job, ndb, &block, &txn, npub.pubkey())
}
Mention::Secret(sec) => push_job_text(&mut job, "--redacted--", PURPLE),
Mention::Relay(relay) => {
push_job_text(&mut job, &abbrev_str(block.as_str()), PURPLE)
}
Mention::Addr(addr) => {
push_job_text(&mut job, &abbrev_str(block.as_str()), PURPLE)
}
};
}
_ => push_job_text(&mut job, block.as_str(), Color32::WHITE),
};
}
@ -351,8 +429,6 @@ fn wrapped_body_blocks(ui: &mut egui::Ui, note: &Note, blocks: &Blocks) {
}
fn wrapped_body_text(ui: &mut egui::Ui, text: &str) {
use egui::text::{LayoutJob, TextFormat};
let format = TextFormat {
font_id: FontId::proportional(52.0),
color: Color32::WHITE,
@ -361,8 +437,7 @@ fn wrapped_body_text(ui: &mut egui::Ui, text: &str) {
..Default::default()
};
let mut job = LayoutJob::single_section(text.to_owned(), format);
let job = LayoutJob::single_section(text.to_owned(), format);
ui.label(job);
}
@ -437,8 +512,6 @@ fn note_ui(app: &Notecrumbs, ctx: &egui::Context, note: &NoteRenderData) {
ui.set_max_size(desired);
ui.set_min_size(desired);
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)?;
@ -446,7 +519,7 @@ fn note_ui(app: &Notecrumbs, ctx: &egui::Context, note: &NoteRenderData) {
let blocks =
app.ndb.get_blocks_by_key(&txn, note.key().unwrap())?;
wrapped_body_blocks(ui, &note, &blocks);
wrapped_body_blocks(ui, &app.ndb, &note, &blocks, &txn);
Ok(())
})();
@ -481,7 +554,7 @@ fn background_texture(ui: &mut egui::Ui, texture: &TextureHandle) {
// Get the painter and draw the texture
let painter = ui.ctx().layer_painter(layer_id);
let tint = Color32::WHITE;
//let tint = Color32::WHITE;
let mut mesh = Mesh::with_texture(texture.into());