commit 99b163da7e4cf1a9580b983e2c83236553c3cf6a
parent 3f26ba1af7ccc3ce9ca508eafd4fa1917451fd5e
Author: William Casarin <jb55@jb55.com>
Date: Fri, 12 Apr 2024 15:09:07 -0700
textmode: fix coloring of abbreviated names
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
2 files changed, 34 insertions(+), 19 deletions(-)
diff --git a/src/app.rs b/src/app.rs
@@ -629,13 +629,11 @@ fn render_irc_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| {
- ui.set_clip_rect(rect);
render_reltime(ui, note_cache, 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.set_clip_rect(rect);
ui.add(
Username::new(profile.as_ref().ok(), note.pubkey())
.abbreviated(8)
diff --git a/src/widgets/username.rs b/src/widgets/username.rs
@@ -1,5 +1,5 @@
use crate::fonts::NamedFontFamily;
-use egui::{Color32, Label, RichText, Widget};
+use egui::{Color32, RichText, Widget};
use nostrdb::ProfileRecord;
pub struct Username<'a> {
@@ -35,15 +35,16 @@ impl<'a> Username<'a> {
impl<'a> Widget for Username<'a> {
fn ui(self, ui: &mut egui::Ui) -> egui::Response {
ui.horizontal(|ui| {
- //ui.spacing_mut().item_spacing.x = 0.0;
+ ui.spacing_mut().item_spacing.x = 0.0;
+
+ let color = if self.pk_colored {
+ Some(pk_color(self.pk))
+ } else {
+ None
+ };
+
if let Some(profile) = self.profile {
if let Some(prof) = profile.record.profile() {
- let color = if self.pk_colored {
- Some(pk_color(self.pk))
- } else {
- None
- };
-
if prof.display_name().is_some() && prof.display_name().unwrap() != "" {
ui_abbreviate_name(ui, prof.display_name().unwrap(), self.abbrev, color);
} else if let Some(name) = prof.name() {
@@ -51,24 +52,40 @@ impl<'a> Widget for Username<'a> {
}
}
} else {
- ui.strong("nostrich");
+ let mut txt = RichText::new("nostrich").family(NamedFontFamily::Medium.as_family());
+ if let Some(col) = color {
+ txt = txt.color(col)
+ }
+ ui.label(txt);
}
})
.response
}
}
+fn colored_name(name: &str, color: Option<Color32>) -> RichText {
+ let mut txt = RichText::new(name).family(NamedFontFamily::Medium.as_family());
+
+ if let Some(color) = color {
+ txt = txt.color(color);
+ }
+
+ txt
+}
+
fn ui_abbreviate_name(ui: &mut egui::Ui, name: &str, len: usize, color: Option<Color32>) {
- if name.len() > len {
+ let should_abbrev = name.len() > len;
+ let name = if should_abbrev {
let closest = crate::abbrev::floor_char_boundary(name, len);
- ui.strong(&name[..closest]);
- ui.strong("...");
+ &name[..closest]
} else {
- let mut txt = RichText::new(name).family(NamedFontFamily::Medium.as_family());
- if let Some(c) = color {
- txt = txt.color(c);
- }
- ui.add(Label::new(txt));
+ name
+ };
+
+ ui.label(colored_name(name, color));
+
+ if should_abbrev {
+ ui.label(colored_name("...", color));
}
}