mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Conditional Mode Change: Source Mode checkboxes with All / None and full mode names
The dialog was the generic form with one From dropdown (a single mode or Any) and raw ids. It now ticks any set of the eight source modes, in two columns with All and None, and lists the targets as Grayscale, RGB Color, CMYK Color and Lab Color. OK sends the ticked modes as the command's from list.
This commit is contained in:
1 parent
359051359f
commit
6f87f89614
3 files changed
+103
-15
No files matched your search
@@ -702,6 +702,7 @@ pub(crate) mod color_picker_preview;
|
||||
pub mod color_picker_ui;
|
||||
pub mod color_range_ui;
|
||||
pub mod color_table_ui;
|
||||
pub(crate) mod conditional_mode_change_dialog;
|
||||
pub mod count_group_name_ui;
|
||||
pub mod credits;
|
||||
pub mod dds_options_ui;
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
//! File › Automate › Conditional Mode Change… (#13676), laid out like Photoshop's dialog: a
|
||||
//! Source Mode section of eight independent checkboxes in two columns with All and None, and a
|
||||
//! Target Mode section whose Mode list holds the modes the command converts to.
|
||||
//!
|
||||
//! The checkboxes are one custom row of the command's form: `from` is the list of ticked source
|
||||
//! ids and `to` the target id, so OK, Reset and `ui.dialog.set` work as for every form.
|
||||
|
||||
use craft::ui::buttons::Button;
|
||||
use craft::ui::choice::Checkbox;
|
||||
use craft::ui::contract::{Bind, Props as _, Show as _};
|
||||
use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, Param, ParamKind};
|
||||
use craft::ui::runtime::env::Label;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
pub(crate) const COMMAND: &str = "file.automate.conditionalModeChange";
|
||||
|
||||
/// Photoshop's mode order. The checkboxes fill the left column, then the right one, and the
|
||||
/// ticked ones go to `from` in this order.
|
||||
const SOURCES: [(&str, &str); 8] = [
|
||||
("bitmap", "Bitmap"),
|
||||
("grayscale", "Grayscale"),
|
||||
("duotone", "Duotone"),
|
||||
("indexed", "Indexed Color"),
|
||||
("rgb", "RGB Color"),
|
||||
("cmyk", "CMYK Color"),
|
||||
("lab", "Lab Color"),
|
||||
("multichannel", "Multichannel"),
|
||||
];
|
||||
|
||||
/// The targets the command converts to, in the same order.
|
||||
const TARGETS: [(&str, &str); 4] = [("grayscale", "Grayscale"), ("rgb", "RGB Color"), ("cmyk", "CMYK Color"), ("lab", "Lab Color")];
|
||||
|
||||
/// The rows of the dialog: every source ticked ("any") and RGB Color.
|
||||
pub(crate) fn rows() -> Vec<Param> {
|
||||
let targets = TARGETS.iter().map(|(id, label)| ((*id).to_owned(), Label::from(tl!(label)))).collect();
|
||||
vec![
|
||||
Param::section(tl!("Source Mode")),
|
||||
Param::custom("from", Label::Empty, sources_row).opens_with(FieldValue::Text("any".into())).full_width(),
|
||||
Param::section(tl!("Target Mode")),
|
||||
Param::new("to", tl!("Mode"), ParamKind::choice(targets, Some("rgb".into()))),
|
||||
]
|
||||
}
|
||||
|
||||
/// A mode id spelled as the command reads it.
|
||||
fn norm(v: &str) -> String {
|
||||
let v = v.to_ascii_lowercase();
|
||||
if v == "gray" { "grayscale".into() } else { v }
|
||||
}
|
||||
|
||||
/// `from` as JSON: the opening "any", the list the checkboxes write, or what `ui.dialog.set` gave.
|
||||
fn from_value(fields: &DialogFields) -> Value {
|
||||
fields.get("from").and_then(|v| serde_json::to_value(v).ok()).unwrap_or(Value::Null)
|
||||
}
|
||||
|
||||
/// Is source `id` ticked? `from` takes every form the command does ("any", one mode, a list),
|
||||
/// and keeps it as it is until a checkbox changes.
|
||||
fn ticked(from: &Value, id: &str) -> bool {
|
||||
let is = |v: &str| {
|
||||
let v = norm(v);
|
||||
v == "any" || v == id
|
||||
};
|
||||
match from {
|
||||
Value::Array(a) => a.iter().filter_map(Value::as_str).any(is),
|
||||
Value::String(v) => is(v),
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
|
||||
/// The eight checkboxes in two columns, All and None under them.
|
||||
fn sources_row(ui: &mut egui::Ui, fields: &mut DialogFields, _: &mut craft::ui::patterns::form_dialogs::CustomContext) -> egui::Response {
|
||||
let from = from_value(fields);
|
||||
let mut on = SOURCES.map(|(id, _)| ticked(&from, id));
|
||||
let before = on;
|
||||
let response = egui::Grid::new("conditional-mode-sources")
|
||||
.num_columns(2)
|
||||
.spacing([24.0, 6.0])
|
||||
.show(ui, |ui| {
|
||||
for row in 0..4 {
|
||||
for i in [row, row + 4] {
|
||||
Checkbox::new(Bind::bound(&mut on[i])).label(tl!(SOURCES[i].1)).show(ui);
|
||||
}
|
||||
ui.end_row();
|
||||
}
|
||||
for (label, all) in [(tl!("All"), true), (tl!("None"), false)] {
|
||||
if Button::new(label).show(ui).clicked() {
|
||||
on = [all; 8];
|
||||
}
|
||||
}
|
||||
ui.end_row();
|
||||
})
|
||||
.response;
|
||||
if on != before {
|
||||
let ticked: Vec<&str> = SOURCES.iter().zip(on).filter(|(_, on)| *on).map(|((id, _), _)| *id).collect();
|
||||
fields.set("from", FieldValue::Json(json!(ticked)));
|
||||
}
|
||||
response
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../conditional_mode_change_controls_tests.rs"]
|
||||
mod tests;
|
||||
@@ -257,21 +257,7 @@ pub(super) fn params_for(app: &mut PhotocraftApp, id: &str) -> Option<(Vec<Param
|
||||
Param::new("dontEnlarge", tl!("Don't Enlarge"), ParamKind::bool(enlarge)).not_remembered(),
|
||||
])
|
||||
}
|
||||
"file.automate.conditionalModeChange" => {
|
||||
let modes = opts(&[
|
||||
("any", tl!("Any")),
|
||||
("rgb", tl!("RGB Color")),
|
||||
("grayscale", tl!("Grayscale")),
|
||||
("cmyk", tl!("CMYK Color")),
|
||||
("lab", tl!("Lab Color")),
|
||||
("indexed", tl!("Indexed Color")),
|
||||
("bitmap", tl!("Bitmap")),
|
||||
("duotone", tl!("Duotone")),
|
||||
("multichannel", tl!("Multichannel")),
|
||||
]);
|
||||
let targets = opts(&[("rgb", tl!("RGB Color")), ("grayscale", tl!("Grayscale")), ("cmyk", tl!("CMYK Color")), ("lab", tl!("Lab Color"))]);
|
||||
plain(vec![choice("from", tl!("From"), modes, "any"), choice("to", tl!("To"), targets, "rgb")])
|
||||
}
|
||||
crate::dialogs::conditional_mode_change_dialog::COMMAND => plain(crate::dialogs::conditional_mode_change_dialog::rows()),
|
||||
"file.export.layersToFiles" => {
|
||||
let (_, _, name) = doc?;
|
||||
plain(vec![
|
||||
|
||||
Reference in new issue
Block a user