Skip to content

Commit

Permalink
note: don't allow nested note previews
Browse files Browse the repository at this point in the history
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Apr 15, 2024
1 parent 6f2aa56 commit a8185d9
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 29 deletions.
25 changes: 21 additions & 4 deletions src/ui/note/contents.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::ui::note::NoteOptions;
use crate::{colors, ui, Damus};
use egui::{Color32, Hyperlink, Image, RichText};
use nostrdb::{BlockType, Mention, Note, NoteKey, Transaction};
Expand All @@ -8,6 +9,7 @@ pub struct NoteContents<'a> {
txn: &'a Transaction,
note: &'a Note<'a>,
note_key: NoteKey,
options: NoteOptions,
}

impl<'a> NoteContents<'a> {
Expand All @@ -16,19 +18,29 @@ impl<'a> NoteContents<'a> {
txn: &'a Transaction,
note: &'a Note,
note_key: NoteKey,
options: ui::note::NoteOptions,
) -> Self {
NoteContents {
damus,
txn,
note,
note_key,
options,
}
}
}

impl egui::Widget for NoteContents<'_> {
fn ui(self, ui: &mut egui::Ui) -> egui::Response {
render_note_contents(ui, self.damus, self.txn, self.note, self.note_key).response
render_note_contents(
ui,
self.damus,
self.txn,
self.note,
self.note_key,
self.options,
)
.response
}
}

Expand Down Expand Up @@ -62,7 +74,11 @@ fn render_note_preview(
*/
};

ui.add(ui::Note::new(app, &note).actionbar(false))
ui.add(
ui::Note::new(app, &note)
.actionbar(false)
.note_previews(false),
)
}

fn render_note_contents(
Expand All @@ -71,6 +87,7 @@ fn render_note_contents(
txn: &Transaction,
note: &Note,
note_key: NoteKey,
options: NoteOptions,
) -> egui::InnerResponse<()> {
#[cfg(feature = "profiling")]
puffin::profile_function!();
Expand Down Expand Up @@ -105,11 +122,11 @@ fn render_note_contents(
}
}

Mention::Note(note) => {
Mention::Note(note) if options.has_note_previews() => {
inline_note = Some((note.id(), block.as_str()));
}

Mention::Event(note) => {
Mention::Event(note) if options.has_note_previews() => {
inline_note = Some((note.id(), block.as_str()));
}

Expand Down
54 changes: 29 additions & 25 deletions src/ui/note/mod.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
pub mod contents;
pub mod options;

pub use contents::NoteContents;
pub use options::NoteOptions;

use crate::{ui, Damus};
use bitflags::bitflags;
use egui::{Color32, Label, RichText, Sense, TextureHandle, Vec2};

bitflags! {
// Attributes can be applied to flags types
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct NoteFlags: u32 {
const actionbar = 0b00000001;
const note_previews = 0b00000010;
}
}

pub struct Note<'a> {
app: &'a mut Damus,
note: &'a nostrdb::Note<'a>,
flags: NoteFlags,
flags: NoteOptions,
}

impl<'a> egui::Widget for Note<'a> {
Expand All @@ -33,24 +25,28 @@ impl<'a> egui::Widget for Note<'a> {

impl<'a> Note<'a> {
pub fn new(app: &'a mut Damus, note: &'a nostrdb::Note<'a>) -> Self {
let flags = NoteFlags::actionbar;
let flags = NoteOptions::actionbar | NoteOptions::note_previews;
Note { app, note, flags }
}

#[inline]
pub fn has_actionbar(&self) -> bool {
(self.flags & NoteFlags::actionbar) == NoteFlags::actionbar
pub fn actionbar(mut self, enable: bool) -> Self {
self.options_mut().set_actionbar(enable);
self
}

pub fn actionbar(mut self, enable: bool) -> Self {
if enable {
self.flags = self.flags | NoteFlags::actionbar;
} else {
self.flags = self.flags & !NoteFlags::actionbar;
}
pub fn note_previews(mut self, enable: bool) -> Self {
self.options_mut().set_note_previews(enable);
self
}

pub fn options(&self) -> NoteOptions {
self.flags
}

pub fn options_mut(&mut self) -> &mut NoteOptions {
&mut self.flags
}

fn textmode_ui(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");
Expand Down Expand Up @@ -80,7 +76,9 @@ impl<'a> Note<'a> {
)
});

ui.add(NoteContents::new(self.app, txn, self.note, note_key));
ui.add(NoteContents::new(
self.app, txn, self.note, note_key, self.flags,
));
});
})
.response
Expand Down Expand Up @@ -130,9 +128,15 @@ impl<'a> Note<'a> {
render_reltime(ui, note_cache, true);
});

ui.add(NoteContents::new(self.app, txn, self.note, note_key));
ui.add(NoteContents::new(
self.app,
txn,
self.note,
note_key,
self.options(),
));

if self.has_actionbar() {
if self.options().has_actionbar() {
render_note_actionbar(ui);
}

Expand Down
41 changes: 41 additions & 0 deletions src/ui/note/options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use bitflags::bitflags;

bitflags! {
// Attributes can be applied to flags types
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct NoteOptions: u32 {
const actionbar = 0b00000001;
const note_previews = 0b00000010;
}
}

impl NoteOptions {
#[inline]
pub fn has_actionbar(self) -> bool {
(self & NoteOptions::actionbar) == NoteOptions::actionbar
}

#[inline]
pub fn has_note_previews(self) -> bool {
(self & NoteOptions::note_previews) == NoteOptions::note_previews
}

#[inline]
pub fn set_note_previews(&mut self, enable: bool) {
if enable {
*self = *self | NoteOptions::note_previews;
} else {
*self = *self & !NoteOptions::note_previews;
}
}

#[inline]
pub fn set_actionbar(&mut self, enable: bool) {
if enable {
*self = *self | NoteOptions::actionbar;
} else {
*self = *self & !NoteOptions::actionbar;
}
}
}

0 comments on commit a8185d9

Please sign in to comment.