split notedeck into crates
This splits notedeck into crates, separating the browser chrome and individual apps: * notedeck: binary file, browser chrome * notedeck_columns: our columns app * enostr: same as before We still need to do more work to cleanly separate the chrome apis from the app apis. Soon I will create notedeck-notebook to see what makes sense to be shared between the apps. Some obvious ones that come to mind: 1. ImageCache We will likely want to move this to the notedeck crate, as most apps will want some kind of image cache. In web browsers, web pages do not need to worry about this, so we will likely have to do something similar 2. Ndb Since NdbRef is threadsafe and Ndb is an Arc<NdbRef>, it can be safely copied to each app. This will simplify things. In the future we might want to create an abstraction over this? Maybe each app shouldn't have access to the same database... we assume the data in DBs are all public anyways, but if we have unwrapped giftwraps that could be a problem. 3. RelayPool / Subscription Manager The browser should probably maintain these. Then apps can use ken's high level subscription manager api and not have to worry about connection pool details 4. Accounts Accounts and key management should be handled by the chrome. Apps should only have a simple signer interface. That's all for now, just something to think about! Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
306
crates/notedeck_columns/src/ui/note/contents.rs
Normal file
306
crates/notedeck_columns/src/ui/note/contents.rs
Normal file
@@ -0,0 +1,306 @@
|
||||
use crate::actionbar::NoteAction;
|
||||
use crate::images::ImageType;
|
||||
use crate::imgcache::ImageCache;
|
||||
use crate::notecache::NoteCache;
|
||||
use crate::ui::note::{NoteOptions, NoteResponse};
|
||||
use crate::ui::ProfilePic;
|
||||
use crate::{colors, ui};
|
||||
use egui::{Color32, Hyperlink, Image, RichText};
|
||||
use nostrdb::{BlockType, Mention, Ndb, Note, NoteKey, Transaction};
|
||||
use tracing::warn;
|
||||
|
||||
pub struct NoteContents<'a> {
|
||||
ndb: &'a Ndb,
|
||||
img_cache: &'a mut ImageCache,
|
||||
note_cache: &'a mut NoteCache,
|
||||
txn: &'a Transaction,
|
||||
note: &'a Note<'a>,
|
||||
note_key: NoteKey,
|
||||
options: NoteOptions,
|
||||
action: Option<NoteAction>,
|
||||
}
|
||||
|
||||
impl<'a> NoteContents<'a> {
|
||||
pub fn new(
|
||||
ndb: &'a Ndb,
|
||||
img_cache: &'a mut ImageCache,
|
||||
note_cache: &'a mut NoteCache,
|
||||
txn: &'a Transaction,
|
||||
note: &'a Note,
|
||||
note_key: NoteKey,
|
||||
options: ui::note::NoteOptions,
|
||||
) -> Self {
|
||||
NoteContents {
|
||||
ndb,
|
||||
img_cache,
|
||||
note_cache,
|
||||
txn,
|
||||
note,
|
||||
note_key,
|
||||
options,
|
||||
action: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn action(&self) -> &Option<NoteAction> {
|
||||
&self.action
|
||||
}
|
||||
}
|
||||
|
||||
impl egui::Widget for &mut NoteContents<'_> {
|
||||
fn ui(self, ui: &mut egui::Ui) -> egui::Response {
|
||||
let result = render_note_contents(
|
||||
ui,
|
||||
self.ndb,
|
||||
self.img_cache,
|
||||
self.note_cache,
|
||||
self.txn,
|
||||
self.note,
|
||||
self.note_key,
|
||||
self.options,
|
||||
);
|
||||
self.action = result.action;
|
||||
result.response
|
||||
}
|
||||
}
|
||||
|
||||
/// Render an inline note preview with a border. These are used when
|
||||
/// notes are references within a note
|
||||
pub fn render_note_preview(
|
||||
ui: &mut egui::Ui,
|
||||
ndb: &Ndb,
|
||||
note_cache: &mut NoteCache,
|
||||
img_cache: &mut ImageCache,
|
||||
txn: &Transaction,
|
||||
id: &[u8; 32],
|
||||
parent: NoteKey,
|
||||
) -> NoteResponse {
|
||||
#[cfg(feature = "profiling")]
|
||||
puffin::profile_function!();
|
||||
|
||||
let note = if let Ok(note) = ndb.get_note_by_id(txn, id) {
|
||||
// TODO: support other preview kinds
|
||||
if note.kind() == 1 {
|
||||
note
|
||||
} else {
|
||||
return NoteResponse::new(ui.colored_label(
|
||||
Color32::RED,
|
||||
format!("TODO: can't preview kind {}", note.kind()),
|
||||
));
|
||||
}
|
||||
} else {
|
||||
return NoteResponse::new(ui.colored_label(Color32::RED, "TODO: COULD NOT LOAD"));
|
||||
/*
|
||||
return ui
|
||||
.horizontal(|ui| {
|
||||
ui.spacing_mut().item_spacing.x = 0.0;
|
||||
ui.colored_label(colors::PURPLE, "@");
|
||||
ui.colored_label(colors::PURPLE, &id_str[4..16]);
|
||||
})
|
||||
.response;
|
||||
*/
|
||||
};
|
||||
|
||||
egui::Frame::none()
|
||||
.fill(ui.visuals().noninteractive().weak_bg_fill)
|
||||
.inner_margin(egui::Margin::same(8.0))
|
||||
.outer_margin(egui::Margin::symmetric(0.0, 8.0))
|
||||
.rounding(egui::Rounding::same(10.0))
|
||||
.stroke(egui::Stroke::new(
|
||||
1.0,
|
||||
ui.visuals().noninteractive().bg_stroke.color,
|
||||
))
|
||||
.show(ui, |ui| {
|
||||
ui::NoteView::new(ndb, note_cache, img_cache, ¬e)
|
||||
.actionbar(false)
|
||||
.small_pfp(true)
|
||||
.wide(true)
|
||||
.note_previews(false)
|
||||
.options_button(true)
|
||||
.parent(parent)
|
||||
.show(ui)
|
||||
})
|
||||
.inner
|
||||
}
|
||||
|
||||
fn is_image_link(url: &str) -> bool {
|
||||
url.ends_with("png") || url.ends_with("jpg") || url.ends_with("jpeg")
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn render_note_contents(
|
||||
ui: &mut egui::Ui,
|
||||
ndb: &Ndb,
|
||||
img_cache: &mut ImageCache,
|
||||
note_cache: &mut NoteCache,
|
||||
txn: &Transaction,
|
||||
note: &Note,
|
||||
note_key: NoteKey,
|
||||
options: NoteOptions,
|
||||
) -> NoteResponse {
|
||||
#[cfg(feature = "profiling")]
|
||||
puffin::profile_function!();
|
||||
|
||||
let selectable = options.has_selectable_text();
|
||||
let mut images: Vec<String> = vec![];
|
||||
let mut inline_note: Option<(&[u8; 32], &str)> = None;
|
||||
let hide_media = options.has_hide_media();
|
||||
|
||||
let response = ui.horizontal_wrapped(|ui| {
|
||||
let blocks = if let Ok(blocks) = ndb.get_blocks_by_key(txn, note_key) {
|
||||
blocks
|
||||
} else {
|
||||
warn!("missing note content blocks? '{}'", note.content());
|
||||
ui.weak(note.content());
|
||||
return;
|
||||
};
|
||||
|
||||
ui.spacing_mut().item_spacing.x = 0.0;
|
||||
|
||||
for block in blocks.iter(note) {
|
||||
match block.blocktype() {
|
||||
BlockType::MentionBech32 => match block.as_mention().unwrap() {
|
||||
Mention::Profile(profile) => {
|
||||
ui.add(ui::Mention::new(ndb, img_cache, txn, profile.pubkey()));
|
||||
}
|
||||
|
||||
Mention::Pubkey(npub) => {
|
||||
ui.add(ui::Mention::new(ndb, img_cache, txn, npub.pubkey()));
|
||||
}
|
||||
|
||||
Mention::Note(note) if options.has_note_previews() => {
|
||||
inline_note = Some((note.id(), block.as_str()));
|
||||
}
|
||||
|
||||
Mention::Event(note) if options.has_note_previews() => {
|
||||
inline_note = Some((note.id(), block.as_str()));
|
||||
}
|
||||
|
||||
_ => {
|
||||
ui.colored_label(colors::PURPLE, format!("@{}", &block.as_str()[4..16]));
|
||||
}
|
||||
},
|
||||
|
||||
BlockType::Hashtag => {
|
||||
#[cfg(feature = "profiling")]
|
||||
puffin::profile_scope!("hashtag contents");
|
||||
ui.colored_label(colors::PURPLE, format!("#{}", block.as_str()));
|
||||
}
|
||||
|
||||
BlockType::Url => {
|
||||
let lower_url = block.as_str().to_lowercase();
|
||||
if !hide_media && is_image_link(&lower_url) {
|
||||
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(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
BlockType::Text => {
|
||||
#[cfg(feature = "profiling")]
|
||||
puffin::profile_scope!("text contents");
|
||||
ui.add(egui::Label::new(block.as_str()).selectable(selectable));
|
||||
}
|
||||
|
||||
_ => {
|
||||
ui.colored_label(colors::PURPLE, block.as_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let note_action = if let Some((id, _block_str)) = inline_note {
|
||||
render_note_preview(ui, ndb, note_cache, img_cache, txn, id, note_key).action
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if !images.is_empty() && !options.has_textmode() {
|
||||
ui.add_space(2.0);
|
||||
let carousel_id = egui::Id::new(("carousel", note.key().expect("expected tx note")));
|
||||
image_carousel(ui, img_cache, images, carousel_id);
|
||||
ui.add_space(2.0);
|
||||
}
|
||||
|
||||
NoteResponse::new(response.response).with_action(note_action)
|
||||
}
|
||||
|
||||
fn image_carousel(
|
||||
ui: &mut egui::Ui,
|
||||
img_cache: &mut ImageCache,
|
||||
images: Vec<String>,
|
||||
carousel_id: egui::Id,
|
||||
) {
|
||||
// let's make sure everything is within our area
|
||||
|
||||
let height = 360.0;
|
||||
let width = ui.available_size().x;
|
||||
let spinsz = if height > width { width } else { height };
|
||||
|
||||
ui.add_sized([width, height], |ui: &mut egui::Ui| {
|
||||
egui::ScrollArea::horizontal()
|
||||
.id_salt(carousel_id)
|
||||
.show(ui, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
for image in images {
|
||||
// If the cache is empty, initiate the fetch
|
||||
let m_cached_promise = img_cache.map().get(&image);
|
||||
if m_cached_promise.is_none() {
|
||||
let res = crate::images::fetch_img(
|
||||
img_cache,
|
||||
ui.ctx(),
|
||||
&image,
|
||||
ImageType::Content(width.round() as u32, height.round() as u32),
|
||||
);
|
||||
img_cache.map_mut().insert(image.to_owned(), res);
|
||||
}
|
||||
|
||||
// What is the state of the fetch?
|
||||
match img_cache.map()[&image].ready() {
|
||||
// Still waiting
|
||||
None => {
|
||||
ui.allocate_space(egui::vec2(spinsz, spinsz));
|
||||
//ui.add(egui::Spinner::new().size(spinsz));
|
||||
}
|
||||
// Failed to fetch image!
|
||||
Some(Err(_err)) => {
|
||||
// FIXME - use content-specific error instead
|
||||
let no_pfp = crate::images::fetch_img(
|
||||
img_cache,
|
||||
ui.ctx(),
|
||||
ProfilePic::no_pfp_url(),
|
||||
ImageType::Profile(128),
|
||||
);
|
||||
img_cache.map_mut().insert(image.to_owned(), no_pfp);
|
||||
// spin until next pass
|
||||
ui.allocate_space(egui::vec2(spinsz, spinsz));
|
||||
//ui.add(egui::Spinner::new().size(spinsz));
|
||||
}
|
||||
// Use the previously resolved image
|
||||
Some(Ok(img)) => {
|
||||
let img_resp = ui.add(
|
||||
Image::new(img)
|
||||
.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
|
||||
});
|
||||
}
|
||||
183
crates/notedeck_columns/src/ui/note/context.rs
Normal file
183
crates/notedeck_columns/src/ui/note/context.rs
Normal file
@@ -0,0 +1,183 @@
|
||||
use crate::colors;
|
||||
use egui::{Rect, Vec2};
|
||||
use enostr::{NoteId, Pubkey};
|
||||
use nostrdb::{Note, NoteKey};
|
||||
|
||||
#[derive(Clone)]
|
||||
#[allow(clippy::enum_variant_names)]
|
||||
pub enum NoteContextSelection {
|
||||
CopyText,
|
||||
CopyPubkey,
|
||||
CopyNoteId,
|
||||
}
|
||||
|
||||
impl NoteContextSelection {
|
||||
pub fn process(&self, ui: &mut egui::Ui, note: &Note<'_>) {
|
||||
match self {
|
||||
NoteContextSelection::CopyText => {
|
||||
ui.output_mut(|w| {
|
||||
w.copied_text = note.content().to_string();
|
||||
});
|
||||
}
|
||||
NoteContextSelection::CopyPubkey => {
|
||||
ui.output_mut(|w| {
|
||||
if let Some(bech) = Pubkey::new(*note.pubkey()).to_bech() {
|
||||
w.copied_text = bech;
|
||||
}
|
||||
});
|
||||
}
|
||||
NoteContextSelection::CopyNoteId => {
|
||||
ui.output_mut(|w| {
|
||||
if let Some(bech) = NoteId::new(*note.id()).to_bech() {
|
||||
w.copied_text = bech;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NoteContextButton {
|
||||
put_at: Option<Rect>,
|
||||
note_key: NoteKey,
|
||||
}
|
||||
|
||||
impl egui::Widget for NoteContextButton {
|
||||
fn ui(self, ui: &mut egui::Ui) -> egui::Response {
|
||||
let r = if let Some(r) = self.put_at {
|
||||
r
|
||||
} else {
|
||||
let mut place = ui.available_rect_before_wrap();
|
||||
let size = Self::max_width();
|
||||
place.set_width(size);
|
||||
place.set_height(size);
|
||||
place
|
||||
};
|
||||
|
||||
Self::show(ui, self.note_key, r)
|
||||
}
|
||||
}
|
||||
|
||||
impl NoteContextButton {
|
||||
pub fn new(note_key: NoteKey) -> Self {
|
||||
let put_at: Option<Rect> = None;
|
||||
NoteContextButton { note_key, put_at }
|
||||
}
|
||||
|
||||
pub fn place_at(mut self, rect: Rect) -> Self {
|
||||
self.put_at = Some(rect);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn max_width() -> f32 {
|
||||
Self::max_radius() * 3.0 + Self::max_distance_between_circles() * 2.0
|
||||
}
|
||||
|
||||
pub fn size() -> Vec2 {
|
||||
let width = Self::max_width();
|
||||
egui::vec2(width, width)
|
||||
}
|
||||
|
||||
fn max_radius() -> f32 {
|
||||
8.0
|
||||
}
|
||||
|
||||
fn min_radius() -> f32 {
|
||||
Self::max_radius() / Self::expansion_multiple()
|
||||
}
|
||||
|
||||
fn max_distance_between_circles() -> f32 {
|
||||
2.0
|
||||
}
|
||||
|
||||
fn expansion_multiple() -> f32 {
|
||||
2.0
|
||||
}
|
||||
|
||||
fn min_distance_between_circles() -> f32 {
|
||||
Self::max_distance_between_circles() / Self::expansion_multiple()
|
||||
}
|
||||
|
||||
pub fn show(ui: &mut egui::Ui, note_key: NoteKey, put_at: Rect) -> egui::Response {
|
||||
#[cfg(feature = "profiling")]
|
||||
puffin::profile_function!();
|
||||
|
||||
let id = ui.id().with(("more_options_anim", note_key));
|
||||
|
||||
let min_radius = Self::min_radius();
|
||||
let anim_speed = 0.05;
|
||||
let response = ui.interact(put_at, id, egui::Sense::click());
|
||||
|
||||
let hovered = response.hovered();
|
||||
let animation_progress = ui.ctx().animate_bool_with_time(id, hovered, anim_speed);
|
||||
|
||||
if hovered {
|
||||
ui.ctx().set_cursor_icon(egui::CursorIcon::PointingHand);
|
||||
}
|
||||
|
||||
let min_distance = Self::min_distance_between_circles();
|
||||
let cur_distance = min_distance
|
||||
+ (Self::max_distance_between_circles() - min_distance) * animation_progress;
|
||||
|
||||
let cur_radius = min_radius + (Self::max_radius() - min_radius) * animation_progress;
|
||||
|
||||
let center = put_at.center();
|
||||
let left_circle_center = center - egui::vec2(cur_distance + cur_radius, 0.0);
|
||||
let right_circle_center = center + egui::vec2(cur_distance + cur_radius, 0.0);
|
||||
|
||||
let translated_radius = (cur_radius - 1.0) / 2.0;
|
||||
|
||||
// This works in both themes
|
||||
let color = colors::GRAY_SECONDARY;
|
||||
|
||||
// Draw circles
|
||||
let painter = ui.painter_at(put_at);
|
||||
painter.circle_filled(left_circle_center, translated_radius, color);
|
||||
painter.circle_filled(center, translated_radius, color);
|
||||
painter.circle_filled(right_circle_center, translated_radius, color);
|
||||
|
||||
response
|
||||
}
|
||||
|
||||
pub fn menu(
|
||||
ui: &mut egui::Ui,
|
||||
button_response: egui::Response,
|
||||
) -> Option<NoteContextSelection> {
|
||||
#[cfg(feature = "profiling")]
|
||||
puffin::profile_function!();
|
||||
|
||||
let mut context_selection: Option<NoteContextSelection> = None;
|
||||
|
||||
stationary_arbitrary_menu_button(ui, button_response, |ui| {
|
||||
ui.set_max_width(200.0);
|
||||
if ui.button("Copy text").clicked() {
|
||||
context_selection = Some(NoteContextSelection::CopyText);
|
||||
ui.close_menu();
|
||||
}
|
||||
if ui.button("Copy user public key").clicked() {
|
||||
context_selection = Some(NoteContextSelection::CopyPubkey);
|
||||
ui.close_menu();
|
||||
}
|
||||
if ui.button("Copy note id").clicked() {
|
||||
context_selection = Some(NoteContextSelection::CopyNoteId);
|
||||
ui.close_menu();
|
||||
}
|
||||
});
|
||||
|
||||
context_selection
|
||||
}
|
||||
}
|
||||
|
||||
fn stationary_arbitrary_menu_button<R>(
|
||||
ui: &mut egui::Ui,
|
||||
button_response: egui::Response,
|
||||
add_contents: impl FnOnce(&mut egui::Ui) -> R,
|
||||
) -> egui::InnerResponse<Option<R>> {
|
||||
let bar_id = ui.id();
|
||||
let mut bar_state = egui::menu::BarState::load(ui.ctx(), bar_id);
|
||||
|
||||
let inner = bar_state.bar_menu(&button_response, add_contents);
|
||||
|
||||
bar_state.store(ui.ctx(), bar_id);
|
||||
egui::InnerResponse::new(inner.map(|r| r.inner), button_response)
|
||||
}
|
||||
757
crates/notedeck_columns/src/ui/note/mod.rs
Normal file
757
crates/notedeck_columns/src/ui/note/mod.rs
Normal file
@@ -0,0 +1,757 @@
|
||||
pub mod contents;
|
||||
pub mod context;
|
||||
pub mod options;
|
||||
pub mod post;
|
||||
pub mod quote_repost;
|
||||
pub mod reply;
|
||||
|
||||
pub use contents::NoteContents;
|
||||
pub use context::{NoteContextButton, NoteContextSelection};
|
||||
pub use options::NoteOptions;
|
||||
pub use post::{PostAction, PostResponse, PostType, PostView};
|
||||
pub use quote_repost::QuoteRepostView;
|
||||
pub use reply::PostReplyView;
|
||||
|
||||
use crate::{
|
||||
actionbar::NoteAction,
|
||||
app_style::NotedeckTextStyle,
|
||||
colors,
|
||||
imgcache::ImageCache,
|
||||
notecache::{CachedNote, NoteCache},
|
||||
ui::{self, View},
|
||||
};
|
||||
use egui::emath::{pos2, Vec2};
|
||||
use egui::{Id, Label, Pos2, Rect, Response, RichText, Sense};
|
||||
use enostr::{NoteId, Pubkey};
|
||||
use nostrdb::{Ndb, Note, NoteKey, NoteReply, Transaction};
|
||||
|
||||
use super::profile::preview::{get_display_name, one_line_display_name_widget};
|
||||
|
||||
pub struct NoteView<'a> {
|
||||
ndb: &'a Ndb,
|
||||
note_cache: &'a mut NoteCache,
|
||||
img_cache: &'a mut ImageCache,
|
||||
parent: Option<NoteKey>,
|
||||
note: &'a nostrdb::Note<'a>,
|
||||
flags: NoteOptions,
|
||||
}
|
||||
|
||||
pub struct NoteResponse {
|
||||
pub response: egui::Response,
|
||||
pub context_selection: Option<NoteContextSelection>,
|
||||
pub action: Option<NoteAction>,
|
||||
}
|
||||
|
||||
impl NoteResponse {
|
||||
pub fn new(response: egui::Response) -> Self {
|
||||
Self {
|
||||
response,
|
||||
context_selection: None,
|
||||
action: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_action(mut self, action: Option<NoteAction>) -> Self {
|
||||
self.action = action;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn select_option(mut self, context_selection: Option<NoteContextSelection>) -> Self {
|
||||
self.context_selection = context_selection;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl View for NoteView<'_> {
|
||||
fn ui(&mut self, ui: &mut egui::Ui) {
|
||||
self.show(ui);
|
||||
}
|
||||
}
|
||||
|
||||
fn reply_desc(
|
||||
ui: &mut egui::Ui,
|
||||
txn: &Transaction,
|
||||
note_reply: &NoteReply,
|
||||
ndb: &Ndb,
|
||||
img_cache: &mut ImageCache,
|
||||
) {
|
||||
#[cfg(feature = "profiling")]
|
||||
puffin::profile_function!();
|
||||
|
||||
let size = 10.0;
|
||||
let selectable = false;
|
||||
|
||||
ui.add(
|
||||
Label::new(
|
||||
RichText::new("replying to")
|
||||
.size(size)
|
||||
.color(colors::GRAY_SECONDARY),
|
||||
)
|
||||
.selectable(selectable),
|
||||
);
|
||||
|
||||
let reply = if let Some(reply) = note_reply.reply() {
|
||||
reply
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
|
||||
let reply_note = if let Ok(reply_note) = ndb.get_note_by_id(txn, reply.id) {
|
||||
reply_note
|
||||
} else {
|
||||
ui.add(
|
||||
Label::new(
|
||||
RichText::new("a note")
|
||||
.size(size)
|
||||
.color(colors::GRAY_SECONDARY),
|
||||
)
|
||||
.selectable(selectable),
|
||||
);
|
||||
return;
|
||||
};
|
||||
|
||||
if note_reply.is_reply_to_root() {
|
||||
// We're replying to the root, let's show this
|
||||
ui.add(
|
||||
ui::Mention::new(ndb, img_cache, txn, reply_note.pubkey())
|
||||
.size(size)
|
||||
.selectable(selectable),
|
||||
);
|
||||
ui.add(
|
||||
Label::new(
|
||||
RichText::new("'s note")
|
||||
.size(size)
|
||||
.color(colors::GRAY_SECONDARY),
|
||||
)
|
||||
.selectable(selectable),
|
||||
);
|
||||
} else if let Some(root) = note_reply.root() {
|
||||
// replying to another post in a thread, not the root
|
||||
|
||||
if let Ok(root_note) = ndb.get_note_by_id(txn, root.id) {
|
||||
if root_note.pubkey() == reply_note.pubkey() {
|
||||
// simply "replying to bob's note" when replying to bob in his thread
|
||||
ui.add(
|
||||
ui::Mention::new(ndb, img_cache, txn, reply_note.pubkey())
|
||||
.size(size)
|
||||
.selectable(selectable),
|
||||
);
|
||||
ui.add(
|
||||
Label::new(
|
||||
RichText::new("'s note")
|
||||
.size(size)
|
||||
.color(colors::GRAY_SECONDARY),
|
||||
)
|
||||
.selectable(selectable),
|
||||
);
|
||||
} else {
|
||||
// replying to bob in alice's thread
|
||||
|
||||
ui.add(
|
||||
ui::Mention::new(ndb, img_cache, txn, reply_note.pubkey())
|
||||
.size(size)
|
||||
.selectable(selectable),
|
||||
);
|
||||
ui.add(
|
||||
Label::new(RichText::new("in").size(size).color(colors::GRAY_SECONDARY))
|
||||
.selectable(selectable),
|
||||
);
|
||||
ui.add(
|
||||
ui::Mention::new(ndb, img_cache, txn, root_note.pubkey())
|
||||
.size(size)
|
||||
.selectable(selectable),
|
||||
);
|
||||
ui.add(
|
||||
Label::new(
|
||||
RichText::new("'s thread")
|
||||
.size(size)
|
||||
.color(colors::GRAY_SECONDARY),
|
||||
)
|
||||
.selectable(selectable),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
ui.add(
|
||||
ui::Mention::new(ndb, img_cache, txn, reply_note.pubkey())
|
||||
.size(size)
|
||||
.selectable(selectable),
|
||||
);
|
||||
ui.add(
|
||||
Label::new(
|
||||
RichText::new("in someone's thread")
|
||||
.size(size)
|
||||
.color(colors::GRAY_SECONDARY),
|
||||
)
|
||||
.selectable(selectable),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> NoteView<'a> {
|
||||
pub fn new(
|
||||
ndb: &'a Ndb,
|
||||
note_cache: &'a mut NoteCache,
|
||||
img_cache: &'a mut ImageCache,
|
||||
note: &'a nostrdb::Note<'a>,
|
||||
) -> Self {
|
||||
let flags = NoteOptions::actionbar | NoteOptions::note_previews;
|
||||
let parent: Option<NoteKey> = None;
|
||||
Self {
|
||||
ndb,
|
||||
note_cache,
|
||||
img_cache,
|
||||
parent,
|
||||
note,
|
||||
flags,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn note_options(mut self, options: NoteOptions) -> Self {
|
||||
*self.options_mut() = options;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn textmode(mut self, enable: bool) -> Self {
|
||||
self.options_mut().set_textmode(enable);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn actionbar(mut self, enable: bool) -> Self {
|
||||
self.options_mut().set_actionbar(enable);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn small_pfp(mut self, enable: bool) -> Self {
|
||||
self.options_mut().set_small_pfp(enable);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn medium_pfp(mut self, enable: bool) -> Self {
|
||||
self.options_mut().set_medium_pfp(enable);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn note_previews(mut self, enable: bool) -> Self {
|
||||
self.options_mut().set_note_previews(enable);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn selectable_text(mut self, enable: bool) -> Self {
|
||||
self.options_mut().set_selectable_text(enable);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn wide(mut self, enable: bool) -> Self {
|
||||
self.options_mut().set_wide(enable);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn options_button(mut self, enable: bool) -> Self {
|
||||
self.options_mut().set_options_button(enable);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn options(&self) -> NoteOptions {
|
||||
self.flags
|
||||
}
|
||||
|
||||
pub fn options_mut(&mut self) -> &mut NoteOptions {
|
||||
&mut self.flags
|
||||
}
|
||||
|
||||
pub fn parent(mut self, parent: NoteKey) -> Self {
|
||||
self.parent = Some(parent);
|
||||
self
|
||||
}
|
||||
|
||||
fn textmode_ui(&mut self, ui: &mut egui::Ui) -> egui::Response {
|
||||
let note_key = self.note.key().expect("todo: implement non-db notes");
|
||||
let txn = self.note.txn().expect("todo: implement non-db notes");
|
||||
|
||||
ui.with_layout(egui::Layout::left_to_right(egui::Align::TOP), |ui| {
|
||||
let profile = self.ndb.get_profile_by_pubkey(txn, self.note.pubkey());
|
||||
|
||||
//ui.horizontal(|ui| {
|
||||
ui.spacing_mut().item_spacing.x = 2.0;
|
||||
|
||||
let cached_note = self
|
||||
.note_cache
|
||||
.cached_note_or_insert_mut(note_key, self.note);
|
||||
|
||||
let (_id, rect) = ui.allocate_space(egui::vec2(50.0, 20.0));
|
||||
ui.allocate_rect(rect, Sense::hover());
|
||||
ui.put(rect, |ui: &mut egui::Ui| {
|
||||
render_reltime(ui, cached_note, false).response
|
||||
});
|
||||
let (_id, rect) = ui.allocate_space(egui::vec2(150.0, 20.0));
|
||||
ui.allocate_rect(rect, Sense::hover());
|
||||
ui.put(rect, |ui: &mut egui::Ui| {
|
||||
ui.add(
|
||||
ui::Username::new(profile.as_ref().ok(), self.note.pubkey())
|
||||
.abbreviated(6)
|
||||
.pk_colored(true),
|
||||
)
|
||||
});
|
||||
|
||||
ui.add(&mut NoteContents::new(
|
||||
self.ndb,
|
||||
self.img_cache,
|
||||
self.note_cache,
|
||||
txn,
|
||||
self.note,
|
||||
note_key,
|
||||
self.flags,
|
||||
));
|
||||
//});
|
||||
})
|
||||
.response
|
||||
}
|
||||
|
||||
pub fn expand_size() -> f32 {
|
||||
5.0
|
||||
}
|
||||
|
||||
fn pfp(
|
||||
&mut self,
|
||||
note_key: NoteKey,
|
||||
profile: &Result<nostrdb::ProfileRecord<'_>, nostrdb::Error>,
|
||||
ui: &mut egui::Ui,
|
||||
) -> egui::Response {
|
||||
if !self.options().has_wide() {
|
||||
ui.spacing_mut().item_spacing.x = 16.0;
|
||||
} else {
|
||||
ui.spacing_mut().item_spacing.x = 4.0;
|
||||
}
|
||||
|
||||
let pfp_size = self.options().pfp_size();
|
||||
|
||||
let sense = Sense::click();
|
||||
match profile
|
||||
.as_ref()
|
||||
.ok()
|
||||
.and_then(|p| p.record().profile()?.picture())
|
||||
{
|
||||
// these have different lifetimes and types,
|
||||
// so the calls must be separate
|
||||
Some(pic) => {
|
||||
let anim_speed = 0.05;
|
||||
let profile_key = profile.as_ref().unwrap().record().note_key();
|
||||
let note_key = note_key.as_u64();
|
||||
|
||||
let (rect, size, resp) = ui::anim::hover_expand(
|
||||
ui,
|
||||
egui::Id::new((profile_key, note_key)),
|
||||
pfp_size,
|
||||
ui::NoteView::expand_size(),
|
||||
anim_speed,
|
||||
);
|
||||
|
||||
ui.put(rect, ui::ProfilePic::new(self.img_cache, pic).size(size))
|
||||
.on_hover_ui_at_pointer(|ui| {
|
||||
ui.set_max_width(300.0);
|
||||
ui.add(ui::ProfilePreview::new(
|
||||
profile.as_ref().unwrap(),
|
||||
self.img_cache,
|
||||
));
|
||||
});
|
||||
resp
|
||||
}
|
||||
None => ui
|
||||
.add(
|
||||
ui::ProfilePic::new(self.img_cache, ui::ProfilePic::no_pfp_url())
|
||||
.size(pfp_size),
|
||||
)
|
||||
.interact(sense),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn show(&mut self, ui: &mut egui::Ui) -> NoteResponse {
|
||||
if self.options().has_textmode() {
|
||||
NoteResponse::new(self.textmode_ui(ui))
|
||||
} else {
|
||||
let txn = self.note.txn().expect("txn");
|
||||
if let Some(note_to_repost) = get_reposted_note(self.ndb, txn, self.note) {
|
||||
let profile = self.ndb.get_profile_by_pubkey(txn, self.note.pubkey());
|
||||
|
||||
let style = NotedeckTextStyle::Small;
|
||||
ui.horizontal(|ui| {
|
||||
ui.vertical(|ui| {
|
||||
ui.add_space(2.0);
|
||||
ui.add_sized([20.0, 20.0], repost_icon(ui.visuals().dark_mode));
|
||||
});
|
||||
ui.add_space(6.0);
|
||||
let resp = ui.add(one_line_display_name_widget(
|
||||
get_display_name(profile.as_ref().ok()),
|
||||
style,
|
||||
));
|
||||
if let Ok(rec) = &profile {
|
||||
resp.on_hover_ui_at_pointer(|ui| {
|
||||
ui.set_max_width(300.0);
|
||||
ui.add(ui::ProfilePreview::new(rec, self.img_cache));
|
||||
});
|
||||
}
|
||||
ui.add_space(4.0);
|
||||
ui.label(
|
||||
RichText::new("Reposted")
|
||||
.color(colors::GRAY_SECONDARY)
|
||||
.text_style(style.text_style()),
|
||||
);
|
||||
});
|
||||
NoteView::new(self.ndb, self.note_cache, self.img_cache, ¬e_to_repost).show(ui)
|
||||
} else {
|
||||
self.show_standard(ui)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn note_header(
|
||||
ui: &mut egui::Ui,
|
||||
note_cache: &mut NoteCache,
|
||||
note: &Note,
|
||||
profile: &Result<nostrdb::ProfileRecord<'_>, nostrdb::Error>,
|
||||
options: NoteOptions,
|
||||
container_right: Pos2,
|
||||
) -> NoteResponse {
|
||||
#[cfg(feature = "profiling")]
|
||||
puffin::profile_function!();
|
||||
|
||||
let note_key = note.key().unwrap();
|
||||
|
||||
let inner_response = ui.horizontal(|ui| {
|
||||
ui.spacing_mut().item_spacing.x = 2.0;
|
||||
ui.add(ui::Username::new(profile.as_ref().ok(), note.pubkey()).abbreviated(20));
|
||||
|
||||
let cached_note = note_cache.cached_note_or_insert_mut(note_key, note);
|
||||
render_reltime(ui, cached_note, true);
|
||||
|
||||
if options.has_options_button() {
|
||||
let context_pos = {
|
||||
let size = NoteContextButton::max_width();
|
||||
let min = Pos2::new(container_right.x - size, container_right.y);
|
||||
Rect::from_min_size(min, egui::vec2(size, size))
|
||||
};
|
||||
|
||||
let resp = ui.add(NoteContextButton::new(note_key).place_at(context_pos));
|
||||
NoteContextButton::menu(ui, resp.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
|
||||
NoteResponse::new(inner_response.response).select_option(inner_response.inner)
|
||||
}
|
||||
|
||||
fn show_standard(&mut self, ui: &mut egui::Ui) -> NoteResponse {
|
||||
#[cfg(feature = "profiling")]
|
||||
puffin::profile_function!();
|
||||
let note_key = self.note.key().expect("todo: support non-db notes");
|
||||
let txn = self.note.txn().expect("todo: support non-db notes");
|
||||
|
||||
let mut note_action: Option<NoteAction> = None;
|
||||
let mut selected_option: Option<NoteContextSelection> = None;
|
||||
|
||||
let hitbox_id = note_hitbox_id(note_key, self.options(), self.parent);
|
||||
let profile = self.ndb.get_profile_by_pubkey(txn, self.note.pubkey());
|
||||
let maybe_hitbox = maybe_note_hitbox(ui, hitbox_id);
|
||||
let container_right = {
|
||||
let r = ui.available_rect_before_wrap();
|
||||
let x = r.max.x;
|
||||
let y = r.min.y;
|
||||
Pos2::new(x, y)
|
||||
};
|
||||
|
||||
// wide design
|
||||
let response = if self.options().has_wide() {
|
||||
ui.vertical(|ui| {
|
||||
ui.horizontal(|ui| {
|
||||
if self.pfp(note_key, &profile, ui).clicked() {
|
||||
note_action =
|
||||
Some(NoteAction::OpenProfile(Pubkey::new(*self.note.pubkey())));
|
||||
};
|
||||
|
||||
let size = ui.available_size();
|
||||
ui.vertical(|ui| {
|
||||
ui.add_sized([size.x, self.options().pfp_size()], |ui: &mut egui::Ui| {
|
||||
ui.horizontal_centered(|ui| {
|
||||
selected_option = NoteView::note_header(
|
||||
ui,
|
||||
self.note_cache,
|
||||
self.note,
|
||||
&profile,
|
||||
self.options(),
|
||||
container_right,
|
||||
)
|
||||
.context_selection;
|
||||
})
|
||||
.response
|
||||
});
|
||||
|
||||
let note_reply = self
|
||||
.note_cache
|
||||
.cached_note_or_insert_mut(note_key, self.note)
|
||||
.reply
|
||||
.borrow(self.note.tags());
|
||||
|
||||
if note_reply.reply().is_some() {
|
||||
ui.horizontal(|ui| {
|
||||
reply_desc(ui, txn, ¬e_reply, self.ndb, self.img_cache);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
let mut contents = NoteContents::new(
|
||||
self.ndb,
|
||||
self.img_cache,
|
||||
self.note_cache,
|
||||
txn,
|
||||
self.note,
|
||||
note_key,
|
||||
self.options(),
|
||||
);
|
||||
|
||||
ui.add(&mut contents);
|
||||
|
||||
if let Some(action) = contents.action() {
|
||||
note_action = Some(*action);
|
||||
}
|
||||
|
||||
if self.options().has_actionbar() {
|
||||
if let Some(action) = render_note_actionbar(ui, self.note.id(), note_key).inner
|
||||
{
|
||||
note_action = Some(action);
|
||||
}
|
||||
}
|
||||
})
|
||||
.response
|
||||
} else {
|
||||
// main design
|
||||
ui.with_layout(egui::Layout::left_to_right(egui::Align::TOP), |ui| {
|
||||
if self.pfp(note_key, &profile, ui).clicked() {
|
||||
note_action = Some(NoteAction::OpenProfile(Pubkey::new(*self.note.pubkey())));
|
||||
};
|
||||
|
||||
ui.with_layout(egui::Layout::top_down(egui::Align::LEFT), |ui| {
|
||||
selected_option = NoteView::note_header(
|
||||
ui,
|
||||
self.note_cache,
|
||||
self.note,
|
||||
&profile,
|
||||
self.options(),
|
||||
container_right,
|
||||
)
|
||||
.context_selection;
|
||||
ui.horizontal(|ui| {
|
||||
ui.spacing_mut().item_spacing.x = 2.0;
|
||||
|
||||
let note_reply = self
|
||||
.note_cache
|
||||
.cached_note_or_insert_mut(note_key, self.note)
|
||||
.reply
|
||||
.borrow(self.note.tags());
|
||||
|
||||
if note_reply.reply().is_some() {
|
||||
reply_desc(ui, txn, ¬e_reply, self.ndb, self.img_cache);
|
||||
}
|
||||
});
|
||||
|
||||
let mut contents = NoteContents::new(
|
||||
self.ndb,
|
||||
self.img_cache,
|
||||
self.note_cache,
|
||||
txn,
|
||||
self.note,
|
||||
note_key,
|
||||
self.options(),
|
||||
);
|
||||
ui.add(&mut contents);
|
||||
|
||||
if let Some(action) = contents.action() {
|
||||
note_action = Some(*action);
|
||||
}
|
||||
|
||||
if self.options().has_actionbar() {
|
||||
if let Some(action) =
|
||||
render_note_actionbar(ui, self.note.id(), note_key).inner
|
||||
{
|
||||
note_action = Some(action);
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
.response
|
||||
};
|
||||
|
||||
let note_action = if note_hitbox_clicked(ui, hitbox_id, &response.rect, maybe_hitbox) {
|
||||
Some(NoteAction::OpenThread(NoteId::new(*self.note.id())))
|
||||
} else {
|
||||
note_action
|
||||
};
|
||||
|
||||
NoteResponse::new(response)
|
||||
.with_action(note_action)
|
||||
.select_option(selected_option)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_reposted_note<'a>(ndb: &Ndb, txn: &'a Transaction, note: &Note) -> Option<Note<'a>> {
|
||||
let new_note_id: &[u8; 32] = if note.kind() == 6 {
|
||||
let mut res = None;
|
||||
for tag in note.tags().iter() {
|
||||
if tag.count() == 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some("e") = tag.get(0).and_then(|t| t.variant().str()) {
|
||||
if let Some(note_id) = tag.get(1).and_then(|f| f.variant().id()) {
|
||||
res = Some(note_id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
res?
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
|
||||
let note = ndb.get_note_by_id(txn, new_note_id).ok();
|
||||
note.filter(|note| note.kind() == 1)
|
||||
}
|
||||
|
||||
fn note_hitbox_id(
|
||||
note_key: NoteKey,
|
||||
note_options: NoteOptions,
|
||||
parent: Option<NoteKey>,
|
||||
) -> egui::Id {
|
||||
Id::new(("note_size", note_key, note_options, parent))
|
||||
}
|
||||
|
||||
fn maybe_note_hitbox(ui: &mut egui::Ui, hitbox_id: egui::Id) -> Option<Response> {
|
||||
ui.ctx()
|
||||
.data_mut(|d| d.get_persisted(hitbox_id))
|
||||
.map(|note_size: Vec2| {
|
||||
// The hitbox should extend the entire width of the
|
||||
// container. The hitbox height was cached last layout.
|
||||
let container_rect = ui.max_rect();
|
||||
let rect = Rect {
|
||||
min: pos2(container_rect.min.x, container_rect.min.y),
|
||||
max: pos2(container_rect.max.x, container_rect.min.y + note_size.y),
|
||||
};
|
||||
|
||||
let response = ui.interact(rect, ui.id().with(hitbox_id), egui::Sense::click());
|
||||
|
||||
response
|
||||
.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Other, true, "hitbox"));
|
||||
|
||||
response
|
||||
})
|
||||
}
|
||||
|
||||
fn note_hitbox_clicked(
|
||||
ui: &mut egui::Ui,
|
||||
hitbox_id: egui::Id,
|
||||
note_rect: &Rect,
|
||||
maybe_hitbox: Option<Response>,
|
||||
) -> bool {
|
||||
// Stash the dimensions of the note content so we can render the
|
||||
// hitbox in the next frame
|
||||
ui.ctx().data_mut(|d| {
|
||||
d.insert_persisted(hitbox_id, note_rect.size());
|
||||
});
|
||||
|
||||
// If there was an hitbox and it was clicked open the thread
|
||||
match maybe_hitbox {
|
||||
Some(hitbox) => hitbox.clicked(),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn render_note_actionbar(
|
||||
ui: &mut egui::Ui,
|
||||
note_id: &[u8; 32],
|
||||
note_key: NoteKey,
|
||||
) -> egui::InnerResponse<Option<NoteAction>> {
|
||||
#[cfg(feature = "profiling")]
|
||||
puffin::profile_function!();
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
let reply_resp = reply_button(ui, note_key);
|
||||
let quote_resp = quote_repost_button(ui, note_key);
|
||||
|
||||
if reply_resp.clicked() {
|
||||
Some(NoteAction::Reply(NoteId::new(*note_id)))
|
||||
} else if quote_resp.clicked() {
|
||||
Some(NoteAction::Quote(NoteId::new(*note_id)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn secondary_label(ui: &mut egui::Ui, s: impl Into<String>) {
|
||||
ui.add(Label::new(
|
||||
RichText::new(s).size(10.0).color(colors::GRAY_SECONDARY),
|
||||
));
|
||||
}
|
||||
|
||||
fn render_reltime(
|
||||
ui: &mut egui::Ui,
|
||||
note_cache: &mut CachedNote,
|
||||
before: bool,
|
||||
) -> egui::InnerResponse<()> {
|
||||
#[cfg(feature = "profiling")]
|
||||
puffin::profile_function!();
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
if before {
|
||||
secondary_label(ui, "⋅");
|
||||
}
|
||||
|
||||
secondary_label(ui, note_cache.reltime_str_mut());
|
||||
|
||||
if !before {
|
||||
secondary_label(ui, "⋅");
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn reply_button(ui: &mut egui::Ui, note_key: NoteKey) -> egui::Response {
|
||||
let img_data = if ui.style().visuals.dark_mode {
|
||||
egui::include_image!("../../../assets/icons/reply.png")
|
||||
} else {
|
||||
egui::include_image!("../../../assets/icons/reply-dark.png")
|
||||
};
|
||||
|
||||
let (rect, size, resp) =
|
||||
ui::anim::hover_expand_small(ui, ui.id().with(("reply_anim", note_key)));
|
||||
|
||||
// align rect to note contents
|
||||
let expand_size = 5.0; // from hover_expand_small
|
||||
let rect = rect.translate(egui::vec2(-(expand_size / 2.0), 0.0));
|
||||
|
||||
let put_resp = ui.put(rect, egui::Image::new(img_data).max_width(size));
|
||||
|
||||
resp.union(put_resp)
|
||||
}
|
||||
|
||||
fn repost_icon(dark_mode: bool) -> egui::Image<'static> {
|
||||
let img_data = if dark_mode {
|
||||
egui::include_image!("../../../assets/icons/repost_icon_4x.png")
|
||||
} else {
|
||||
egui::include_image!("../../../assets/icons/repost_light_4x.png")
|
||||
};
|
||||
egui::Image::new(img_data)
|
||||
}
|
||||
|
||||
fn quote_repost_button(ui: &mut egui::Ui, note_key: NoteKey) -> egui::Response {
|
||||
let (rect, size, resp) =
|
||||
ui::anim::hover_expand_small(ui, ui.id().with(("repost_anim", note_key)));
|
||||
|
||||
let expand_size = 5.0;
|
||||
let rect = rect.translate(egui::vec2(-(expand_size / 2.0), 0.0));
|
||||
|
||||
let put_resp = ui.put(rect, repost_icon(ui.visuals().dark_mode).max_width(size));
|
||||
|
||||
resp.union(put_resp)
|
||||
}
|
||||
111
crates/notedeck_columns/src/ui/note/options.rs
Normal file
111
crates/notedeck_columns/src/ui/note/options.rs
Normal file
@@ -0,0 +1,111 @@
|
||||
use crate::ui::ProfilePic;
|
||||
use bitflags::bitflags;
|
||||
|
||||
bitflags! {
|
||||
// Attributes can be applied to flags types
|
||||
#[repr(transparent)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct NoteOptions: u64 {
|
||||
const actionbar = 0b0000000000000001;
|
||||
const note_previews = 0b0000000000000010;
|
||||
const small_pfp = 0b0000000000000100;
|
||||
const medium_pfp = 0b0000000000001000;
|
||||
const wide = 0b0000000000010000;
|
||||
const selectable_text = 0b0000000000100000;
|
||||
const textmode = 0b0000000001000000;
|
||||
const options_button = 0b0000000010000000;
|
||||
const hide_media = 0b0000000100000000;
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for NoteOptions {
|
||||
fn default() -> NoteOptions {
|
||||
NoteOptions::options_button | NoteOptions::note_previews | NoteOptions::actionbar
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! create_setter {
|
||||
($fn_name:ident, $option:ident) => {
|
||||
#[inline]
|
||||
pub fn $fn_name(&mut self, enable: bool) {
|
||||
if enable {
|
||||
*self |= NoteOptions::$option;
|
||||
} else {
|
||||
*self &= !NoteOptions::$option;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl NoteOptions {
|
||||
create_setter!(set_small_pfp, small_pfp);
|
||||
create_setter!(set_medium_pfp, medium_pfp);
|
||||
create_setter!(set_note_previews, note_previews);
|
||||
create_setter!(set_selectable_text, selectable_text);
|
||||
create_setter!(set_textmode, textmode);
|
||||
create_setter!(set_actionbar, actionbar);
|
||||
create_setter!(set_wide, wide);
|
||||
create_setter!(set_options_button, options_button);
|
||||
create_setter!(set_hide_media, hide_media);
|
||||
|
||||
pub fn new(is_universe_timeline: bool) -> Self {
|
||||
let mut options = NoteOptions::default();
|
||||
options.set_hide_media(is_universe_timeline);
|
||||
options
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn has_actionbar(self) -> bool {
|
||||
(self & NoteOptions::actionbar) == NoteOptions::actionbar
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn has_hide_media(self) -> bool {
|
||||
(self & NoteOptions::hide_media) == NoteOptions::hide_media
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn has_selectable_text(self) -> bool {
|
||||
(self & NoteOptions::selectable_text) == NoteOptions::selectable_text
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn has_textmode(self) -> bool {
|
||||
(self & NoteOptions::textmode) == NoteOptions::textmode
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn has_note_previews(self) -> bool {
|
||||
(self & NoteOptions::note_previews) == NoteOptions::note_previews
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn has_small_pfp(self) -> bool {
|
||||
(self & NoteOptions::small_pfp) == NoteOptions::small_pfp
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn has_medium_pfp(self) -> bool {
|
||||
(self & NoteOptions::medium_pfp) == NoteOptions::medium_pfp
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn has_wide(self) -> bool {
|
||||
(self & NoteOptions::wide) == NoteOptions::wide
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn has_options_button(self) -> bool {
|
||||
(self & NoteOptions::options_button) == NoteOptions::options_button
|
||||
}
|
||||
|
||||
pub fn pfp_size(&self) -> f32 {
|
||||
if self.has_small_pfp() {
|
||||
ProfilePic::small_size()
|
||||
} else if self.has_medium_pfp() {
|
||||
ProfilePic::medium_size()
|
||||
} else {
|
||||
ProfilePic::default_size()
|
||||
}
|
||||
}
|
||||
}
|
||||
305
crates/notedeck_columns/src/ui/note/post.rs
Normal file
305
crates/notedeck_columns/src/ui/note/post.rs
Normal file
@@ -0,0 +1,305 @@
|
||||
use crate::draft::{Draft, Drafts};
|
||||
use crate::imgcache::ImageCache;
|
||||
use crate::notecache::NoteCache;
|
||||
use crate::post::NewPost;
|
||||
use crate::ui::{self, Preview, PreviewConfig, View};
|
||||
use crate::Result;
|
||||
use egui::widgets::text_edit::TextEdit;
|
||||
use egui::{Frame, Layout};
|
||||
use enostr::{FilledKeypair, FullKeypair, NoteId, RelayPool};
|
||||
use nostrdb::{Config, Ndb, Transaction};
|
||||
use tracing::info;
|
||||
|
||||
use super::contents::render_note_preview;
|
||||
|
||||
pub struct PostView<'a> {
|
||||
ndb: &'a Ndb,
|
||||
draft: &'a mut Draft,
|
||||
post_type: PostType,
|
||||
img_cache: &'a mut ImageCache,
|
||||
note_cache: &'a mut NoteCache,
|
||||
poster: FilledKeypair<'a>,
|
||||
id_source: Option<egui::Id>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum PostType {
|
||||
New,
|
||||
Quote(NoteId),
|
||||
Reply(NoteId),
|
||||
}
|
||||
|
||||
pub struct PostAction {
|
||||
post_type: PostType,
|
||||
post: NewPost,
|
||||
}
|
||||
|
||||
impl PostAction {
|
||||
pub fn new(post_type: PostType, post: NewPost) -> Self {
|
||||
PostAction { post_type, post }
|
||||
}
|
||||
|
||||
pub fn execute(
|
||||
&self,
|
||||
ndb: &Ndb,
|
||||
txn: &Transaction,
|
||||
pool: &mut RelayPool,
|
||||
drafts: &mut Drafts,
|
||||
) -> Result<()> {
|
||||
let seckey = self.post.account.secret_key.to_secret_bytes();
|
||||
|
||||
let note = match self.post_type {
|
||||
PostType::New => self.post.to_note(&seckey),
|
||||
|
||||
PostType::Reply(target) => {
|
||||
let replying_to = ndb.get_note_by_id(txn, target.bytes())?;
|
||||
self.post.to_reply(&seckey, &replying_to)
|
||||
}
|
||||
|
||||
PostType::Quote(target) => {
|
||||
let quoting = ndb.get_note_by_id(txn, target.bytes())?;
|
||||
self.post.to_quote(&seckey, "ing)
|
||||
}
|
||||
};
|
||||
|
||||
let raw_msg = format!("[\"EVENT\",{}]", note.json().unwrap());
|
||||
info!("sending {}", raw_msg);
|
||||
pool.send(&enostr::ClientMessage::raw(raw_msg));
|
||||
drafts.get_from_post_type(&self.post_type).clear();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PostResponse {
|
||||
pub action: Option<PostAction>,
|
||||
pub edit_response: egui::Response,
|
||||
}
|
||||
|
||||
impl<'a> PostView<'a> {
|
||||
pub fn new(
|
||||
ndb: &'a Ndb,
|
||||
draft: &'a mut Draft,
|
||||
post_type: PostType,
|
||||
img_cache: &'a mut ImageCache,
|
||||
note_cache: &'a mut NoteCache,
|
||||
poster: FilledKeypair<'a>,
|
||||
) -> Self {
|
||||
let id_source: Option<egui::Id> = None;
|
||||
PostView {
|
||||
ndb,
|
||||
draft,
|
||||
img_cache,
|
||||
note_cache,
|
||||
poster,
|
||||
id_source,
|
||||
post_type,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn id_source(mut self, id_source: impl std::hash::Hash) -> Self {
|
||||
self.id_source = Some(egui::Id::new(id_source));
|
||||
self
|
||||
}
|
||||
|
||||
fn editbox(&mut self, txn: &nostrdb::Transaction, ui: &mut egui::Ui) -> egui::Response {
|
||||
ui.spacing_mut().item_spacing.x = 12.0;
|
||||
|
||||
let pfp_size = 24.0;
|
||||
|
||||
// TODO: refactor pfp control to do all of this for us
|
||||
let poster_pfp = self
|
||||
.ndb
|
||||
.get_profile_by_pubkey(txn, self.poster.pubkey.bytes())
|
||||
.as_ref()
|
||||
.ok()
|
||||
.and_then(|p| Some(ui::ProfilePic::from_profile(self.img_cache, p)?.size(pfp_size)));
|
||||
|
||||
if let Some(pfp) = poster_pfp {
|
||||
ui.add(pfp);
|
||||
} else {
|
||||
ui.add(
|
||||
ui::ProfilePic::new(self.img_cache, ui::ProfilePic::no_pfp_url()).size(pfp_size),
|
||||
);
|
||||
}
|
||||
|
||||
let response = ui.add_sized(
|
||||
ui.available_size(),
|
||||
TextEdit::multiline(&mut self.draft.buffer)
|
||||
.hint_text(egui::RichText::new("Write a banger note here...").weak())
|
||||
.frame(false),
|
||||
);
|
||||
|
||||
let focused = response.has_focus();
|
||||
|
||||
ui.ctx().data_mut(|d| d.insert_temp(self.id(), focused));
|
||||
|
||||
response
|
||||
}
|
||||
|
||||
fn focused(&self, ui: &egui::Ui) -> bool {
|
||||
ui.ctx()
|
||||
.data(|d| d.get_temp::<bool>(self.id()).unwrap_or(false))
|
||||
}
|
||||
|
||||
fn id(&self) -> egui::Id {
|
||||
self.id_source.unwrap_or_else(|| egui::Id::new("post"))
|
||||
}
|
||||
|
||||
pub fn outer_margin() -> f32 {
|
||||
16.0
|
||||
}
|
||||
|
||||
pub fn inner_margin() -> f32 {
|
||||
12.0
|
||||
}
|
||||
|
||||
pub fn ui(&mut self, txn: &nostrdb::Transaction, ui: &mut egui::Ui) -> PostResponse {
|
||||
let focused = self.focused(ui);
|
||||
let stroke = if focused {
|
||||
ui.visuals().selection.stroke
|
||||
} else {
|
||||
//ui.visuals().selection.stroke
|
||||
ui.visuals().noninteractive().bg_stroke
|
||||
};
|
||||
|
||||
let mut frame = egui::Frame::default()
|
||||
.inner_margin(egui::Margin::same(PostView::inner_margin()))
|
||||
.outer_margin(egui::Margin::same(PostView::outer_margin()))
|
||||
.fill(ui.visuals().extreme_bg_color)
|
||||
.stroke(stroke)
|
||||
.rounding(12.0);
|
||||
|
||||
if focused {
|
||||
frame = frame.shadow(egui::epaint::Shadow {
|
||||
offset: egui::vec2(0.0, 0.0),
|
||||
blur: 8.0,
|
||||
spread: 0.0,
|
||||
color: stroke.color,
|
||||
});
|
||||
}
|
||||
|
||||
frame
|
||||
.show(ui, |ui| {
|
||||
ui.vertical(|ui| {
|
||||
let edit_response = ui.horizontal(|ui| self.editbox(txn, ui)).inner;
|
||||
|
||||
let action = ui
|
||||
.horizontal(|ui| {
|
||||
if let PostType::Quote(id) = self.post_type {
|
||||
let avail_size = ui.available_size_before_wrap();
|
||||
ui.with_layout(Layout::left_to_right(egui::Align::TOP), |ui| {
|
||||
Frame::none().show(ui, |ui| {
|
||||
ui.vertical(|ui| {
|
||||
ui.set_max_width(avail_size.x * 0.8);
|
||||
render_note_preview(
|
||||
ui,
|
||||
self.ndb,
|
||||
self.note_cache,
|
||||
self.img_cache,
|
||||
txn,
|
||||
id.bytes(),
|
||||
nostrdb::NoteKey::new(0),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::BOTTOM), |ui| {
|
||||
if ui
|
||||
.add_sized(
|
||||
[91.0, 32.0],
|
||||
post_button(!self.draft.buffer.is_empty()),
|
||||
)
|
||||
.clicked()
|
||||
{
|
||||
let new_post = NewPost::new(
|
||||
self.draft.buffer.clone(),
|
||||
self.poster.to_full(),
|
||||
);
|
||||
Some(PostAction::new(self.post_type.clone(), new_post))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.inner
|
||||
})
|
||||
.inner;
|
||||
|
||||
PostResponse {
|
||||
action,
|
||||
edit_response,
|
||||
}
|
||||
})
|
||||
.inner
|
||||
})
|
||||
.inner
|
||||
}
|
||||
}
|
||||
|
||||
fn post_button(interactive: bool) -> impl egui::Widget {
|
||||
move |ui: &mut egui::Ui| {
|
||||
let button = egui::Button::new("Post now");
|
||||
if interactive {
|
||||
ui.add(button)
|
||||
} else {
|
||||
ui.add(
|
||||
button
|
||||
.sense(egui::Sense::hover())
|
||||
.fill(ui.visuals().widgets.noninteractive.bg_fill)
|
||||
.stroke(ui.visuals().widgets.noninteractive.bg_stroke),
|
||||
)
|
||||
.on_hover_cursor(egui::CursorIcon::NotAllowed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod preview {
|
||||
use super::*;
|
||||
|
||||
pub struct PostPreview {
|
||||
ndb: Ndb,
|
||||
img_cache: ImageCache,
|
||||
note_cache: NoteCache,
|
||||
draft: Draft,
|
||||
poster: FullKeypair,
|
||||
}
|
||||
|
||||
impl PostPreview {
|
||||
fn new() -> Self {
|
||||
let ndb = Ndb::new(".", &Config::new()).expect("ndb");
|
||||
|
||||
PostPreview {
|
||||
ndb,
|
||||
img_cache: ImageCache::new(".".into()),
|
||||
note_cache: NoteCache::default(),
|
||||
draft: Draft::new(),
|
||||
poster: FullKeypair::generate(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl View for PostPreview {
|
||||
fn ui(&mut self, ui: &mut egui::Ui) {
|
||||
let txn = Transaction::new(&self.ndb).expect("txn");
|
||||
PostView::new(
|
||||
&self.ndb,
|
||||
&mut self.draft,
|
||||
PostType::New,
|
||||
&mut self.img_cache,
|
||||
&mut self.note_cache,
|
||||
self.poster.to_filled(),
|
||||
)
|
||||
.ui(&txn, ui);
|
||||
}
|
||||
}
|
||||
|
||||
impl Preview for PostView<'_> {
|
||||
type Prev = PostPreview;
|
||||
|
||||
fn preview(_cfg: PreviewConfig) -> Self::Prev {
|
||||
PostPreview::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
64
crates/notedeck_columns/src/ui/note/quote_repost.rs
Normal file
64
crates/notedeck_columns/src/ui/note/quote_repost.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use enostr::{FilledKeypair, NoteId};
|
||||
use nostrdb::Ndb;
|
||||
|
||||
use crate::{draft::Draft, imgcache::ImageCache, notecache::NoteCache, ui};
|
||||
|
||||
use super::{PostResponse, PostType};
|
||||
|
||||
pub struct QuoteRepostView<'a> {
|
||||
ndb: &'a Ndb,
|
||||
poster: FilledKeypair<'a>,
|
||||
note_cache: &'a mut NoteCache,
|
||||
img_cache: &'a mut ImageCache,
|
||||
draft: &'a mut Draft,
|
||||
quoting_note: &'a nostrdb::Note<'a>,
|
||||
id_source: Option<egui::Id>,
|
||||
}
|
||||
|
||||
impl<'a> QuoteRepostView<'a> {
|
||||
pub fn new(
|
||||
ndb: &'a Ndb,
|
||||
poster: FilledKeypair<'a>,
|
||||
note_cache: &'a mut NoteCache,
|
||||
img_cache: &'a mut ImageCache,
|
||||
draft: &'a mut Draft,
|
||||
quoting_note: &'a nostrdb::Note<'a>,
|
||||
) -> Self {
|
||||
let id_source: Option<egui::Id> = None;
|
||||
QuoteRepostView {
|
||||
ndb,
|
||||
poster,
|
||||
note_cache,
|
||||
img_cache,
|
||||
draft,
|
||||
quoting_note,
|
||||
id_source,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn show(&mut self, ui: &mut egui::Ui) -> PostResponse {
|
||||
let id = self.id();
|
||||
let quoting_note_id = self.quoting_note.id();
|
||||
|
||||
ui::PostView::new(
|
||||
self.ndb,
|
||||
self.draft,
|
||||
PostType::Quote(NoteId::new(quoting_note_id.to_owned())),
|
||||
self.img_cache,
|
||||
self.note_cache,
|
||||
self.poster,
|
||||
)
|
||||
.id_source(id)
|
||||
.ui(self.quoting_note.txn().unwrap(), ui)
|
||||
}
|
||||
|
||||
pub fn id_source(mut self, id: egui::Id) -> Self {
|
||||
self.id_source = Some(id);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn id(&self) -> egui::Id {
|
||||
self.id_source
|
||||
.unwrap_or_else(|| egui::Id::new("quote-repost-view"))
|
||||
}
|
||||
}
|
||||
135
crates/notedeck_columns/src/ui/note/reply.rs
Normal file
135
crates/notedeck_columns/src/ui/note/reply.rs
Normal file
@@ -0,0 +1,135 @@
|
||||
use crate::draft::Draft;
|
||||
use crate::imgcache::ImageCache;
|
||||
use crate::notecache::NoteCache;
|
||||
use crate::ui;
|
||||
use crate::ui::note::{PostResponse, PostType};
|
||||
use enostr::{FilledKeypair, NoteId};
|
||||
use nostrdb::Ndb;
|
||||
|
||||
pub struct PostReplyView<'a> {
|
||||
ndb: &'a Ndb,
|
||||
poster: FilledKeypair<'a>,
|
||||
note_cache: &'a mut NoteCache,
|
||||
img_cache: &'a mut ImageCache,
|
||||
draft: &'a mut Draft,
|
||||
note: &'a nostrdb::Note<'a>,
|
||||
id_source: Option<egui::Id>,
|
||||
}
|
||||
|
||||
impl<'a> PostReplyView<'a> {
|
||||
pub fn new(
|
||||
ndb: &'a Ndb,
|
||||
poster: FilledKeypair<'a>,
|
||||
draft: &'a mut Draft,
|
||||
note_cache: &'a mut NoteCache,
|
||||
img_cache: &'a mut ImageCache,
|
||||
note: &'a nostrdb::Note<'a>,
|
||||
) -> Self {
|
||||
let id_source: Option<egui::Id> = None;
|
||||
PostReplyView {
|
||||
ndb,
|
||||
poster,
|
||||
draft,
|
||||
note,
|
||||
note_cache,
|
||||
img_cache,
|
||||
id_source,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn id_source(mut self, id: egui::Id) -> Self {
|
||||
self.id_source = Some(id);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn id(&self) -> egui::Id {
|
||||
self.id_source
|
||||
.unwrap_or_else(|| egui::Id::new("post-reply-view"))
|
||||
}
|
||||
|
||||
pub fn show(&mut self, ui: &mut egui::Ui) -> PostResponse {
|
||||
ui.vertical(|ui| {
|
||||
let avail_rect = ui.available_rect_before_wrap();
|
||||
|
||||
// This is the offset of the post view's pfp. We use this
|
||||
// to indent things so that the reply line is aligned
|
||||
let pfp_offset = ui::PostView::outer_margin()
|
||||
+ ui::PostView::inner_margin()
|
||||
+ ui::ProfilePic::small_size() / 2.0;
|
||||
|
||||
let note_offset = pfp_offset
|
||||
- ui::ProfilePic::medium_size() / 2.0
|
||||
- ui::NoteView::expand_size() / 2.0;
|
||||
|
||||
egui::Frame::none()
|
||||
.outer_margin(egui::Margin::same(note_offset))
|
||||
.show(ui, |ui| {
|
||||
ui::NoteView::new(self.ndb, self.note_cache, self.img_cache, self.note)
|
||||
.actionbar(false)
|
||||
.medium_pfp(true)
|
||||
.options_button(true)
|
||||
.show(ui);
|
||||
});
|
||||
|
||||
let id = self.id();
|
||||
let replying_to = self.note.id();
|
||||
let rect_before_post = ui.min_rect();
|
||||
|
||||
let post_response = {
|
||||
ui::PostView::new(
|
||||
self.ndb,
|
||||
self.draft,
|
||||
PostType::Reply(NoteId::new(*replying_to)),
|
||||
self.img_cache,
|
||||
self.note_cache,
|
||||
self.poster,
|
||||
)
|
||||
.id_source(id)
|
||||
.ui(self.note.txn().unwrap(), ui)
|
||||
};
|
||||
|
||||
//
|
||||
// reply line
|
||||
//
|
||||
|
||||
// Position and draw the reply line
|
||||
let mut rect = ui.min_rect();
|
||||
|
||||
// Position the line right above the poster's profile pic in
|
||||
// the post box. Use the PostView's margin values to
|
||||
// determine this offset.
|
||||
rect.min.x = avail_rect.min.x + pfp_offset;
|
||||
|
||||
// honestly don't know what the fuck I'm doing here. just trying
|
||||
// to get the line under the profile picture
|
||||
rect.min.y = avail_rect.min.y
|
||||
+ (ui::ProfilePic::medium_size() / 2.0
|
||||
+ ui::ProfilePic::medium_size()
|
||||
+ ui::NoteView::expand_size() * 2.0)
|
||||
+ 1.0;
|
||||
|
||||
// For some reason we need to nudge the reply line's height a
|
||||
// few more pixels?
|
||||
let nudge = if post_response.edit_response.has_focus() {
|
||||
// we nudge by one less pixel if focused, otherwise it
|
||||
// overlaps the focused PostView purple border color
|
||||
2.0
|
||||
} else {
|
||||
// we have to nudge by one more pixel when not focused
|
||||
// otherwise it looks like there's a gap(?)
|
||||
3.0
|
||||
};
|
||||
|
||||
rect.max.y = rect_before_post.max.y + ui::PostView::outer_margin() + nudge;
|
||||
|
||||
ui.painter().vline(
|
||||
rect.left(),
|
||||
rect.y_range(),
|
||||
ui.visuals().widgets.noninteractive.bg_stroke,
|
||||
);
|
||||
|
||||
post_response
|
||||
})
|
||||
.inner
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user