Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(ui): grey out Select and Mask in the options bar without a selection (#7520)

VishnuByrraju1ee1354530ab
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 8bf2be90d..6c800afda 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -3482,3 +3482,5 @@ mod channel_tint_tests {
#[cfg(test)]
mod options_hit_area_tests;
+#[cfg(test)]
+mod select_and_mask_button_tests;
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs
index 18cef34c9..f47e3c8a5 100644
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@ -1193,9 +1193,7 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
widgets::value_field(ui, &mut o.marquee_height, range, unit, 62.0);
});
widgets::vline(ui, 22.0);
- if widgets::secondary_button(ui, tl!("Select and Mask…"), 0.0).clicked() {
- let _ = crate::menus::invoke(app, ui.ctx(), "select.selectAndMask", json!({}));
- }
+ select_and_mask_button(app, ui);
}
Tool::Lasso | Tool::PolygonLasso | Tool::MagneticLasso if t.pro => {
selection_mode_buttons(app, ui);
@@ -1220,9 +1218,7 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
}
widgets::vline(ui, 22.0);
}
- if widgets::secondary_button(ui, tl!("Select and Mask…"), 0.0).clicked() {
- let _ = crate::menus::invoke(app, ui.ctx(), "select.selectAndMask", json!({}));
- }
+ select_and_mask_button(app, ui);
if crate::lasso_ui::active(app) || (app.ui.tool == Tool::PolygonLasso && !app.ui.polygon.is_empty()) {
hint(
ui,
@@ -1257,9 +1253,7 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
let r = app.run("select.subject", json!({}));
crate::retouch_ui::report_smart_result(app, r, tl!("No subject found — try Object Selection or the Lasso"));
}
- if widgets::secondary_button(ui, tl!("Select and Mask…"), 0.0).clicked() {
- let _ = crate::menus::invoke(app, ui.ctx(), "select.selectAndMask", json!({}));
- }
+ select_and_mask_button(app, ui);
}
Tool::PaintBucket if t.pro => {
opt_label(ui, tl!("Fill"));
@@ -4493,6 +4487,16 @@ fn effect_rows(app: &mut PhotocraftApp, ui: &mut egui::Ui, l: &Layer, depth: usi
}
}
+/// The options bar's Select and Mask… button: greyed out, as the menu item is, until there is a
+/// selection (or a layer mask) to refine (#7520).
+fn select_and_mask_button(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
+ let enabled = crate::menus::is_enabled(app, "select.selectAndMask");
+ let clicked = ui.add_enabled_ui(enabled, |ui| widgets::secondary_button(ui, tl!("Select and Mask…"), 0.0).clicked()).inner;
+ if clicked {
+ let _ = crate::menus::invoke(app, ui.ctx(), "select.selectAndMask", json!({}));
+ }
+}
+
/// New / add / subtract / intersect selection buttons (shared by selection tools).
fn selection_mode_buttons(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
ui.spacing_mut().item_spacing.x = 2.0;
diff --git a/crates/ui-egui/src/select_and_mask_button_tests.rs b/crates/ui-egui/src/select_and_mask_button_tests.rs
new file mode 100644
index 000000000..5426b778a
--- /dev/null
+++ b/crates/ui-egui/src/select_and_mask_button_tests.rs
@@ -0,0 +1,46 @@
+//! #7520: the options bar's Select and Mask… button is greyed out while nothing is selected.
+use crate::PhotocraftApp;
+use crate::state::Tool;
+use egui::vec2;
+use egui_kittest::{
+ Harness,
+ kittest::{NodeT, Queryable},
+};
+use serde_json::json;
+
+fn harness(tool: Tool, select: bool) -> Harness<'static, PhotocraftApp> {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 64, "height": 64})).unwrap();
+ app.sync_views();
+ if select {
+ app.run("select.all", json!({})).unwrap();
+ }
+ app.ui.tool = tool;
+ let mut h = Harness::builder().with_size(vec2(1400.0, 60.0)).build_ui_state(
+ |ui, app: &mut PhotocraftApp| {
+ if ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
+ crate::panels::options_bar(app, ui);
+ }
+ },
+ app,
+ );
+ PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::Pro);
+ h.run_steps(4);
+ h
+}
+
+#[test]
+fn select_and_mask_is_disabled_without_a_selection() {
+ for tool in [Tool::RectMarquee, Tool::Lasso, Tool::MagicWand] {
+ let h = harness(tool, false);
+ assert!(h.get_by_label("Select and Mask…").accesskit_node().is_disabled(), "{tool:?}: button should be greyed out with no selection");
+ }
+}
+
+#[test]
+fn select_and_mask_is_enabled_with_a_selection() {
+ for tool in [Tool::RectMarquee, Tool::Lasso, Tool::MagicWand] {
+ let h = harness(tool, true);
+ assert!(!h.get_by_label("Select and Mask…").accesskit_node().is_disabled(), "{tool:?}: button should be live with a selection");
+ }
+}