diff --git a/crates/layers/src/ui/panel/body/map.rs b/crates/layers/src/ui/panel/body/map.rs index bada4da1f..41d023c12 100644 --- a/crates/layers/src/ui/panel/body/map.rs +++ b/crates/layers/src/ui/panel/body/map.rs @@ -12,7 +12,7 @@ use super::super::Session; use super::super::context::PanelContext; use super::super::drag::{LayersDrag, drop_event, mask_drop_event}; use super::super::events::{ActivateOn, LayerColumn, LayerSelect, LayerToggle, LayersEvent, MenuTarget}; -use super::super::menu::OpenMenu; +use super::super::menu::{OpenMenu, shared_label}; use super::super::row::{EYE, FX_PART, LOCK_PART, NAME, OPTIONS_PART}; use super::rows::{ToggleSweeps, extra_parts, thumb_click_replaces_select, thumb_part}; use crate::ui::tree::{LayerRowKey, LayerTree}; @@ -99,9 +99,7 @@ pub(super) fn map_events( let selected = cx.selection.layers.contains(&layer); if eye { let rows = if selected { cx.selection.layers.clone() } else { vec![layer] }; - let mut labels = rows.iter().map(|id| tree.info(id).label); - let first = labels.next().flatten(); - let current = if labels.all(|label| label == first) { first } else { None }; + let current = shared_label(tree, &rows); session.menu = Some(OpenMenu::Eye { rows, current }); } else if plain { if !selected { diff --git a/crates/layers/src/ui/panel/events.rs b/crates/layers/src/ui/panel/events.rs index 9c4e5c6a0..bb8d2df09 100644 --- a/crates/layers/src/ui/panel/events.rs +++ b/crates/layers/src/ui/panel/events.rs @@ -42,8 +42,8 @@ pub enum LayersEvent { /// Escape during an Alt eye sweep: undo the `ShowOnly` of that sweep, which turned the other layers' eyes off. /// The key is the layer the sweep began on. UndoShowOnly(LayerRowKey), - /// A pick in the eye cell's colour menu (None and the label colours): for every selected - /// layer when the row is in a multi-selection, else for the row. + /// A pick in the eye cell's colour menu or the row menu's Label Color submenu (None and the label colours): for + /// every selected layer when the row is in a multi-selection, else for the row. #[non_exhaustive] LabelColor { /// The layers to colour. diff --git a/crates/layers/src/ui/panel/menu.rs b/crates/layers/src/ui/panel/menu.rs index c683097ab..255c2b813 100644 --- a/crates/layers/src/ui/panel/menu.rs +++ b/crates/layers/src/ui/panel/menu.rs @@ -1,10 +1,11 @@ -//! The panel's commands and keys, its panel-menu rows, the row menu and the eye's label-colour menu. +//! The panel's commands and keys, its panel-menu rows, the row menu with its Label Color submenu and the eye's +//! label-colour menu. use craft_theme::LabelColor; use craft_ui::contract::{CommandSet, Entry, Show as _, ShowContainer as _}; use craft_ui::display::Divider; use craft_ui::list::{ListWords, label_color_label}; -use craft_ui::menu::{Menu, MenuItem, PanelRow}; +use craft_ui::menu::{Menu, MenuItem, PanelRow, entry_rows}; use craft_ui::runtime::commands::CommandRef; use egui::{Response, Ui}; use std::sync::Arc; @@ -16,7 +17,7 @@ use super::drag::stacking_event; use super::events::{LayersEvent, MenuTarget}; use crate::ui::options::LayersSettings; use crate::ui::thumbs::ThumbContents; -use crate::ui::tree::LayerTree; +use crate::ui::tree::{LayerRowKey, LayerTree}; /// The commands of the Layers panel: its panel-menu rows and the stacking keys. /// @@ -158,8 +159,15 @@ pub(super) enum OpenMenu { /// The host's menu for a row: its entries for where it was opened. pub(super) type RowMenu<'a, Id> = dyn Fn(&MenuTarget) -> Vec> + 'a; -/// The label colour rows of the eye column's menu: None, a rule, then each colour on its own tint, the current one -/// checked. Returns the colour a row picked. +/// The colour `rows` share, `None` when they have none or differ: the check in the label colour menus. +pub(super) fn shared_label(tree: &T, rows: &[T::Id]) -> Option { + let mut labels = rows.iter().map(|id| tree.info(id).label); + let first = labels.next().flatten(); + if labels.all(|label| label == first) { first } else { None } +} + +/// The label colour rows of the eye column's menu and the row menu's Label Color submenu: None, a rule, then each +/// colour on its own tint, the current one checked. Returns the colour a row picked. fn eye_rows(ui: &mut Ui, words: &ListWords, current: Option) -> Option> { let mut picked = None; if MenuItem::new(label_color_label(None, words)).check(current.is_none()).show(ui).ran { @@ -174,13 +182,31 @@ fn eye_rows(ui: &mut Ui, words: &ListWords, current: Option) -> Opti picked } +/// What a row menu pick ran: the host's command, or a colour of the Label Color submenu. +enum RowPick { + Command(String), + Color(Option), +} + +/// The layers the row menu's Label Color submenu colours: every selected layer when the menu acts on the selection, +/// else the row's layer. `None` where the submenu is not shown: on a thumbnail, whose menu is the host's own, and on +/// effects and Smart Filter rows. +fn label_rows(target: &MenuTarget, selected: &[Id]) -> Option> { + let LayerRowKey::Layer(layer) = &target.row else { return None }; + if target.part.is_some() { + return None; + } + Some(if target.on_selection { selected.to_vec() } else { vec![layer.clone()] }) +} + /// Shows the menu the last context request chose, hanging from `opener` (the list). A right-click -/// on a row opens it at the pointer. +/// on a row opens it at the pointer. With `label_colors`, a layer row's menu ends with the Label Color submenu. pub(super) fn show_row_menus( ui: &mut Ui, cx: &PanelContext<'_, T>, opener: &Response, row_menu: Option<&RowMenu<'_, T::Id>>, + label_colors: bool, session: &Session, ) -> Vec> { let words = cx.words; @@ -188,8 +214,25 @@ pub(super) fn show_row_menus( Some(OpenMenu::Row(target)) => { let Some(menu) = row_menu else { return Vec::new() }; let entries = menu(&target); - let picked = Menu::context(words.row_menu.clone(), opener).pick(ui, &entries).picked; - picked.map(|command| LayersEvent::Menu { target, command }).into_iter().collect() + let colored = if label_colors { label_rows(&target, &cx.selection.layers) } else { None }; + let current = colored.as_deref().and_then(|rows| shared_label(cx.tree, rows)); + let shown = Menu::context(words.row_menu.clone(), opener).show(ui, |ui, _| { + let ran = entry_rows(ui, &entries).and_then(|at| entries.get(at)).and_then(Entry::value).cloned(); + let mut color = None; + if colored.is_some() { + let _ = Menu::submenu_row(ui, words.label_color_menu.clone(), |ui, _| { + if let Some(picked) = eye_rows(ui, cx.list_words, current) { + color = Some(picked); + } + }); + } + ran.map(RowPick::Command).or(color.map(RowPick::Color)) + }); + match shown.inner.flatten() { + Some(RowPick::Command(command)) => vec![LayersEvent::Menu { target, command }], + Some(RowPick::Color(color)) => colored.map(|rows| LayersEvent::LabelColor { rows, color }).into_iter().collect(), + None => Vec::new(), + } } Some(OpenMenu::Eye { rows, current }) => { let picked = Menu::context(words.label_color_menu.clone(), opener).show(ui, |ui, _| eye_rows(ui, cx.list_words, current)).inner.flatten(); diff --git a/crates/layers/src/ui/panel/mod.rs b/crates/layers/src/ui/panel/mod.rs index 94fa6ebcf..89b2e0bdc 100644 --- a/crates/layers/src/ui/panel/mod.rs +++ b/crates/layers/src/ui/panel/mod.rs @@ -169,6 +169,7 @@ pub struct LayersPanel<'a, T: LayerTree> { filter_bar: bool, find_name: bool, row_menu: Option>>, + row_label_colors: bool, trailing: Option + 'a>>, reveal: Option, rename: Option, @@ -213,6 +214,7 @@ impl<'a, T: LayerTree> LayersPanel<'a, T> { filter_bar: true, find_name: false, row_menu: None, + row_label_colors: true, trailing: None, reveal: None, rename: None, @@ -282,6 +284,14 @@ impl<'a, T: LayerTree> LayersPanel<'a, T> { self } + /// Ends the row menu of a layer row with the Label Color submenu (default on): the eye menu's colours, for the + /// selected layers when the menu acts on the selection; a pick is [`LayersEvent::LabelColor`]. A thumbnail's menu + /// and the effects and Smart Filter rows' menus have none. + pub fn row_label_colors(mut self, on: bool) -> Self { + self.row_label_colors = on; + self + } + /// The host's component at each layer row's trailing end, after the indicators and never dropped for the name /// (Illustrator's target circle, a selection square): `build` makes the layer's component and `output` gets what /// it reports, with the layer, in the pass it is shown. @@ -318,6 +328,7 @@ impl<'a, T: LayerTree> LayersPanel<'a, T> { filter_bar, find_name, row_menu, + row_label_colors, trailing, reveal, rename, @@ -390,7 +401,7 @@ impl<'a, T: LayerTree> LayersPanel<'a, T> { start_rename(ui, list, layer); } let body = show_body(ui, &cx, &rows, session, &plan, rows.filtered, &unlock_label); - let menus = show_row_menus(ui, &cx, &body.response, row_menu.as_deref(), session); + let menus = show_row_menus(ui, &cx, &body.response, row_menu.as_deref(), row_label_colors, session); let mut reported = body.events; reported.extend(menus); while let Some((key, _)) = stack.take_command::() { diff --git a/crates/layers/tests/it/panel_drag_menu.rs b/crates/layers/tests/it/panel_drag_menu.rs index 4a3bcc385..eb6469709 100644 --- a/crates/layers/tests/it/panel_drag_menu.rs +++ b/crates/layers/tests/it/panel_drag_menu.rs @@ -1,12 +1,13 @@ -//! `LayersPanel` drags and menus: a mask thumbnail dropped on another layer, and what a drag carries for the app's -//! own drop targets. +//! `LayersPanel` drags and menus: a mask thumbnail dropped on another layer, what a drag carries for the app's own +//! drop targets, and the row menu's Label Color submenu. use craft_behavior::EditEvent; use craft_layers::ui::mock::FakeLayers; use craft_layers::ui::panel::{LayerDrop, LayersDrag, LayersEvent, LayersPanel, MaskMove}; use craft_layers::ui::tree::LayerPart; +use craft_theme::LabelColor; use craft_ui::contract::Entry; -use craft_ui::testing::{Fixture, Target, trial}; +use craft_ui::testing::{Fixture, Target, pointer, trial}; use egui::{Modifiers, Pos2, Vec2}; use std::cell::RefCell; use std::rc::Rc; @@ -89,3 +90,27 @@ fn a_row_drag_carries_its_layers_for_targets_outside_the_panel() { assert!(held(&fixture).layers().is_empty(), "a mask is no layer"); fixture.release_at(mask + away).idle(2); } + +/// The row menu ends with the Label Color submenu, whose pick colours the row's layer as the eye's menu does. +#[trial] +fn the_row_menus_label_color_submenu_colours_the_layer() { + let (mut fixture, seen) = rig(); + let title = node(&fixture, "Title").center(); + fixture.frame(vec![pointer::moved(title)]); + fixture.frame(vec![pointer::secondary(title, true)]); + fixture.frame(vec![pointer::secondary(title, false)]); + fixture.idle(3); + let submenu = node(&fixture, "Label Color"); + fixture.click(&submenu).idle(3); + let red = node(&fixture, "Red"); + fixture.click(&red).idle(2); + let picked: Vec<(Vec, Option)> = seen + .borrow() + .iter() + .filter_map(|event| match event { + LayersEvent::LabelColor { rows, color, .. } => Some((rows.clone(), *color)), + _ => None, + }) + .collect(); + assert_eq!(picked, [(vec![4], Some(LabelColor::Red))]); +}