ui: add note truncation

Truncate notes by default. We still need a show more button though
This commit is contained in:
William Casarin
2025-04-22 10:48:11 -07:00
parent e4658df847
commit c4084a1fb5
4 changed files with 63 additions and 19 deletions

View File

@@ -75,18 +75,24 @@ pub fn render_timeline_route(
} }
} }
TimelineKind::Thread(id) => ui::ThreadView::new( TimelineKind::Thread(id) => {
timeline_cache, // don't truncate thread notes for now, since they are
unknown_ids, // default truncated everywher eelse
id.selected_or_root(), note_options.set_truncate(false);
note_options,
&accounts.mutefun(), ui::ThreadView::new(
note_context, timeline_cache,
&accounts.get_selected_account().map(|a| (&a.key).into()), unknown_ids,
) id.selected_or_root(),
.id_source(egui::Id::new(("threadscroll", col))) note_options,
.ui(ui) &accounts.mutefun(),
.map(Into::into), note_context,
&accounts.get_selected_account().map(|a| (&a.key).into()),
)
.id_source(egui::Id::new(("threadscroll", col)))
.ui(ui)
.map(Into::into)
}
} }
} }

View File

@@ -119,6 +119,10 @@ pub fn render_note_contents(
let hide_media = options.has_hide_media(); let hide_media = options.has_hide_media();
let link_color = ui.visuals().hyperlink_color; let link_color = ui.visuals().hyperlink_color;
// The current length of the rendered blocks. Used in trucation logic
let mut current_len: usize = 0;
let truncate_len = 280;
if !options.has_is_preview() { if !options.has_is_preview() {
// need this for the rect to take the full width of the column // need this for the rect to take the full width of the column
let _ = ui.allocate_at_least(egui::vec2(ui.available_width(), 0.0), egui::Sense::click()); let _ = ui.allocate_at_least(egui::vec2(ui.available_width(), 0.0), egui::Sense::click());
@@ -210,18 +214,39 @@ pub fn render_note_contents(
} }
BlockType::Text => { BlockType::Text => {
// truncate logic
let mut truncate = false;
let block_str = if options.has_truncate()
&& (current_len + block.as_str().len() > truncate_len)
{
truncate = true;
// The current block goes over the truncate length,
// we'll need to truncate this block
let block_str = block.as_str();
let closest = notedeck::abbrev::floor_char_boundary(
block_str,
truncate_len - current_len,
);
&(block_str[..closest].to_string() + "")
} else {
let block_str = block.as_str();
current_len += block_str.len();
block_str
};
if options.has_scramble_text() { if options.has_scramble_text() {
ui.add( ui.add(
egui::Label::new(rot13(block.as_str())) egui::Label::new(rot13(block_str))
.wrap() .wrap()
.selectable(selectable), .selectable(selectable),
); );
} else { } else {
ui.add( ui.add(egui::Label::new(block_str).wrap().selectable(selectable));
egui::Label::new(block.as_str()) }
.wrap()
.selectable(selectable), // don't render any more blocks
); if truncate {
break;
} }
} }

View File

@@ -114,6 +114,11 @@ impl<'a, 'd> NoteView<'a, 'd> {
self self
} }
pub fn truncate(mut self, enable: bool) -> Self {
self.options_mut().set_truncate(enable);
self
}
pub fn small_pfp(mut self, enable: bool) -> Self { pub fn small_pfp(mut self, enable: bool) -> Self {
self.options_mut().set_small_pfp(enable); self.options_mut().set_small_pfp(enable);
self self

View File

@@ -21,12 +21,19 @@ bitflags! {
/// Whether the current note is a preview /// Whether the current note is a preview
const is_preview = 0b0000010000000000; const is_preview = 0b0000010000000000;
/// Is the content truncated? If the length is over a certain size it
/// will end with a ... and a "Show more" button.
const truncate = 0b0000100000000000;
} }
} }
impl Default for NoteOptions { impl Default for NoteOptions {
fn default() -> NoteOptions { fn default() -> NoteOptions {
NoteOptions::options_button | NoteOptions::note_previews | NoteOptions::actionbar NoteOptions::options_button
| NoteOptions::note_previews
| NoteOptions::actionbar
| NoteOptions::truncate
} }
} }
@@ -60,6 +67,7 @@ impl NoteOptions {
create_bit_methods!(set_hide_media, has_hide_media, hide_media); create_bit_methods!(set_hide_media, has_hide_media, hide_media);
create_bit_methods!(set_scramble_text, has_scramble_text, scramble_text); create_bit_methods!(set_scramble_text, has_scramble_text, scramble_text);
create_bit_methods!(set_is_preview, has_is_preview, is_preview); create_bit_methods!(set_is_preview, has_is_preview, is_preview);
create_bit_methods!(set_truncate, has_truncate, truncate);
pub fn new(is_universe_timeline: bool) -> Self { pub fn new(is_universe_timeline: bool) -> Self {
let mut options = NoteOptions::default(); let mut options = NoteOptions::default();