Align content and tag values in a table
Each field row was an independent box whose title label only set a minimum width (width_chars 12), so a key longer than twelve characters — "Lightning address", a long JSON key — gave that row a wider title column and pushed its value to the right. Measured under Broadway, values sat at x=120 except the long-key row at x=136. Render the structured views as a two-column GtkGrid instead: titles in column 0, values in column 1, so column 0 is sized to the widest title and every value starts at the same x. Applied to the inline content card, the preview's formatted page, nested objects/arrays, and the Tags card. The title stays capped at 20 characters so a long key wraps rather than squeezing the value, and plain::field_title is now shared with the standalone field rows.
This commit is contained in:
2 files changed
+112
-68
No files matched your search
+92
-56
@@ -299,24 +299,23 @@ pub fn tags_card(
|
||||
|
||||
/// The tag rows, capped, with a "+N more tags" row when there are more.
|
||||
fn tags_body(tags: &[TagView], limit: usize) -> gtk::Widget {
|
||||
let boxed = gtk::Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.spacing(6)
|
||||
.build();
|
||||
for tag in tags.iter().take(limit) {
|
||||
boxed.append(&tag_row(tag));
|
||||
let table = field_table();
|
||||
for (row, tag) in tags.iter().take(limit).enumerate() {
|
||||
attach_field(
|
||||
&table,
|
||||
row as i32,
|
||||
&tag.name,
|
||||
&plain::label(&tag.values.join(" ")),
|
||||
);
|
||||
}
|
||||
let boxed = vertical();
|
||||
boxed.append(&table);
|
||||
if tags.len() > limit {
|
||||
boxed.append(&dim(&format!("+{} more tags", tags.len() - limit)));
|
||||
}
|
||||
boxed.upcast()
|
||||
}
|
||||
|
||||
/// One tag: its name beside its full value(s).
|
||||
fn tag_row(tag: &TagView) -> gtk::Box {
|
||||
plain::field(&tag.name, &plain::label(&tag.values.join(" ")))
|
||||
}
|
||||
|
||||
/// The full tag list, in a dialog like the content preview.
|
||||
fn present_tags_preview(parent: Option<>k::Window>, tags: &[TagView], deadline: Option<Instant>) {
|
||||
let page = padded_scroller(&tags_body(tags, tags.len()));
|
||||
@@ -346,22 +345,24 @@ fn present_tags_preview(parent: Option<>k::Window>, tags: &[TagView], deadline
|
||||
fn inline(content: &str, kind_number: u16) -> gtk::Widget {
|
||||
match view(content, kind_number) {
|
||||
View::Fields(fields) => {
|
||||
let boxed = gtk::Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.spacing(6)
|
||||
.build();
|
||||
let fields = collapse(fields);
|
||||
let (shown, omitted) = inline_slice(&fields);
|
||||
for field in shown {
|
||||
boxed.append(&field_row(&field.label, &field.value));
|
||||
let table = field_table();
|
||||
for (row, field) in shown.iter().enumerate() {
|
||||
attach_field(
|
||||
&table,
|
||||
row as i32,
|
||||
&field.label,
|
||||
&inline_value(&field.value),
|
||||
);
|
||||
}
|
||||
let boxed = vertical();
|
||||
boxed.append(&table);
|
||||
if omitted > 0 {
|
||||
let more = plain::label(&format!(
|
||||
boxed.append(&dim(&format!(
|
||||
"+{omitted} more field{}",
|
||||
if omitted == 1 { "" } else { "s" }
|
||||
));
|
||||
more.add_css_class("dim-label");
|
||||
boxed.append(&more);
|
||||
)));
|
||||
}
|
||||
boxed.upcast()
|
||||
}
|
||||
@@ -379,11 +380,11 @@ fn empty_body() -> gtk::Widget {
|
||||
plain::label("(empty)").upcast()
|
||||
}
|
||||
|
||||
/// One inline field: a scalar value, or a summary for a nested object/array.
|
||||
fn field_row(label: &str, value: &Value) -> gtk::Box {
|
||||
/// The inline value for one field: a scalar, or a summary for a nested value.
|
||||
fn inline_value(value: &Value) -> gtk::Widget {
|
||||
match value {
|
||||
Value::Object(_) | Value::Array(_) => plain::text_field(label, &summary(value)),
|
||||
other => plain::field(label, &plain::label(&scalar(other))),
|
||||
Value::Object(_) | Value::Array(_) => plain::label(&summary(value)).upcast(),
|
||||
other => plain::label(&scalar(other)).upcast(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -509,11 +510,18 @@ fn padded_scroller(child: &impl IsA<gtk::Widget>) -> gtk::ScrolledWindow {
|
||||
fn formatted_page(classified: View) -> gtk::Widget {
|
||||
match classified {
|
||||
View::Fields(fields) => {
|
||||
let boxed = vertical();
|
||||
let (shown, omitted) = fields.split_at(fields.len().min(PREVIEW_ROWS));
|
||||
for field in shown {
|
||||
boxed.append(&full_field(&field.label, &field.value, 1));
|
||||
let table = field_table();
|
||||
for (row, field) in shown.iter().enumerate() {
|
||||
attach_field(
|
||||
&table,
|
||||
row as i32,
|
||||
&field.label,
|
||||
&full_value(&field.value, 1),
|
||||
);
|
||||
}
|
||||
let boxed = vertical();
|
||||
boxed.append(&table);
|
||||
if !omitted.is_empty() {
|
||||
boxed.append(&dim(&format!("… and {} more fields", omitted.len())));
|
||||
}
|
||||
@@ -525,51 +533,58 @@ fn formatted_page(classified: View) -> gtk::Widget {
|
||||
}
|
||||
}
|
||||
|
||||
/// One preview field, recursing into nested objects and arrays.
|
||||
fn full_field(label: &str, value: &Value, depth: usize) -> gtk::Box {
|
||||
/// The value widget for one preview field: a scalar label, a nested table, or a
|
||||
/// summary for something too deep.
|
||||
fn full_value(value: &Value, depth: usize) -> gtk::Widget {
|
||||
match value {
|
||||
Value::Object(_) | Value::Array(_) if depth <= MAX_DEPTH => {
|
||||
plain::field(label, &nested(value, depth))
|
||||
Value::Object(_) | Value::Array(_) if depth <= MAX_DEPTH => nested(value, depth),
|
||||
Value::Object(_) | Value::Array(_) => {
|
||||
plain::label(&format!("{} (shown as text in Original)", summary(value))).upcast()
|
||||
}
|
||||
Value::Object(_) | Value::Array(_) => plain::text_field(
|
||||
label,
|
||||
&format!("{} (shown as text in Original)", summary(value)),
|
||||
),
|
||||
other => plain::field(label, &plain::label(&scalar(other))),
|
||||
other => plain::label(&scalar(other)).upcast(),
|
||||
}
|
||||
}
|
||||
|
||||
fn nested(value: &Value, depth: usize) -> gtk::Box {
|
||||
let boxed = vertical();
|
||||
/// A nested object or array, as its own table so its values align too.
|
||||
fn nested(value: &Value, depth: usize) -> gtk::Widget {
|
||||
match value {
|
||||
Value::Object(map) => {
|
||||
let (shown, omitted) = split_rows(map.len());
|
||||
for (key, value) in map.iter().take(shown) {
|
||||
boxed.append(&full_field(key, value, depth + 1));
|
||||
let table = field_table();
|
||||
for (row, (key, value)) in map.iter().take(shown).enumerate() {
|
||||
attach_field(&table, row as i32, key, &full_value(value, depth + 1));
|
||||
}
|
||||
let boxed = vertical();
|
||||
boxed.append(&table);
|
||||
if omitted > 0 {
|
||||
boxed.append(&dim(&format!("… and {omitted} more")));
|
||||
}
|
||||
boxed.upcast()
|
||||
}
|
||||
Value::Array(items) if items.iter().all(is_scalar) => {
|
||||
// A list of simple values reads better as one block than as N rows.
|
||||
plain::label(&items.iter().map(scalar).collect::<Vec<_>>().join("\n")).upcast()
|
||||
}
|
||||
Value::Array(items) => {
|
||||
if items.iter().all(is_scalar) {
|
||||
// A list of simple values reads better as one block than as N
|
||||
// rows.
|
||||
let joined = items.iter().map(scalar).collect::<Vec<_>>().join("\n");
|
||||
boxed.append(&plain::label(&joined));
|
||||
} else {
|
||||
let (shown, omitted) = split_rows(items.len());
|
||||
for (index, value) in items.iter().take(shown).enumerate() {
|
||||
boxed.append(&full_field(&(index + 1).to_string(), value, depth + 1));
|
||||
}
|
||||
if omitted > 0 {
|
||||
boxed.append(&dim(&format!("… and {omitted} more")));
|
||||
}
|
||||
let (shown, omitted) = split_rows(items.len());
|
||||
let table = field_table();
|
||||
for (row, value) in items.iter().take(shown).enumerate() {
|
||||
attach_field(
|
||||
&table,
|
||||
row as i32,
|
||||
&(row + 1).to_string(),
|
||||
&full_value(value, depth + 1),
|
||||
);
|
||||
}
|
||||
let boxed = vertical();
|
||||
boxed.append(&table);
|
||||
if omitted > 0 {
|
||||
boxed.append(&dim(&format!("… and {omitted} more")));
|
||||
}
|
||||
boxed.upcast()
|
||||
}
|
||||
other => boxed.append(&plain::label(&scalar(other))),
|
||||
other => plain::label(&scalar(other)).upcast(),
|
||||
}
|
||||
boxed
|
||||
}
|
||||
|
||||
fn is_scalar(value: &Value) -> bool {
|
||||
@@ -581,6 +596,27 @@ fn split_rows(total: usize) -> (usize, usize) {
|
||||
(shown, total - shown)
|
||||
}
|
||||
|
||||
/// A two-column table: titles in column 0, values in column 1.
|
||||
///
|
||||
/// The grid sizes column 0 to the widest title, so every value starts at the
|
||||
/// same x even when one key is longer than the others. A row of independent
|
||||
/// boxes sizes each title column on its own, so a long key drifts its value
|
||||
/// right.
|
||||
fn field_table() -> gtk::Grid {
|
||||
gtk::Grid::builder()
|
||||
.orientation(gtk::Orientation::Horizontal)
|
||||
.row_spacing(6)
|
||||
.column_spacing(12)
|
||||
.build()
|
||||
}
|
||||
|
||||
/// Attach one title/value row to a table.
|
||||
fn attach_field(grid: >k::Grid, row: i32, title: &str, value: &impl IsA<gtk::Widget>) {
|
||||
grid.attach(&plain::field_title(title), 0, row, 1, 1);
|
||||
value.set_hexpand(true);
|
||||
grid.attach(value, 1, row, 1, 1);
|
||||
}
|
||||
|
||||
fn vertical() -> gtk::Box {
|
||||
gtk::Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
|
||||
+20
-12
@@ -77,6 +77,25 @@ pub fn label(text: &str) -> gtk::Label {
|
||||
.build()
|
||||
}
|
||||
|
||||
/// The dim, width-bounded title label of a field.
|
||||
///
|
||||
/// The content table uses this for its title column and [`field`] for a
|
||||
/// standalone row. The width is a minimum of 12 characters and a maximum of 20,
|
||||
/// so a long key wraps rather than squeezing the value.
|
||||
pub fn field_title(text: &str) -> gtk::Label {
|
||||
gtk::Label::builder()
|
||||
.label(text)
|
||||
.use_markup(false)
|
||||
.xalign(0.0)
|
||||
.valign(gtk::Align::Start)
|
||||
.width_chars(12)
|
||||
.max_width_chars(20)
|
||||
.wrap(true)
|
||||
.wrap_mode(gtk::pango::WrapMode::WordChar)
|
||||
.css_classes(["dim-label"])
|
||||
.build()
|
||||
}
|
||||
|
||||
/// A label/value row: a dim, fixed-width title beside a value widget.
|
||||
///
|
||||
/// The title wraps and is width-bounded so an attacker-controlled JSON key
|
||||
@@ -86,19 +105,8 @@ pub fn field(title: &str, value: &impl IsA<gtk::Widget>) -> gtk::Box {
|
||||
.orientation(gtk::Orientation::Horizontal)
|
||||
.spacing(12)
|
||||
.build();
|
||||
let title = gtk::Label::builder()
|
||||
.label(title)
|
||||
.use_markup(false)
|
||||
.xalign(0.0)
|
||||
.valign(gtk::Align::Start)
|
||||
.width_chars(12)
|
||||
.max_width_chars(20)
|
||||
.wrap(true)
|
||||
.wrap_mode(gtk::pango::WrapMode::WordChar)
|
||||
.css_classes(["dim-label"])
|
||||
.build();
|
||||
value.set_hexpand(true);
|
||||
row.append(&title);
|
||||
row.append(&field_title(title));
|
||||
row.append(value);
|
||||
row
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user