Skip to content
1 change: 1 addition & 0 deletions crates/ui-egui/src/layoutview/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod checklist;
mod colorbar;
mod frame;
mod legend;
mod legend_breaks;
mod north;
mod picture;
mod scalebar;
Expand Down
33 changes: 32 additions & 1 deletion crates/ui-egui/src/layoutview/format/checklist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ pub(super) struct Row {
pub(super) ticked: bool,
/// Whether ▲ and ▼ move it, past other movable rows.
pub(super) movable: bool,
/// A second check beside the arrows, where the section has one.
pub(super) mark: Option<Mark>,
}

/// A row's second check. Its texts come translated.
pub(super) struct Mark {
pub(super) on: bool,
/// What it's named for, apart from the row's own check.
pub(super) name: String,
pub(super) hover: &'static str,
/// Why it can't be changed, shown as its hover then.
pub(super) disabled: Option<String>,
}

/// What the section says and allows. Its texts come translated.
Expand Down Expand Up @@ -49,6 +61,8 @@ pub(super) enum Pick {
Shown(Vec<usize>, bool),
/// Two rows to change places, the upper first.
Swap(usize, usize),
/// A row's second check ticked (true) or unticked.
Mark(usize, bool),
Reset,
}

Expand Down Expand Up @@ -150,6 +164,15 @@ pub(super) fn checklist(
if arrow(ui, "▲", above.is_some(), tl!("Move up"), crate::i18n::fmt("Move {name} up", &args)) {
swap = above.map(|j| (j, i));
}
if let Some(m) = &row.mark {
let (free, mut on) = (m.disabled.is_none(), m.on);
let r = ui.add_enabled(free, egui::Checkbox::new(&mut on, "↵"));
r.widget_info(|| egui::WidgetInfo::selected(egui::Role::CheckBox, free, m.on, &m.name));
if r.on_hover_text(m.hover).on_disabled_hover_text(m.disabled.as_deref().unwrap_or_default()).changed()
{
pick = Some(Pick::Mark(i, on));
}
}
ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| {
ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Truncate);
if let Some(d) = &details {
Expand Down Expand Up @@ -226,7 +249,15 @@ mod tests {
};
let rows = || {
count.set(count.get() + 1);
vec![Row { name: "a".into(), key: "a".into(), text: "a".into(), also: String::new(), ticked: true, movable: true }]
vec![Row {
name: "a".into(),
key: "a".into(),
text: "a".into(),
also: String::new(),
ticked: true,
movable: true,
mark: None,
}]
};
checklist(ui, &spec, rows, None);
});
Expand Down
56 changes: 49 additions & 7 deletions crates/ui-egui/src/layoutview/format/legend.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! A legend's Format rows.

