selectable text option

Add a selectable text option to various note views. We don't want
selection events to interfere with back drag, so this is the first step
toward ensure back drag works.

Vertical scrollviews also interfere with back drag, so we'll still need
a way to compose gestures. It's not clear if this is currently possibly
with egui (union of responses somehow maybe?)

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2024-08-12 13:26:29 -07:00
parent f769ddad09
commit 579b47fc40
7 changed files with 160 additions and 90 deletions

View File

@@ -110,6 +110,7 @@ fn render_note_contents(
#[cfg(feature = "profiling")]
puffin::profile_function!();
let selectable = options.has_selectable_text();
let images: Vec<String> = vec![];
let mut inline_note: Option<(&[u8; 32], &str)> = None;
@@ -173,7 +174,7 @@ fn render_note_contents(
BlockType::Text => {
#[cfg(feature = "profiling")]
puffin::profile_scope!("text contents");
ui.label(block.as_str());
ui.add(egui::Label::new(block.as_str()).selectable(selectable));
}
_ => {

View File

@@ -33,11 +33,17 @@ fn reply_desc(ui: &mut egui::Ui, txn: &Transaction, note_reply: &NoteReply, app:
#[cfg(feature = "profiling")]
puffin::profile_function!();
ui.add(Label::new(
RichText::new("replying to")
.size(10.0)
.color(colors::GRAY_SECONDARY),
));
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
@@ -48,55 +54,91 @@ fn reply_desc(ui: &mut egui::Ui, txn: &Transaction, note_reply: &NoteReply, app:
let reply_note = if let Ok(reply_note) = app.ndb.get_note_by_id(txn, reply.id) {
reply_note
} else {
ui.add(Label::new(
RichText::new("a note")
.size(10.0)
.color(colors::GRAY_SECONDARY),
));
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(app, txn, reply_note.pubkey()).size(10.0));
ui.add(Label::new(
RichText::new("'s note")
.size(10.0)
.color(colors::GRAY_SECONDARY),
));
ui.add(
ui::Mention::new(app, 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) = app.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(app, txn, reply_note.pubkey()).size(10.0));
ui.add(Label::new(
RichText::new("'s note")
.size(10.0)
.color(colors::GRAY_SECONDARY),
));
ui.add(
ui::Mention::new(app, 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(app, txn, reply_note.pubkey()).size(10.0));
ui.add(Label::new(
RichText::new("in").size(10.0).color(colors::GRAY_SECONDARY),
));
ui.add(ui::Mention::new(app, txn, root_note.pubkey()).size(10.0));
ui.add(Label::new(
RichText::new("'s thread")
.size(10.0)
.color(colors::GRAY_SECONDARY),
));
ui.add(
ui::Mention::new(app, 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(app, 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(app, txn, reply_note.pubkey()).size(10.0));
ui.add(Label::new(
RichText::new("in someone's thread")
.size(10.0)
.color(colors::GRAY_SECONDARY),
));
ui.add(
ui::Mention::new(app, 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),
);
}
}
}
@@ -127,6 +169,11 @@ impl<'a> NoteView<'a> {
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

View File

@@ -6,20 +6,45 @@ bitflags! {
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct NoteOptions: u32 {
const actionbar = 0b00000001;
const note_previews = 0b00000010;
const small_pfp = 0b00000100;
const medium_pfp = 0b00001000;
const wide = 0b00010000;
const actionbar = 0b00000001;
const note_previews = 0b00000010;
const small_pfp = 0b00000100;
const medium_pfp = 0b00001000;
const wide = 0b00010000;
const selectable_text = 0b00100000;
}
}
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_actionbar, actionbar);
#[inline]
pub fn has_actionbar(self) -> bool {
(self & NoteOptions::actionbar) == NoteOptions::actionbar
}
#[inline]
pub fn has_selectable_text(self) -> bool {
(self & NoteOptions::selectable_text) == NoteOptions::selectable_text
}
#[inline]
pub fn has_note_previews(self) -> bool {
(self & NoteOptions::note_previews) == NoteOptions::note_previews
@@ -58,40 +83,4 @@ impl NoteOptions {
*self &= !NoteOptions::wide;
}
}
#[inline]
pub fn set_small_pfp(&mut self, enable: bool) {
if enable {
*self |= NoteOptions::small_pfp;
} else {
*self &= !NoteOptions::small_pfp;
}
}
#[inline]
pub fn set_medium_pfp(&mut self, enable: bool) {
if enable {
*self |= NoteOptions::medium_pfp;
} else {
*self &= !NoteOptions::medium_pfp;
}
}
#[inline]
pub fn set_note_previews(&mut self, enable: bool) {
if enable {
*self |= NoteOptions::note_previews;
} else {
*self &= !NoteOptions::note_previews;
}
}
#[inline]
pub fn set_actionbar(&mut self, enable: bool) {
if enable {
*self |= NoteOptions::actionbar;
} else {
*self &= !NoteOptions::actionbar;
}
}
}