PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Filter dialogs: a Random button for seeds (#5710) (#5886)
Michael Higginsb8e48d6d4c23
diff --git a/crates/ui-egui/src/filter_dialog.rs b/crates/ui-egui/src/filter_dialog.rsindex 3da073520..92b96f914 100644--- a/crates/ui-egui/src/filter_dialog.rs+++ b/crates/ui-egui/src/filter_dialog.rs@@ -822,6 +822,11 @@ pub fn body(ui: &mut egui::Ui, f: &mut Map<String, Value>) {ui.horizontal(|ui| {ui.label(egui::RichText::new(label(&p.key)).color(t.text_dim));crate::widgets::value_field(ui, &mut v, -30000.0..=30000.0, "", 80.0);+ // A seed gets a fresh random value in one click (#5710): Clouds, Add Noise,+ // Fibers… then render a new pattern, as each run of Photoshop's does.+ if p.key == "seed" && ui.button(tl!("Random")).clicked() {+ v = random_seed(ui.input(|i| i.time).to_bits(), v as i64) as f32;+ }});f.insert(p.key, json!(v.round() as i64));}@@ -839,6 +844,15 @@ pub fn body(ui: &mut egui::Ui, f: &mut Map<String, Value>) {}}+/// A new seed for a dialog's Random button: 0..=30000 (what the field takes), never `current`.+/// `entropy` varies per click (the input time); the process's random hasher keys mix it.+fn random_seed(entropy: u64, current: i64) -> i64 {+ use std::hash::BuildHasher;+ let h = std::collections::hash_map::RandomState::new().hash_one(entropy);+ let seed = (h % 30_001) as i64;+ if seed == current { (seed + 1) % 30_001 } else { seed }+}+/// User-facing params (strip the dialog's private `__` keys).pub fn params_of(f: &Map<String, Value>) -> Value {// A document picker left at "None" (-1) means "not given".@@ -1005,6 +1019,28 @@ impl FilterPreviewKey {mod tests {use super::*;+ /// #5710: the seed field's Random button always gives a different seed the field can hold.+ #[test]+ fn random_seeds_stay_in_range_and_change() {+ let mut seen = std::collections::HashSet::new();+ for i in 0..200u64 {+ let s = random_seed(i, 7);+ assert!((0..=30_000).contains(&s), "{s}");+ assert_ne!(s, 7, "a click always changes the seed");+ seen.insert(s);+ }+ assert!(seen.len() > 100, "seeds spread out: {}", seen.len());+ assert_ne!(random_seed(1, 30_000), 30_000);+ // Every seed dialog shows the button: Clouds, Add Noise and the like carry a `seed` param.+ let specs = photocraft_engine::command_specs();+ assert!(+ specs+ .iter()+ .filter(|c| c.id.starts_with("filter."))+ .any(|c| parse_spec(c.params).iter().any(|p| p.key == "seed" && matches!(p.kind, Kind::Int { .. })))+ );+ }+/// Preset names such as "iPhone 14 Pro Max" (the artboard dialog's choices) are text, not/// camelCase keys: they used to read "I Phone 14 Pro Max".#[test]diff --git a/crates/ui-egui/src/i18n/fr.tsv b/crates/ui-egui/src/i18n/fr.tsvindex db40d97ab..84a68be74 100644--- a/crates/ui-egui/src/i18n/fr.tsv+++ b/crates/ui-egui/src/i18n/fr.tsv@@ -3179,3 +3179,4 @@ curves Smooth Lisser# View › Guides › right-click a guideEdit Selected Guides Modifier les repères sélectionnésClear Selected Guides Effacer les repères sélectionnés+ Random Aléatoirediff --git a/crates/ui-egui/src/i18n/id.tsv b/crates/ui-egui/src/i18n/id.tsvindex 544ead05a..eb5167c2e 100644--- a/crates/ui-egui/src/i18n/id.tsv+++ b/crates/ui-egui/src/i18n/id.tsv@@ -3166,3 +3166,4 @@ curves Smooth Haluskan# View › Guides › right-click a guideEdit Selected Guides Edit Panduan TerpilihClear Selected Guides Hapus Panduan Terpilih+ Random Acakdiff --git a/crates/ui-egui/src/i18n/ru.tsv b/crates/ui-egui/src/i18n/ru.tsvindex 862788630..75c91a883 100644--- a/crates/ui-egui/src/i18n/ru.tsv+++ b/crates/ui-egui/src/i18n/ru.tsv@@ -3144,3 +3144,4 @@ curves Smooth Сгладить# View › Guides › right-click a guideEdit Selected Guides Изменить выбранные направляющиеClear Selected Guides Удалить выбранные направляющие+ Random Случайноdiff --git a/crates/ui-egui/src/i18n/zh-hans.tsv b/crates/ui-egui/src/i18n/zh-hans.tsvindex 2307739ed..1ddd5fd84 100644--- a/crates/ui-egui/src/i18n/zh-hans.tsv+++ b/crates/ui-egui/src/i18n/zh-hans.tsv@@ -2790,3 +2790,4 @@ curves Smooth 平滑# View › Guides › right-click a guideEdit Selected Guides 编辑所选参考线Clear Selected Guides 清除所选参考线+ Random 随机diff --git a/crates/ui-egui/src/i18n/zh-hant.tsv b/crates/ui-egui/src/i18n/zh-hant.tsvindex 478489f8c..b14d5ec8c 100644--- a/crates/ui-egui/src/i18n/zh-hant.tsv+++ b/crates/ui-egui/src/i18n/zh-hant.tsv@@ -2775,3 +2775,4 @@ curves Smooth 平滑# View › Guides › right-click a guideEdit Selected Guides 編輯所選參考線Clear Selected Guides 清除所選參考線+ Random 隨機