Cut npub rows instead of Pango's middle ellipsis

Pango pads an ellipsized line to fill its target width by widening the
ellipsis glyph, so ELLIPSIZE_MIDDLE leaves a gap between the … and the tail
on a space-less token like an npub. Cut head…tail ourselves instead
(plain::shorten_npub / shorten_uri) and keep the row's own end ellipsis as
a backstop. The full value rides along as a plain tooltip, and the approval
dialog's "Signing as" npub is no longer selectable — a shortened label would
only copy the cut.
This commit is contained in:
raucao committed 2026-11-26 13:35:10 +04:00
1 parent 5b5edfd6f4
commit 5429418b48
6 files changed
+81 -93

No files matched your search

+25 -22
View File
@@ -586,28 +586,31 @@ UI lives in `data/resources/ui/*.blp` (Blueprint) and is compiled to `.ui` by
`pack_end` ends up rightmost. The main window puts every control on the left
with `pack_start`, laid out `[menu][identity switcher][lock] …` in call
order. Remember the prepend if a control is ever moved back to `pack_end`.
13. An `AdwActionRow`'s `subtitle-lines` decides both the line count **and** the
ellipsis mode: `set_subtitle_lines(1)` makes libadwaita set the subtitle
label's `PANGO_ELLIPSIZE_END`, and the row exposes no setter for the mode. A
long subtitle (the `bunker://` connection string) therefore truncates at the
end, and pre-shortening it with `plain::shorten` only moves the threshold —
it is a character count, so the row still end-ellipsizes as soon as the font
or window width differs. `plain::middle_ellipsis(row)` finds the subtitle
label in the row's own widget tree and sets `MIDDLE`; `set_subtitle` does not
reset it, so once at construction is enough. The row then elides the middle
at whatever width it gets. Every npub byline/row uses it (identities, Apps
details, "Signing as" in the Connect and approval dialogs, the approval
dialog's client key and peer) — and a wrapped npub
is also what made the "Connect an app" dialog clip its bottom: the scroller
measures natural height at the *unwrapped* width (trap 15), so a long token
must stay on one line.
14. `AdwExpanderRow` is **not** an `AdwActionRow`: both derive from
`AdwPreferencesRow`, but neither is a subclass of the other, and the two do
not share a `subtitle` accessor trait. `plain::middle_ellipsis` therefore
takes an `&AdwActionRow`; for an expander's subtitle use
`plain::middle_ellipsis_text(&row, &subtitle)`, which finds the label by its
text. `set_subtitle_lines(1)` alone there means end-ellipsis, which is fine
for a short byline but not for a long token.
13. **Never use `PANGO_ELLIPSIZE_MIDDLE` for a long, space-less token** (an npub,
a `bunker://` URI). Pango pads an ellipsized line to fill its target width by
widening the *ellipsis glyph* (`pango/ellipsize.c` `fixup_ellipsis_run`,
commit `c754c4d6` / pango#24, "so ellipsized text doesn't vibrate"). With
`END` that space lands at the line's right edge (invisible); with `MIDDLE` it
lands between the `…` and the tail — a gap of up to one glyph (measured
12 px on a rendered npub). Do the cut ourselves instead:
`plain::shorten_npub` / `plain::shorten_uri` return a fixed `head…tail`
string, and the row's `set_subtitle_lines(1)` stays as an *end*-ellipsis
backstop (whose padding is invisible). The cut lengths are sized to fit the
narrowest real row — measured under Broadway with the real fonts — so the
tail survives; re-measure if the fonts or row widths change. Every npub
byline/row uses it (identities, Apps details, "Signing as" in the Connect and
approval dialogs, the approval dialog's client key and peer), plus the
Connect dialog's connection string. Note a wrapped npub also made the
"Connect an app" dialog clip its bottom: the scroller measures natural height
at the *unwrapped* width (trap 15), so a long token must stay on one line.
14. A shortened subtitle hides the full value, so carry it as a plain
`tooltip_text` (never `tooltip_markup` — it is still attacker-supplied text
for a client key). The approval dialog's "Signing as" npub is a plain
`GtkLabel`, deliberately **not** `selectable`: a shortened label would only
copy the cut. The identities window still offers "Copy public key".
`AdwExpanderRow` is not an `AdwActionRow`, but that no longer matters: its
byline is shortened like any other subtitle instead of reaching into the
label to change its ellipsis mode.
15. A `GtkScrolledWindow`'s **natural height is 0** unless
`propagate_natural_height` is set, and `AdwDialog` sizes itself to its child's
natural height. So a dialog whose child is a scroller collapses to just its
+18 -14
View File
@@ -481,12 +481,10 @@ fn details(question: &Question) -> gtk::Box {
fn more_details(question: &Question) -> gtk::ListBox {
let client = &question.client;
let row = plain::expander("Details", "Key and request metadata");
let client_key = plain::row(
"Client key",
&client.public_key.to_bech32().unwrap_or_default(),
);
let client_npub = client.public_key.to_bech32().unwrap_or_default();
let client_key = plain::row("Client key", &plain::shorten_npub(&client_npub));
client_key.set_subtitle_lines(1);
plain::middle_ellipsis(&client_key);
client_key.set_tooltip_text(Some(&client_npub));
row.add_row(&client_key);
if let Some(url) = &client.url {
row.add_row(&plain::row("Declared URL", url));
@@ -506,9 +504,9 @@ fn more_details(question: &Question) -> gtk::ListBox {
}
Request::Encrypt { method, peer, .. } | Request::Decrypt { method, peer, .. } => {
row.add_row(&plain::row("Method", method));
let peer_row = plain::row("Peer", peer);
let peer_row = plain::row("Peer", &plain::shorten_npub(peer));
peer_row.set_subtitle_lines(1);
plain::middle_ellipsis(&peer_row);
peer_row.set_tooltip_text(Some(peer));
row.add_row(&peer_row);
}
}
@@ -538,13 +536,19 @@ fn identity_field(identity: &crate::signer::approval::SigningKey) -> gtk::Box {
.as_deref()
.map(str::trim)
.filter(|name| !name.is_empty());
// The npub is a long token: one line, ellipsized in the middle like every
// other npub, rather than wrapping. A wrapping label reports the full npub
// width and pushes the dialog wider; with the ellipsis, `plain`'s
// `max_width_chars` cap takes effect and the dialog stays at 480.
let npub_label = plain(&npub);
npub_label.set_wrap(false);
npub_label.set_ellipsize(gtk::pango::EllipsizeMode::Middle);
// The npub is a long token: cut to `head…tail` on one line. Not Pango's
// middle ellipsis, which pads the `…` to fill the width and would widen the
// dialog. The label is **not** selectable — a shortened label would only
// copy the cut — so the full npub is the tooltip; the identities window
// still offers "Copy public key".
let npub_label = gtk::Label::builder()
.label(plain::shorten_npub(&npub))
.use_markup(false)
.xalign(0.0)
.halign(gtk::Align::Start)
.ellipsize(gtk::pango::EllipsizeMode::End)
.build();
npub_label.set_tooltip_text(Some(&npub));
match name {
Some(name) => {
value.append(&plain(name));
+9 -8
View File
@@ -244,16 +244,17 @@ impl Ui {
// client-supplied URL is display data only — it proves nothing.
let byline = match app.url.as_deref() {
Some(url) => declared_host(url),
None => npub.clone(),
None => plain::shorten_npub(&npub),
};
let row = plain::expander(&name, &byline);
row.set_subtitle_lines(1);
// An `AdwExpanderRow` is not an `AdwActionRow`, so it needs the
// text-based helper. The full npub is kept; the label ellipsizes it.
plain::middle_ellipsis_text(&row, &byline);
let public_key = plain::row("Public key", &npub);
// When the byline is a shortened npub, keep the full key in a tooltip.
if app.url.is_none() {
row.set_tooltip_text(Some(&npub));
}
let public_key = plain::row("Public key", &plain::shorten_npub(&npub));
public_key.set_subtitle_lines(1);
plain::middle_ellipsis(&public_key);
public_key.set_tooltip_text(Some(&npub));
row.add_row(&public_key);
if let Some(url) = &app.url {
row.add_row(&plain::row("Declared URL", url));
@@ -272,9 +273,9 @@ impl Ui {
"paired before identities existed".to_string(),
),
};
let signing_as_row = plain::row(&signing_as, &signing_npub);
let signing_as_row = plain::row(&signing_as, &plain::shorten_npub(&signing_npub));
signing_as_row.set_subtitle_lines(1);
plain::middle_ellipsis(&signing_as_row);
signing_as_row.set_tooltip_text(Some(&signing_npub));
row.add_row(&signing_as_row);
if app.policy.kinds.is_empty() && app.policy.methods.is_empty() {
+10 -11
View File
@@ -75,11 +75,11 @@ impl ConnectDialog {
// --- The signer-initiated direction: hidden until asked for. ---
let uri = plain::row("Connection string", "—");
// The connection string is long and stays on one line, ellipsized in
// the middle: the head and the secret tail are what identify it, and
// the middle is dropped as the row narrows.
// The connection string is long and stays on one line, cut to
// `head…tail`: the head and the secret tail are what identify it. (Not
// Pango's middle ellipsis — see `plain::shorten_uri`.) The full string
// is what the copy button and the QR code use.
uri.set_subtitle_lines(1);
plain::middle_ellipsis(&uri);
let copy = gtk::Button::builder()
.icon_name("edit-copy-symbolic")
.tooltip_text("Copy connection string")
@@ -267,17 +267,16 @@ impl ConnectDialog {
"Signing as {}",
self.state.identity_label(&identity)
));
self.signing_as.set_subtitle(&npub);
// Cut for the row; the full npub stays reachable as a tooltip.
self.signing_as.set_subtitle(&plain::shorten_npub(&npub));
self.signing_as.set_tooltip_text(Some(&npub));
}
None => {
self.signing_as.set_title("Signing as");
self.signing_as.set_subtitle("Add an identity key first.");
self.signing_as.set_tooltip_text(None);
}
}
// Re-applied each time: the subtitle changes, and this is what keeps the
// npub on one line (also what keeps the dialog from clipping its
// bottom — a wrapped npub is taller than the natural-height measure).
plain::middle_ellipsis(&self.signing_as);
match self.runner.status() {
Status::Stopped => {
@@ -307,8 +306,8 @@ impl ConnectDialog {
self.reveal
.set_subtitle("The connection string embeds the pairing secret.");
// Display only: the copy button and the QR code still use the
// full string. The row ellipsizes it in the middle.
self.uri.set_subtitle(signer.bunker_uri());
// full string. The row shows the head and the secret tail.
self.uri.set_subtitle(&plain::shorten_uri(signer.bunker_uri()));
if self.reveal.is_expanded() {
self.show_qr(signer.bunker_uri());
}
+5 -4
View File
@@ -181,14 +181,15 @@ fn key_row(
let npub = public_key
.map(|key| key.to_bech32().unwrap_or_default())
.unwrap_or_else(|| hex.to_string());
let short_npub = plain::shorten_npub(&npub);
let subtitle = match profile {
Some(profile) => format!("{profile} · {npub}"),
None => npub.clone(),
Some(profile) => format!("{profile} · {short_npub}"),
None => short_npub.clone(),
};
let row = plain::row(name, &subtitle);
// The npub is a long token: one line, ellipsized in the middle.
// One line, cut to `head…tail`; the full npub stays reachable as a tooltip.
row.set_subtitle_lines(1);
plain::middle_ellipsis(&row);
row.set_tooltip_text(Some(&npub));
// Initials avatar, from the kind-0 name when there is one.
row.add_prefix(&adw::Avatar::new(32, Some(profile.unwrap_or(name)), true));
+14 -34
View File
@@ -65,44 +65,24 @@ pub fn expander(title: &str, subtitle: &str) -> adw::ExpanderRow {
row
}
/// Make an `ActionRow`'s subtitle ellipsize in the middle instead of at the end.
/// A fixed `head…tail` cut for an npub shown in a one-line row.
///
/// `set_subtitle_lines(1)` is what keeps the subtitle on one line, but
/// libadwaita then hard-codes `PANGO_ELLIPSIZE_END`, and the row exposes no
/// setter for the mode. The subtitle label is reachable in the row's own widget
/// tree; `set_subtitle` does not reset the mode, so setting it once is enough. A
/// row whose subtitle label cannot be found is left as it is.
pub fn middle_ellipsis(row: &adw::ActionRow) {
if let Some(subtitle) = row.subtitle() {
middle_ellipsis_text(row, subtitle.as_str());
}
/// Deliberately **not** Pango's `ELLIPSIZE_MIDDLE`: Pango pads the ellipsized
/// line to fill its width by widening the ellipsis glyph (pango/ellipsize.c
/// `fixup_ellipsis_run`), and for a space-less token that padding shows up as a
/// gap between the `…` and the tail. Sized to fit the narrowest row we render an
/// npub in; the row's own `set_subtitle_lines(1)` stays as an end-ellipsis
/// backstop for a narrower one.
pub fn shorten_npub(value: &str) -> String {
shorten(value, 15, 15)
}
/// [`middle_ellipsis`] for a row that is not an `AdwActionRow`.
/// The `bunker://` connection string, shortened for its one-line row.
///
/// An `AdwExpanderRow`'s subtitle is the same trap: it is not an
/// `AdwActionRow` (trap 14), so it has no `subtitle()` accessor shared with one.
/// `text` is the subtitle currently set, which is how the label is found.
pub fn middle_ellipsis_text(row: &impl IsA<gtk::Widget>, text: &str) {
if let Some(label) = find_label(row.upcast_ref(), text) {
label.set_ellipsize(gtk::pango::EllipsizeMode::Middle);
}
}
fn find_label(widget: &gtk::Widget, text: &str) -> Option<gtk::Label> {
if let Some(label) = widget.downcast_ref::<gtk::Label>() {
if label.text().as_str() == text {
return Some(label.clone());
}
}
let mut child = widget.first_child();
while let Some(widget) = child {
if let Some(found) = find_label(&widget, text) {
return Some(found);
}
child = widget.next_sibling();
}
None
/// Same fixed cut as [`shorten_npub`], for the same Pango reason. Head and tail
/// are what identify the string, so both are kept.
pub fn shorten_uri(value: &str) -> String {
shorten(value, 20, 22)
}
/// `value` shortened to `head…tail` characters, for one-line subtitles.