diff --git a/crates/imaging/src/ui/channels/footer.rs b/crates/imaging/src/ui/channels/footer.rs index 3685bdbbc..8c93b54b3 100644 --- a/crates/imaging/src/ui/channels/footer.rs +++ b/crates/imaging/src/ui/channels/footer.rs @@ -9,13 +9,13 @@ use craft_ui::sections::{FooterAction, FooterBar}; use egui::{Id, Ui}; use super::labels::ChannelsWords; -use super::model::{ChannelKind, ChannelRow, ChannelsEvent, targeted_row}; +use super::model::{ChannelKind, ChannelRow, ChannelsEvent, targeted_for_delete, targeted_row}; /// A button of the Channels footer. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[non_exhaustive] pub enum ChannelsFooterButton { - /// Delete current channel: disabled while the target is the composite, a colour channel or Quick Mask. + /// Delete the targeted channel when it can be deleted. Delete, /// Create new channel; a row dropped on it is duplicated. New, @@ -26,19 +26,20 @@ pub enum ChannelsFooterButton { } /// What a row drag carries for the footer's buttons: the list it started in, the row the pointer grabbed and that -/// row's kind. +/// row's deletion capabilities. #[derive(Clone, Debug, PartialEq)] pub(super) struct ChannelDrag { pub(super) list: Id, pub(super) grabbed: K, pub(super) kind: ChannelKind, + pub(super) can_delete_color: bool, } -/// Whether `button` takes a drop of a row of `kind`: Delete refuses the composite, colour channels and Quick Mask; -/// New and Load take every row; Save takes none. -pub(super) fn accepts_footer_drop(button: ChannelsFooterButton, kind: ChannelKind) -> bool { +/// Delete takes saved channels, a layer mask or an opted-in process colour. New and Load take +/// every row. Save takes none. +fn accepts_footer_drop(button: ChannelsFooterButton, kind: ChannelKind, can_delete_color: bool) -> bool { match button { - ChannelsFooterButton::Delete => !matches!(kind, ChannelKind::Composite | ChannelKind::Color | ChannelKind::QuickMask(_)), + ChannelsFooterButton::Delete => can_delete_color || !matches!(kind, ChannelKind::Composite | ChannelKind::Color | ChannelKind::QuickMask(_)), ChannelsFooterButton::New | ChannelsFooterButton::LoadSelection => true, ChannelsFooterButton::SaveSelection => false, } @@ -63,7 +64,7 @@ fn button_at(at: usize) -> Option { /// Shows the footer and returns its events in order. /// /// - Buttons from the leading end: Load channel as selection, Save selection as channel, New, Delete. Delete is -/// disabled for a click while the targeted row is the composite, a colour channel or Quick Mask, or there is none. +/// disabled for a click unless the target is deletable. A process colour must be the only process target. /// - A click reports [`ChannelsEvent::Footer`] with `rows` = [`targeted_row`] and the modifiers held. /// - Delete, New and Load take a dragged row of the panel's list: a drop reports [`ChannelsEvent::FooterDrop`] with /// the row the pointer grabbed. A button shows the drop outline, and Delete reads enabled, while the grabbed row is @@ -75,8 +76,11 @@ pub(super) fn show_footer( list: Id, ) -> Vec> { let target = targeted_row(rows); - let delete_enabled = target.is_some_and(|row| accepts_footer_drop(ChannelsFooterButton::Delete, row.kind)); - let takes = |button: ChannelsFooterButton| move |drag: &ChannelDrag| drag.list == list && accepts_footer_drop(button, drag.kind); + let delete_target = targeted_for_delete(rows); + let delete_enabled = delete_target.is_some_and(|row| accepts_footer_drop(ChannelsFooterButton::Delete, row.kind, row.can_delete_color())); + let takes = |button: ChannelsFooterButton| { + move |drag: &ChannelDrag| drag.list == list && accepts_footer_drop(button, drag.kind, drag.can_delete_color) + }; let icon = |name: &'static str| IconSpec::svg(IconId::new(name)); let actions = [ FooterAction::new(icon("trash"), words.delete.clone()) @@ -90,6 +94,7 @@ pub(super) fn show_footer( let out = FooterBar::actions(actions).show_footer(ui); let mut events = Vec::new(); if let Some(button) = out.picked.and_then(button_at) { + let target = if button == ChannelsFooterButton::Delete { delete_target } else { target }; events.push(ChannelsEvent::Footer { button, rows: target.map(|row| row.key.clone()).into_iter().collect(), mods: out.mods }); } if let Some(dropped) = &out.dropped diff --git a/crates/imaging/src/ui/channels/menu.rs b/crates/imaging/src/ui/channels/menu.rs index 67b135b0a..8a8d402c0 100644 --- a/crates/imaging/src/ui/channels/menu.rs +++ b/crates/imaging/src/ui/channels/menu.rs @@ -12,7 +12,7 @@ use egui::{Response, Ui}; use std::sync::Arc; use super::labels::ChannelsWords; -use super::model::{ChannelKind, ChannelRow, ChannelsEvent, ColorIndicates, OVERLAY_OPACITIES, OverlayPreset, targeted_row}; +use super::model::{ChannelKind, ChannelRow, ChannelsEvent, ColorIndicates, OVERLAY_OPACITIES, OverlayPreset, targeted_for_delete, targeted_row}; /// The commands of the Channels panel: its panel-menu rows, its row menu, and the keys of the list. /// @@ -217,10 +217,14 @@ impl ChannelsCommands { } } + fn applies_to_row(self, row: Option<&ChannelRow>) -> bool { + self.applies_to(row.map(|row| row.kind)) || (self == Self::Delete && row.is_some_and(ChannelRow::can_delete_color)) + } + /// The event the command makes over `rows` with the modifiers `mods` held; `None` when it does nothing there. /// Rename has no event: the panel opens the row's rename field. pub fn event(self, rows: &[ChannelRow], mods: Mods) -> Option> { - let target = targeted_row(rows)?; + let target = if self == Self::Delete { targeted_for_delete(rows) } else { targeted_row(rows) }?; match self { ChannelsCommands::LoadSelection => { Some(ChannelsEvent::LoadSelection { key: target.key.clone(), operation: SelectionOperation::from_mods(mods) }) @@ -229,7 +233,7 @@ impl ChannelsCommands { ChannelsCommands::ChannelOptions => target.kind.has_options().then(|| ChannelsEvent::Options(target.key.clone())), ChannelsCommands::RenameChannel => None, command => { - self.applies_to(Some(target.kind)).then(|| ChannelsEvent::Command { command, key: command_key(command).then(|| target.key.clone()) }) + self.applies_to_row(Some(target)).then(|| ChannelsEvent::Command { command, key: command_key(command).then(|| target.key.clone()) }) } } } @@ -294,7 +298,7 @@ fn add_commands(lines: &mut Vec, commands: &[ChannelsCommands]) { } /// The menu lines for a row of `kind`; with no row, the document's. -fn plan(kind: Option) -> Vec { +fn plan(kind: Option, can_delete_color: bool) -> Vec { use ChannelsCommands as C; let mut lines: Vec = Vec::new(); match kind { @@ -313,6 +317,7 @@ fn plan(kind: Option) -> Vec { lines.extend(overlay_plan()); } Some(ChannelKind::LayerMask(_)) => add_commands(&mut lines, &[C::ToggleLayerMask, C::DeleteLayerMask, C::ChannelOptions]), + Some(ChannelKind::Color) if can_delete_color => add_commands(&mut lines, &[C::Duplicate, C::Delete]), Some(ChannelKind::Composite | ChannelKind::Color) => add_commands(&mut lines, &[C::Duplicate]), None => {} } @@ -342,10 +347,17 @@ pub fn panel_rows() -> Vec { ] } -/// Each command greyed by the targeted row's kind, the one rule the panel's publish loop and the dock's panel menu share. +/// Enabled states shared by the panel and the dock's panel menu. pub fn states(rows: &[ChannelRow]) -> Vec<(ChannelsCommands, CommandState)> { - let kind = targeted_row(rows).map(|row| row.kind); - ChannelsCommands::ALL.into_iter().map(|command| (command, CommandState::new().enabled(command.applies_to(kind)))).collect() + let target = targeted_row(rows); + let delete_target = targeted_for_delete(rows); + ChannelsCommands::ALL + .into_iter() + .map(|command| { + let row = if command == ChannelsCommands::Delete { delete_target } else { target }; + (command, CommandState::new().enabled(command.applies_to_row(row))) + }) + .collect() } fn label_of(command: ChannelsCommands, kind: Option, words: &ChannelsWords) -> Label { @@ -403,7 +415,7 @@ fn checked(command: ChannelsCommands, kind: Option) -> Option pub(super) fn show_row_menu(ui: &mut Ui, opener: &Response, row: &ChannelRow, words: &ChannelsWords) -> Option { let kind = Some(row.kind); let units = Env::of(ui).units(); - let lines = plan(kind); + let lines = plan(kind, row.can_delete_color()); let shown = Menu::context(words.row_menu.clone(), opener).show(ui, |ui, _| { use craft_ui::contract::Show as _; let mut ran = None; @@ -413,7 +425,7 @@ pub(super) fn show_row_menu(ui: &mut Ui, opener: &Response, row: &ChannelRow< craft_ui::display::Divider::horizontal().show(ui); } Plan::Command(command) => { - let mut item = MenuItem::new(label_of(command, kind, words)).enabled(command.applies_to(kind)); + let mut item = MenuItem::new(label_of(command, kind, words)).enabled(command.applies_to_row(Some(row))); if let Some(on) = checked(command, kind) { item = item.radio_mark(on); } diff --git a/crates/imaging/src/ui/channels/model.rs b/crates/imaging/src/ui/channels/model.rs index a7f8247aa..a7f8c4dfa 100644 --- a/crates/imaging/src/ui/channels/model.rs +++ b/crates/imaging/src/ui/channels/model.rs @@ -115,18 +115,23 @@ pub struct ChannelRow { /// The app's highlight: the targeted channel, or every colour row while the composite is /// targeted (PhotoCraft). pub targeted: bool, + /// The host supports deleting this individual process colour row. Off by default. + pub allow_color_deletion: bool, } impl ChannelRow { /// A visible, untargeted row. pub fn new(key: K, kind: ChannelKind, name: impl Into) -> Self { - Self { key, kind, name: name.into(), visible: true, targeted: false } + Self { key, kind, name: name.into(), visible: true, targeted: false, allow_color_deletion: false } + } + + pub(super) fn can_delete_color(&self) -> bool { + self.kind == ChannelKind::Color && self.allow_color_deletion } } -/// The row the footer's Delete and Load act on: the targeted layer-mask, alpha, spot or Quick -/// Mask row; else the composite when it is targeted; else the one targeted colour row; `None` -/// with no rows or no target. +/// Target-based commands prefer a targeted layer mask, alpha, spot or Quick Mask row. +/// Otherwise they use the targeted composite or the first targeted process colour. pub fn targeted_row(rows: &[ChannelRow]) -> Option<&ChannelRow> { let targeted = |kinds: fn(ChannelKind) -> bool| rows.iter().find(|row| row.targeted && kinds(row.kind)); targeted(|kind| !matches!(kind, ChannelKind::Composite | ChannelKind::Color)) @@ -134,6 +139,12 @@ pub fn targeted_row(rows: &[ChannelRow]) -> Option<&ChannelRow> { .or_else(|| targeted(|kind| matches!(kind, ChannelKind::Color))) } +/// Target-based deletion needs one component; an explicit row menu or drag already identifies it. +pub(super) fn targeted_for_delete(rows: &[ChannelRow]) -> Option<&ChannelRow> { + let row = targeted_row(rows)?; + (row.kind != ChannelKind::Color || rows.iter().filter(|row| row.targeted && row.kind == ChannelKind::Color).take(2).count() == 1).then_some(row) +} + /// What the Channels panel reports, in the order it happened. #[derive(Clone, Debug, PartialEq)] #[non_exhaustive] diff --git a/crates/imaging/src/ui/channels/panel.rs b/crates/imaging/src/ui/channels/panel.rs index a2ca34c22..9651cc020 100644 --- a/crates/imaging/src/ui/channels/panel.rs +++ b/crates/imaging/src/ui/channels/panel.rs @@ -22,7 +22,7 @@ use egui::Ui; use super::footer::{ChannelDrag, show_footer}; use super::labels::{ChannelsWords, shortcut_numbers}; use super::menu::{ChannelsCommands, lands, show_row_menu}; -use super::model::{ChannelKind, ChannelRow, ChannelsEvent, ChannelsOutput, targeted_row}; +use super::model::{ChannelKind, ChannelRow, ChannelsEvent, ChannelsOutput, targeted_for_delete, targeted_row}; use super::rows::{EYE, NAME, RowContext, ThumbHost, channel_row}; /// What the panel keeps between passes, in its root slot: the row the last context request opened the menu on. @@ -93,7 +93,7 @@ impl ItemSource for Order<'_, K> { /// [`Rename`](ChannelsEvent::Rename). /// - **Drag**: alpha and spot rows reorder, before or after another one or before Quick Mask /// ([`Reorder`](ChannelsEvent::Reorder); Cmd/Ctrl+] and [ too). Any row drops on the footer's New and Load buttons, -/// and an alpha or spot row on Delete; a drop acts on the row grabbed. +/// and a deletable row on Delete. A drop acts on the row grabbed. /// - **Menus**: a right-click or the Menu key opens the row's context menu; [`panel_rows`](super::panel_rows) is the /// panel menu's fixed list, each command greyed by [`states`](super::states). /// - **Right to left**: the list and the footer mirror; the chord sits at the trailing end. @@ -172,7 +172,7 @@ impl<'a, K: Clone + Eq + Hash + Debug + Send + Sync + 'static> ChannelsPanel<'a, let lead = targeted_row(rows).map(|row| row.key.clone()); let mut selection = Selection::new(rows.iter().filter(|row| row.targeted).map(|row| row.key.clone()).collect(), lead.clone(), lead); let kinds: Vec<(K, ChannelKind)> = rows.iter().map(|row| (row.key.clone(), row.kind)).collect(); - let payload_kinds = kinds.clone(); + let payload_rows: Vec<_> = rows.iter().map(|row| (row.key.clone(), row.kind, row.can_delete_color())).collect(); let kind_of = |key: &K| kinds.iter().find(|(k, _)| k == key).map(|(_, kind)| *kind); let columns = vec![ ColumnSpec::toggle(EYE, Label::Empty, ToggleColumn::sweepable()).icon(IconId::new("eye")), @@ -198,8 +198,8 @@ impl<'a, K: Clone + Eq + Hash + Debug + Send + Sync + 'static> ChannelsPanel<'a, lands(&remaining, insert.saturating_sub(moved_before)) }) .drag_payload(move |drag: &ListDrag| { - let kind = payload_kinds.iter().find(|(key, _)| *key == drag.grabbed).map(|(_, kind)| *kind)?; - Some(ChannelDrag { list: drag.list, grabbed: drag.grabbed.clone(), kind }) + let (_, kind, can_delete_color) = payload_rows.iter().find(|(key, _, _)| *key == drag.grabbed)?; + Some(ChannelDrag { list: drag.list, grabbed: drag.grabbed.clone(), kind: *kind, can_delete_color: *can_delete_color }) }) .show_items(ui, &order, &mut |_key, item| { let at = item.at; @@ -264,7 +264,11 @@ fn map_events( } CollectionEvent::Reorder(edit) => events.push(ChannelsEvent::Reorder(edit)), CollectionEvent::Delete(keys) => { - for key in keys.into_iter().filter(|key| kind_of(key).is_some_and(ChannelKind::moves)) { + let delete_color = matches!( + keys.as_slice(), + [key] if targeted_for_delete(rows).is_some_and(|row| &row.key == key && row.can_delete_color()) + ); + for key in keys.into_iter().filter(|key| delete_color || kind_of(key).is_some_and(ChannelKind::moves)) { events.push(ChannelsEvent::Command { command: ChannelsCommands::Delete, key: Some(key) }); } } diff --git a/crates/imaging/tests/it/channels_delete.rs b/crates/imaging/tests/it/channels_delete.rs new file mode 100644 index 000000000..15c870535 --- /dev/null +++ b/crates/imaging/tests/it/channels_delete.rs @@ -0,0 +1,100 @@ +//! Deleting one process row uses its own key, from the context menu, keyboard and trash button. + +use craft_ui::testing::{Fixture, pointer, trial}; +use egui::{Event, Key, Modifiers, PointerButton, Vec2}; +use std::cell::RefCell; +use std::rc::Rc; + +use crate::ui::channels::{ChannelKind, ChannelRow, ChannelsCommands, ChannelsEvent, ChannelsFooterButton, ChannelsPanel}; + +const RGB: u8 = 0; +const GREEN: u8 = 1; +const BLUE: u8 = 2; +type Events = Rc>>>; + +fn panel(targets: &[u8], allow_color_deletion: bool) -> (Fixture, Events) { + let rows = + [(RGB, ChannelKind::Composite, "RGB"), (GREEN, ChannelKind::Color, "Green"), (BLUE, ChannelKind::Color, "Blue")].map(|(key, kind, name)| { + let mut row = ChannelRow::new(key, kind, name); + row.targeted = targets.contains(&key); + row.allow_color_deletion = allow_color_deletion; + row + }); + let events = Events::default(); + let reported = Rc::clone(&events); + let mut fixture = Fixture::new(move |ui| { + reported.borrow_mut().extend(ChannelsPanel::new(&rows).show(ui).events); + }); + fixture.room(Vec2::new(900.0, 700.0)).idle(6); + (fixture, events) +} + +fn context(fixture: &mut Fixture, name: &str) { + let pos = fixture.by_label(name).expect("a named channel row").center(); + fixture.frame(vec![pointer::moved(pos)]); + for pressed in [true, false] { + fixture.frame(vec![Event::PointerButton { pos, button: PointerButton::Secondary, pressed, modifiers: Modifiers::NONE }]); + } + fixture.idle(2); +} + +fn deleted(events: &Events) -> Vec { + events + .borrow() + .iter() + .filter_map(|event| match event { + ChannelsEvent::Command { command: ChannelsCommands::Delete, key: Some(key), .. } => Some(std::slice::from_ref(key)), + ChannelsEvent::Footer { button: ChannelsFooterButton::Delete, rows, .. } + | ChannelsEvent::FooterDrop { button: ChannelsFooterButton::Delete, rows, .. } => Some(rows.as_slice()), + _ => None, + }) + .flatten() + .copied() + .collect() +} + +#[trial] +fn process_channel_delete_uses_the_clicked_or_dragged_row() { + let (mut menu, events) = panel(&[RGB, GREEN, BLUE], true); + context(&mut menu, "Green"); + assert!(menu.by_label("Duplicate Channel").is_some(), "the process row opens its regular context menu"); + let delete = menu.by_label("Delete Channel").expect("the process row's Delete Channel menu item"); + menu.click(&delete).idle(2); + assert_eq!(deleted(&events), [GREEN], "the menu deletes the right-clicked row, even with the composite targeted"); + + let (mut drag, events) = panel(&[RGB, GREEN, BLUE], true); + let row = drag.by_label("Green").expect("the Green row"); + let trash = drag.by_label("Delete current channel").expect("the trash button"); + drag.drag(&row, trash.center() - row.center()).idle(3); + assert_eq!(deleted(&events), [GREEN], "the drop deletes the grabbed row, even with the composite targeted"); +} + +#[trial] +fn process_channel_delete_from_the_keyboard_and_footer_requires_one_target() { + let (mut single, events) = panel(&[GREEN], true); + let row = single.by_label("Green").unwrap(); + single.click(&row); + assert!(single.focused_role().is_some(), "clicking the process row gives the list focus"); + single.key(Key::Delete, Modifiers::NONE).idle(3); + assert_eq!(deleted(&events), [GREEN], "Delete on one focused process row"); + events.borrow_mut().clear(); + let trash = single.by_label("Delete current channel").unwrap(); + single.click(&trash).idle(2); + assert_eq!(deleted(&events), [GREEN], "the trash button deletes a single targeted process row"); + + for (targets, opted_in) in [(&[GREEN, BLUE][..], true), (&[GREEN][..], false), (&[RGB, GREEN, BLUE][..], true)] { + let (mut blocked, events) = panel(targets, opted_in); + let name = if targets.contains(&RGB) { "RGB" } else { "Green" }; + let row = blocked.by_label(name).unwrap(); + blocked.click(&row).key(Key::Delete, Modifiers::NONE).idle(2); + let trash = blocked.by_label("Delete current channel").unwrap(); + blocked.click(&trash).idle(2); + assert!(deleted(&events).is_empty(), "ambiguous, composite and unconfigured targets have no process delete"); + if !opted_in || targets.contains(&RGB) { + blocked.drag(&row, trash.center() - row.center()).idle(3); + assert!(deleted(&events).is_empty(), "this row also refuses a trash drop"); + context(&mut blocked, name); + assert!(blocked.by_label("Delete Channel").is_none(), "this row's context menu keeps the default"); + } + } +} diff --git a/crates/imaging/tests/it/main.rs b/crates/imaging/tests/it/main.rs index 4b4b02eff..a6c7a4b9b 100644 --- a/crates/imaging/tests/it/main.rs +++ b/crates/imaging/tests/it/main.rs @@ -3,6 +3,7 @@ mod adjust_editors; mod adjust_tone; mod blur_pins; +mod channels_delete; mod color_range; mod contour_field; mod curve;