Add optional toolbar icon captions saved with Midra projects

This commit is contained in:
Dandaedre 2026-11-13 15:18:55 +01:00
parent 0bef5b93a2
commit 1849729f8d
8 changed files with 226 additions and 24 deletions

View File

@ -29,6 +29,7 @@ pub struct Editor {
output_draft: Option<OutputDraft>,
edit_content: bool,
show_pattern: bool,
show_toolbar_icon_names: bool,
reference: Option<super::reference::Reference>,
reference_open: bool,
reference_error: String,
@ -184,6 +185,7 @@ impl Default for Editor {
output_draft: None,
edit_content: false,
show_pattern: false,
show_toolbar_icon_names: false,
reference: None,
reference_open: false,
reference_error: String::new(),
@ -1321,14 +1323,40 @@ impl Editor {
{
self.output_draft = Some(OutputDraft::new(output));
}
// A single compact strip: view tools, then history tools.
ink.card(72.0, 20.0, 150.0, 36.0, FIELD, LINE);
ink.line([147.0, 28.0], [147.0, 48.0], LINE);
// Keep captions separate from the buttons, with room for their full names.
let names = self.show_toolbar_icon_names;
let extra_height = if names { 22.0 } else { 0.0 };
let (strip_x, strip_width, separator, positions, align_x, import_x, project_x, options_x) =
if names {
(
88.0,
240.0,
226.0,
[92.0, 164.0, 236.0, 292.0],
356.0,
420.0,
476.0,
536.0,
)
} else {
(
72.0,
150.0,
147.0,
[76.0, 110.0, 152.0, 186.0],
232.0,
280.0,
328.0,
390.0,
)
};
ink.card(strip_x, 20.0, strip_width, 36.0, FIELD, LINE);
ink.line([separator, 28.0], [separator, 48.0], LINE);
for (x, text, icon) in [
(76.0, "Fit screen", ToolbarIcon::Screen),
(110.0, "Fit selected", ToolbarIcon::Selection),
(152.0, "Undo", ToolbarIcon::Undo),
(186.0, "Redo", ToolbarIcon::Redo),
(positions[0], "Fit screen", ToolbarIcon::Screen),
(positions[1], "Fit selected", ToolbarIcon::Selection),
(positions[2], "Undo", ToolbarIcon::Undo),
(positions[3], "Redo", ToolbarIcon::Redo),
] {
let rect = Rect::from_min_size(ink.point(x, 22.0), Vec2::splat(32.0));
if toolbar_icon(ui, rect, text, icon).clicked() {
@ -1349,7 +1377,7 @@ impl Editor {
ui.scope_builder(
egui::UiBuilder::new()
.max_rect(Rect::from_min_size(
ink.point(232.0, 21.0),
ink.point(align_x, 21.0),
Vec2::new(36.0, 34.0),
))
.layout(egui::Layout::left_to_right(egui::Align::Center)),
@ -1387,7 +1415,7 @@ impl Editor {
ui.scope_builder(
egui::UiBuilder::new()
.max_rect(Rect::from_min_size(
ink.point(280.0, 21.0),
ink.point(import_x, 21.0),
Vec2::new(36.0, 34.0),
))
.layout(egui::Layout::left_to_right(egui::Align::Center)),
@ -1427,7 +1455,7 @@ impl Editor {
ui.scope_builder(
egui::UiBuilder::new()
.max_rect(Rect::from_min_size(
ink.point(328.0, 21.0),
ink.point(project_x, 21.0),
Vec2::new(100.0, 34.0),
))
.layout(egui::Layout::left_to_right(egui::Align::Center)),
@ -1439,7 +1467,7 @@ impl Editor {
ui.scope_builder(
egui::UiBuilder::new()
.max_rect(Rect::from_min_size(
ink.point(390.0, 21.0),
ink.point(options_x, 21.0),
Vec2::new(36.0, 34.0),
))
.layout(egui::Layout::left_to_right(egui::Align::Center)),
@ -1464,6 +1492,26 @@ impl Editor {
ui.painter().circle_stroke(center, 3.5, stroke);
},
);
if names {
for (x, label) in [
(42.0, "Output"),
(positions[0] + 16.0, "Fit screen"),
(positions[1] + 16.0, "Fit selected"),
(positions[2] + 16.0, "Undo"),
(positions[3] + 16.0, "Redo"),
(align_x + 18.0, "Align"),
(import_x + 18.0, "Import"),
(options_x + 18.0, "Options"),
] {
ui.painter().text(
ink.point(x, 61.0),
egui::Align2::CENTER_TOP,
label,
egui::FontId::proportional(11.0),
MUTED,
);
}
}
if self.layout.selected().is_none() {
self.edit_content = false;
}
@ -1487,11 +1535,14 @@ impl Editor {
)
};
ui.put(
Rect::from_min_size(ink.point(24.0, 62.0), Vec2::new(r.width() - 48.0, 52.0)),
Rect::from_min_size(
ink.point(24.0, 62.0 + extra_height),
Vec2::new(r.width() - 48.0, 52.0),
),
egui::Label::new(egui::RichText::new(text).color(color)).wrap(),
);
let area = Rect::from_min_max(
r.min + Vec2::new(20.0, 131.0),
r.min + Vec2::new(20.0, 131.0 + extra_height),
r.max - Vec2::new(20.0, 85.0),
);
let base = ((area.width() - 48.0) / sw).min((area.height() - 48.0) / sh);
@ -2382,6 +2433,7 @@ mod tests {
for label in [
"Keep aspect ratio when resizing",
"Show source-coordinate test pattern in viewport",
"Show toolbar icon names",
] {
let output = frame(&mut app, &ctx, vec![]);
let pos = output
@ -2403,6 +2455,7 @@ mod tests {
}
assert!(!app.keep_aspect_ratio);
assert!(app.show_pattern);
assert!(app.show_toolbar_icon_names);
assert_eq!(*app.layout.layers(), before);
assert_eq!(*app.layout.formats(), formats);
}

View File

@ -40,6 +40,7 @@ impl Editor {
}
}
pub(super) fn viewport_options_menu(&mut self, ui: &mut egui::Ui) {
ui.checkbox(&mut self.show_toolbar_icon_names, "Show toolbar icon names");
ui.checkbox(
&mut self.keep_aspect_ratio,
"Keep aspect ratio when resizing",

View File

@ -76,6 +76,7 @@ pub struct EditorSettings {
pub viewport_zoom: f32,
pub viewport_pan: [f32; 2],
pub show_source_pattern: bool,
pub show_toolbar_icon_names: bool,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
@ -92,7 +93,7 @@ impl Project {
pub fn from_layout(layout: &Layout) -> Self {
Self {
format: FORMAT.into(),
version: 1,
version: 2,
output_resolution: layout.formats.output.into(),
inputs: std::array::from_fn(|i| {
let source = layout.formats.sources[i];
@ -123,14 +124,15 @@ impl Project {
viewport_zoom: 1.0,
viewport_pan: [0.0; 2],
show_source_pattern: false,
show_toolbar_icon_names: false,
},
}
}
/// Build a fresh document only after validating the entire file. No history is restored.
pub fn layout(&self) -> Result<Layout, String> {
if self.format != FORMAT || self.version != 1 {
if self.format != FORMAT || self.version != 2 {
return Err(
"Unsupported project format/version; expected beffroi.midra-layout version 1."
"Unsupported project format/version; expected beffroi.midra-layout version 2."
.into(),
);
}
@ -196,8 +198,30 @@ impl Project {
if text.len() as u64 > MAX_PROJECT_BYTES {
return Err("Project exceeds 96 MiB.".into());
}
let p: Self =
// Version 1 predates toolbar captions. Migrate only that known schema;
// current files still require every field and reject unknown fields.
#[derive(Deserialize)]
struct Header {
version: u32,
}
let header: Header =
serde_json::from_str(text).map_err(|e| format!("Invalid project JSON: {e}"))?;
let p: Self = if header.version == 1 {
let mut value: serde_json::Value =
serde_json::from_str(text).map_err(|e| format!("Invalid project JSON: {e}"))?;
let editor = value
.get_mut("editor")
.and_then(|v| v.as_object_mut())
.ok_or("Invalid version 1 editor settings.")?;
if editor.contains_key("show_toolbar_icon_names") {
return Err("Unexpected toolbar captions setting in version 1 project.".into());
}
editor.insert("show_toolbar_icon_names".into(), false.into());
value["version"] = 2.into();
serde_json::from_value(value).map_err(|e| format!("Invalid project JSON: {e}"))?
} else {
serde_json::from_str(text).map_err(|e| format!("Invalid project JSON: {e}"))?
};
p.layout()?;
Ok(p)
}

View File

@ -17,6 +17,7 @@ impl Editor {
p.editor.viewport_zoom = self.zoom;
p.editor.viewport_pan = [self.pan.x, self.pan.y];
p.editor.show_source_pattern = self.show_pattern;
p.editor.show_toolbar_icon_names = self.show_toolbar_icon_names;
p.reference = self.reference.as_ref().map(|r| SavedReference {
name: r.name.clone(),
image_base64: STANDARD.encode(&r.bytes),
@ -54,6 +55,7 @@ impl Editor {
zoom: e.viewport_zoom,
pan: Vec2::from(e.viewport_pan),
show_pattern: e.show_source_pattern,
show_toolbar_icon_names: e.show_toolbar_icon_names,
..Default::default()
};
self.sync();
@ -211,6 +213,7 @@ mod tests {
app.corner = Corner::BottomRight;
app.keep_aspect_ratio = false;
app.show_pattern = true;
app.show_toolbar_icon_names = true;
let project = Project::from_json(&app.project().to_json().unwrap()).unwrap();
let mut restored = Editor {
edit_content: true,

View File

@ -52,7 +52,7 @@ fn full_precision_project_roundtrip_restores_sources_crops_flags_and_selection()
fn malformed_or_incompatible_projects_are_refused() {
let original = serde_json::to_value(Project::from_layout(&Layout::default())).unwrap();
for (pointer, value) in [
("/version", serde_json::json!(2)),
("/version", serde_json::json!(3)),
("/format", serde_json::json!("other")),
("/layers/0/source_id", serde_json::json!(0)),
("/inputs/1/id", serde_json::json!(1)),
@ -151,3 +151,24 @@ fn documented_example_is_a_valid_version_one_project() {
Project::from_layout(&Layout::default())
);
}
#[test]
fn version_two_requires_captions_preference_and_preserves_it() {
let mut project = Project::from_layout(&Layout::default());
project.editor.show_toolbar_icon_names = true;
assert_eq!(
Project::from_json(&project.to_json().unwrap()).unwrap(),
project
);
let mut missing = serde_json::to_value(&project).unwrap();
missing["editor"]
.as_object_mut()
.unwrap()
.remove("show_toolbar_icon_names");
assert!(Project::from_json(&missing.to_string()).is_err());
let example = include_str!("../../../docs/interfaces/midra-layout-v2.example.json");
assert_eq!(
Project::from_json(example).unwrap(),
Project::from_layout(&Layout::default())
);
}

View File

@ -0,0 +1,91 @@
{
"format": "beffroi.midra-layout",
"version": 2,
"output_resolution": {
"width": 1920,
"height": 1080
},
"inputs": [
{
"id": 1,
"source_format": "sixteen_nine",
"native_resolution": {
"width": 1920,
"height": 1080
},
"input_crop": {
"left": 0.0,
"right": 0.0,
"top": 0.0,
"bottom": 0.0
}
},
{
"id": 2,
"source_format": "sixteen_nine",
"native_resolution": {
"width": 1920,
"height": 1080
},
"input_crop": {
"left": 0.0,
"right": 0.0,
"top": 0.0,
"bottom": 0.0
}
}
],
"layers": [
{
"id": 1,
"source_id": 1,
"bounds": {
"x": 120.0,
"y": 180.0,
"width": 640.0,
"height": 360.0
},
"advanced_crop": {
"left": 0.0,
"right": 0.0,
"top": 0.0,
"bottom": 0.0
},
"visible": true,
"locked": false
},
{
"id": 2,
"source_id": 2,
"bounds": {
"x": 1160.0,
"y": 180.0,
"width": 640.0,
"height": 360.0
},
"advanced_crop": {
"left": 0.0,
"right": 0.0,
"top": 0.0,
"bottom": 0.0
},
"visible": true,
"locked": false
}
],
"reference": null,
"editor": {
"selected_layer_ids": [
2
],
"position_corner": "top_left",
"keep_aspect_ratio": true,
"viewport_zoom": 1.0,
"viewport_pan": [
0.0,
0.0
],
"show_source_pattern": false,
"show_toolbar_icon_names": false
}
}

View File

@ -5,21 +5,25 @@ These are offline editor formats, not Analog Way REST payloads. Implementation:
[`project.rs`](../../crates/midra-layout/tests/project.rs). Update this contract,
examples and tests in the same change as any save/export schema change.
## Project JSON, version 1
## Project JSON, version 2
Suggested filename: `layout.midra.json`. Encoding: UTF-8, pretty-printed JSON,
with a trailing newline. A project preserves applied document values at their
stored floating-point precision. No display rounding is applied. The loader
requires every field below, rejects unknown fields, and rejects unsupported
versions. It does not guess, migrate, or partially apply incompatible files.
versions. Version 1 files are migrated to version 2 by setting
`editor.show_toolbar_icon_names` to `false`; all other values are preserved.
The [version 1 example](midra-layout-v1.example.json) remains a migration fixture.
Saving always writes version 2. Older applications reject version 2 files.
Incompatible files are never partially applied.
[Complete example](midra-layout-v1.example.json) is a valid default project,
[Complete example](midra-layout-v2.example.json) is a valid default project,
without a reference image. It is loaded by the regression tests.
| Root field | Representation / meaning |
| --- | --- |
| `format` | Exactly `"beffroi.midra-layout"` |
| `version` | Integer `1` |
| `version` | Integer `2` |
| `output_resolution` | `{ "width": integer, "height": integer }`, screen pixels |
| `inputs` | Exactly two input objects, IDs 1 and 2, in that order |
| `layers` | Exactly two layer objects, IDs 1 and 2, in that order |
@ -84,6 +88,7 @@ Editor object:
| `viewport_zoom` | Positive fit-relative scale, 0.0001–256 |
| `viewport_pan` | `[x, y]`, viewport pan in screen-coordinate pixels; finite, absolute value at most 1e9 |
| `show_source_pattern` | Boolean source-coordinate preview setting |
| `show_toolbar_icon_names` | Boolean: show names beneath toolbar icons; false by default |
Resolution dimensions must be whole pixels from 1 to 16384. Layer x/y must be finite and within ±1,000,000 pixels; layer widths/heights
and retained input dimensions must be positive, finite, and at most 1,000,000
@ -138,9 +143,9 @@ and `viewBox="0 0 W H"` equal to the output screen. The root carries
## Compatibility changes
Version 1 is a strict contract. A breaking project field/meaning change must bump
Project version 2 and SVG version 1 are strict contracts. A breaking project field/meaning change must bump
`version` and either implement a tested migration or clearly reject older/newer
files. Changing rectangle/marker semantics requires a corresponding documented
SVG interface revision and importer tests. Update this document and its example
with every schema or semantic change, including additive fields under the strict
unknown-field policy. Never silently interpret a future version as version 1.
unknown-field policy. Never silently interpret a future version as a supported version.

View File

@ -426,3 +426,7 @@ The header’s **Menu** brings the application’s commands together:
The toolbar, cog and right-click shortcuts use the same commands and preferences.
Detailed numeric controls remain in their existing properties dialogs.
Enable **Options → Show toolbar icon names** (also available from the toolbar cog)
to display captions beneath the icons. It is off by default and saved with the
project; older projects open with captions off.