mirror of
https://github.com/storytold/deckcraft.git
synced 2026-12-01 10:47:56 +01:00
panels-w8: net change of 7 commits
- panels-w8: written by an agent - panels-w8: finished by an agent - panels-w8: finished by an agent - panels-w8: finished by an agent - panels-w8: review fixes (review-0) - panels-w8: the moved solid-fill trial lands in its format_shape_tests.rs (lead) - panels-w8: review fixes (review-1)
This commit is contained in:
1 parent
63930fa450
commit
ea9aaa361c
4 files changed
+719
-690
No files matched your search
@@ -1,7 +1,6 @@
|
||||
impl crate::SlideApp {
|
||||
/// Returns the current presentation colour scheme.
|
||||
#[allow(dead_code)] // P3(panels-w8): pn.A6
|
||||
pub(crate) fn color_scheme(&self) -> deckcraft_color::ColorScheme {
|
||||
todo!("pn.A6")
|
||||
self.session.active().and_then(|d| d.doc.masters.first().map(|m| m.scheme())).unwrap_or_else(|| deckcraft_model::Theme::default().colors)
|
||||
}
|
||||
}
|
||||
@@ -1,32 +1,82 @@
|
||||
/// Shows a colour row and reports the selected colour.
|
||||
#[allow(dead_code)] // P3(panels-w8): pn.A7
|
||||
//! Colour rows share the same conversion and delayed eyedropper route as ribbon menus.
|
||||
|
||||
use craft::color_ui::ui::{ColorPick, ThemeColorMenu};
|
||||
use craft::ui::buttons::Button;
|
||||
use craft::ui::contract::{Bind, BodyCx, FieldLayout, Props as _, Show as _, ShowContainer as _};
|
||||
use craft::ui::controls::disclosure::DisclosureGlyph;
|
||||
use craft::ui::form::Field;
|
||||
use craft::ui::popover::Popover;
|
||||
use craft::ui::runtime::env::{Env, Label, Word};
|
||||
use deckcraft_model::ColorRef;
|
||||
use egui::Ui;
|
||||
|
||||
use crate::SlideApp;
|
||||
use crate::eyedropper::{self, Target};
|
||||
|
||||
craft::ui::words! {
|
||||
/// The accessible name of a colour opener beside its property label.
|
||||
struct ColorRowWords {
|
||||
/// `{label}` identifies the property whose colour will be edited.
|
||||
named: template "formatShape.namedColor" = "{label} color";
|
||||
}
|
||||
}
|
||||
|
||||
fn color_ref(pick: ColorPick) -> Option<Option<ColorRef>> {
|
||||
match pick {
|
||||
ColorPick::Scheme { slot, mods, .. } => Some(Some(mods.into_iter().fold(ColorRef::scheme(slot), ColorRef::with))),
|
||||
ColorPick::Rgb(color) => Some(Some(ColorRef::rgb(color))),
|
||||
ColorPick::None => Some(None),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Shows the shared menu body and arms its keyed eyedropper request.
|
||||
pub(crate) fn color_menu(app: &mut SlideApp, ui: &mut Ui, cx: &mut BodyCx<'_>, key: &str, none: Option<&str>) -> Option<Option<ColorRef>> {
|
||||
let scheme = app.color_scheme();
|
||||
let mut menu = ThemeColorMenu::new(Word::plain("Color"), &scheme).key(key);
|
||||
if let Some(label) = none {
|
||||
menu = menu.none(label.to_owned());
|
||||
}
|
||||
let output = menu.show(ui, cx);
|
||||
if output.eyedropper.is_some() {
|
||||
eyedropper::arm(app, Target::Menu(key.to_owned()));
|
||||
}
|
||||
output.pick.and_then(color_ref)
|
||||
}
|
||||
|
||||
/// Takes one answer for this opener after its popover has closed.
|
||||
pub(crate) fn take_color_answer(app: &mut SlideApp, key: &str) -> Option<Option<ColorRef>> {
|
||||
ThemeColorMenu::take_answer(&mut app.host_answers, key).and_then(color_ref)
|
||||
}
|
||||
|
||||
/// Shows a labelled colour opener for a semantic target/property key scoped to the active document.
|
||||
pub(crate) fn color_row(
|
||||
app: &mut crate::SlideApp,
|
||||
ui: &mut egui::Ui,
|
||||
app: &mut SlideApp,
|
||||
ui: &mut Ui,
|
||||
key: &str,
|
||||
label: &str,
|
||||
none: Option<&str>,
|
||||
on_pick: impl FnOnce(&mut crate::SlideApp, Option<deckcraft_model::ColorRef>),
|
||||
on_pick: impl FnOnce(&mut SlideApp, Option<ColorRef>),
|
||||
) {
|
||||
let _ = (app, ui, label, none, on_pick);
|
||||
todo!("pn.A7")
|
||||
}
|
||||
|
||||
/// Reports the colour menu or eyedropper answer.
|
||||
#[allow(dead_code)] // P3(panels-w8): pn.A15
|
||||
pub(crate) fn color_menu(
|
||||
app: &mut crate::SlideApp,
|
||||
ui: &mut egui::Ui,
|
||||
cx: &mut craft::ui::contract::BodyCx<'_>,
|
||||
key: &str,
|
||||
none: Option<&str>,
|
||||
) -> Option<Option<deckcraft_model::ColorRef>> {
|
||||
let _ = (app, ui, cx, key, none);
|
||||
todo!("pn.A15")
|
||||
}
|
||||
|
||||
/// Reports the colour menu or eyedropper answer.
|
||||
#[allow(dead_code)] // P3(panels-w8): pn.A15
|
||||
pub(crate) fn take_color_answer(app: &mut crate::SlideApp, key: &str) -> Option<Option<deckcraft_model::ColorRef>> {
|
||||
let _ = (app, key);
|
||||
todo!("pn.A15")
|
||||
let Some(uid) = app.session.active().map(|st| st.uid) else { return };
|
||||
let key = format!("fmt-color:{uid}:{key}");
|
||||
let name = if label == "Color" {
|
||||
Label::from(Word::plain("Color"))
|
||||
} else {
|
||||
Label::from(Env::of(ui).words::<ColorRowWords>().named.fill(&[("label", label)]))
|
||||
};
|
||||
let mut field = Field::new(label.to_owned()).layout(FieldLayout::Beside).id_salt(&key).show(ui, |ui, _| {
|
||||
Button::new(name.clone()).painted(Word::plain("Color")).keeps_name(true).trailing_icon(DisclosureGlyph::dropdown()).show(ui)
|
||||
});
|
||||
field.settle(|_, output| {
|
||||
let mut popup = Popover::new(Word::plain("Color"), Bind::kept().reset(false))
|
||||
.id_salt(&key)
|
||||
.opened_by(&output.response)
|
||||
.show(ui, |ui, cx| color_menu(app, ui, cx, &key, none));
|
||||
let picked = popup.settle(|_, pick| pick.take()).flatten();
|
||||
let answer = take_color_answer(app, &key);
|
||||
if let Some(pick) = picked.or(answer) {
|
||||
on_pick(app, pick);
|
||||
}
|
||||
});
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,272 @@
|
||||
//! Regression coverage for fill editing and selection-dependent Format Shape rows.
|
||||
|
||||
use deckcraft_color::{ColorTransform, SchemeSlot};
|
||||
use deckcraft_engine::Session;
|
||||
use deckcraft_model::{ColorRef, Fill, ShapeId, ShapeKind};
|
||||
use egui::accesskit::{Role, Toggled};
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::{NodeT, Queryable};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::format_shape::{fill_transparency_to_apply, solid_fill_transparency_percent};
|
||||
use crate::{Services, SlideApp};
|
||||
|
||||
type TestResult<T = ()> = Result<T, Box<dyn std::error::Error>>;
|
||||
|
||||
fn pane(session: Session) -> Harness<'static, SlideApp> {
|
||||
let mut h = crate::test_harness::harness_with(
|
||||
session,
|
||||
Services::default(),
|
||||
Harness::builder().with_size(egui::vec2(1800.0, 1500.0)),
|
||||
|_| {},
|
||||
);
|
||||
h.state_mut().ui.pane = Some("format".into());
|
||||
h.state_mut().ui.format_tab = "fill".into();
|
||||
h.run_steps(3);
|
||||
h
|
||||
}
|
||||
|
||||
fn rectangle(session: &mut Session) -> TestResult<u32> {
|
||||
let result = session.execute("shape.insert", &json!({"preset": "rect", "rect": [40.0, 40.0, 120.0, 80.0]}))?;
|
||||
Ok(u32::try_from(result["id"].as_u64().ok_or("shape.insert did not return an id")?)?)
|
||||
}
|
||||
|
||||
fn fill_pane(params: Option<Value>) -> TestResult<Harness<'static, SlideApp>> {
|
||||
let mut session = Session::with_new();
|
||||
session.execute("slide.new", &json!({"layout": "blank"}))?;
|
||||
let id = rectangle(&mut session)?;
|
||||
session.execute("edit.select", &json!({"ids": [id]}))?;
|
||||
if let Some(params) = params {
|
||||
session.execute("shape.fill", ¶ms)?;
|
||||
}
|
||||
Ok(pane(session))
|
||||
}
|
||||
|
||||
fn picture_data() -> String {
|
||||
// Generated pixels keep the fixture independent of external assets.
|
||||
let png = deckcraft_render::Image { width: 2, height: 2, pixels: [255, 0, 0, 255].repeat(4) }.to_png();
|
||||
deckcraft_engine::cmd::base64_encode(&png)
|
||||
}
|
||||
|
||||
fn selected_fill(h: &Harness<'_, SlideApp>) -> TestResult<Option<Fill>> {
|
||||
let state = h.state().session.active().ok_or("no active document")?;
|
||||
Ok(state.selected_shapes().first().ok_or("no selected shape")?.fill.clone())
|
||||
}
|
||||
|
||||
fn shape_fill(h: &Harness<'_, SlideApp>, id: u32) -> TestResult<Option<Fill>> {
|
||||
Ok(h.state().session.active().ok_or("no active document")?.shape(ShapeId(id)).ok_or("shape is absent")?.fill.clone())
|
||||
}
|
||||
|
||||
fn undo_len(h: &Harness<'_, SlideApp>) -> TestResult<usize> {
|
||||
Ok(h.state().session.active().ok_or("no active document")?.history.undo.len())
|
||||
}
|
||||
|
||||
fn transparency(h: &Harness<'_, SlideApp>) -> TestResult<f64> {
|
||||
Ok(h.get_by_role_and_label(Role::Slider, "Transparency").accesskit_node().numeric_value().ok_or("slider has no numeric value")?)
|
||||
}
|
||||
|
||||
fn color_rows(h: &Harness<'_, SlideApp>) -> usize {
|
||||
h.query_all_by_role_and_label(Role::Button, "Color").count()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solid_fill_transparency_reads_the_alpha_shape_fill_writes() -> TestResult {
|
||||
let plain = Fill::solid(ColorRef::scheme(SchemeSlot::Accent1));
|
||||
let opaque = solid_fill_transparency_percent(Some(&plain));
|
||||
for fill in [None, Some(&Fill::None), Some(&Fill::Group)] {
|
||||
assert_eq!(solid_fill_transparency_percent(fill), opaque);
|
||||
}
|
||||
// Alpha order is document order: .2 then .8 leaves .8 opacity, hence .2 transparency.
|
||||
let earlier = ColorTransform::Alpha(20_000);
|
||||
let later = ColorTransform::Alpha(80_000);
|
||||
let layered = Fill::solid(ColorRef::scheme(SchemeSlot::Accent1).with(earlier).with(later));
|
||||
let last_color = ColorRef::scheme(SchemeSlot::Accent1).with(later);
|
||||
let last = Fill::solid(last_color.clone());
|
||||
let shown = solid_fill_transparency_percent(Some(&layered));
|
||||
assert_eq!(shown, solid_fill_transparency_percent(Some(&last)));
|
||||
assert!(shown > opaque);
|
||||
let clear = Fill::solid(ColorRef::scheme(SchemeSlot::Accent1).with(ColorTransform::Alpha(0)));
|
||||
assert!(solid_fill_transparency_percent(Some(&clear)) > shown);
|
||||
assert!(fill_transparency_to_apply(shown, shown).is_none());
|
||||
assert!(fill_transparency_to_apply(shown + 0.005, shown).is_none());
|
||||
assert!(fill_transparency_to_apply(f32::NAN, shown).is_none());
|
||||
let fraction = fill_transparency_to_apply(shown, opaque).ok_or("a changed transparency must be an edit")?;
|
||||
assert!((fraction - (1.0 - last_color.alpha())).abs() < 0.000_001);
|
||||
assert!(fill_transparency_to_apply(opaque, shown).ok_or("returning to opaque must be an edit")? < fraction);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fill_transparency_slider_follows_the_selection() -> TestResult {
|
||||
let mut session = Session::with_new();
|
||||
session.execute("slide.new", &json!({"layout": "blank"}))?;
|
||||
let a = rectangle(&mut session)?;
|
||||
let b = rectangle(&mut session)?;
|
||||
session.execute("shape.fill", &json!({"id": a, "color": "accent1", "transparency": 0.0}))?;
|
||||
session.execute("shape.fill", &json!({"id": b, "color": "accent2", "transparency": 0.4}))?;
|
||||
session.execute("edit.select", &json!({"ids": [a]}))?;
|
||||
let mut h = pane(session);
|
||||
let first = transparency(&h)?;
|
||||
let before = shape_fill(&h, b)?;
|
||||
h.state_mut().session.execute("edit.select", &json!({"ids": [b]}))?;
|
||||
h.run_steps(3);
|
||||
let second = transparency(&h)?;
|
||||
assert!(second > first);
|
||||
assert_eq!(selected_fill(&h)?, before, "opening and changing the selection must not edit its fill");
|
||||
assert!((second - f64::from(solid_fill_transparency_percent(before.as_ref()))).abs() < 0.01);
|
||||
|
||||
let undo = undo_len(&h)?;
|
||||
h.get_by_role_and_label(Role::Slider, "Transparency").click();
|
||||
h.run_steps(2);
|
||||
let after = transparency(&h)?;
|
||||
assert!(after > second);
|
||||
assert_ne!(selected_fill(&h)?, before);
|
||||
assert!((after - f64::from(solid_fill_transparency_percent(selected_fill(&h)?.as_ref()))).abs() < 0.01);
|
||||
assert_eq!(undo_len(&h)?, undo + 1);
|
||||
h.state_mut().session.execute("edit.select", &json!({"ids": [a]}))?;
|
||||
h.run_steps(2);
|
||||
assert_eq!(transparency(&h)?, first, "the other shape retains its own value");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fill_color_and_transparency_show_only_where_they_change_the_fill() -> TestResult {
|
||||
let baseline = fill_pane(Some(json!({"none": true})))?;
|
||||
let line_colors = color_rows(&baseline);
|
||||
let opaque = f64::from(solid_fill_transparency_percent(Some(&Fill::solid(ColorRef::scheme(SchemeSlot::Accent1)))));
|
||||
for (kind, params, color, slider, gradient, pattern) in [
|
||||
("automatic", None, true, true, false, false),
|
||||
// shape_solid_fill_keeps_its_transparency_without_a_pattern_gallery shares this table
|
||||
// so its slider and absent pattern controls are checked against the other fill kinds.
|
||||
("solid", Some(json!({"color": "accent2", "transparency": 0.4})), true, true, false, false),
|
||||
("none", Some(json!({"none": true})), false, false, false, false),
|
||||
(
|
||||
"gradient",
|
||||
Some(json!({"gradient": {"stops": [[0, "accent1"], [1, "accent2"]], "angle": 90}})),
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
),
|
||||
("picture", Some(json!({"picture": picture_data(), "transparency": 0.4})), false, true, false, false),
|
||||
("pattern", Some(json!({"pattern": {"preset": "pct25", "fg": "accent1", "bg": "bg1"}})), false, false, false, true),
|
||||
] {
|
||||
let mut h = fill_pane(params)?;
|
||||
if gradient {
|
||||
h.get_by_role_and_label(Role::GraphicsObject, "Color stop 1 of 2").click();
|
||||
h.run_steps(2);
|
||||
}
|
||||
assert_eq!(color_rows(&h) > line_colors, color, "{kind}: fill colour row");
|
||||
assert_eq!(h.query_by_role_and_label(Role::Slider, "Transparency").is_some(), slider, "{kind}: fill transparency");
|
||||
assert_eq!(h.query_by_role_and_label(Role::SpinButton, "Opacity").is_some(), gradient, "{kind}: stop opacity");
|
||||
assert_eq!(h.query_by_role_and_label(Role::Group, "Gradient").is_some(), gradient, "{kind}: gradient editor");
|
||||
assert_eq!(h.query_by_role_and_label(Role::Button, "Foreground color").is_some(), pattern, "{kind}: pattern colour row");
|
||||
let tile_name = crate::fillui::pattern_label("zigZag");
|
||||
assert_eq!(h.query_by_role_and_label(Role::ListItem, tile_name).is_some(), pattern, "{kind}: pattern preset tile");
|
||||
if let Some(Fill::Picture(picture)) = selected_fill(&h)? {
|
||||
assert!((transparency(&h)? / 100.0 - (1.0 - picture.alpha.unwrap_or(1.0))).abs() < 0.000_001);
|
||||
assert!(h.query_by_role_and_label(Role::CheckBox, "Tile picture as texture").is_some());
|
||||
}
|
||||
if let Some(Fill::Solid { .. }) = selected_fill(&h)? {
|
||||
assert!((transparency(&h)? - f64::from(solid_fill_transparency_percent(selected_fill(&h)?.as_ref()))).abs() < 0.01);
|
||||
}
|
||||
if kind == "solid" {
|
||||
assert!(transparency(&h)? > opaque, "{kind}: a translucent solid retains its transparency without a pattern gallery");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slide_background_fill_is_a_fill_option() -> TestResult {
|
||||
for kind in ["rectangle", "picture", "group", "retained gradient"] {
|
||||
let mut session = Session::with_new();
|
||||
session.execute("slide.new", &json!({"layout": "blank"}))?;
|
||||
let mut picture_id = None;
|
||||
let mut shape_id = None;
|
||||
if kind == "rectangle" {
|
||||
shape_id = Some(rectangle(&mut session)?);
|
||||
} else {
|
||||
let result = session.execute("insert.picture", &json!({"data": picture_data(), "name": "generated.png"}))?;
|
||||
let picture = u32::try_from(result["id"].as_u64().ok_or("insert.picture did not return an id")?)?;
|
||||
picture_id = Some(picture);
|
||||
if kind != "picture" {
|
||||
let shape = rectangle(&mut session)?;
|
||||
shape_id = Some(shape);
|
||||
session.execute("edit.select", &json!({"ids": [picture, shape]}))?;
|
||||
session.execute("arrange.group", &json!({}))?;
|
||||
let fill = if kind == "retained gradient" {
|
||||
json!({"gradient": {"stops": [[0, "accent1"], [1, "accent2"]]}})
|
||||
} else {
|
||||
json!({"color": "accent1"})
|
||||
};
|
||||
session.execute("shape.fill", &fill)?;
|
||||
if kind == "retained gradient" {
|
||||
session.edit(|doc, _| {
|
||||
for slide in &mut doc.slides {
|
||||
if let Some(picture) = std::sync::Arc::make_mut(slide).shape_mut(ShapeId(picture)) {
|
||||
picture.fill = Some(Fill::Group);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut h = pane(session);
|
||||
let allowed = kind != "picture";
|
||||
let options = h.get_by_role_and_label(Role::RadioGroup, "Fill type");
|
||||
assert!(options.query_by_role_and_label(Role::RadioButton, "Pattern fill").is_some(), "{kind}: fill options are shown");
|
||||
assert_eq!(options.query_by_role_and_label(Role::RadioButton, "Slide background fill").is_some(), allowed, "{kind}");
|
||||
let initial = match kind {
|
||||
"group" => "Solid fill",
|
||||
"retained gradient" => "Gradient fill",
|
||||
_ => "Automatic",
|
||||
};
|
||||
assert_eq!(options.get_by_role_and_label(Role::RadioButton, initial).accesskit_node().toggled(), Some(Toggled::True), "{kind}");
|
||||
if !allowed {
|
||||
continue;
|
||||
}
|
||||
let radio = h.get_by_role_and_label(Role::RadioButton, "Slide background fill");
|
||||
assert_ne!(radio.accesskit_node().toggled(), Some(Toggled::True));
|
||||
let group_fill = selected_fill(&h)?;
|
||||
let picture_fill = picture_id.map(|id| shape_fill(&h, id)).transpose()?;
|
||||
let undo = undo_len(&h)?;
|
||||
radio.click();
|
||||
h.run_steps(3);
|
||||
assert_eq!(undo_len(&h)?, undo + 1, "{kind}: one undo step");
|
||||
let selected = h.get_by_role_and_label(Role::RadioButton, "Slide background fill");
|
||||
assert_eq!(selected.accesskit_node().toggled(), Some(Toggled::True), "{kind}: the displayed radio follows members");
|
||||
assert!(h.query_by_role_and_label(Role::Slider, "Transparency").is_none());
|
||||
assert!(h.query_by_role_and_label(Role::Group, "Gradient").is_none(), "a retained group fill must not expose an editor");
|
||||
assert_eq!(shape_fill(&h, shape_id.ok_or("selection has no shape")?)?, Some(Fill::Background));
|
||||
if let Some(id) = picture_id {
|
||||
assert_eq!(shape_fill(&h, id)?, picture_fill.ok_or("picture fill was not captured")?, "pictures retain their fill");
|
||||
let state = h.state().session.active().ok_or("no active document")?;
|
||||
assert!(state.selected_shapes().first().is_some_and(|shape| matches!(shape.kind, ShapeKind::Group { .. })));
|
||||
if kind == "retained gradient" {
|
||||
assert_eq!(selected_fill(&h)?, group_fill, "the picture still inherits the retained gradient");
|
||||
} else {
|
||||
assert!(selected_fill(&h)?.is_none());
|
||||
}
|
||||
}
|
||||
h.state_mut().session.execute("edit.undo", &json!({}))?;
|
||||
h.run_steps(2);
|
||||
assert_ne!(h.get_by_role_and_label(Role::RadioButton, "Slide background fill").accesskit_node().toggled(), Some(Toggled::True));
|
||||
}
|
||||
let h = fill_pane(Some(json!({"background": true})))?;
|
||||
assert_eq!(h.get_by_role_and_label(Role::RadioButton, "Slide background fill").accesskit_node().toggled(), Some(Toggled::True));
|
||||
let chosen_automatic = h
|
||||
.query_all_by_role_and_label(Role::RadioButton, "Automatic")
|
||||
.filter(|node| node.accesskit_node().toggled() == Some(Toggled::True))
|
||||
.count();
|
||||
let line_only = fill_pane(Some(json!({"none": true})))?;
|
||||
let line_automatic = line_only
|
||||
.query_all_by_role_and_label(Role::RadioButton, "Automatic")
|
||||
.filter(|node| node.accesskit_node().toggled() == Some(Toggled::True))
|
||||
.count();
|
||||
assert_eq!(chosen_automatic, line_automatic, "only the line's Automatic is selected");
|
||||
assert_eq!(color_rows(&h), color_rows(&line_only));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user