use super::checklist::{Details, Pick, Row, Spec, checklist};
use super::checklist::{Details, Mark, Pick, Row, Spec, checklist};
use super::legend_breaks::{break_change, break_mark};
use super::*;
use crate::RustGisApp;
use crate::widgets::Dragged;
Expand All @@ -21,6 +22,7 @@ pub(super) fn legend_property_rows(app: &mut RustGisApp, ui: &mut Ui, li: usize,
hide_other,
show_no_data,
columns,
column_breaks,
text_size,
fit,
show_layer_names,
Expand Down Expand Up @@ -65,21 +67,36 @@ pub(super) fn legend_property_rows(app: &mut RustGisApp, ui: &mut Ui, li: usize,
let input_label = ui.label(tl!("Columns"));
let mut n = *columns as u64;
let drag = Dragged::lend(ui, drag_key("columns"), &mut n);
// Column breaks set the columns, so the command refuses these beside them (#3445).
let broken = !column_breaks.is_empty();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
let by_breaks = tl!("Set by the legend's column breaks");
let r = ui
.add(egui::DragValue::new(&mut n).update_while_editing(false).range(1..=MAX_LEGEND_COLUMNS as u64))
.add_enabled(!broken, egui::DragValue::new(&mut n).update_while_editing(false).range(1..=MAX_LEGEND_COLUMNS as u64))
.labelled_by(input_label.id)
.on_hover_text(tl!("The columns its rows are laid in; a fit that adds columns starts from these"));
.on_hover_text(tl!("The columns its rows are laid in; a fit that adds columns starts from these"))
.on_disabled_hover_text(by_breaks);
if drag.settled(ui, &r, &mut n) && n as usize != *columns {
update(app, json!({"columns": n}));
}
ui.end_row();
// Every break at once: one on a layer the legend no longer lists has no check of its own.
if broken {
ui.label("");
let remove = ui.button(tl!("Remove Column Breaks"));
if remove.on_hover_text(tl!("Let the legend share its rows out among its columns again")).clicked() {
update(app, json!({"columnBreaks": []}));
}
ui.end_row();
}
let input_label = ui.label(tl!("Fit"));
let current = fit.kept();
egui::ComboBox::from_id_salt(("legend_fit", id))
.selected_text(tl!(current.label()))
.show_ui(ui, |ui| {
for v in LegendFit::ALL {
if ui.selectable_label(current == v, tl!(v.label())).clicked() && current != v {
let adds = matches!(v, LegendFit::AdjustColumns | LegendFit::AdjustColumnsAndSize);
let row = egui::Button::selectable(current == v, tl!(v.label()));
if ui.add_enabled(!(broken && adds), row).on_disabled_hover_text(by_breaks).clicked() && current != v {
update(app, json!({"fit": v.code()}));
}
}
Expand Down Expand Up @@ -171,7 +188,7 @@ pub(super) fn legend_layers(
// The layers a legend lists: a checkbox for each vector layer of its frame's map, each raster
// drawn by classes (#1679) and each WMS or WMTS layer with a legend, ticked when listed. Names
// gone from the map drop out of the list as it's written back.
if let ElementKind::Legend { frame, exclude, layer_order, .. } = &e.kind {
if let ElementKind::Legend { frame, exclude, layer_order, column_breaks, columns, fit, .. } = &e.kind {
let frame_map = layout.element(*frame).and_then(|f| match &f.kind {
ElementKind::MapFrame { map, .. } => Some(map),
_ => None,
Expand Down Expand Up @@ -209,15 +226,34 @@ pub(super) fn legend_layers(
// With none to list, still there to reset the legend's own order or the names left out.
if !names.is_empty() || !layer_order.is_empty() || !kept.is_empty() {
let left_out = |n: &str| exclude.iter().any(|x| x.eq_ignore_ascii_case(n));
// The breaks the map still has, which a change writes back.
let breaks_kept: Vec<String> = column_breaks.iter().filter(|x| on_map(x)).cloned().collect();
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
let first = names.iter().position(|n| !left_out(n));
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
let rows = || {
names
.iter()
.map(|n| {
.enumerate()
.map(|(i, n)| {
// By the legend's own text for it, still naming its layer: to a screen reader,
// the keyboard and among equal texts.
let own = legend_text(&e.kind, n, None);
let text = if own.is_empty() || own == *n { n.clone() } else { format!("{own} ({n})") };
Row { name: n.clone(), key: n.to_ascii_lowercase(), text, also: own, ticked: !left_out(n), movable: true }
let on = column_breaks.iter().any(|b| b.eq_ignore_ascii_case(n));
let mark = Mark {
on,
name: crate::i18n::fmt("{name} starts a column", &[("name", n.as_str())]),
hover: tl!("Starts a column"),
disabled: break_mark(!left_out(n), first == Some(i), on, breaks_kept.len()),
};
Row {
name: n.clone(),
key: n.to_ascii_lowercase(),
text,
also: own,
ticked: !left_out(n),
movable: true,
mark: Some(mark),
}
})
.collect()
};
Expand Down Expand Up @@ -307,6 +343,12 @@ pub(super) fn legend_layers(
next.extend(&unlisted);
json!({"layerOrder": next})
}
Pick::Mark(i, on) => {
let name = names.get(i).map_or("", String::as_str);
// Of the breaks, only those the map has are written back.
let has = |x: &str| breaks_kept.iter().any(|b| b == x);
break_change(&names, column_breaks, has, (*columns, *fit), name, on)
}
Pick::Reset => json!({"exclude": [], "layerOrder": []}),
});
}
Expand Down
111 changes: 111 additions & 0 deletions crates/ui-egui/src/layoutview/format/legend_breaks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//! What the Format pane sends for a legend's column breaks (#3445).

use rustgis_engine::map::layout::{LegendFit, MAX_LEGEND_COLUMNS};
use serde_json::{Value, json};

/// The `layout.update` that makes the layer `name` start a column (`on`) or stop. `drawn` are the
/// layers listed, in the order drawn; `breaks` the legend's own, kept for those the map still has.
/// Breaks set the columns, so with any left the same call takes `columns` to 1 and a fit that adds
/// columns to the one that doesn't: the command refuses them together, and one call is one undo.
pub(super) fn break_change(
drawn: &[String],
breaks: &[String],
on_map: impl Fn(&str) -> bool,
(columns, fit): (usize, LegendFit),
name: &str,
on: bool,
) -> Value {
let mut next: Vec<&String> =
drawn.iter().filter(|n| if n.eq_ignore_ascii_case(name) { on } else { breaks.iter().any(|b| b.eq_ignore_ascii_case(n)) }).collect();
next.extend(breaks.iter().filter(|b| on_map(b) && !drawn.iter().any(|n| n.eq_ignore_ascii_case(b))));
let mut change = json!({"columnBreaks": next});
if !next.is_empty() {
if columns > 1 {
change["columns"] = json!(1);
}
let kept = match fit.kept() {
LegendFit::AdjustColumns => Some(LegendFit::Manual),
LegendFit::AdjustColumnsAndSize => Some(LegendFit::AdjustSize),
_ => None,
};
if let Some(fit) = kept {
change["fit"] = json!(fit.code());
}
}
change
}

/// Why a layer's check can't be ticked, if it can't: a layer left out or the first listed starts
/// no column of its own, and `breaks` already set can fill the columns a legend has. A ticked one
/// is never disabled: its break can always come off, which frees Columns and the fits.
pub(super) fn break_mark(listed: bool, first: bool, on: bool, breaks: usize) -> Option<String> {
if on {
None
} else if !listed {
Some(tl!("Not listed in the legend").into())
} else if first {
Some(tl!("The first layer starts the first column").into())
} else if breaks + 1 >= MAX_LEGEND_COLUMNS {
Some(crate::i18n::fmt("A legend has at most {max} columns", &[("max", &MAX_LEGEND_COLUMNS.to_string())]))
} else {
None
}
}

#[cfg(test)]
mod tests {
use super::*;

fn names(n: &[&str]) -> Vec<String> {
n.iter().map(|n| n.to_string()).collect()
}

fn change(breaks: &[&str], set: (usize, LegendFit), name: &str, on: bool) -> Value {
break_change(&names(&["A", "B", "C"]), &names(breaks), |n| n != "Gone", set, name, on)
}

const PLAIN: (usize, LegendFit) = (1, LegendFit::Manual);

#[test]
fn a_check_says_why_it_cant_be_ticked() {
let most = MAX_LEGEND_COLUMNS - 1;
assert_eq!(break_mark(true, false, false, most - 1), None);
let full = format!("A legend has at most {MAX_LEGEND_COLUMNS} columns");
assert_eq!(break_mark(true, false, false, most), Some(full));
assert_eq!(break_mark(false, false, false, 0).as_deref(), Some("Not listed in the legend"));
assert_eq!(break_mark(true, true, false, 0).as_deref(), Some("The first layer starts the first column"));
assert_eq!(break_mark(false, true, true, most), None, "a ticked one always comes off");
}

#[test]
fn breaks_go_in_the_order_drawn() {
assert_eq!(change(&[], PLAIN, "B", true), json!({"columnBreaks": ["B"]}));
assert_eq!(change(&["C"], PLAIN, "B", true), json!({"columnBreaks": ["B", "C"]}));
assert_eq!(change(&["B", "C"], PLAIN, "B", false), json!({"columnBreaks": ["C"]}));
}

#[test]
fn the_layers_are_matched_by_name_in_any_case() {
assert_eq!(change(&["b"], PLAIN, "C", true), json!({"columnBreaks": ["B", "C"]}));
}

#[test]
fn a_break_not_listed_is_kept_only_while_the_map_has_it() {
assert_eq!(change(&["Hidden", "Gone"], PLAIN, "B", true), json!({"columnBreaks": ["B", "Hidden"]}));
}

#[test]
fn a_break_takes_columns_and_the_fit_with_it() {
use LegendFit::{AdjustColumns, AdjustColumnsAndSize, AdjustSize};
assert_eq!(change(&[], (3, LegendFit::Manual), "B", true), json!({"columnBreaks": ["B"], "columns": 1}));
assert_eq!(change(&[], (1, AdjustColumns), "B", true), json!({"columnBreaks": ["B"], "fit": "manual"}));
assert_eq!(change(&[], (2, AdjustColumnsAndSize), "B", true), json!({"columnBreaks": ["B"], "columns": 1, "fit": "adjustSize"}));
assert_eq!(change(&[], (1, AdjustSize), "B", true), json!({"columnBreaks": ["B"]}));
}

#[test]
fn taking_the_last_break_off_changes_nothing_else() {
// A file edited by hand can hold both.
assert_eq!(change(&["B"], (3, LegendFit::AdjustColumns), "B", false), json!({"columnBreaks": []}));
}
}
3 changes: 3 additions & 0 deletions crates/ui-egui/src/layoutview/format/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub(super) fn table_columns(app: &mut RustGisApp, ui: &mut Ui, layout: &Layout,
also: String::new(),
ticked,
movable: ticked,
mark: None,
};
// Without a choice, every field in the layer's order. A chosen one keeps the name the
// table stores, as it is drawn: one the layer has lost stays listed.
Expand Down Expand Up @@ -97,6 +98,8 @@ pub(super) fn table_columns(app: &mut RustGisApp, ui: &mut Ui, layout: &Layout,
Pick::Swap(a, b) if a < next.len() && b < next.len() => next.swap(a, b),
Pick::Swap(..) => return,
Pick::Reset => next.clear(),
// A table's rows have no second check.
Pick::Mark(..) => return,
}
// Every field in the layer's order is no choice: the table saves none.
if next.iter().copied().eq(all()) {
Expand Down
Loading
Loading