mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge #17950 into the merge queue batch
Disable New Channel in Bitmap documents
This commit is contained in:
commit
8f74db0d9a
8 files changed
+73
-15
No files matched your search
@@ -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}");
|
||||
}
|
||||
}
|
||||
@@ -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<CommandSpec> {
|
||||
[],
|
||||
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!(
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -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| {
|
||||
|
||||
@@ -172,10 +172,13 @@ pub fn open(app: &mut PhotocraftApp, channel: &Value) -> Option<DialogId> {
|
||||
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<DialogId> {
|
||||
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<DialogId, String> {
|
||||
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.
|
||||
|
||||
@@ -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).
|
||||
|
||||
@@ -437,8 +437,8 @@ pub(crate) fn panel_rows(_app: &PhotocraftApp) -> Vec<PanelRow> {
|
||||
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,
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in new issue
Block a user