mirror of
https://github.com/damus-io/notecrumbs.git
synced 2024-09-29 18:10:43 +00:00
Compare commits
4 Commits
df56cb9fcd
...
e688f74e49
Author | SHA1 | Date | |
---|---|---|---|
|
e688f74e49 | ||
|
0e7b8d7c98 | ||
|
83e63394e8 | ||
|
1c1835d56b |
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -1461,7 +1461,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "nostrdb"
|
name = "nostrdb"
|
||||||
version = "0.1.6"
|
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 = [
|
dependencies = [
|
||||||
"bindgen 0.69.1",
|
"bindgen 0.69.1",
|
||||||
"cc",
|
"cc",
|
||||||
|
@ -13,7 +13,7 @@ hyper-util = { version = "0.1.1", features = ["full"] }
|
|||||||
http-body-util = "0.1"
|
http-body-util = "0.1"
|
||||||
log = "0.4.20"
|
log = "0.4.20"
|
||||||
env_logger = "0.10.1"
|
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 = { path = "/home/jb55/src/rust/nostrdb-rs" }
|
||||||
#nostrdb = "0.1.6"
|
#nostrdb = "0.1.6"
|
||||||
#nostr-sdk = { git = "https://github.com/damus-io/nostr-sdk.git", rev = "fc0dc7b38f5060f171228b976b9700c0135245d3" }
|
#nostr-sdk = { git = "https://github.com/damus-io/nostr-sdk.git", rev = "fc0dc7b38f5060f171228b976b9700c0135245d3" }
|
||||||
|
@ -271,13 +271,14 @@ fn get_env_timeout() -> Duration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_gradient() -> egui::ColorImage {
|
fn get_gradient() -> egui::ColorImage {
|
||||||
use egui::{pos2, Color32, ColorImage};
|
use egui::{Color32, ColorImage};
|
||||||
|
//use egui::pos2;
|
||||||
use gradient::Gradient;
|
use gradient::Gradient;
|
||||||
|
|
||||||
//let gradient = Gradient::linear(Color32::LIGHT_GRAY, Color32::DARK_GRAY);
|
//let gradient = Gradient::linear(Color32::LIGHT_GRAY, Color32::DARK_GRAY);
|
||||||
let size = pfp::PFP_SIZE as usize;
|
//let size = pfp::PFP_SIZE as usize;
|
||||||
let radius = (pfp::PFP_SIZE as f32) / 2.0;
|
//let radius = (pfp::PFP_SIZE as f32) / 2.0;
|
||||||
let center = pos2(radius, radius);
|
//let center = pos2(radius, radius);
|
||||||
|
|
||||||
let scol = [0x1C, 0x55, 0xFF];
|
let scol = [0x1C, 0x55, 0xFF];
|
||||||
//let ecol = [0xFA, 0x0D, 0xD4];
|
//let ecol = [0xFA, 0x0D, 0xD4];
|
||||||
|
157
src/render.rs
157
src/render.rs
@ -1,5 +1,4 @@
|
|||||||
use crate::{fonts, Error, Notecrumbs};
|
use crate::{fonts, Error, Notecrumbs};
|
||||||
use egui::emath::Rot2;
|
|
||||||
use egui::epaint::Shadow;
|
use egui::epaint::Shadow;
|
||||||
use egui::{
|
use egui::{
|
||||||
pos2,
|
pos2,
|
||||||
@ -9,9 +8,13 @@ use egui::{
|
|||||||
};
|
};
|
||||||
use log::{debug, info, warn};
|
use log::{debug, info, warn};
|
||||||
use nostr_sdk::nips::nip19::Nip19;
|
use nostr_sdk::nips::nip19::Nip19;
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::{json, Event, EventId, Nip19Event, XOnlyPublicKey};
|
||||||
use nostrdb::{BlockType, Blocks, Note, Transaction};
|
use nostrdb::{Block, BlockType, Blocks, Mention, Ndb, Note, Transaction};
|
||||||
use std::f32::consts::PI;
|
|
||||||
|
const PURPLE: Color32 = Color32::from_rgb(0xcc, 0x43, 0xc5);
|
||||||
|
|
||||||
|
//use egui::emath::Rot2;
|
||||||
|
//use std::f32::consts::PI;
|
||||||
|
|
||||||
impl ProfileRenderData {
|
impl ProfileRenderData {
|
||||||
pub fn default(pfp: egui::ImageData) -> Self {
|
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);
|
fonts::setup_fonts(font_data, ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wrapped_body_blocks(ui: &mut egui::Ui, note: &Note, blocks: &Blocks) {
|
fn push_job_text(job: &mut LayoutJob, s: &str, color: Color32) {
|
||||||
let size = 50.0;
|
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();
|
let mut job = LayoutJob::default();
|
||||||
job.justify = false;
|
job.justify = false;
|
||||||
job.halign = egui::Align::LEFT;
|
job.halign = egui::Align::LEFT;
|
||||||
@ -311,39 +382,46 @@ fn wrapped_body_blocks(ui: &mut egui::Ui, note: &Note, blocks: &Blocks) {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let purple = Color32::from_rgb(0xcc, 0x43, 0xc5);
|
|
||||||
|
|
||||||
for block in blocks.iter(note) {
|
for block in blocks.iter(note) {
|
||||||
match block.blocktype() {
|
match block.blocktype() {
|
||||||
BlockType::Url => job.append(
|
BlockType::Url => push_job_text(&mut job, block.as_str(), PURPLE),
|
||||||
block.as_str(),
|
|
||||||
0.0,
|
|
||||||
TextFormat {
|
|
||||||
font_id: FontId::new(size, FontFamily::Proportional),
|
|
||||||
color: purple,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
),
|
|
||||||
|
|
||||||
BlockType::Hashtag => job.append(
|
BlockType::Hashtag => {
|
||||||
&format!("#{}", block.as_str()),
|
push_job_text(&mut job, "#", PURPLE);
|
||||||
0.0,
|
push_job_text(&mut job, block.as_str(), PURPLE);
|
||||||
TextFormat {
|
}
|
||||||
font_id: FontId::new(size, FontFamily::Proportional),
|
|
||||||
color: purple,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
),
|
|
||||||
|
|
||||||
_ => job.append(
|
BlockType::MentionBech32 => {
|
||||||
block.as_str(),
|
let pk = match block.as_mention().unwrap() {
|
||||||
0.0,
|
Mention::Event(ev) => push_job_text(
|
||||||
TextFormat {
|
&mut job,
|
||||||
font_id: FontId::new(size, FontFamily::Proportional),
|
&format!("@{}", &abbrev_str(block.as_str())),
|
||||||
color: Color32::WHITE,
|
PURPLE,
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
|
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) {
|
fn wrapped_body_text(ui: &mut egui::Ui, text: &str) {
|
||||||
use egui::text::{LayoutJob, TextFormat};
|
|
||||||
|
|
||||||
let format = TextFormat {
|
let format = TextFormat {
|
||||||
font_id: FontId::proportional(52.0),
|
font_id: FontId::proportional(52.0),
|
||||||
color: Color32::WHITE,
|
color: Color32::WHITE,
|
||||||
@ -361,8 +437,7 @@ fn wrapped_body_text(ui: &mut egui::Ui, text: &str) {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut job = LayoutJob::single_section(text.to_owned(), format);
|
let job = LayoutJob::single_section(text.to_owned(), format);
|
||||||
|
|
||||||
ui.label(job);
|
ui.label(job);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -437,8 +512,6 @@ fn note_ui(app: &Notecrumbs, ctx: &egui::Context, note: &NoteRenderData) {
|
|||||||
ui.set_max_size(desired);
|
ui.set_max_size(desired);
|
||||||
ui.set_min_size(desired);
|
ui.set_min_size(desired);
|
||||||
|
|
||||||
let mut rendered = false;
|
|
||||||
|
|
||||||
let ok = (|| -> Result<(), nostrdb::Error> {
|
let ok = (|| -> Result<(), nostrdb::Error> {
|
||||||
let txn = Transaction::new(&app.ndb)?;
|
let txn = Transaction::new(&app.ndb)?;
|
||||||
let note_id = note.note.id.ok_or(nostrdb::Error::NotFound)?;
|
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 =
|
let blocks =
|
||||||
app.ndb.get_blocks_by_key(&txn, note.key().unwrap())?;
|
app.ndb.get_blocks_by_key(&txn, note.key().unwrap())?;
|
||||||
|
|
||||||
wrapped_body_blocks(ui, ¬e, &blocks);
|
wrapped_body_blocks(ui, &app.ndb, ¬e, &blocks, &txn);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
})();
|
})();
|
||||||
@ -481,7 +554,7 @@ fn background_texture(ui: &mut egui::Ui, texture: &TextureHandle) {
|
|||||||
|
|
||||||
// Get the painter and draw the texture
|
// Get the painter and draw the texture
|
||||||
let painter = ui.ctx().layer_painter(layer_id);
|
let painter = ui.ctx().layer_painter(layer_id);
|
||||||
let tint = Color32::WHITE;
|
//let tint = Color32::WHITE;
|
||||||
|
|
||||||
let mut mesh = Mesh::with_texture(texture.into());
|
let mut mesh = Mesh::with_texture(texture.into());
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user