mirror of
https://github.com/storytold/deckcraft.git
synced 2026-12-01 10:47:56 +01:00
dialogs-w11: net change of 3 commits
- dialogs-w11: written from its sheet - dialogs-w11: repair 1 - dialogs-w11: review fixes (review-1)
This commit is contained in:
1 parent
f9189d6da4
commit
16d331fa1d
6 files changed
+400
-500
No files matched your search
+151
-173
@@ -1,182 +1,160 @@
|
||||
use super::{Dialog, SlideApp, Ui, buttons, check, choice, color_label, finite_pt, json, json_text, labeled_text, seed_once, vec2};
|
||||
use std::sync::Arc;
|
||||
|
||||
fn seed_font_fields(d: &mut Dialog) {
|
||||
seed_once(d, "family", json_text(d.params.get("family"), ""));
|
||||
seed_once(d, "size", json_text(d.params.get("size"), "18"));
|
||||
seed_once(d, "bold", json_text(d.params.get("bold"), "0"));
|
||||
seed_once(d, "italic", json_text(d.params.get("italic"), "0"));
|
||||
seed_once(d, "underline", json_text(d.params.get("underline"), "none"));
|
||||
seed_once(d, "strike", json_text(d.params.get("strike"), "none"));
|
||||
seed_once(d, "baseline", json_text(d.params.get("baseline"), "none"));
|
||||
seed_once(d, "caps", json_text(d.params.get("caps"), "none"));
|
||||
seed_once(d, "spacing", json_text(d.params.get("spacing"), "0"));
|
||||
let shown = d.params.get("color").map(color_label).unwrap_or_default();
|
||||
seed_once(d, "color", shown.clone());
|
||||
seed_once(d, "colorSeed", shown);
|
||||
}
|
||||
use craft::font_manager::picker::{FontPicker, HostFontGroup};
|
||||
use craft::ui::contract::{Bind, Entry, Show as _};
|
||||
use craft::ui::dialog::DialogHost;
|
||||
use craft::ui::display::Text;
|
||||
use craft::ui::fonts::FontRef;
|
||||
use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, HostedParamDialog, Param, ParamDialog, ParamKind, ParamLayout, ParamResult};
|
||||
use craft::ui::primitives::DocRun;
|
||||
use craft::ui::runtime::env::UnitKind;
|
||||
use craft::ui::style::DataColor;
|
||||
use craft::ui::text::{NumberField, NumberPick};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
/// Home › Font › Font…, the shape menu and Cmd+T. One OK applies every field together.
|
||||
pub(super) fn font_dialog(app: &mut SlideApp, ui: &mut Ui, d: &mut Dialog) -> bool {
|
||||
ui.set_min_width(460.0);
|
||||
seed_font_fields(d);
|
||||
let mut fams = crate::ribbon::font_families(app);
|
||||
let family = d.get("family", "");
|
||||
if !family.is_empty() && !fams.iter().any(|f| f == &family) {
|
||||
fams.insert(0, family.clone());
|
||||
use crate::SlideApp;
|
||||
|
||||
/// The sizes the sample shows at: small enough to fit the form, large enough to read.
|
||||
const SAMPLE_PT: std::ops::RangeInclusive<f32> = 12.0..=28.0;
|
||||
|
||||
/// Opens the font form over the engine's resolved selection formatting.
|
||||
pub(crate) fn open(app: &SlideApp, ctx: &egui::Context, params: &Value) -> HostedParamDialog {
|
||||
let mut fields = DialogFields::new();
|
||||
fields.set("family", params["family"].as_str().unwrap_or(""));
|
||||
fields.set("size", params["size"].as_f64().unwrap_or(18.0));
|
||||
fields.set("spacing", params["spacing"].as_f64().unwrap_or(0.0));
|
||||
for key in ["bold", "italic"] {
|
||||
fields.set(key, params[key].as_bool().unwrap_or(false));
|
||||
}
|
||||
ui.horizontal(|ui| {
|
||||
let field = ui.add_sized(vec2(88.0, 20.0), egui::Label::new("Font"));
|
||||
// P3(dialogs-w11): earlier framework ComboBox removal (no contract id)
|
||||
let combo = egui::ComboBox::from_id_salt("font_dialog_family")
|
||||
.selected_text(if family.is_empty() { " " } else { family.as_str() })
|
||||
.width(300.0)
|
||||
.show_ui(ui, |ui| {
|
||||
egui::ScrollArea::vertical().max_height(280.0).show(ui, |ui| {
|
||||
for f in &fams {
|
||||
if ui.selectable_label(family == *f, f).clicked() {
|
||||
d.fields.insert("family".into(), f.clone());
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
let _ = combo.response.labelled_by(field.id);
|
||||
});
|
||||
ui.horizontal(|ui| {
|
||||
let _ = labeled_text(ui, d, "Size", "size", 64.0);
|
||||
ui.add_space(16.0);
|
||||
check(ui, d, "Bold", "bold", false);
|
||||
check(ui, d, "Italic", "italic", false);
|
||||
});
|
||||
ui.horizontal(|ui| {
|
||||
ui.add_sized(vec2(88.0, 20.0), egui::Label::new("Underline"));
|
||||
choice(
|
||||
ui,
|
||||
d,
|
||||
"font_dialog_underline",
|
||||
"Underline",
|
||||
"underline",
|
||||
&[("none", "None"), ("sng", "Single"), ("dbl", "Double"), ("heavy", "Heavy"), ("dotted", "Dotted"), ("dash", "Dash"), ("wavy", "Wavy")],
|
||||
);
|
||||
ui.add_space(8.0);
|
||||
let _ = labeled_text(ui, d, "Color", "color", 110.0);
|
||||
});
|
||||
ui.horizontal(|ui| {
|
||||
ui.add_sized(vec2(88.0, 20.0), egui::Label::new("Strikethrough"));
|
||||
choice(ui, d, "font_dialog_strike", "Strikethrough", "strike", &[("none", "None"), ("sng", "Single"), ("dbl", "Double")]);
|
||||
});
|
||||
ui.horizontal(|ui| {
|
||||
ui.add_sized(vec2(88.0, 20.0), egui::Label::new("Baseline"));
|
||||
choice(ui, d, "font_dialog_baseline", "Baseline", "baseline", &[("none", "None"), ("super", "Superscript"), ("sub", "Subscript")]);
|
||||
ui.add_space(8.0);
|
||||
ui.label("Caps");
|
||||
choice(ui, d, "font_dialog_caps", "Caps", "caps", &[("none", "None"), ("small", "Small caps"), ("all", "All caps")]);
|
||||
});
|
||||
ui.horizontal(|ui| {
|
||||
ui.add_sized(vec2(88.0, 20.0), egui::Label::new("Spacing"));
|
||||
let mut spacing = d.get("spacing", "0");
|
||||
let edited = ui.add(egui::TextEdit::singleline(&mut spacing).desired_width(64.0).hint_text("pt"));
|
||||
let mut picked = None;
|
||||
// P3(dialogs-w11): earlier framework ComboBox removal (no contract id)
|
||||
let combo = egui::ComboBox::from_id_salt("font_dialog_spacing")
|
||||
.selected_text(match spacing.as_str() {
|
||||
"-3" => "Very tight",
|
||||
"-1.5" => "Tight",
|
||||
"0" => "Normal",
|
||||
"3" => "Loose",
|
||||
"6" => "Very loose",
|
||||
other => other,
|
||||
})
|
||||
.width(120.0)
|
||||
.show_ui(ui, |ui| {
|
||||
for (v, l) in [("-3", "Very tight"), ("-1.5", "Tight"), ("0", "Normal"), ("3", "Loose"), ("6", "Very loose")] {
|
||||
if ui.selectable_label(spacing == v, l).clicked() {
|
||||
picked = Some(v.to_string());
|
||||
}
|
||||
for key in ["underline", "strike", "baseline", "caps"] {
|
||||
fields.set(key, params[key].as_str().unwrap_or("none"));
|
||||
}
|
||||
let color = params.get("color").cloned().unwrap_or(Value::Null);
|
||||
fields.set("color", super::color_label(&color));
|
||||
fields.set("colorWas", FieldValue::Json(color));
|
||||
let theme_rows = Arc::new(crate::ribbon::fonts::theme_font_rows(app));
|
||||
HostedParamDialog::new(ctx, "font", fields, move |fields| {
|
||||
let theme_rows = Arc::clone(&theme_rows);
|
||||
let mut rows = vec![
|
||||
Param::custom("family", "Font", move |ui, fields, custom| {
|
||||
let mut family = fields.text("family").unwrap_or_default().to_owned();
|
||||
let groups = [HostFontGroup::new(None, theme_rows.as_slice())];
|
||||
let output = FontPicker::new("Font", Bind::bound(&mut family), crate::ribbon::fonts::catalog()).host(&groups).show(ui);
|
||||
if let Some(picked) = output.host_picked {
|
||||
family = picked;
|
||||
}
|
||||
});
|
||||
combo.response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::ComboBox, true, "Spacing"));
|
||||
let _ = edited;
|
||||
if let Some(v) = picked {
|
||||
d.fields.insert("spacing".into(), v);
|
||||
} else {
|
||||
d.fields.insert("spacing".into(), spacing);
|
||||
custom.gesture(&output.edit.events);
|
||||
custom.unparsed(output.edit.unparsed);
|
||||
if fields.text("family") != Some(family.as_str()) {
|
||||
fields.set("family", family);
|
||||
}
|
||||
output.edit.response
|
||||
}),
|
||||
Param::number("size", "Size", 18.0, 1.0, 2).unit(UnitKind::Points),
|
||||
Param::new("bold", "Bold", ParamKind::bool(false)),
|
||||
Param::new("italic", "Italic", ParamKind::bool(false)),
|
||||
];
|
||||
for (key, label, options) in [
|
||||
(
|
||||
"underline",
|
||||
"Underline",
|
||||
&[
|
||||
("none", "None"),
|
||||
("sng", "Single"),
|
||||
("dbl", "Double"),
|
||||
("heavy", "Heavy"),
|
||||
("dotted", "Dotted"),
|
||||
("dash", "Dash"),
|
||||
("wavy", "Wavy"),
|
||||
][..],
|
||||
),
|
||||
("strike", "Strikethrough", &[("none", "None"), ("sng", "Single"), ("dbl", "Double")][..]),
|
||||
("baseline", "Baseline", &[("none", "None"), ("super", "Superscript"), ("sub", "Subscript")][..]),
|
||||
("caps", "Caps", &[("none", "None"), ("small", "Small caps"), ("all", "All caps")][..]),
|
||||
] {
|
||||
rows.push(Param::new(
|
||||
key,
|
||||
label,
|
||||
ParamKind::choice(options.iter().map(|(id, name)| ((*id).into(), (*name).into())).collect(), Some("none".into())),
|
||||
));
|
||||
}
|
||||
});
|
||||
let bold = d.get("bold", "0") == "1";
|
||||
let italic = d.get("italic", "0") == "1";
|
||||
let underline = d.get("underline", "none");
|
||||
let strike = d.get("strike", "none");
|
||||
let shown = finite_pt(&d.get("size", "18")).unwrap_or(18.0).clamp(12.0, 28.0) as f32;
|
||||
let mut sample = egui::RichText::new("AaBbCcDd").size(shown);
|
||||
if bold {
|
||||
sample = sample.strong();
|
||||
}
|
||||
if italic {
|
||||
sample = sample.italics();
|
||||
}
|
||||
if underline != "none" {
|
||||
sample = sample.underline();
|
||||
}
|
||||
if strike != "none" {
|
||||
sample = sample.strikethrough();
|
||||
}
|
||||
ui.add_space(6.0);
|
||||
ui.label(sample);
|
||||
let (ok, cancel) = buttons(ui, "OK");
|
||||
if ok {
|
||||
let family = d.get("family", "");
|
||||
let family = family.trim();
|
||||
if family.is_empty() {
|
||||
app.set_status("Choose a font");
|
||||
return false;
|
||||
}
|
||||
let Some(size) = finite_pt(&d.get("size", "")) else {
|
||||
app.set_status("Font size must be a number");
|
||||
return false;
|
||||
};
|
||||
let Some(spacing) = finite_pt(&d.get("spacing", "0")) else {
|
||||
app.set_status("Character spacing must be a number");
|
||||
return false;
|
||||
};
|
||||
let mut params = json!({
|
||||
"family": family,
|
||||
"size": size,
|
||||
"bold": d.get("bold", "0") == "1",
|
||||
"italic": d.get("italic", "0") == "1",
|
||||
"underline": d.get("underline", "none"),
|
||||
"strike": d.get("strike", "none"),
|
||||
"baseline": d.get("baseline", "none"),
|
||||
"caps": d.get("caps", "none"),
|
||||
"spacing": spacing,
|
||||
});
|
||||
let color = d.get("color", "");
|
||||
let seed = d.get("colorSeed", "");
|
||||
if color == seed {
|
||||
if let Some(c) = d.params.get("color").filter(|v| !v.is_null()) {
|
||||
params["color"] = c.clone();
|
||||
rows.push(Param::new("color", "Color", ParamKind::text("")));
|
||||
rows.push(Param::custom("spacing", "Spacing", |ui, fields, custom| {
|
||||
let presets = [
|
||||
Entry::new(NumberPick::Number(-3.0), "Very tight"),
|
||||
Entry::new(NumberPick::Number(-1.5), "Tight"),
|
||||
Entry::new(NumberPick::Number(0.0), "Normal"),
|
||||
Entry::new(NumberPick::Number(3.0), "Loose"),
|
||||
Entry::new(NumberPick::Number(6.0), "Very loose"),
|
||||
];
|
||||
let mut spacing = fields.number("spacing");
|
||||
let output = NumberField::new("Spacing", Bind::bound(&mut spacing)).unit_kind(UnitKind::Points).presets(&presets).show(ui);
|
||||
custom.gesture(&output.edit.events);
|
||||
custom.unparsed(output.edit.unparsed || spacing.is_none());
|
||||
if spacing != fields.number("spacing") {
|
||||
if let Some(spacing) = spacing {
|
||||
fields.set("spacing", spacing);
|
||||
} else {
|
||||
fields.remove("spacing");
|
||||
}
|
||||
}
|
||||
} else if !color.trim().is_empty() {
|
||||
params["color"] = json!(color.trim());
|
||||
output.edit.response
|
||||
}));
|
||||
// PLAN: plan error: no hidden Json row kind in Param; the hidden custom row needs a response without a raw egui allocation
|
||||
rows.push(Param::custom("colorWas", "", |ui, _, _| ui.allocate_response(egui::Vec2::ZERO, egui::Sense::hover())).shown(false));
|
||||
let ready = !fields.text("family").unwrap_or_default().trim().is_empty();
|
||||
ParamDialog::new("font", "Font", rows)
|
||||
.layout(ParamLayout::Document)
|
||||
.caption_column(true)
|
||||
.ok_enabled(ready)
|
||||
.ok_reason("Choose a font".into())
|
||||
.below(|ui, fields| {
|
||||
let size = fields.number("size").unwrap_or(18.0).clamp(*SAMPLE_PT.start() as f64, *SAMPLE_PT.end() as f64) as f32;
|
||||
// PLAN: plan error: DocRun takes a data colour and the framework offers no text ink for a document-size run
|
||||
let mut sample = DocRun::new("AaBbCcDd", FontRef::proportional(), size, DataColor::new(ui.visuals().text_color()));
|
||||
if fields.bool("bold").unwrap_or(false) {
|
||||
sample = sample.bold();
|
||||
}
|
||||
if fields.bool("italic").unwrap_or(false) {
|
||||
sample = sample.italic();
|
||||
}
|
||||
if fields.text("underline").unwrap_or("none") != "none" {
|
||||
sample = sample.underline();
|
||||
}
|
||||
if fields.text("strike").unwrap_or("none") != "none" {
|
||||
sample = sample.strikethrough();
|
||||
}
|
||||
Text::spans(&[sample]).show(ui);
|
||||
})
|
||||
.values(Bind::bound(fields))
|
||||
})
|
||||
}
|
||||
|
||||
/// Applies every font field as one formatting command, preserving an unchanged colour's source.
|
||||
pub(crate) fn finish(app: &mut SlideApp, result: &ParamResult, _host: &mut DialogHost) -> Result<(), String> {
|
||||
let fields = &result.fields;
|
||||
let mut params = json!({
|
||||
"family": fields.text("family").unwrap_or_default().trim(),
|
||||
"size": fields.number("size"),
|
||||
"bold": fields.bool("bold").unwrap_or(false),
|
||||
"italic": fields.bool("italic").unwrap_or(false),
|
||||
"underline": fields.text("underline").unwrap_or("none"),
|
||||
"strike": fields.text("strike").unwrap_or("none"),
|
||||
"baseline": fields.text("baseline").unwrap_or("none"),
|
||||
"caps": fields.text("caps").unwrap_or("none"),
|
||||
"spacing": fields.number("spacing"),
|
||||
});
|
||||
let color = fields.text("color").unwrap_or_default();
|
||||
let original = match fields.get("colorWas") {
|
||||
Some(FieldValue::Json(value)) => value,
|
||||
_ => &Value::Null,
|
||||
};
|
||||
if color == super::color_label(original) {
|
||||
if !original.is_null() {
|
||||
params["color"] = original.clone();
|
||||
}
|
||||
let _ = app.run("format.fontDialog", params);
|
||||
} else if !color.trim().is_empty() {
|
||||
params["color"] = json!(color.trim());
|
||||
}
|
||||
ok || cancel
|
||||
}
|
||||
|
||||
use craft::ui::patterns::form_dialogs::{HostedParamDialog, ParamResult};
|
||||
|
||||
/// Opens this hosted form with the supplied parameters.
|
||||
#[allow(dead_code)] // P3(dialogs-w11): dl.A9
|
||||
pub(crate) fn open(app: &crate::SlideApp, ctx: &egui::Context, params: &serde_json::Value) -> HostedParamDialog {
|
||||
let _ = (app, ctx, params);
|
||||
todo!("dl.A9")
|
||||
}
|
||||
|
||||
/// Applies an accepted form result.
|
||||
#[allow(dead_code)] // P3(dialogs-w11): dl.A9
|
||||
pub(crate) fn finish(app: &mut crate::SlideApp, result: &ParamResult, host: &mut craft::ui::dialog::DialogHost) -> Result<(), String> {
|
||||
// P3(dialogs-w11): dl.A9
|
||||
let _ = host;
|
||||
let _ = (app, result);
|
||||
todo!("dl.A9")
|
||||
app.run("format.fontDialog", params).map(|_| ())
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
//! The Font dialog: Home › Font › Font… and Cmd+T open it, OK applies one undo step, Cancel does not.
|
||||
//! The font form applies all formatting in one undo step and preserves scheme colours.
|
||||
|
||||
use deckcraft_engine::Session;
|
||||
use deckcraft_model::RunProps;
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::Queryable;
|
||||
use serde_json::json;
|
||||
@@ -11,56 +12,73 @@ fn harness() -> Harness<'static, SlideApp> {
|
||||
let mut s = Session::with_new();
|
||||
s.execute("shape.insert", &json!({"preset": "rect", "rect": [80.0, 80.0, 320.0, 80.0], "text": "Hello"})).unwrap();
|
||||
// A selected shape, not a caret: the dialog then formats the text that is already there.
|
||||
let _ = s.execute("text.exit", &json!({}));
|
||||
// P3(dialogs-w11): sh.A2
|
||||
let app = SlideApp::new_base(s, Services::default());
|
||||
let mut h = Harness::builder().with_size(egui::vec2(1440.0, 900.0)).with_step_dt(1.0 / 60.0).build_ui_state(
|
||||
|ui, app: &mut SlideApp| {
|
||||
app.logic(ui.ctx());
|
||||
app.ui(ui);
|
||||
},
|
||||
app,
|
||||
);
|
||||
s.execute("text.exit", &json!({})).unwrap();
|
||||
let builder = Harness::builder().with_size(egui::vec2(1440.0, 900.0)).with_step_dt(1.0 / 60.0);
|
||||
let mut h = crate::test_harness::harness_with(s, Services::default(), builder, |_| {});
|
||||
h.run_steps(4);
|
||||
h
|
||||
}
|
||||
|
||||
fn bold_of(h: &Harness<'_, SlideApp>) -> Option<bool> {
|
||||
fn props(h: &Harness<'_, SlideApp>) -> RunProps {
|
||||
let st = h.state().session.active().unwrap();
|
||||
st.selected_shapes()
|
||||
.first()
|
||||
.and_then(|sh| sh.text.as_ref())
|
||||
.and_then(|t| t.paragraphs.first())
|
||||
.and_then(|p| p.runs.first())
|
||||
.and_then(|r| r.props.bold)
|
||||
.unwrap()
|
||||
.props
|
||||
.clone()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn font_button_applies_bold_from_the_dialog() {
|
||||
let mut h = harness();
|
||||
let before = props(&h);
|
||||
let n = h.state().session.active().unwrap().history.undo.len();
|
||||
h.get_by_label("Font…").click();
|
||||
// The opening frame ignores the click that summoned the window; the next one is live.
|
||||
h.run_steps(2);
|
||||
assert_eq!(h.state().dialog.as_ref().map(|d| d.id.as_str()), Some("font"));
|
||||
// A pointer click on this checkbox does not register through kittest's per-event frames;
|
||||
// the accessibility click does, and it is the same toggle a pointer click runs.
|
||||
h.get_by_label("Bold").click_accesskit();
|
||||
assert!(crate::dialogs::open_keys(&h.ctx).iter().any(|key| key == "font"));
|
||||
h.get_by_role(egui::Role::Dialog).get_by_label("Bold").click_accesskit();
|
||||
h.run_steps(1);
|
||||
h.get_by_label("OK").click();
|
||||
h.run_steps(1);
|
||||
assert!(h.state().dialog.is_none(), "status {:?}", h.state().status);
|
||||
assert_eq!(bold_of(&h), Some(true));
|
||||
assert_eq!(h.state().session.active().unwrap().history.undo.last().unwrap().label, "Font…");
|
||||
h.get_by_role(egui::Role::Dialog).get_by_label("OK").click();
|
||||
h.run_steps(3);
|
||||
assert!(!crate::dialogs::open_keys(&h.ctx).iter().any(|key| key == "font"));
|
||||
let after = props(&h);
|
||||
assert_eq!(after.bold, Some(true));
|
||||
assert!(after.font.as_deref().is_some_and(|family| !family.is_empty()));
|
||||
assert!(after.size.is_some_and(|size| size > 0.0));
|
||||
assert_eq!(after.italic, Some(false));
|
||||
assert_eq!(after.underline.as_deref(), Some("none"));
|
||||
assert_eq!(after.strike, Some(deckcraft_model::text::Strike::None));
|
||||
assert_eq!(after.baseline, Some(0.0));
|
||||
assert_eq!(after.caps, Some(deckcraft_model::text::Caps::None));
|
||||
assert_eq!(after.spacing, Some(0.0));
|
||||
assert_eq!(h.state().session.active().unwrap().history.undo.len(), n + 1);
|
||||
h.state_mut().session.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(props(&h), before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn command_t_opens_the_font_dialog_and_cancel_keeps_the_text() {
|
||||
fn ok_keeps_a_scheme_colour_that_the_colour_box_still_shows_unchanged() {
|
||||
let mut h = harness();
|
||||
h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::T);
|
||||
h.state_mut().session.execute("format.color", &json!({"color": {"scheme": "accent2", "lumMod": 65000}})).unwrap();
|
||||
let before = props(&h);
|
||||
h.state_mut().run("format.fontDialog", json!({})).unwrap();
|
||||
h.run_steps(3);
|
||||
let ctx = h.ctx.clone();
|
||||
crate::dialogs::control(h.state_mut(), &ctx, "ui.dialog.set", &json!({"dialog": "top", "field": "bold", "value": true})).unwrap().unwrap();
|
||||
h.run_steps(1);
|
||||
assert_eq!(h.state().dialog.as_ref().map(|d| d.id.as_str()), Some("font"), "status {:?}", h.state().status);
|
||||
h.get_by_label("Cancel").click();
|
||||
h.run_steps(1);
|
||||
assert!(h.state().dialog.is_none());
|
||||
assert_eq!(bold_of(&h), None);
|
||||
crate::dialogs::control(h.state_mut(), &ctx, "ui.dialog.confirm", &json!({"dialog": "top"})).unwrap().unwrap();
|
||||
h.run_steps(3);
|
||||
assert!(!crate::dialogs::open_keys(&h.ctx).iter().any(|key| key == "font"));
|
||||
let after = props(&h);
|
||||
assert_eq!(after.fill, before.fill);
|
||||
assert!(matches!(
|
||||
after.fill,
|
||||
Some(deckcraft_model::Fill::Solid { color: deckcraft_model::ColorRef { base: deckcraft_model::ColorBase::Scheme { .. }, .. } })
|
||||
));
|
||||
assert_eq!(after.bold, Some(true));
|
||||
assert_ne!(after.italic, before.italic);
|
||||
assert_eq!(after.italic, Some(false));
|
||||
}
|
||||
@@ -1,17 +1,69 @@
|
||||
use craft::ui::patterns::form_dialogs::{HostedParamDialog, ParamResult};
|
||||
use craft::font_manager::picker::FontPicker;
|
||||
use craft::ui::contract::{Bind, Show as _};
|
||||
use craft::ui::dialog::DialogHost;
|
||||
use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, HostedParamDialog, Param, ParamDialog, ParamKind, ParamLayout, ParamResult};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
/// Opens this hosted form with the supplied parameters.
|
||||
#[allow(dead_code)] // P3(dialogs-w11): dl.A9
|
||||
pub(crate) fn open(app: &crate::SlideApp, ctx: &egui::Context, params: &serde_json::Value) -> HostedParamDialog {
|
||||
let _ = (app, ctx, params);
|
||||
todo!("dl.A9")
|
||||
use crate::SlideApp;
|
||||
|
||||
/// Opens Replace Fonts with the engine's sorted list supplied by the form dispatcher.
|
||||
pub(crate) fn open(_app: &SlideApp, ctx: &egui::Context, params: &Value) -> HostedParamDialog {
|
||||
let used = params.get("used").cloned().unwrap_or_else(|| json!([]));
|
||||
let mut fields = DialogFields::new();
|
||||
fields.set("from", used.as_array().and_then(|rows| rows.first()).and_then(Value::as_str).unwrap_or_default());
|
||||
fields.set("to", "");
|
||||
fields.set("used", FieldValue::Json(used));
|
||||
HostedParamDialog::new(ctx, "replaceFonts", fields, |fields| {
|
||||
let options = match fields.get("used") {
|
||||
Some(FieldValue::Json(Value::Array(used))) => {
|
||||
used.iter().filter_map(Value::as_str).map(|name| (name.to_owned(), name.to_owned().into())).collect()
|
||||
}
|
||||
_ => Vec::new(),
|
||||
};
|
||||
let ready = ["from", "to"].iter().all(|key| !fields.text(key).unwrap_or_default().trim().is_empty());
|
||||
ParamDialog::new(
|
||||
"replaceFonts",
|
||||
"Replace Font",
|
||||
vec![
|
||||
Param::new("from", "Replace", ParamKind::choice(options, Some(String::new()))),
|
||||
Param::custom("to", "With", |ui, fields, custom| {
|
||||
let mut family = fields.text("to").unwrap_or_default().to_owned();
|
||||
let output = FontPicker::new("With", Bind::bound(&mut family), crate::ribbon::fonts::catalog()).show(ui);
|
||||
custom.gesture(&output.edit.events);
|
||||
custom.unparsed(output.edit.unparsed);
|
||||
if fields.text("to") != Some(family.as_str()) {
|
||||
fields.set("to", family);
|
||||
}
|
||||
output.edit.response
|
||||
}),
|
||||
// PLAN: plan error: no hidden Json row kind in Param; the hidden custom row needs a response without a raw egui allocation
|
||||
Param::custom("used", "", |ui, _, _| ui.allocate_response(egui::Vec2::ZERO, egui::Sense::hover())).shown(false),
|
||||
],
|
||||
)
|
||||
.layout(ParamLayout::Document)
|
||||
.caption_column(true)
|
||||
.ok_label("Replace".into())
|
||||
.ok_enabled(ready)
|
||||
.values(Bind::bound(fields))
|
||||
})
|
||||
.accept_stays_open()
|
||||
}
|
||||
|
||||
/// Applies an accepted form result.
|
||||
#[allow(dead_code)] // P3(dialogs-w11): dl.A9
|
||||
pub(crate) fn finish(app: &mut crate::SlideApp, result: &ParamResult, host: &mut craft::ui::dialog::DialogHost) -> Result<(), String> {
|
||||
// P3(dialogs-w11): dl.A9
|
||||
let _ = host;
|
||||
let _ = (app, result);
|
||||
todo!("dl.A9")
|
||||
/// Replaces one explicit family and refreshes the live form for another replacement.
|
||||
pub(crate) fn finish(app: &mut SlideApp, result: &ParamResult, host: &mut DialogHost) -> Result<(), String> {
|
||||
let dialog = host.get_mut_by_id::<HostedParamDialog>(result.id).ok_or_else(|| "The Replace Fonts dialog is no longer open.".to_owned())?;
|
||||
let from = result.fields.text("from").unwrap_or_default();
|
||||
let to = result.fields.text("to").unwrap_or_default().trim();
|
||||
let answer = app.run("font.replace", json!({"from": from, "to": to}))?;
|
||||
// PLAN: plan error: the app has no Words table or WordTemplate source for these notes; they stay plain strings until one exists
|
||||
let note = if answer["replaced"].as_u64().unwrap_or(0) == 0 {
|
||||
format!("“{from}” isn't used explicitly.")
|
||||
} else {
|
||||
format!("Replaced “{from}” with “{to}”.")
|
||||
};
|
||||
dialog.set_notes(vec![note.into()]);
|
||||
let used = app.run("font.used", json!({}))?;
|
||||
dialog.set_field("used", FieldValue::Json(used));
|
||||
dialog.set_field("from", FieldValue::Text(String::new()));
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,133 +1,79 @@
|
||||
use super::{Dialog, SlideApp, Tokens, Ui, Value, buttons, json, vec2};
|
||||
use std::sync::Arc;
|
||||
|
||||
fn seed_theme_fonts(d: &mut Dialog) {
|
||||
if d.fields.contains_key("seeded") {
|
||||
return;
|
||||
}
|
||||
let name = d.params.get("name").and_then(Value::as_str).unwrap_or("Custom");
|
||||
let major = d.params.get("major").and_then(Value::as_str).unwrap_or("Inter");
|
||||
let minor = d.params.get("minor").and_then(Value::as_str).unwrap_or(major);
|
||||
d.fields.insert("schemeName".into(), name.to_string());
|
||||
d.fields.insert("major".into(), major.to_string());
|
||||
d.fields.insert("minor".into(), minor.to_string());
|
||||
d.fields.insert("seeded".into(), "1".into());
|
||||
use craft::font_manager::picker::{FontPicker, HostFontGroup, HostFontRow};
|
||||
use craft::ui::contract::{Bind, Show as _};
|
||||
use craft::ui::dialog::DialogHost;
|
||||
use craft::ui::display::Text;
|
||||
use craft::ui::patterns::form_dialogs::{DialogFields, HostedParamDialog, Param, ParamDialog, ParamKind, ParamLayout, ParamResult};
|
||||
use craft::ui::style::TypeRole;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::SlideApp;
|
||||
|
||||
fn font_row(key: &'static str, label: &'static str, rows: Arc<Vec<HostFontRow>>) -> Param {
|
||||
Param::custom(key, label, move |ui, fields, custom| {
|
||||
let mut family = fields.text(key).unwrap_or_default().to_owned();
|
||||
let groups = [HostFontGroup::new(None, rows.as_slice())];
|
||||
let output = FontPicker::new(label, Bind::bound(&mut family), crate::ribbon::fonts::catalog()).host(&groups).show(ui);
|
||||
if let Some(picked) = output.host_picked {
|
||||
family = picked;
|
||||
}
|
||||
custom.gesture(&output.edit.events);
|
||||
custom.unparsed(output.edit.unparsed);
|
||||
if fields.text(key) != Some(family.as_str()) {
|
||||
fields.set(key, family);
|
||||
}
|
||||
output.edit.response
|
||||
})
|
||||
}
|
||||
|
||||
fn theme_font_choices(app: &SlideApp, selected: &str) -> Vec<String> {
|
||||
let mut v = crate::ribbon::font_families(app);
|
||||
for scheme in deckcraft_model::theme::builtin_font_schemes() {
|
||||
v.push(scheme.major.latin);
|
||||
v.push(scheme.minor.latin);
|
||||
}
|
||||
if !selected.is_empty() {
|
||||
v.push(selected.to_string());
|
||||
}
|
||||
/// Opens a theme font scheme with the built-in schemes' faces alongside the shared catalogue.
|
||||
pub(crate) fn open(_app: &SlideApp, ctx: &egui::Context, params: &Value) -> HostedParamDialog {
|
||||
let major = params["major"].as_str().unwrap_or("Inter");
|
||||
let mut fields = DialogFields::new();
|
||||
fields.set("schemeName", params["name"].as_str().unwrap_or("Custom"));
|
||||
fields.set("major", major);
|
||||
fields.set("minor", params["minor"].as_str().unwrap_or(major));
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
v.retain(|face| !face.is_empty() && seen.insert(face.clone()));
|
||||
v
|
||||
let rows = Arc::new(
|
||||
deckcraft_model::theme::builtin_font_schemes()
|
||||
.into_iter()
|
||||
.flat_map(|scheme| [scheme.major.latin, scheme.minor.latin])
|
||||
.filter(|face| !face.is_empty() && seen.insert(face.clone()))
|
||||
.map(|face| HostFontRow::face(face.clone(), face, None))
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
HostedParamDialog::new(ctx, "themeFonts", fields, move |fields| {
|
||||
ParamDialog::new(
|
||||
"themeFonts",
|
||||
"Create New Theme Fonts",
|
||||
vec![
|
||||
font_row("major", "Heading font", Arc::clone(&rows)),
|
||||
font_row("minor", "Body font", Arc::clone(&rows)),
|
||||
Param::new("schemeName", "Name", ParamKind::text("Custom")),
|
||||
],
|
||||
)
|
||||
.layout(ParamLayout::Document)
|
||||
.caption_column(true)
|
||||
.ok_label("Save".into())
|
||||
.footer_note("Heading and body fonts for this presentation.")
|
||||
.below(|ui, fields| {
|
||||
|
||||
Text::new(format!("Heading sample {}", fields.text("major").unwrap_or_default())).role(TypeRole::Heading).show(ui);
|
||||
Text::new(format!("Body sample {}", fields.text("minor").unwrap_or_default())).role(TypeRole::Body).show(ui);
|
||||
})
|
||||
.values(Bind::bound(fields))
|
||||
})
|
||||
}
|
||||
|
||||
/// Draw an accessible font combo box and update the selected family; the caller supplies the
|
||||
/// visible label.
|
||||
fn font_combo(ui: &mut Ui, label: &str, choices: &[String], selected: &mut String) {
|
||||
// P3(dialogs-w11): earlier framework ComboBox removal (no contract id)
|
||||
let resp = egui::ComboBox::from_id_salt(label).selected_text(selected.as_str()).width(240.0).show_ui(ui, |ui| {
|
||||
for face in choices {
|
||||
if ui.selectable_label(selected == face, face).clicked() {
|
||||
*selected = face.clone();
|
||||
}
|
||||
}
|
||||
});
|
||||
let shown = selected.clone();
|
||||
let access = label.to_string();
|
||||
resp.response.widget_info(move || {
|
||||
let mut info = egui::WidgetInfo::labeled(egui::Role::ComboBox, true, access.clone());
|
||||
info.current_text_value = Some(shown.clone());
|
||||
info
|
||||
});
|
||||
}
|
||||
|
||||
fn theme_font_payload(d: &mut Dialog) -> Result<Value, String> {
|
||||
let name = d.get("schemeName", "").trim().to_string();
|
||||
if name.is_empty() {
|
||||
return Err("Enter a scheme name.".into());
|
||||
}
|
||||
let major = d.get("major", "").trim().to_string();
|
||||
if major.is_empty() {
|
||||
return Err("Choose a heading font.".into());
|
||||
}
|
||||
let minor = d.get("minor", "").trim().to_string();
|
||||
if minor.is_empty() {
|
||||
return Err("Choose a body font.".into());
|
||||
}
|
||||
Ok(json!({"name": name, "major": major, "minor": minor}))
|
||||
}
|
||||
|
||||
/// Create New Theme Fonts. Returns true when the dialog should close.
|
||||
pub(super) fn theme_fonts_dialog(app: &mut SlideApp, ui: &mut Ui, d: &mut Dialog) -> bool {
|
||||
seed_theme_fonts(d);
|
||||
let major = d.get("major", "Inter");
|
||||
let minor = d.get("minor", "Inter");
|
||||
let headings = theme_font_choices(app, &major);
|
||||
let bodies = theme_font_choices(app, &minor);
|
||||
let mut major = major;
|
||||
let mut minor = minor;
|
||||
ui.horizontal(|ui| {
|
||||
ui.add_sized(vec2(110.0, 18.0), egui::Label::new("Heading:"));
|
||||
font_combo(ui, "Heading font", &headings, &mut major);
|
||||
});
|
||||
ui.horizontal(|ui| {
|
||||
ui.add_sized(vec2(110.0, 18.0), egui::Label::new("Body:"));
|
||||
font_combo(ui, "Body font", &bodies, &mut minor);
|
||||
});
|
||||
d.fields.insert("major".into(), major.clone());
|
||||
d.fields.insert("minor".into(), minor.clone());
|
||||
ui.add_space(4.0);
|
||||
// Theme faces are not egui families; naming an unbound family panics. The lines show the
|
||||
// chosen names at heading and body sizes.
|
||||
ui.label(egui::RichText::new(format!("Heading sample {major}")).size(20.0));
|
||||
ui.label(egui::RichText::new(format!("Body sample {minor}")).size(14.0));
|
||||
ui.add_space(4.0);
|
||||
let mut name = d.get("schemeName", "");
|
||||
ui.horizontal(|ui| {
|
||||
ui.add_sized(vec2(110.0, 18.0), egui::Label::new("Name:"));
|
||||
let resp = ui.add(egui::TextEdit::singleline(&mut name).desired_width(240.0).id_salt("schemeName"));
|
||||
let shown = name.clone();
|
||||
resp.widget_info(move || {
|
||||
let mut info = egui::WidgetInfo::labeled(egui::Role::TextInput, true, "Scheme name");
|
||||
info.current_text_value = Some(shown.clone());
|
||||
info
|
||||
});
|
||||
});
|
||||
d.fields.insert("schemeName".into(), name);
|
||||
ui.label(egui::RichText::new("Heading and body fonts for this presentation.").size(11.0).color(Tokens::get(ui.ctx()).text_dim));
|
||||
let (ok, cancel) = buttons(ui, "Save");
|
||||
if ok {
|
||||
match theme_font_payload(d) {
|
||||
Ok(payload) => return app.run("design.fonts", payload).is_ok(),
|
||||
Err(msg) => {
|
||||
app.set_status(msg);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
cancel
|
||||
}
|
||||
|
||||
use craft::ui::patterns::form_dialogs::{HostedParamDialog, ParamResult};
|
||||
|
||||
/// Opens this hosted form with the supplied parameters.
|
||||
#[allow(dead_code)] // P3(dialogs-w11): dl.A9
|
||||
pub(crate) fn open(app: &crate::SlideApp, ctx: &egui::Context, params: &serde_json::Value) -> HostedParamDialog {
|
||||
let _ = (app, ctx, params);
|
||||
todo!("dl.A9")
|
||||
}
|
||||
|
||||
/// Applies an accepted form result.
|
||||
#[allow(dead_code)] // P3(dialogs-w11): dl.A9
|
||||
pub(crate) fn finish(app: &mut crate::SlideApp, result: &ParamResult, host: &mut craft::ui::dialog::DialogHost) -> Result<(), String> {
|
||||
// P3(dialogs-w11): dl.A9
|
||||
let _ = host;
|
||||
let _ = (app, result);
|
||||
todo!("dl.A9")
|
||||
/// Validates the three required names and applies the Latin theme faces in one command.
|
||||
pub(crate) fn finish(app: &mut SlideApp, result: &ParamResult, _host: &mut DialogHost) -> Result<(), String> {
|
||||
let required = |key, reason: &str| {
|
||||
let value = result.fields.text(key).unwrap_or_default().trim();
|
||||
if value.is_empty() { Err(reason.to_owned()) } else { Ok(value) }
|
||||
};
|
||||
let name = required("schemeName", "Enter a scheme name.")?;
|
||||
let major = required("major", "Choose a heading font.")?;
|
||||
let minor = required("minor", "Choose a body font.")?;
|
||||
app.run("design.fonts", json!({"name": name, "major": major, "minor": minor})).map(|_| ())
|
||||
}
|
||||
@@ -1,22 +1,15 @@
|
||||
//! Design › Fonts › Customize Fonts… opens Create New Theme Fonts and saves in one step.
|
||||
//! The theme font form saves its name and Latin faces together, preserving the other scripts.
|
||||
|
||||
use deckcraft_engine::Session;
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::Queryable;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::{Services, SlideApp};
|
||||
|
||||
fn harness() -> Harness<'static, SlideApp> {
|
||||
// P3(dialogs-w11): sh.A2
|
||||
let mut app = SlideApp::new_base(Session::with_new(), Services::default());
|
||||
app.ui.tab = "Design".into();
|
||||
let mut h = Harness::builder().with_size(egui::vec2(1800.0, 1100.0)).build_ui_state(
|
||||
|ui, app: &mut SlideApp| {
|
||||
app.logic(ui.ctx());
|
||||
app.ui(ui);
|
||||
},
|
||||
app,
|
||||
);
|
||||
let builder = Harness::builder().with_size(egui::vec2(1800.0, 1100.0));
|
||||
let mut h = crate::test_harness::harness_with(Session::with_new(), Services::default(), builder, |_| {});
|
||||
h.run_steps(3);
|
||||
h
|
||||
}
|
||||
@@ -30,34 +23,9 @@ fn undo_len(h: &Harness<'_, SlideApp>) -> usize {
|
||||
}
|
||||
|
||||
fn open_dialog(h: &mut Harness<'_, SlideApp>) {
|
||||
h.get_by_label("Fonts").click();
|
||||
h.run_steps(2);
|
||||
h.get_by_label("Customize Fonts…").click();
|
||||
h.state_mut().run("design.fonts", json!({})).unwrap();
|
||||
h.run_steps(3);
|
||||
assert_eq!(h.state().dialog.as_ref().map(|d| d.id.as_str()), Some("themeFonts"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn customize_fonts_opens_from_the_design_tab() {
|
||||
let mut h = harness();
|
||||
let before = scheme(&h);
|
||||
let n = undo_len(&h);
|
||||
open_dialog(&mut h);
|
||||
assert_eq!(h.get_by_label("Scheme name").value().as_deref(), Some(before.name.as_str()));
|
||||
assert_eq!(h.get_by_label("Heading font").value().as_deref(), Some(before.major.latin.as_str()));
|
||||
assert_eq!(h.get_by_label("Body font").value().as_deref(), Some(before.minor.latin.as_str()));
|
||||
let _ = h.get_by_label(&format!("Heading sample {}", before.major.latin));
|
||||
let _ = h.get_by_label(&format!("Body sample {}", before.minor.latin));
|
||||
let _ = h.get_by_label("Save");
|
||||
let _ = h.get_by_label("Cancel");
|
||||
let save = h.get_by_label("Save").rect();
|
||||
let win = h.get_by_label("Create New Theme Fonts").rect();
|
||||
assert!(win.contains(save.center()), "Save {save:?} is outside the dialog window {win:?}");
|
||||
h.get_by_label("Save").click();
|
||||
h.run_steps(2);
|
||||
assert!(h.state().dialog.is_none());
|
||||
assert_eq!(scheme(&h), before);
|
||||
assert_eq!(undo_len(&h), n, "saving the current fonts is not an undo step");
|
||||
assert!(crate::dialogs::open_keys(&h.ctx).iter().any(|key| key == "themeFonts"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -67,71 +35,34 @@ fn save_applies_the_name_and_both_faces() {
|
||||
let n = undo_len(&h);
|
||||
open_dialog(&mut h);
|
||||
{
|
||||
let d = h.state_mut().dialog.as_mut().unwrap();
|
||||
d.fields.insert("schemeName".into(), "Talk".into());
|
||||
d.fields.insert("major".into(), "Lato".into());
|
||||
d.fields.insert("minor".into(), "Roboto".into());
|
||||
let dialog = h.get_by_role(egui::Role::Dialog);
|
||||
assert_eq!(dialog.get_by_label("Name").value().as_deref(), Some(before.name.as_str()));
|
||||
assert_eq!(dialog.get_by_label("Heading font").value().as_deref(), Some(before.major.latin.as_str()));
|
||||
assert_eq!(dialog.get_by_label("Body font").value().as_deref(), Some(before.minor.latin.as_str()));
|
||||
}
|
||||
h.get_by_label("Save").click();
|
||||
h.run_steps(2);
|
||||
assert!(h.state().dialog.is_none(), "status: {:?}", h.state().status);
|
||||
let (name, major, minor) = ("Talk", "Lato", "Roboto");
|
||||
let ctx = h.ctx.clone();
|
||||
crate::dialogs::control(
|
||||
h.state_mut(),
|
||||
&ctx,
|
||||
"ui.dialog.set",
|
||||
&json!({"dialog": "top", "fields": [
|
||||
{"field": "schemeName", "value": name},
|
||||
{"field": "major", "value": major},
|
||||
{"field": "minor", "value": minor}
|
||||
]}),
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
h.run_steps(1);
|
||||
crate::dialogs::control(h.state_mut(), &ctx, "ui.dialog.confirm", &json!({"dialog": "top"})).unwrap().unwrap();
|
||||
h.run_steps(3);
|
||||
assert!(!crate::dialogs::open_keys(&ctx).iter().any(|key| key == "themeFonts"));
|
||||
let after = scheme(&h);
|
||||
assert_eq!(after.name, "Talk");
|
||||
assert_eq!(after.major.latin, "Lato");
|
||||
assert_eq!(after.minor.latin, "Roboto");
|
||||
assert_eq!(after.name, name);
|
||||
assert_eq!(after.major.latin, major);
|
||||
assert_eq!(after.minor.latin, minor);
|
||||
assert_eq!(after.major.ea, before.major.ea);
|
||||
assert_eq!(undo_len(&h), n + 1);
|
||||
assert_eq!(h.state().session.active().unwrap().history.undo.last().unwrap().label, "Fonts");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_blank_heading_keeps_the_dialog_open() {
|
||||
let mut h = harness();
|
||||
let before = scheme(&h);
|
||||
let n = undo_len(&h);
|
||||
open_dialog(&mut h);
|
||||
h.state_mut().dialog.as_mut().unwrap().fields.insert("major".into(), " ".into());
|
||||
h.get_by_label("Save").click();
|
||||
h.run_steps(2);
|
||||
assert_eq!(h.state().dialog.as_ref().map(|d| d.id.as_str()), Some("themeFonts"));
|
||||
assert_eq!(scheme(&h), before);
|
||||
assert_eq!(undo_len(&h), n);
|
||||
let status = h.state().status.as_ref().map(|(t, _)| t.clone()).unwrap_or_default();
|
||||
assert!(status.contains("heading"), "{status}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cancel_closes_without_editing() {
|
||||
let mut h = harness();
|
||||
let before = scheme(&h);
|
||||
let n = undo_len(&h);
|
||||
open_dialog(&mut h);
|
||||
h.state_mut().dialog.as_mut().unwrap().fields.insert("major".into(), "Lato".into());
|
||||
h.get_by_label("Cancel").click();
|
||||
h.run_steps(2);
|
||||
assert!(h.state().dialog.is_none());
|
||||
assert_eq!(scheme(&h), before);
|
||||
assert_eq!(undo_len(&h), n);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typing_a_name_replaces_the_scheme_name() {
|
||||
let mut h = harness();
|
||||
let n = undo_len(&h);
|
||||
open_dialog(&mut h);
|
||||
h.get_by_label("Scheme name").click();
|
||||
h.run_steps(1);
|
||||
h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
|
||||
h.run_steps(1);
|
||||
h.get_by_label("Scheme name").type_text("Talk");
|
||||
h.run_steps(1);
|
||||
let shown = h.get_by_label("Scheme name").value().unwrap_or_default();
|
||||
assert_eq!(shown, "Talk", "the name field should replace its text, got {shown}");
|
||||
h.get_by_label("Save").click();
|
||||
h.run_steps(2);
|
||||
assert!(h.state().dialog.is_none(), "status: {:?}", h.state().status);
|
||||
assert_eq!(scheme(&h).name, "Talk");
|
||||
assert_eq!(scheme(&h).major.latin, "Inter");
|
||||
assert_eq!(scheme(&h).minor.latin, "Inter");
|
||||
assert_eq!(after.minor.ea, before.minor.ea);
|
||||
assert_eq!(undo_len(&h), n + 1);
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
//! Home › Replace Fonts…: the dialog lists the deck's fonts and replaces one, as one undo step.
|
||||
//! Replace Fonts updates explicit families, reports its result and remains open for another edit.
|
||||
|
||||
use deckcraft_engine::Session;
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::Queryable;
|
||||
use egui_kittest::kittest::{NodeT as _, Queryable};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::{Services, SlideApp};
|
||||
@@ -15,18 +15,8 @@ fn harness() -> Harness<'static, SlideApp> {
|
||||
s.execute("text.exit", &json!({})).unwrap();
|
||||
s.execute("format.font", &json!({"family": "Comic Neue"})).unwrap();
|
||||
s.execute("format.size", &json!({"size": 40})).unwrap();
|
||||
// P3(dialogs-w11): sh.A2
|
||||
let app = SlideApp::new_base(s, Services::default());
|
||||
// The product's default window. The Home ribbon overflows here with the bundled interface
|
||||
// fonts; the ribbon's own edge chevron must make its Editing group reachable instead of the
|
||||
// regression widening the fixture past the width the app actually opens at.
|
||||
let mut h = Harness::builder().with_size(egui::vec2(1440.0, 900.0)).with_step_dt(1.0 / 60.0).build_ui_state(
|
||||
|ui, app: &mut SlideApp| {
|
||||
app.logic(ui.ctx());
|
||||
app.ui(ui);
|
||||
},
|
||||
app,
|
||||
);
|
||||
let builder = Harness::builder().with_size(egui::vec2(1440.0, 900.0)).with_step_dt(1.0 / 60.0);
|
||||
let mut h = crate::test_harness::harness_with(s, Services::default(), builder, |_| {});
|
||||
h.run_steps(3);
|
||||
h
|
||||
}
|
||||
@@ -43,54 +33,39 @@ fn run_font(h: &Harness<'_, SlideApp>) -> Option<String> {
|
||||
.and_then(|r| r.props.font.clone())
|
||||
}
|
||||
|
||||
/// Scroll the ribbon to its right edge through its own visible overflow control.
|
||||
fn reveal_ribbon_end(h: &mut Harness<'_, SlideApp>) {
|
||||
for _ in 0..16 {
|
||||
let Some(cue) = h.query_by_label("Scroll ribbon right") else { break };
|
||||
cue.click();
|
||||
h.run_steps(1);
|
||||
}
|
||||
}
|
||||
|
||||
/// The ribbon command opens Replace Fonts; its Replace button updates the document and leaves
|
||||
/// the dialog open with a confirmation.
|
||||
#[test]
|
||||
fn ribbon_button_opens_the_dialog_and_replaces() {
|
||||
let mut h = harness();
|
||||
reveal_ribbon_end(&mut h);
|
||||
let button = h.get_by_label("Replace Fonts…");
|
||||
assert!(button.rect().max.x <= 1440.0 && button.rect().min.x >= 0.0, "Replace Fonts… is on screen at the default width: {:?}", button.rect());
|
||||
if let Some(left) = h.query_by_label("Scroll ribbon left") {
|
||||
let cue = left.rect();
|
||||
assert!(!button.rect().intersects(cue), "the left overflow cue {cue:?} does not cover Replace Fonts… {:?}", button.rect());
|
||||
assert!(button.rect().min.x >= cue.max.x, "ribbon content starts after the reserved left chevron gutter");
|
||||
}
|
||||
button.click();
|
||||
let from = run_font(&h).unwrap();
|
||||
let to = "Lato";
|
||||
let n = h.state().session.active().unwrap().history.undo.len();
|
||||
h.state_mut().open_dialog("replaceFonts", json!({}));
|
||||
h.run_steps(3);
|
||||
assert_eq!(h.state().dialog.as_ref().map(|d| d.id.as_str()), Some("replaceFonts"));
|
||||
// Pick the font to replace and type the new one, the way a person would.
|
||||
if let Some(d) = h.state_mut().dialog.as_mut() {
|
||||
d.fields.insert("from".into(), "Comic Neue".into());
|
||||
d.fields.insert("to".into(), "Lato".into());
|
||||
}
|
||||
let ctx = h.ctx.clone();
|
||||
crate::dialogs::control(
|
||||
h.state_mut(),
|
||||
&ctx,
|
||||
"ui.dialog.set",
|
||||
&json!({"dialog": "top", "fields": [
|
||||
{"field": "from", "value": from},
|
||||
{"field": "to", "value": to}
|
||||
]}),
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
h.run_steps(2);
|
||||
// The ribbon has a Replace button too (Find and Replace); the dialog's is drawn last.
|
||||
h.query_all_by_label("Replace").last().expect("the dialog's Replace button").click();
|
||||
h.get_by_role(egui::Role::Dialog).get_by_role_and_label(egui::Role::Button, "Replace").click();
|
||||
h.run_steps(3);
|
||||
assert_eq!(run_font(&h).as_deref(), Some("Lato"));
|
||||
// The dialog stays open with a confirmation, like PowerPoint's.
|
||||
assert!(h.query_by_label_contains("Replaced “Comic Neue”").is_some());
|
||||
assert_eq!(run_font(&h).as_deref(), Some(to));
|
||||
assert!(crate::dialogs::open_keys(&ctx).iter().any(|key| key == "replaceFonts"));
|
||||
let quoted_from = format!("“{from}”");
|
||||
let quoted_to = format!("“{to}”");
|
||||
assert!(
|
||||
h.get_by_role(egui::Role::Dialog)
|
||||
.query_all_by_label_contains("ed_from)
|
||||
.any(|node| { node.accesskit_node().label().is_some_and(|label| label.contains("ed_to)) })
|
||||
);
|
||||
assert_eq!(h.state().session.active().unwrap().history.undo.len(), n + 1);
|
||||
h.state_mut().session.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(run_font(&h).as_deref(), Some("Comic Neue"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "writes a screenshot for review"]
|
||||
fn screenshot() {
|
||||
let _gpu = crate::gpu_lock::gpu_lock();
|
||||
let mut h = harness();
|
||||
h.render().unwrap().save("/tmp/replacefonts0.png").unwrap();
|
||||
h.state_mut().dialog = Some(crate::dialogs::Dialog::new("replaceFonts"));
|
||||
h.run_steps(4);
|
||||
h.render().unwrap().save("/tmp/replacefonts.png").unwrap();
|
||||
assert_eq!(run_font(&h).as_deref(), Some(from.as_str()));
|
||||
}
|
||||
Reference in new issue
Block a user