mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #17582
This commit is contained in:
commit
6913df28a0
3 files changed
+118
-1
No files matched your search
@@ -0,0 +1,99 @@
|
||||
//! The Background row's context menu starts with Layer from Background… and a separator, the Layer › New entry
|
||||
//! (#16764); other rows, a locked pixel layer among them, keep the menu they had.
|
||||
|
||||
use craft::layers::ui::panel::{LayersEvent, MenuTarget};
|
||||
use craft::layers::ui::tree::LayerRowKey;
|
||||
use photocraft_doc::LayerId;
|
||||
use photocraft_engine::Session;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::panels::layers_tree::{PhotoLayers, row_menu};
|
||||
|
||||
const FROM_BACKGROUND: &str = "layer.new.layerFromBackground";
|
||||
|
||||
/// A document with its Background and a pixel layer "Ink" above it; "Ink" is active.
|
||||
fn setup() -> Box<PhotocraftApp> {
|
||||
let mut app = PhotocraftApp::new(Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 32, "height": 24, "background": "white"})).unwrap();
|
||||
app.run("layer.new.layer", json!({"name": "Ink"})).unwrap();
|
||||
app
|
||||
}
|
||||
|
||||
fn background(app: &PhotocraftApp) -> Option<LayerId> {
|
||||
let st = app.session.active()?;
|
||||
st.doc.walk().into_iter().map(|(_, _, l)| l.id).find(|id| st.doc.is_background(*id))
|
||||
}
|
||||
|
||||
fn active(app: &PhotocraftApp) -> LayerId {
|
||||
app.session.active().and_then(|st| st.active_layer).expect("a layer is active")
|
||||
}
|
||||
|
||||
/// The right-clicked row's menu as (value, enabled); a separator is `None`.
|
||||
fn menu(app: &PhotocraftApp, id: LayerId) -> Vec<Option<(String, bool)>> {
|
||||
let st = app.session.active().unwrap();
|
||||
let tree = PhotoLayers::new(app, st, &Default::default(), false, None);
|
||||
row_menu(app, &tree, &MenuTarget::new(Some(LayerRowKey::Layer(id))))
|
||||
.iter()
|
||||
.map(|e| if e.is_separator() { None } else { e.value().map(|v| (v.clone(), e.is_enabled())) })
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn values(menu: &[Option<(String, bool)>]) -> Vec<Option<String>> {
|
||||
menu.iter().map(|e| e.as_ref().map(|(v, _)| v.clone())).collect()
|
||||
}
|
||||
|
||||
fn offers(menu: &[Option<(String, bool)>]) -> bool {
|
||||
menu.iter().flatten().any(|(v, _)| v == FROM_BACKGROUND)
|
||||
}
|
||||
|
||||
fn last_step(app: &PhotocraftApp) -> Option<String> {
|
||||
app.session.active().unwrap().history.entries().last().cloned()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_background_row_starts_with_layer_from_background_and_a_separator() {
|
||||
let mut app = setup();
|
||||
let ink = values(&menu(&app, active(&app)));
|
||||
let bg = background(&app).expect("the document has a Background");
|
||||
// The right-click selects the row it opens on.
|
||||
app.run("layer.select", json!({"layer": bg.0, "mode": "replace"})).unwrap();
|
||||
let m = menu(&app, bg);
|
||||
assert_eq!(m.first().cloned().flatten(), Some((FROM_BACKGROUND.to_string(), true)), "first and available: {m:?}");
|
||||
assert_eq!(m.get(1), Some(&None), "a separator follows: {m:?}");
|
||||
assert_eq!(values(&m)[2..], ink[..], "the rest is the menu another pixel layer gets");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn other_rows_have_no_layer_from_background() {
|
||||
let mut app = setup();
|
||||
let ink = active(&app);
|
||||
assert!(!offers(&menu(&app, ink)), "a pixel layer");
|
||||
// A pixel layer above the Background with its locks: it is not the Background.
|
||||
app.run("layer.lockLayers", json!({"transparency": true, "position": true})).unwrap();
|
||||
let locked = app.session.active().unwrap().doc.layer(ink).unwrap().clone();
|
||||
assert!(locked.locks.transparency && locked.locks.position, "the locks took");
|
||||
assert!(!offers(&menu(&app, ink)), "a locked pixel layer");
|
||||
app.run("layer.groupLayers", json!({"name": "G"})).unwrap();
|
||||
assert!(!offers(&menu(&app, active(&app))), "a group");
|
||||
}
|
||||
|
||||
/// Picking the entry converts the Background in the same History step as Layer › New › Layer from Background…
|
||||
#[test]
|
||||
fn picking_the_entry_converts_the_background_like_the_layer_menu() {
|
||||
let mut app = setup();
|
||||
let bg = background(&app).unwrap();
|
||||
app.run("layer.select", json!({"layer": bg.0, "mode": "replace"})).unwrap();
|
||||
let ctx = app.ctx().clone();
|
||||
let pick = LayersEvent::menu(MenuTarget::new(Some(LayerRowKey::Layer(bg))), FROM_BACKGROUND);
|
||||
crate::panels::layers_events::apply_layers_events(&mut app, &ctx, vec![pick]);
|
||||
assert_eq!(background(&app), None, "the Background became a normal layer");
|
||||
assert!(!offers(&menu(&app, bg)), "and its row no longer offers the entry");
|
||||
|
||||
let mut menu_app = setup();
|
||||
let menu_bg = background(&menu_app).unwrap();
|
||||
menu_app.run("layer.select", json!({"layer": menu_bg.0, "mode": "replace"})).unwrap();
|
||||
crate::menus::invoke(&mut menu_app, &ctx, FROM_BACKGROUND, json!({})).unwrap();
|
||||
assert_eq!(background(&menu_app), None);
|
||||
assert_eq!(last_step(&app), last_step(&menu_app), "the same History step as the Layer menu");
|
||||
}
|
||||
@@ -62,6 +62,19 @@ pub(crate) fn layer_count_label(command: &str, multi: bool) -> Option<&'static s
|
||||
Some(if multi { several } else { one })
|
||||
}
|
||||
|
||||
/// The context menu entries for a layer's row: [`entries`], led on the Background's row
|
||||
/// (`background`, [`photocraft_doc::Document::is_background`]) by Layer from Background… and a
|
||||
/// separator, the Layer › New entry (#16764). A multi-selection keeps the plain entries.
|
||||
pub fn row_entries(l: &Layer, multi: bool, has_selection: bool, background: bool) -> Vec<Entry> {
|
||||
let mut v: Vec<Entry> = Vec::new();
|
||||
if background && !multi {
|
||||
v.push(Some((tl!("Layer from Background…"), "layer.new.layerFromBackground")));
|
||||
v.push(None);
|
||||
}
|
||||
v.extend(entries(l, multi, has_selection));
|
||||
v
|
||||
}
|
||||
|
||||
/// The context menu entries for a layer (Photoshop 2026 order, trimmed to the layer kind).
|
||||
pub fn entries(l: &Layer, multi: bool, has_selection: bool) -> Vec<Entry> {
|
||||
let mut v: Vec<Entry> = vec![Some((tl!("Blending Options…"), "layer.layerStyle.blendingOptions"))];
|
||||
@@ -185,3 +198,7 @@ mod tests;
|
||||
#[cfg(test)]
|
||||
#[path = "layer_menu_artboard_group_tests.rs"]
|
||||
mod layer_menu_artboard_group_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "background_row_menu_tests.rs"]
|
||||
mod background_row_menu_tests;
|
||||
@@ -773,7 +773,8 @@ pub(crate) fn row_menu(app: &PhotocraftApp, tree: &PhotoLayers, target: &MenuTar
|
||||
MenuKind::Effects => commands(crate::layer_menu_ui::effects_entries(), false),
|
||||
MenuKind::Layer(id) => {
|
||||
let Some(l) = tree.layer(&id) else { return Vec::new() };
|
||||
let mut out = commands(crate::layer_menu_ui::entries(l, target.on_selection, has_selection), target.on_selection);
|
||||
let background = tree.doc().is_background(id);
|
||||
let mut out = commands(crate::layer_menu_ui::row_entries(l, target.on_selection, has_selection, background), target.on_selection);
|
||||
out.push(Entry::separator());
|
||||
out.push(Entry::new(RENAME.to_string(), tl!("Rename Layer…").to_string()));
|
||||
out
|
||||
|
||||
Reference in new issue
Block a user