LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
fix(ui): dropdown menus grow back after a search with no matches (#3154)
aiongg3a3e49af53ae
diff --git a/Cargo.lock b/Cargo.lockindex 378669bb9..4b52046c4 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1031,6 +1031,15 @@ dependencies = ["error-code",]+[[package]]+name = "cobs"+version = "0.3.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "0fa961b519f0b462e3a3b4a34b64d119eeaca1d59af726fe450bbba07a9fc0a1"+dependencies = [+ "thiserror 2.0.21",+]+[[package]]name = "codespan-reporting"version = "0.13.1"@@ -1175,25 +1184,61 @@ dependencies = [[[package]]name = "craft-docpath"version = "0.1.0"-source = "git+https://github.com/storytold/craft-libs?rev=1e6f127844ec61b3b8afdcc2bb76e7cfe470153f#1e6f127844ec61b3b8afdcc2bb76e7cfe470153f"+source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"++[[package]]+name = "craft-fonts-build"+version = "0.1.0"+source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"++[[package]]+name = "craft-i18n"+version = "0.1.0"+source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"+dependencies = [+ "fixed_decimal",+ "icu_decimal",+ "icu_locale_core",+ "icu_locale_fallback",+ "icu_normalizer",+ "icu_plurals",+ "icu_properties",+ "icu_provider",+ "icu_provider_adapters",+ "icu_provider_blob",+ "log",+ "objc2-foundation 0.3.2",+ "sys-locale",+ "unicode-bidi",+ "windows-registry",+ "writeable",+][[package]]name = "craft-ui"version = "0.1.0"-source = "git+https://github.com/storytold/craft-libs?rev=1e6f127844ec61b3b8afdcc2bb76e7cfe470153f#1e6f127844ec61b3b8afdcc2bb76e7cfe470153f"+source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"dependencies = [+ "craft-fonts-build",+ "craft-i18n","egui",+ "epaint",+ "js-sys","log","objc2 0.6.5","objc2-app-kit 0.3.2","objc2-foundation 0.3.2","serde",+ "skrifa",+ "wasm-bindgen",+ "wasm-bindgen-futures",+ "web-sys",][[package]]name = "craft-update"version = "0.1.0"-source = "git+https://github.com/storytold/craft-libs?rev=1e6f127844ec61b3b8afdcc2bb76e7cfe470153f#1e6f127844ec61b3b8afdcc2bb76e7cfe470153f"+source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"dependencies = ["chrono","dunce",@@ -1218,7 +1263,7 @@ dependencies = [[[package]]name = "craft-window"version = "0.1.0"-source = "git+https://github.com/storytold/craft-libs?rev=1e6f127844ec61b3b8afdcc2bb76e7cfe470153f#1e6f127844ec61b3b8afdcc2bb76e7cfe470153f"+source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"dependencies = ["egui","objc2 0.6.5",@@ -1415,6 +1460,17 @@ dependencies = ["objc2 0.6.5",]+[[package]]+name = "displaydoc"+version = "0.2.7"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c6232dd377dcc64799954cbd3a9bb882e9cdc1308ccd87b1c098f1fb2eaf82a8"+dependencies = [+ "proc-macro2",+ "quote",+ "syn 3.0.6",+]+[[package]]name = "dlib"version = "0.5.3"@@ -1947,6 +2003,17 @@ version = "0.1.14"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "aedcfb3409746eddb02b9e19ebda1c3394f759a152e48ee875a0844d1b955484"+[[package]]+name = "fixed_decimal"+version = "0.7.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "2214fb00f669ee96bee5bb29dfb1ddabe058b2bc426817efc79afa03c9bc55ae"+dependencies = [+ "displaydoc",+ "smallvec",+ "writeable",+]+[[package]]name = "fixedbitset"version = "0.5.7"@@ -2803,6 +2870,183 @@ version = "1.10.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"+[[package]]+name = "icu_collections"+version = "2.3.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "fa68d21081c4a05d5a901a1c62add574c77048b6a1c67be3b50ce0b60d4ca513"+dependencies = [+ "displaydoc",+ "potential_utf",+ "utf8_iter",+ "yoke 0.8.3",+ "zerofrom",+ "zerovec",+]++[[package]]+name = "icu_decimal"+version = "2.3.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "9eb8f655bba2d0c0459e43a5b0e6cd3cd388109898fb3d85d048165e8b0abd08"+dependencies = [+ "displaydoc",+ "fixed_decimal",+ "icu_locale_core",+ "icu_pattern",+ "icu_plurals",+ "icu_provider",+ "serde",+ "writeable",+ "zerovec",+]++[[package]]+name = "icu_locale_core"+version = "2.3.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d56e28588da92eee5c3201a6eff33fabdd49b62269c8938d4ff050ce4d900deb"+dependencies = [+ "displaydoc",+ "litemap",+ "serde",+ "tinystr",+ "writeable",+ "zerovec",+]++[[package]]+name = "icu_locale_fallback"+version = "2.3.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "251af8e57c9400e3eb58242fe5b8b1152b2a64fdf4cf632f923c38ccee6f2fa9"+dependencies = [+ "icu_locale_core",+ "icu_locale_fallback_data",+ "icu_provider",+ "potential_utf",+ "tinystr",+ "zerovec",+]++[[package]]+name = "icu_locale_fallback_data"+version = "2.3.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "decf2a22ec8fa68f1a0c1129a3f8583f8f8bc24e8b9ccbe98ead99f62a4dc3a8"++[[package]]+name = "icu_normalizer"+version = "2.3.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "12f9cf5f235641ed274641dd81c3f28d870e276763d0797aeeab72317b1c646f"+dependencies = [+ "icu_collections",+ "icu_normalizer_data",+ "icu_properties",+ "icu_provider",+ "smallvec",+ "utf16_iter",+ "utf8_iter",+ "write16",+ "zerovec",+]++[[package]]+name = "icu_normalizer_data"+version = "2.3.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "1563da1ed3e0b3bf3d74c9b85917ac9c56464d2f57242270c09c9e752f8021a0"++[[package]]+name = "icu_pattern"+version = "0.5.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "2b3db611446bd92c246b03e4aa0bafa71ddb194af431ddbb42b9d70e4bfce1a0"+dependencies = [+ "displaydoc",+ "either",+ "serde",+ "writeable",+ "zerovec",+]++[[package]]+name = "icu_plurals"+version = "2.3.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "e475e6766ef87b1d3c1f97be6363995b0bfaeddbc789671615df598a97ff0593"+dependencies = [+ "displaydoc",+ "fixed_decimal",+ "icu_provider",+ "serde",+ "zerovec",+]++[[package]]+name = "icu_properties"+version = "2.3.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "7e7ca276ad3145661a65914e6daf131ca5120cd3dcee8f8f3214b8875184a148"+dependencies = [+ "displaydoc",+ "icu_collections",+ "icu_locale_core",+ "icu_properties_data",+ "icu_provider",+ "zerotrie",+ "zerovec",+]++[[package]]+name = "icu_properties_data"+version = "2.3.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "e590f038c1464a96894fd6d10127e90a8be4509f56ff7ecef851b15cee0b7caa"++[[package]]+name = "icu_provider"+version = "2.3.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d27bbb9d3abbefac45d55f647c9de1d44aafcd1186eb91879afef17c396c3e73"+dependencies = [+ "displaydoc",+ "icu_locale_core",+ "postcard",+ "serde",+ "stable_deref_trait",+ "writeable",+ "yoke 0.8.3",+ "zerofrom",+ "zerotrie",+ "zerovec",+]++[[package]]+name = "icu_provider_adapters"+version = "2.3.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "df887bc4f55dbc773c7c45a0c7dfa75be886f7b7812830a71ab7271cc1801820"+dependencies = [+ "icu_locale_fallback",+ "icu_provider",+]++[[package]]+name = "icu_provider_blob"+version = "2.3.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "5916b44fac319d3f58e4d8d805a627df19e27937b582cd99fcde3bb6b7ce1b0e"+dependencies = [+ "icu_provider",+ "postcard",+ "serde",+ "writeable",+ "zerotrie",+ "zerovec",+]+[[package]]name = "image"version = "0.25.10"@@ -3793,6 +4037,15 @@ version = "0.12.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"+[[package]]+name = "litemap"+version = "0.8.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "47d9d19d1d6efa0109d2f65ff4c85cddd50bd572e5a00127ab10987290bcefae"+dependencies = [+ "serde_core",+]+[[package]]name = "litrs"version = "1.0.0"@@ -4967,6 +5220,27 @@ dependencies = ["portable-atomic",]+[[package]]+name = "postcard"+version = "1.1.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "6764c3b5dd454e283a30e6dfe78e9b31096d9e32036b5d1eaac7a6119ccb9a24"+dependencies = [+ "cobs",+ "serde",+]++[[package]]+name = "potential_utf"+version = "0.1.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d83eb9bc6d8e5cf568e7a1101d60ee05e81ed50ea106026f3d18deeb046d7661"+dependencies = [+ "serde_core",+ "writeable",+ "zerovec",+]+[[package]]name = "powerfmt"version = "0.2.1"@@ -6145,6 +6419,18 @@ dependencies = ["syn 3.0.6",]+[[package]]+name = "sys-locale"+version = "0.3.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "8eab9a99a024a169fe8a903cf9d4a3b3601109bcc13bd9e3c6fff259138626c4"+dependencies = [+ "js-sys",+ "libc",+ "wasm-bindgen",+ "web-sys",+]+[[package]]name = "sysctl"version = "0.5.5"@@ -6295,6 +6581,17 @@ dependencies = ["time-core",]+[[package]]+name = "tinystr"+version = "0.8.4"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "b1e27c91459209c2986af3dcf603a5a74a4368754ce37414f59acc971167f643"+dependencies = [+ "displaydoc",+ "serde_core",+ "zerovec",+]+[[package]]name = "tokio"version = "1.53.1"@@ -6564,12 +6861,24 @@ dependencies = ["log",]+[[package]]+name = "utf16_iter"+version = "1.0.5"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c8232dd3cdaed5356e0f716d285e4b40b932ac434100fe9b7e0e8e935b9e6246"+[[package]]name = "utf8-zero"version = "0.8.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "b8c0a043c9540bae7c578c88f91dda8bd82e59ae27c21baca69c8b191aaf5a6e"+[[package]]+name = "utf8_iter"+version = "1.0.4"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"+[[package]]name = "uuid"version = "1.26.1"@@ -7179,6 +7488,17 @@ dependencies = ["windows-link",]+[[package]]+name = "windows-registry"+version = "0.6.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "02752bf7fbdcce7f2a27a742f798510f3e5ad88dbe84871e5168e2120c3d5720"+dependencies = [+ "windows-link",+ "windows-result",+ "windows-strings",+]+[[package]]name = "windows-result"version = "0.4.1"@@ -7406,6 +7726,21 @@ dependencies = ["wayland-protocols-wlr",]+[[package]]+name = "write16"+version = "1.0.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d1890f4022759daae28ed4fe62859b1236caebfc61ede2f63ed4e695f3f6d936"++[[package]]+name = "writeable"+version = "0.6.4"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc"+dependencies = [+ "either",+]+[[package]]name = "x11-dl"version = "2.21.0"@@ -7710,6 +8045,43 @@ dependencies = ["syn 2.0.119",]+[[package]]+name = "zerotrie"+version = "0.2.5"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "4ea269c3bd32f0a32c321907a2ae912ba6f4649bb0fc764a15627e99a7095a3f"+dependencies = [+ "displaydoc",+ "litemap",+ "serde_core",+ "yoke 0.8.3",+ "zerofrom",+ "zerovec",+]++[[package]]+name = "zerovec"+version = "0.11.8"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "bb0464e17806c1d976d5cba29399c7f08e516e279e2ba493f63123b5fca67dd8"+dependencies = [+ "serde",+ "yoke 0.8.3",+ "zerofrom",+ "zerovec-derive",+]++[[package]]+name = "zerovec-derive"+version = "0.11.7"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c3743ddbf47738d929cd1ae2e199d40d0162979dfe31aef8ae646d2bf00bf97a"+dependencies = [+ "proc-macro2",+ "quote",+ "syn 3.0.6",+]+[[package]]name = "zip"version = "7.2.0"diff --git a/Cargo.toml b/Cargo.tomlindex f0a5935e6..0686b502d 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -10,10 +10,10 @@ rust-version = "1.95"repository = "https://github.com/storytold/lightcraft"[workspace.dependencies]-craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "1e6f127844ec61b3b8afdcc2bb76e7cfe470153f", features = ["serde"] }-craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "1e6f127844ec61b3b8afdcc2bb76e7cfe470153f" }-craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "1e6f127844ec61b3b8afdcc2bb76e7cfe470153f", features = ["egui"] }-craft-window = { git = "https://github.com/storytold/craft-libs", rev = "1e6f127844ec61b3b8afdcc2bb76e7cfe470153f", features = ["egui"] }+craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", features = ["serde"] }+craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" }+craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", features = ["egui"] }+craft-window = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", features = ["egui"] }# internallightcraft-denoise-core = { path = "crates/denoise-core" }lightcraft-denoise = { path = "crates/denoise", default-features = false }diff --git a/clippy.toml b/clippy.tomlindex 4b6c79f55..287e14edc 100644--- a/clippy.toml+++ b/clippy.toml@@ -2,3 +2,6 @@ allow-unwrap-in-tests = trueallow-expect-in-tests = trueallow-panic-in-tests = trueallow-indexing-slicing-in-tests = true+disallowed-types = [+ { path = "egui::ComboBox", reason = "use craft_ui::ComboBox: egui's combo box menu can't grow back while open (egui#5225)" },+]diff --git a/crates/ui-egui/src/dialogs/print_view.rs b/crates/ui-egui/src/dialogs/print_view.rsindex 4a9940307..82a2b564d 100644--- a/crates/ui-egui/src/dialogs/print_view.rs+++ b/crates/ui-egui/src/dialogs/print_view.rs@@ -407,7 +407,7 @@ fn settings(app: &mut LightcraftApp, ui: &mut Ui, t: &Tokens, pu: &mut PrintUi,heading(ui, t, "Page Setup");row(ui, t, "Paper", |ui| {let current = current_paper(&l.page.paper);- let c = egui::ComboBox::from_id_salt("printPaper").selected_text(paper_label(current)).show_ui(ui, |ui| {+ let c = craft_ui::ComboBox::from_id_salt("printPaper").selected_text(paper_label(current)).show_ui(ui, |ui| {for name in layout::paper_names().chain(std::iter::once("custom")) {if ui.selectable_label(current == name, paper_label(name)).clicked() {patch(app, &ctx, json!({"layout": {"page": {"paper": name}}}));@@ -499,7 +499,7 @@ fn print_job_color(app: &mut LightcraftApp, ui: &mut Ui, t: &Tokens, color: &Pri};let mut pick = false;row(ui, t, "Profile", |ui| {- let c = egui::ComboBox::from_id_salt("print-profile").width(190.0).selected_text(shown.clone()).show_ui(ui, |ui| {+ let c = craft_ui::ComboBox::from_id_salt("print-profile").width(190.0).selected_text(shown.clone()).show_ui(ui, |ui| {for (space, label) in [(ManagedSpace::Srgb, "Managed by Printer (sRGB)"), (ManagedSpace::AdobeRgb, "Managed by Printer (Adobe RGB)")] {let r = ui.selectable_label(color.profile == managed(space), crate::i18n::tr(label));register(ui, format!("item:printProfile-{}", to_value(space).as_str().unwrap_or_default()), r.rect);diff --git a/crates/ui-egui/src/tests/album_picker.rs b/crates/ui-egui/src/tests/album_picker.rsindex 97305cba6..c99f85770 100644--- a/crates/ui-egui/src/tests/album_picker.rs+++ b/crates/ui-egui/src/tests/album_picker.rs@@ -119,6 +119,48 @@ fn albums_that_would_loop_are_greyed() {assert_eq!(value(&h), json!(null), "but not picked");}+/// The registered rectangle of `id` in the current frame.+fn rect_of(h: &Headless, id: &str) -> egui::Rect {+ h.app.widgets.iter().find(|(w, _)| w == id).map(|(_, r)| *r).unwrap_or_else(|| panic!("no widget {id}"))+}++/// The height of the smallest area (the popup, not the dialog) that holds the search field, as egui keeps it between frames.+fn popup_height(h: &Headless, search: egui::Rect) -> f32 {+ let ctx = &h.view.ctx;+ let layers = ctx.memory(|m| m.areas().visible_layer_ids());+ layers+ .into_iter()+ .filter_map(|layer| egui::AreaState::load(ctx, layer.id))+ .filter(|a| a.rect().contains_rect(search))+ .filter_map(|a| a.size.map(|s| s.y))+ .fold(f32::MAX, f32::min)+}++/// Filtering to nothing shrinks the popup and clearing the filter grows it back (egui#5225),+/// with the search field in the same place throughout.+#[test]+fn the_popup_grows_back_when_the_search_is_cleared() {+ let Library { mut h, .. } = library();+ click(&mut h, "albumPicker:rules-0");+ let search = rect_of(&h, "albumPickerSearch:rules-0");+ let full = popup_height(&h, search);+ assert!(full > 80.0, "the open popup shows the tree: {full}");+ let r = h.request("ui.text", json!({"text": "zzzz"}), T);+ assert_eq!(r["ok"], true, "{r}");+ h.settle(SETTLE);+ let none = popup_height(&h, search);+ assert!(none < full - 20.0, "no match shrinks the popup: {none} vs {full}");+ assert_eq!(rect_of(&h, "albumPickerSearch:rules-0"), search, "the search field stays put");+ for _ in 0..4 {+ let r = h.request("ui.key", json!({"key": "Backspace"}), T);+ assert_eq!(r["ok"], true, "{r}");+ h.settle(SETTLE);+ }+ let again = popup_height(&h, search);+ assert!((again - full).abs() < 1.0, "cleared, the popup is {again} tall, not {full} as when it opened");+ assert_eq!(rect_of(&h, "albumPickerSearch:rules-0"), search, "the search field stays put");+}+/// After opening a folder, typing still goes to the search: the box keeps the keyboard.#[test]fn typing_after_a_folder_click_still_searches() {diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rsindex 0a613baed..558edfe8d 100644--- a/crates/ui-egui/src/widgets.rs+++ b/crates/ui-egui/src/widgets.rs@@ -9,8 +9,8 @@ use crate::icons::{Icon, paint};use crate::theme::Tokens;/// Standard dropdown chevrons, including settings, import/export and rule editors.-pub fn combo_box(id: impl egui::AsIdSalt) -> egui::ComboBox<'static> {- egui::ComboBox::from_id_salt(id)+pub fn combo_box(id: impl egui::AsIdSalt) -> craft_ui::ComboBox<'static> {+ craft_ui::ComboBox::from_id_salt(id).icon(|ui, rect, visuals, _| {paint(ui.painter(), Rect::from_center_size(rect.center(), vec2(16.0, 16.0)), Icon::ChevronDown, visuals.fg_stroke.color);})diff --git a/crates/ui-egui/src/widgets/album_picker.rs b/crates/ui-egui/src/widgets/album_picker.rsindex 1c59bcae8..a88e5f1aa 100644--- a/crates/ui-egui/src/widgets/album_picker.rs+++ b/crates/ui-egui/src/widgets/album_picker.rs@@ -166,6 +166,9 @@ struct State {focus: bool,}+/// The popup's maximum height: the search field and the list.+const MENU_HEIGHT: f32 = 320.0;+/// A button that edits an album id; see the module docs.pub struct AlbumPicker<'a> {id: String,@@ -218,151 +221,172 @@ impl<'a> AlbumPicker<'a> {let open = chosen.map(|c| ancestors(entries, c)).unwrap_or_default();ui.data_mut(|d| d.insert_temp(key, State { open, focus: true, ..Default::default() }));}- let mut state: State = ui.data(|d| d.get_temp(key)).unwrap_or_default();- let mut picked: Option<u64> = None;- egui::Popup::from_toggle_button_response(&button).close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside).show(|ui| {- // A wide popup remembered before a window resize must still fit the current viewport.- let margin = egui::Frame::popup(ui.style()).total_margin().sum().x;- let max_width = (ui.ctx().content_rect().width() - margin).max(1.0);- ui.set_min_width(width.max(260.0).min(max_width));- ui.set_max_width((width.max(260.0) + 120.0).min(max_width));- if choices(ui) {- ui.close();- return;- }- let search_box =- ui.add(egui::TextEdit::singleline(&mut state.query).hint_text(crate::i18n::tr("Search albums")).desired_width(f32::INFINITY));- register(ui, format!("albumPickerSearch:{id}"), search_box.rect);- if state.focus {- search_box.request_focus();- state.focus = false;- }- if search_box.changed() {- state.highlight = 0;- }- let t = crate::theme::Tokens::get(ui.ctx());- // `scroll`: bring the row into view (the arrow keys just moved to it); never every frame,- // which would fight the mouse wheel- let row = |ui: &mut Ui, e: &AlbumEntry, label: &str, indent: f32, highlighted: bool, scroll: bool, picked: &mut Option<u64>| {- ui.horizontal(|ui| {- ui.add_space(indent);- let (r, _) = ui.allocate_exact_size(vec2(14.0, 14.0), egui::Sense::hover());- let icon = if e.smart { Icon::SmartAlbum } else { Icon::Album };- crate::icons::paint(ui.painter(), r, icon, if e.blocked.is_some() { t.text_disabled } else { t.icon });- let resp = ui- .add_enabled(e.blocked.is_none(), egui::Button::selectable(Some(e.id) == chosen || highlighted, label).truncate())- .on_hover_text(label);- register(ui, format!("albumPickerItem:{}:{id}", e.id), resp.rect);- let resp = match &e.blocked {- Some(why) => resp.on_disabled_hover_text(why.as_str()),- None => resp,- };- if resp.clicked() {- *picked = Some(e.id);+ let state: State = ui.data(|d| d.get_temp(key)).unwrap_or_default();+ let state = std::cell::RefCell::new(state);+ let picked = craft_ui::combo::Menu::new(ui, &button)+ .height(MENU_HEIGHT)+ .close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside)+ .show_ui_with_header(+ |ui| {+ // A wide popup remembered before a window resize must still fit the current viewport.+ let margin = egui::Frame::popup(ui.style()).total_margin().sum().x;+ let max_width = (ui.ctx().content_rect().width() - margin).max(1.0);+ ui.set_min_width(width.max(260.0).min(max_width));+ ui.set_max_width((width.max(260.0) + 120.0).min(max_width));+ // Esc closes the popup and stops there: the dialog around it keeps its edits+ if ui.input(|i| i.key_pressed(egui::Key::Escape)) {+ crate::widgets::take_escape(ui.ctx());+ ui.close();+ }+ if choices(ui) {+ ui.close();+ return true;+ }+ let mut state = state.borrow_mut();+ let search_box =+ ui.add(egui::TextEdit::singleline(&mut state.query).hint_text(crate::i18n::tr("Search albums")).desired_width(f32::INFINITY));+ register(ui, format!("albumPickerSearch:{id}"), search_box.rect);+ if state.focus {+ search_box.request_focus();+ state.focus = false;}- if scroll {- resp.scroll_to_me(None);+ if search_box.changed() {+ state.highlight = 0;}- });- };- egui::ScrollArea::vertical().max_height(320.0).auto_shrink([false, true]).show(ui, |ui| {- if state.query.trim().is_empty() {- for i in visible_tree(entries, &state.open) {- let e = &entries[i];- let indent = e.depth as f32 * 16.0;- if e.folder {- // a folder row folds and unfolds, like the sidebar's: triangle, icon, name- let open = state.open.contains(&e.id);- let height = ui.spacing().interact_size.y;- let (rect, r) = ui.allocate_exact_size(vec2(ui.available_width(), height), egui::Sense::click());- if r.hovered() {- ui.painter().rect_filled(rect, 3.0, t.hover);+ false+ },+ |ui, closed| {+ let mut picked: Option<u64> = None;+ if closed {+ return picked;+ }+ let mut state = state.borrow_mut();+ let t = crate::theme::Tokens::get(ui.ctx());+ // `scroll`: bring the row into view (the arrow keys just moved to it); never every frame,+ // which would fight the mouse wheel+ let row = |ui: &mut Ui, e: &AlbumEntry, label: &str, indent: f32, highlighted: bool, scroll: bool, picked: &mut Option<u64>| {+ ui.horizontal(|ui| {+ ui.add_space(indent);+ let (r, _) = ui.allocate_exact_size(vec2(14.0, 14.0), egui::Sense::hover());+ let icon = if e.smart { Icon::SmartAlbum } else { Icon::Album };+ crate::icons::paint(ui.painter(), r, icon, if e.blocked.is_some() { t.text_disabled } else { t.icon });+ let resp = ui+ .add_enabled(e.blocked.is_none(), egui::Button::selectable(Some(e.id) == chosen || highlighted, label).truncate())+ .on_hover_text(label);+ register(ui, format!("albumPickerItem:{}:{id}", e.id), resp.rect);+ let resp = match &e.blocked {+ Some(why) => resp.on_disabled_hover_text(why.as_str()),+ None => resp,+ };+ if resp.clicked() {+ *picked = Some(e.id);+ }+ if scroll {+ resp.scroll_to_me(None);}- let x = rect.left() + indent;- disclosure(ui.painter(), egui::pos2(x + 7.0, rect.center().y), open, if r.hovered() { t.text } else { t.text_dim });- let icon = egui::Rect::from_center_size(egui::pos2(x + 23.0, rect.center().y), vec2(14.0, 14.0));- crate::icons::paint(ui.painter(), icon, Icon::Folder, t.icon);- ui.painter().text(- egui::pos2(x + 36.0, rect.center().y),- egui::Align2::LEFT_CENTER,- &e.name,- egui::TextStyle::Button.resolve(ui.style()),- t.text_label,- );- register(ui, format!("albumPickerFolder:{}:{id}", e.id), r.rect);- // what assistive tools announce: the folder, and whether it is open- let name = e.name.clone();- crate::widgets::announce_disclosure(&r, true, open, &name);- if r.clicked() {- // the click took the keyboard from the search box: give it back- state.focus = true;- if open {- state.open.retain(|o| *o != e.id);+ });+ };+ {+ if state.query.trim().is_empty() {+ for i in visible_tree(entries, &state.open) {+ let e = &entries[i];+ let indent = e.depth as f32 * 16.0;+ if e.folder {+ // a folder row folds and unfolds, like the sidebar's: triangle, icon, name+ let open = state.open.contains(&e.id);+ let height = ui.spacing().interact_size.y;+ let (rect, r) = ui.allocate_exact_size(vec2(ui.available_width(), height), egui::Sense::click());+ if r.hovered() {+ ui.painter().rect_filled(rect, 3.0, t.hover);+ }+ let x = rect.left() + indent;+ disclosure(+ ui.painter(),+ egui::pos2(x + 7.0, rect.center().y),+ open,+ if r.hovered() { t.text } else { t.text_dim },+ );+ let icon = egui::Rect::from_center_size(egui::pos2(x + 23.0, rect.center().y), vec2(14.0, 14.0));+ crate::icons::paint(ui.painter(), icon, Icon::Folder, t.icon);+ ui.painter().text(+ egui::pos2(x + 36.0, rect.center().y),+ egui::Align2::LEFT_CENTER,+ &e.name,+ egui::TextStyle::Button.resolve(ui.style()),+ t.text_label,+ );+ register(ui, format!("albumPickerFolder:{}:{id}", e.id), r.rect);+ // what assistive tools announce: the folder, and whether it is open+ let name = e.name.clone();+ crate::widgets::announce_disclosure(&r, true, open, &name);+ if r.clicked() {+ // the click took the keyboard from the search box: give it back+ state.focus = true;+ if open {+ state.open.retain(|o| *o != e.id);+ } else {+ state.open.push(e.id);+ }+ }} else {- state.open.push(e.id);+ // past the triangle column, so names line up with folder names+ row(ui, e, &e.name, indent + 14.0, false, false, &mut picked);}}+ if entries.iter().all(|e| e.folder) {+ ui.label(RichText::new(crate::i18n::tr("No albums yet")).weak());+ }} else {- // past the triangle column, so names line up with folder names- row(ui, e, &e.name, indent + 14.0, false, false, &mut picked);- }- }- if entries.iter().all(|e| e.folder) {- ui.label(RichText::new(crate::i18n::tr("No albums yet")).weak());- }- } else {- let hits = search(entries, &state.query);- // ↑ / ↓ move through the results that can be picked, Enter picks one- let pickable: Vec<usize> = hits.iter().copied().filter(|&i| entries[i].blocked.is_none()).collect();- let (down, up, enter) = ui.input_mut(|i| {- (- i.consume_key(egui::Modifiers::NONE, egui::Key::ArrowDown),- i.consume_key(egui::Modifiers::NONE, egui::Key::ArrowUp),- i.consume_key(egui::Modifiers::NONE, egui::Key::Enter),- )- });- if down {- state.highlight = (state.highlight + 1).min(pickable.len().saturating_sub(1));- }- if up {- state.highlight = state.highlight.saturating_sub(1);- }- let highlighted = pickable.get(state.highlight).copied();- if enter {- match highlighted {- Some(i) => picked = Some(entries[i].id),- // Enter took the keyboard from the search box and picked nothing: give it back- None => state.focus = true,+ let hits = search(entries, &state.query);+ // ↑ / ↓ move through the results that can be picked, Enter picks one+ let pickable: Vec<usize> = hits.iter().copied().filter(|&i| entries[i].blocked.is_none()).collect();+ let (down, up, enter) = ui.input_mut(|i| {+ (+ i.consume_key(egui::Modifiers::NONE, egui::Key::ArrowDown),+ i.consume_key(egui::Modifiers::NONE, egui::Key::ArrowUp),+ i.consume_key(egui::Modifiers::NONE, egui::Key::Enter),+ )+ });+ if down {+ state.highlight = (state.highlight + 1).min(pickable.len().saturating_sub(1));+ }+ if up {+ state.highlight = state.highlight.saturating_sub(1);+ }+ let highlighted = pickable.get(state.highlight).copied();+ if enter {+ match highlighted {+ Some(i) => picked = Some(entries[i].id),+ // Enter took the keyboard from the search box and picked nothing: give it back+ None => state.focus = true,+ }+ }+ let by_id = index(entries);+ for i in &hits {+ let e = &entries[*i];+ row(+ ui,+ e,+ &path_in(entries, &by_id, e.id),+ 0.0,+ highlighted == Some(*i),+ (up || down) && highlighted == Some(*i),+ &mut picked,+ );+ }+ if hits.is_empty() {+ ui.label(RichText::new(crate::i18n::tr("No albums match")).weak());+ }}}- let by_id = index(entries);- for i in &hits {- let e = &entries[*i];- row(- ui,- e,- &path_in(entries, &by_id, e.id),- 0.0,- highlighted == Some(*i),- (up || down) && highlighted == Some(*i),- &mut picked,- );- }- if hits.is_empty() {- ui.label(RichText::new(crate::i18n::tr("No albums match")).weak());+ if picked.is_some() {+ ui.close();}- }- });- // Esc closes the popup and stops there: the dialog around it keeps its edits- if ui.input(|i| i.key_pressed(egui::Key::Escape)) {- crate::widgets::take_escape(ui.ctx());- ui.close();- }- if picked.is_some() {- ui.close();- }- });- ui.data_mut(|d| d.insert_temp(key, state));+ picked+ },+ )+ .flatten();+ ui.data_mut(|d| d.insert_temp(key, state.into_inner()));if let Some(p) = picked {*value = Some(p);button.mark_changed();