diff --git a/crates/engine-core/src/bitmap_new_channel_tests.rs b/crates/engine-core/src/bitmap_new_channel_tests.rs new file mode 100644 index 0000000000..1fe64aa19e --- /dev/null +++ b/crates/engine-core/src/bitmap_new_channel_tests.rs @@ -0,0 +1,37 @@ +use serde_json::json; + +use crate::EngineError; + +#[test] +fn bitmap_disables_new_channel_without_adding_a_channel_or_history() { + let mut session = crate::session(); + session.execute("file.new", json!({"width": 16, "height": 16, "mode": "bitmap", "depth": 1})).unwrap(); + let before = session.execute("channel.list", json!({})).unwrap(); + let history = session.active().unwrap().history.past_len(); + + let enabled = session.is_enabled("channel.new"); + let result = session.execute("channel.new", json!({"name": "A"})); + + assert!(matches!(&result, Err(EngineError::Disabled(id, _)) if id == "channel.new"), "{result:?}"); + assert!(!enabled); + assert_eq!(session.execute("channel.list", json!({})).unwrap(), before); + assert_eq!(session.active().unwrap().history.past_len(), history); +} + +#[test] +fn new_channel_is_available_in_other_document_modes() { + for mode in ["rgb", "grayscale", "cmyk", "lab"] { + let mut session = crate::session(); + session.execute("file.new", json!({"width": 16, "height": 16, "mode": mode, "depth": 8})).unwrap(); + let before = session.execute("channel.list", json!({})).unwrap(); + + assert!(session.is_enabled("channel.new"), "{mode}"); + session.execute("channel.new", json!({"name": "A"})).unwrap(); + + let channels = session.execute("channel.list", json!({})).unwrap(); + let before = before["alpha"].as_array().unwrap(); + let after = channels["alpha"].as_array().unwrap(); + assert_eq!(after.len(), before.len() + 1, "{mode}"); + assert_eq!(after.last().unwrap()["name"], "A", "{mode}"); + } +} diff --git a/crates/engine-core/src/cmd/channel_cmds.rs b/crates/engine-core/src/cmd/channel_cmds.rs index 0c7619d0cb..bee14e27d7 100644 --- a/crates/engine-core/src/cmd/channel_cmds.rs +++ b/crates/engine-core/src/cmd/channel_cmds.rs @@ -32,6 +32,10 @@ mod tests; fn has_doc(s: &Session) -> std::result::Result<(), String> { s.active().map(|_| ()).ok_or_else(|| "no document open".into()) } +fn can_new_channel(s: &Session) -> std::result::Result<(), String> { + let active = s.active().ok_or("no document open")?; + if active.doc.mode == ColorMode::Bitmap { Err("not available in Bitmap mode".into()) } else { Ok(()) } +} fn has_channels(s: &Session) -> std::result::Result<(), String> { has_doc(s)?; if s.active().is_some_and(|d| !d.doc.channels.is_empty()) { Ok(()) } else { Err("the document has no alpha channels".into()) } @@ -198,7 +202,7 @@ pub fn specs() -> Vec { [], None, r##"{"name":str?,"fill":"black|white|selection"="black","color":"#rrggbb"="#ff0000","opacity":0..100=50,"indicates":"masked|selected"="masked"}"##, - has_doc, + can_new_channel, new_channel ), spec!( diff --git a/crates/engine-core/src/lib.rs b/crates/engine-core/src/lib.rs index 728198dc95..3f6f605c33 100644 --- a/crates/engine-core/src/lib.rs +++ b/crates/engine-core/src/lib.rs @@ -326,6 +326,8 @@ pub struct Session { pub whole_redraws: usize, } +#[cfg(test)] +mod bitmap_new_channel_tests; #[cfg(test)] mod embed_all_linked_tests; #[cfg(test)] diff --git a/crates/ui-egui/src/control/dialogs.rs b/crates/ui-egui/src/control/dialogs.rs index ede12b7e8b..eb886375a1 100644 --- a/crates/ui-egui/src/control/dialogs.rs +++ b/crates/ui-egui/src/control/dialogs.rs @@ -35,12 +35,9 @@ pub(crate) const KINDS: &[(&str, Opener)] = &[ }), ("newSnapshot", |app, _, _| crate::snapshot_dialog::open(app).ok_or_else(|| "no document is open".to_string())), ("newBrushPreset", |app, _, _| Ok(crate::brush_preset_dialog::open(app, None))), - ("channelOptions", |app, _, p| { - let opened = match p.get("channel") { - Some(channel) => crate::channel_options_ui::open(app, channel), - None => crate::channel_options_ui::open_new_channel(app), - }; - opened.ok_or_else(|| "no channel to show options for".to_string()) + ("channelOptions", |app, _, p| match p.get("channel") { + Some(channel) => crate::channel_options_ui::open(app, channel).ok_or_else(|| "no channel to show options for".to_string()), + None => crate::channel_options_ui::open_new_channel(app), }), ("mergeChannels", |app, _, _| crate::dialogs::merge_channels_ui::open(app).ok_or_else(|| "Merge Channels needs open documents".to_string())), ("formatOptions", |app, ctx, p| { diff --git a/crates/ui-egui/src/dialogs/channel_options_ui.rs b/crates/ui-egui/src/dialogs/channel_options_ui.rs index 7b4b6410e6..3eb0fd55c2 100644 --- a/crates/ui-egui/src/dialogs/channel_options_ui.rs +++ b/crates/ui-egui/src/dialogs/channel_options_ui.rs @@ -172,10 +172,13 @@ pub fn open(app: &mut PhotocraftApp, channel: &Value) -> Option { Some(open_with(app, COMMAND, f, channel.clone())) } -/// Opens New Channel, or `None` without a document. -pub fn open_new_channel(app: &mut PhotocraftApp) -> Option { - let f = new_channel_fields(app)?; - Some(open_with(app, NEW_CHANNEL, f, Value::Null)) +/// Opens New Channel when the engine can create a channel in the active document. +pub fn open_new_channel(app: &mut PhotocraftApp) -> Result { + if let Some(why) = app.session.disabled_reason(NEW_CHANNEL) { + return Err(why); + } + let f = new_channel_fields(app).ok_or("no document open")?; + Ok(open_with(app, NEW_CHANNEL, f, Value::Null)) } /// Opens New Spot Channel, or `None` without a document. diff --git a/crates/ui-egui/src/menus/invoke.rs b/crates/ui-egui/src/menus/invoke.rs index 2b0885fd1f..3deb7c8709 100644 --- a/crates/ui-egui/src/menus/invoke.rs +++ b/crates/ui-egui/src/menus/invoke.rs @@ -512,8 +512,7 @@ pub(crate) fn invoke_unguarded(app: &mut PhotocraftApp, ctx: &egui::Context, id: // Channels › New Channel… without params: its dialog (with params: the engine). The // panel's New button makes one at once, as Photoshop's does. crate::channel_options_ui::NEW_CHANNEL if params.as_object().is_none_or(|o| o.is_empty()) => { - let dialog = crate::channel_options_ui::open_new_channel(app).ok_or("no document is open")?; - Ok(dialog_reply(dialog)) + crate::channel_options_ui::open_new_channel(app).map(dialog_reply) } // Channels › Merge Channels… without params: its dialog, which picks the mode and the // documents (with params: the engine). diff --git a/crates/ui-egui/src/panels/channels_panel.rs b/crates/ui-egui/src/panels/channels_panel.rs index 356abab352..ae1d2cbea3 100644 --- a/crates/ui-egui/src/panels/channels_panel.rs +++ b/crates/ui-egui/src/panels/channels_panel.rs @@ -437,8 +437,8 @@ pub(crate) fn panel_rows(_app: &PhotocraftApp) -> Vec { channels::panel_rows() } -/// The state of each command of the set: the framework's rule over the rows, with Merge Channels… waiting for -/// enough grayscale documents of one size. +/// The state of each command of the set: the framework's rule over the rows, with engine +/// preconditions for New Channel… and Merge Channels…. pub(crate) fn command_states(app: &PhotocraftApp) -> Vec<(CommandRef, CommandState)> { let merge = crate::menus::is_enabled(app, "channel.merge"); let rows = rows(app); @@ -451,6 +451,7 @@ pub(crate) fn command_states(app: &PhotocraftApp) -> Vec<(CommandRef, CommandSta .into_iter() .map(|(command, state)| { let state = match command { + ChannelsCommands::NewChannel => CommandState::new().enabled(app.session.is_enabled(crate::channel_options_ui::NEW_CHANNEL)), ChannelsCommands::MergeChannels => CommandState::new().enabled(merge), ChannelsCommands::Duplicate if composite_of_several || several_targeted => CommandState::new().enabled(false), _ => state, diff --git a/crates/ui-egui/src/panels/channels_panel/tests.rs b/crates/ui-egui/src/panels/channels_panel/tests.rs index 91e6a097a1..025bb62bc8 100644 --- a/crates/ui-egui/src/panels/channels_panel/tests.rs +++ b/crates/ui-egui/src/panels/channels_panel/tests.rs @@ -303,6 +303,21 @@ fn channel_commands_follow_the_target() { assert_eq!(alpha_names(&app).len(), 1, "OK in New Channel adds a channel"); } +#[test] +fn bitmap_disables_new_channel_in_the_panel_menu_and_refuses_creation() { + let mut app = app_with("rgb"); + let ctx = app.ctx().clone(); + assert!(enabled(&app, ChannelsCommands::NewChannel)); + + app.run("file.new", json!({"width": 32, "height": 24, "mode": "bitmap", "depth": 1})).unwrap(); + assert!(!enabled(&app, ChannelsCommands::NewChannel)); + assert!(crate::menus::invoke(&mut app, &ctx, "channel.new", json!({})).is_err()); + apply_footer(&mut app, &ctx, ChannelsFooterButton::New, &[], Mods::default()); + + assert!(!app.dialog_open()); + assert!(alpha_names(&app).is_empty()); +} + /// Merge Spot Channel prints the targeted spot channel into the image and removes it: a solid blue ink over the left /// half turns those pixels blue and leaves the rest white. #[test]