fix(app): Modify > Document Settings opens a dialog (M8)

The menu item ran doc.set_stage with nothing to set, so nothing happened. It now
opens Document Settings (stage size, frame rate, stage colour; OK is one undo
step, Cancel changes nothing), with Animate's Ctrl+J. Headless, the command takes
the values as before.

Fixes #505
This commit is contained in:
Tanish Manku committed 2026-11-20 06:03:18 -07:00
1 parent 51e27953e2
commit 541d2b170b
4 files changed
+175 -3

No files matched your search

+1 -1
View File
@@ -125,7 +125,7 @@ pub fn specs() -> Vec<CommandSpec> {
id: "doc.set_stage",
label: "Document Settings",
menu: "Modify",
shortcut: None,
shortcut: Some("Ctrl+J"),
params: "{width?, height?, fps?, background?: \"#rrggbb\"}",
mutates: true,
run: set_stage,
+13 -1
View File
@@ -394,6 +394,8 @@ pub struct App {
pub shape_dialog: Option<crate::shape_dialogs::ShapeDialog>,
pub shape_last: crate::shape_dialogs::Last,
pub trace: Option<crate::trace_dialog::TraceDialog>,
/// Modify › Document Settings (#505): the dialog while it is open.
pub doc_settings: Option<crate::doc_settings::DocSettings>,
pub trace_last: anicraft_engine::trace::TraceSettings,
/// Convert Audio to Keyframes and Auto Lip-Sync (ADR 0114): the dialog while it is open
/// (its preview is a gesture it keeps open until OK or Cancel), and what it was last given.
@@ -805,6 +807,7 @@ impl App {
convert_last: Default::default(),
shape_dialog: None,
trace: None,
doc_settings: None,
trace_last: Default::default(),
audio_keys: None,
data_view: None,
@@ -1809,6 +1812,8 @@ impl App {
}
self.run("element.select", json!({"ids": ids}));
}
// Modify › Document Settings (#505): its dialog over the sample.
Some("docsettings") => crate::doc_settings::open(self),
Some("tracebitmap") => {
// Trace Bitmap (#463): a small picture (sky, sun, hills) placed twice. The left
// one is traced with the defaults and shows as selected raw shapes; the right
@@ -3385,7 +3390,7 @@ impl App {
/// Any modal (prompt, confirmation, close guard) is open: global shortcuts pause.
pub fn modal_open(&self) -> bool {
self.prompt.is_some() || self.convert.is_some() || self.shape_dialog.is_some() || self.trace.is_some() || self.audio_keys.is_some() || self.confirm.is_some() || self.tabs.asking() || self.plugin_dialog.is_some() || self.templates.save.is_some() || self.video_import.is_some()
self.prompt.is_some() || self.convert.is_some() || self.shape_dialog.is_some() || self.trace.is_some() || self.doc_settings.is_some() || self.audio_keys.is_some() || self.confirm.is_some() || self.tabs.asking() || self.plugin_dialog.is_some() || self.templates.save.is_some() || self.video_import.is_some()
}
/// The single entry point for user-initiated commands: asks first when the engine says the
@@ -3815,6 +3820,12 @@ impl App {
crate::shape_dialogs::open(self, crate::shape_dialogs::Kind::Soften);
return;
}
// Modify › Document Settings asks for the stage's size, frame rate and colour (#505;
// headless, the command takes them).
if id == "doc.set_stage" {
crate::doc_settings::open(self);
return;
}
// Modify › Bitmap › Trace Bitmap asks for Animate's four settings (#463).
if id == "modify.trace_bitmap" {
crate::trace_dialog::open(self);
@@ -4800,6 +4811,7 @@ impl App {
crate::convert_symbol::window(self, &ctx);
crate::shape_dialogs::window(self, &ctx);
crate::trace_dialog::window(self, &ctx);
crate::doc_settings::window(self, &ctx);
crate::aids_dialog::window(self, &ctx);
crate::templates::save_window(self, &ctx);
crate::layer_props::window(self, &ctx);
+159
View File
@@ -0,0 +1,159 @@
//! Modify › Document Settings (Ctrl+J, #505): the stage's size, frame rate and colour in one
//! small dialog, as Animate's Document Settings. OK runs one `doc.set_stage`, one undo step;
//! Cancel changes nothing. The same values are in Properties while nothing is selected, where
//! they change live.
use crate::a11y::Named;
use crate::app::App;
use crate::theme::{FORM_LABEL_DIALOG, Type, form_row, text, with_type};
use anicraft_engine::doc::Stage;
use egui::Key;
use serde_json::json;
/// The largest stage side and frame rate offered (as Properties offers them).
pub const MAX_EDGE: u32 = 8192;
pub const MAX_FPS: f32 = 120.0;
/// The dialog while it is open: what OK would set.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DocSettings {
pub width: u32,
pub height: u32,
pub fps: f32,
pub background: [u8; 3],
/// The width field takes the keyboard on the first frame.
focus: bool,
}
impl DocSettings {
fn of(stage: &Stage) -> DocSettings {
let c = stage.background.sanitized();
let b = |v: f32| (v * 255.0).round() as u8;
DocSettings { width: stage.width, height: stage.height, fps: stage.fps, background: [b(c.r), b(c.g), b(c.b)], focus: true }
}
}
/// Opens the dialog on the document's settings.
pub fn open(app: &mut App) {
app.playing = false;
app.doc_settings = Some(DocSettings::of(&app.s.doc.stage));
}
/// What the dialog's values ask `doc.set_stage` for, brought into range.
pub fn params(d: &DocSettings) -> serde_json::Value {
let [r, g, b] = d.background;
json!({
"width": d.width.clamp(1, MAX_EDGE),
"height": d.height.clamp(1, MAX_EDGE),
"fps": if d.fps.is_finite() { d.fps.clamp(1.0, MAX_FPS) } else { 24.0 },
"background": format!("#{r:02x}{g:02x}{b:02x}"),
})
}
pub fn window(app: &mut App, ctx: &egui::Context) {
let Some(d) = &mut app.doc_settings else { return };
let (mut submit, mut cancel) = (false, false);
crate::windows::new(ctx, tr!("Document Settings")).id(egui::Id::new("doc_settings")).collapsible(false).resizable(false).anchor(egui::Align2::CENTER_CENTER, [0.0, -80.0]).show(ctx, |ui| {
ui.set_min_width(340.0);
let entered = |ui: &egui::Ui, r: &egui::Response| r.lost_focus() && ui.input(|i| i.key_pressed(Key::Enter));
form_row(ui, FORM_LABEL_DIALOG, tr!("Stage size"), |ui| {
let w = with_type(ui, Type::Data, |ui| ui.add(egui::DragValue::new(&mut d.width).range(1..=MAX_EDGE).suffix(" px"))).named(tr!("Stage width")).on_hover_text(tr!("The stage's width, in pixels"));
if d.focus {
w.request_focus();
d.focus = false;
}
submit |= entered(ui, &w);
let h = with_type(ui, Type::Data, |ui| ui.add(egui::DragValue::new(&mut d.height).range(1..=MAX_EDGE).suffix(" px"))).named(tr!("Stage height")).on_hover_text(tr!("The stage's height, in pixels"));
submit |= entered(ui, &h);
});
form_row(ui, FORM_LABEL_DIALOG, tr!("Frame rate"), |ui| {
let r = with_type(ui, Type::Data, |ui| ui.add(egui::DragValue::new(&mut d.fps).range(1.0..=MAX_FPS).suffix(" fps").speed(0.1).max_decimals(2))).named(tr!("Frame rate")).on_hover_text(tr!("Frames shown each second"));
submit |= entered(ui, &r);
});
form_row(ui, FORM_LABEL_DIALOG, tr!("Stage colour"), |ui| {
crate::colour::button(ui, &mut d.background).named(tr!("Stage colour"));
});
ui.label(text(Type::Caption, tr!("The artwork keeps its place and size: the stage changes around it.")));
ui.add_space(4.0);
ui.horizontal(|ui| {
if ui.button(tr!("OK")).clicked() {
submit = true;
}
if ui.button(tr!("Cancel")).clicked() || ui.input(|i| i.key_pressed(Key::Escape)) {
cancel = true;
}
});
});
if submit {
accept(app);
} else if cancel {
app.doc_settings = None;
}
}
/// OK: one `doc.set_stage` with the dialog's values (nothing is recorded when they are the
/// document's already).
fn accept(app: &mut App) {
let Some(d) = app.doc_settings.take() else { return };
app.request("doc.set_stage", params(&d));
}
#[cfg(test)]
mod tests {
use super::*;
use anicraft_engine::Session;
use anicraft_engine::doc::Document;
fn app(ctx: &egui::Context) -> App {
App::with_context(ctx, Session::new(Document::default()), Some(std::path::PathBuf::from("unused.png")))
}
/// #505: Modify › Document Settings ran `doc.set_stage` with nothing to set, so nothing
/// happened. The menu item (and Ctrl+J) opens the dialog; OK sets the stage in one step.
#[test]
fn the_menu_item_opens_the_dialog_and_ok_sets_the_stage_in_one_step() {
let ctx = egui::Context::default();
let mut app = app(&ctx);
let before = app.s.doc.stage;
app.invoke("doc.set_stage");
let d = app.doc_settings.as_mut().expect("Modify › Document Settings opens its dialog");
assert_eq!((d.width, d.height, d.fps), (before.width, before.height, before.fps), "it starts from the document");
assert!(app.modal_open());
// Drawn, it changes nothing until OK.
let mut out = ctx.run_ui(egui::RawInput::default(), |ui| window(&mut app, ui.ctx()));
out.textures_delta.clear();
assert_eq!(app.s.doc.stage, before);
let d = app.doc_settings.as_mut().expect("still open");
(d.width, d.height, d.fps, d.background) = (1920, 1080, 30.0, [0x10, 0x20, 0x30]);
accept(&mut app);
assert!(app.doc_settings.is_none());
let st = app.s.doc.stage;
assert_eq!((st.width, st.height, st.fps, st.background.to_hex().as_str()), (1920, 1080, 30.0, "#102030"));
assert_eq!(app.s.undo_label(), Some("Document Settings"));
app.run("edit.undo", json!({}));
assert_eq!(app.s.doc.stage, before, "one undo step");
// Cancel (closing the dialog) leaves the document alone.
open(&mut app);
if let Some(d) = app.doc_settings.as_mut() {
d.width = 64;
}
app.doc_settings = None;
assert_eq!(app.s.doc.stage, before);
}
#[test]
fn values_out_of_range_are_brought_into_it() {
let d = DocSettings { width: 0, height: 99_999, fps: f32::NAN, background: [255, 0, 128], focus: false };
assert_eq!(params(&d), json!({"width": 1, "height": 8192, "fps": 24.0f32, "background": "#ff0080"}));
let d = DocSettings { fps: 900.0, ..d };
assert_eq!(params(&d)["fps"], json!(120.0f32));
}
/// Animate's key for Document Settings.
#[test]
fn document_settings_has_animates_shortcut() {
let spec = anicraft_engine::find_command("doc.set_stage").expect("the command exists");
assert_eq!(spec.shortcut, Some("Ctrl+J"));
assert_eq!(spec.menu, "Modify");
}
}
+2 -1
View File
@@ -46,6 +46,7 @@ mod layer_depth;
mod layer_props;
mod desktop_ui;
mod dialogs;
mod doc_settings;
mod dock;
mod dock_float;
mod dock_ui;
@@ -302,7 +303,7 @@ fn parse_args(args: impl IntoIterator<Item = String>) -> Result<Args, String> {
/// `--help` (the console's text, in English).
#[cfg(not(target_arch = "wasm32"))]
// i18n-exempt: the --help console text
const HELP: &str = "anicraft [file.anicraft|file.fla|file.xfl] [--version] [--screenshot out.png [--shot-width <px>]] [--renderer gl|wgpu|vulkan|dx12|metal] [--theme dark|light|high-contrast|<file>.json] [--check-theme <file>.json] [--scale <percent>] [--lang <code>|auto|pseudo] [--control <port>] [--open palette|shortcuts|helpbone|lastscene|start|drop|droplibrary|select|selectzoom|about|files|confirm|transform|shapecommands|expandfill|softenfill|tracebitmap|origins|pivot|layeropacity|points|merge|rawparts|rawedit|rawsub|rawpoints|rawsnap|rulers|editguides|editgrid|symboledit|groups|combine|nineslice|nineslicedit|brush|brushing|polystar|paintinside|drawingedit|shapeenvelope|brushlibrary|brushcurve|paintbrush|eraser|erasing|erasemerge|tiltbrush|eyedropper|pen|frames|span|bucket|tlzoom|tlwide|motion|motionspan|motionpath|motionpoints|motionedit|motionpins|motionpinsspan|hints|hintsend|camera|cameraview|bones|springbones|shapebones|shapeweights|mixedbones|layerdepth|layerdepthview|filters|strokes|prefs|licenses|zoom|recovery|plugins|actions|script|scriptproblems|testmovie|testmovieinside|testscript|textfields|testtext|richtext|softmask|pathtext|button|testhits|exporthtml|publish|publishreport|publishanim|sound|envelope|ease|easepreset|plugineases|pluginexport|pluginexportwindow|library|librarymenu|libraryfolders|colour|layerprops|frameview|layercolour|timelinepolish|dock|dockicons|floattabs|docknarrow|workspaces|animator|designer|gradient|gradientbucket|swatches|swatchpanel|video|videoimport|datachart|datatable]\n\
const HELP: &str = "anicraft [file.anicraft|file.fla|file.xfl] [--version] [--screenshot out.png [--shot-width <px>]] [--renderer gl|wgpu|vulkan|dx12|metal] [--theme dark|light|high-contrast|<file>.json] [--check-theme <file>.json] [--scale <percent>] [--lang <code>|auto|pseudo] [--control <port>] [--open palette|shortcuts|helpbone|lastscene|start|drop|droplibrary|select|selectzoom|about|files|confirm|transform|shapecommands|expandfill|softenfill|tracebitmap|docsettings|origins|pivot|layeropacity|points|merge|rawparts|rawedit|rawsub|rawpoints|rawsnap|rulers|editguides|editgrid|symboledit|groups|combine|nineslice|nineslicedit|brush|brushing|polystar|paintinside|drawingedit|shapeenvelope|brushlibrary|brushcurve|paintbrush|eraser|erasing|erasemerge|tiltbrush|eyedropper|pen|frames|span|bucket|tlzoom|tlwide|motion|motionspan|motionpath|motionpoints|motionedit|motionpins|motionpinsspan|hints|hintsend|camera|cameraview|bones|springbones|shapebones|shapeweights|mixedbones|layerdepth|layerdepthview|filters|strokes|prefs|licenses|zoom|recovery|plugins|actions|script|scriptproblems|testmovie|testmovieinside|testscript|textfields|testtext|richtext|softmask|pathtext|button|testhits|exporthtml|publish|publishreport|publishanim|sound|envelope|ease|easepreset|plugineases|pluginexport|pluginexportwindow|library|librarymenu|libraryfolders|colour|layerprops|frameview|layercolour|timelinepolish|dock|dockicons|floattabs|docknarrow|workspaces|animator|designer|gradient|gradientbucket|swatches|swatchpanel|video|videoimport|datachart|datatable]\n\
--renderer the window's graphics (also ANICRAFT_RENDERER); default automatic, falling back to OpenGL\n\
--theme a built-in look, or a theme file of your own, for this run only\n\
--check-theme says whether a theme file would be used (exit 0) or why it is refused (exit 1)\n\