Merge #17982 into the merge queue batch

fix(ui): name the targeted channel in the document tab title (#17711)
This commit is contained in:
David Mann committed 2026-11-29 21:15:41 +01:00
commit 761c7fe4a9
3 files changed
+116 -6

No files matched your search

+22 -5
View File
@@ -223,20 +223,33 @@ pub(crate) fn tab_context_items(doc: DocId, index: usize, count: usize, has_path
}
/// Photoshop's tab title: "name @ 50% (Layer 1, RGB/8)" at `zoom` percent, with "(Layer 1, Layer Mask/8)" when the
/// mask is targeted and "(Quick Mask/8)" in Quick Mask mode; the Background layer's name is omitted. The tab's
/// modified mark shows unsaved changes, so the text carries none.
/// mask is targeted, "(Layer 1, Quick Mask/8)" in Quick Mask mode and "(Layer 1, Red/8)" or "(Layer 1, Alpha 1/8)"
/// when a single colour or alpha channel is; the Background layer's name is omitted. The tab's modified mark shows
/// unsaved changes, so the text carries none.
pub(crate) fn tab_title_at(app: &PhotocraftApp, i: usize, zoom: f32) -> String {
use photocraft_engine::channel_cmds::ChannelTarget;
use photocraft_engine::channel_cmds::{self, ChannelTarget};
let Some(st) = app.session.documents().get(i) else { return String::new() };
// Quick Mask mode targets the mask, not the layer (see `paint_target`): painting and Free
// Transform change the red overlay, so the tab says so, as Photoshop's does.
// Transform change the red overlay, so the tab names it after the layer, as Photoshop's does.
let quick_mask = st.doc.quick_mask.is_some() && st.channel_view.target == ChannelTarget::Composite;
let active_layer = st.active_layer.and_then(|id| st.doc.layer(id)).filter(|l| !quick_mask && !st.doc.is_background(l.id));
let active_layer = st.active_layer.and_then(|id| st.doc.layer(id)).filter(|l| !st.doc.is_background(l.id));
let mask = app.session.active_index() == Some(i) && app.ui.mask_target && active_layer.is_some_and(|l| l.mask.is_some());
// A single channel target names that channel, as Photoshop does (#17711): the Channels panel's
// colour names ("Red", "Cyan", "Lightness") or the alpha channel's own name. Two colour
// channels together, and the Background active, are not measured: they keep the mode's name.
let alone = |k: usize| st.channel_view.selected_colors.iter().all(|&c| c == k);
let channel = match st.channel_view.target {
_ if active_layer.is_none() => None,
ChannelTarget::Alpha(k) => st.doc.channels.get(k).map(|c| c.name.clone()),
ChannelTarget::Color(k) if alone(k) => channel_cmds::color_names(st.doc.mode).get(k).map(|name| (*name).to_string()),
_ => None,
};
let model = if quick_mask {
tl!("Quick Mask").to_string()
} else if mask {
tl!("Layer Mask").to_string()
} else if let Some(name) = &channel {
name.clone()
} else {
mode_label(&st.doc).to_string()
};
@@ -293,3 +306,7 @@ mod file_info_copyright_title_tests;
#[cfg(test)]
#[path = "tab_indexed_bitmap_label_tests.rs"]
mod tab_indexed_bitmap_label_tests;
#[cfg(test)]
#[path = "../tab_title_channel_target_tests.rs"]
mod tab_title_channel_target_tests;
+1 -1
View File
@@ -131,7 +131,7 @@ fn quick_mask_mode_names_the_quick_mask_in_the_tab() {
h.state_mut().run("select.editInQuickMaskMode", json!({"on": true})).unwrap();
let detail = title(&h);
assert!(detail.contains("Quick Mask"), "Quick Mask mode is named: {detail}");
assert!(!detail.contains("Layer 1") && !detail.contains("RGB"), "the layer is not the target: {detail}");
assert!(detail.contains("Layer 1") && !detail.contains("RGB"), "the quick mask replaces the mode, the layer stays: {detail}");
h.state_mut().run("select.editInQuickMaskMode", json!({"on": false})).unwrap();
let detail = title(&h);
@@ -0,0 +1,93 @@
//! #17711: the document tab names the paint target after the layer — the targeted colour channel
//! ("(Top, Red/8)"), the targeted alpha channel by its own name ("(Top, Alpha 1/8)") and the Quick
//! Mask ("(Top, Quick Mask/8)"); the layer mask and the composite keep their titles.
use serde_json::json;
use crate::PhotocraftApp;
use super::tab_title_at;
/// The tab's title of a document named "oracle" with a pixel layer "Top" active, so a channel or
/// mask target still has a layer name to show.
fn app(depth: u8) -> Box<PhotocraftApp> {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 400, "height": 300, "name": "oracle", "depth": depth})).unwrap();
app.run("layer.new.layer", json!({"name": "Top"})).unwrap();
app
}
/// The tab's title; the tab's modified mark is not part of the text.
fn title(app: &PhotocraftApp) -> String {
tab_title_at(app, 0, 100.0)
}
/// A layer mask on "Top", targeted as the Layers panel's thumbnail click does.
fn mask_targeted(app: &mut PhotocraftApp) {
let top = app.session.active().unwrap().active_layer.unwrap();
app.run("layer.layerMask.revealAll", json!({"layer": top.0})).unwrap();
app.run("channel.target", json!({"channel": "mask"})).unwrap();
}
#[test]
fn a_targeted_alpha_channel_is_named_after_the_layer() {
let mut app = app(8);
assert_eq!(title(&app), "oracle @ 100% (Top, RGB/8)");
// A new channel is targeted as it is made, as in Photoshop.
app.run("channel.new", json!({})).unwrap();
assert_eq!(title(&app), "oracle @ 100% (Top, Alpha 1/8)");
app.run("channel.rename", json!({"channel": 0, "name": "Spot 1"})).unwrap();
assert_eq!(title(&app), "oracle @ 100% (Top, Spot 1/8)", "the channel's own name, not a fixed one");
}
#[test]
fn a_targeted_colour_channel_is_named() {
let mut app = app(8);
for (channel, name) in [("red", "Red"), ("green", "Green"), ("blue", "Blue")] {
app.run("channel.target", json!({"channel": channel})).unwrap();
assert_eq!(title(&app), format!("oracle @ 100% (Top, {name}/8)"));
}
app.run("channel.target", json!({"channel": "composite"})).unwrap();
assert_eq!(title(&app), "oracle @ 100% (Top, RGB/8)", "the composite keeps the mode");
}
#[test]
fn quick_mask_names_the_layer_and_the_mask() {
let mut app = app(8);
app.run("select.editInQuickMaskMode", json!({"on": true})).unwrap();
assert_eq!(title(&app), "oracle @ 100% (Top, Quick Mask/8)");
app.run("select.editInQuickMaskMode", json!({"on": false})).unwrap();
assert_eq!(title(&app), "oracle @ 100% (Top, RGB/8)", "leaving Quick Mask brings back the mode");
}
#[test]
fn a_layer_mask_target_keeps_its_title() {
let mut app = app(8);
mask_targeted(&mut app);
assert_eq!(title(&app), "oracle @ 100% (Top, Layer Mask/8)");
app.run("channel.target", json!({"channel": "composite"})).unwrap();
assert_eq!(title(&app), "oracle @ 100% (Top, RGB/8)");
}
/// #17711 leaves the Background active unmeasured, so it keeps today's title: the mode's name
/// with no layer, whatever channel is targeted.
#[test]
fn the_background_keeps_the_mode_label() {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 400, "height": 300, "name": "oracle", "depth": 8})).unwrap();
assert_eq!(title(&app), "oracle @ 100% (RGB/8)");
app.run("channel.target", json!({"channel": "red"})).unwrap();
assert_eq!(title(&app), "oracle @ 100% (RGB/8)", "the Background's tab keeps the mode");
app.run("channel.target", json!({"channel": "composite"})).unwrap();
app.run("channel.new", json!({})).unwrap();
assert_eq!(title(&app), "oracle @ 100% (RGB/8)", "nor does an alpha channel name it");
}
#[test]
fn the_bit_depth_follows_the_document() {
let mut app = app(16);
app.run("channel.new", json!({})).unwrap();
assert_eq!(title(&app), "oracle @ 100% (Top, Alpha 1/16)");
app.run("channel.target", json!({"channel": "red"})).unwrap();
assert_eq!(title(&app), "oracle @ 100% (Top, Red/16)");
}