mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-17 03:13:37 -06:00
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
de694152a9 | ||
|
|
a216825578 | ||
|
|
ebe2f430d8 | ||
|
|
8fa2895202 | ||
|
|
d7215f4441 |
@@ -610,11 +610,12 @@ fn lin_node(n: &MNode, s: &mut String, depth: usize) {
|
||||
}
|
||||
MNode::Nary { chr, sub_hide, sup_hide, sub, sup, e, .. } => {
|
||||
s.push(*chr);
|
||||
if !sub_hide && !sub.is_empty() {
|
||||
// A shown limit is written even when empty (`_()`): left out, it reads back hidden (#6076).
|
||||
if !sub_hide {
|
||||
s.push('_');
|
||||
lin_script(sub, s, depth);
|
||||
}
|
||||
if !sup_hide && !sup.is_empty() {
|
||||
if !sup_hide {
|
||||
s.push('^');
|
||||
lin_script(sup, s, depth);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
//! An n-ary operator's empty but shown limit survives linear text (#6076): half-typed `∑_` has an
|
||||
//! empty lower limit that is shown, and `to_linear` writes it as `_()`. Left out, it reads back as a
|
||||
//! hidden limit.
|
||||
|
||||
use wordcraft_doc::math::{MNode, MRun, parse_linear, to_linear};
|
||||
|
||||
#[test]
|
||||
fn an_empty_shown_limit_prints_back_the_same() {
|
||||
for s in ["∑_", "∑^", "∫_0^▒x", "∑_()^n▒i"] {
|
||||
let n = parse_linear(s);
|
||||
assert_eq!(parse_linear(&to_linear(&n)), n, "{s} → {}", to_linear(&n));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hidden_and_filled_limits_print_as_before() {
|
||||
let nary = |sub_hide, sup_hide, sub: &str, sup: &str| {
|
||||
let arg = |t: &str| if t.is_empty() { Vec::new() } else { vec![MNode::Run(MRun::new(t))] };
|
||||
vec![MNode::Nary { chr: '∑', lim_loc: None, grow: false, sub_hide, sup_hide, sub: arg(sub), sup: arg(sup), e: arg("x") }]
|
||||
};
|
||||
assert_eq!(to_linear(&nary(true, true, "", "")), "∑▒x");
|
||||
assert_eq!(to_linear(&nary(false, false, "i", "n")), "∑_i^n▒x");
|
||||
assert_eq!(to_linear(&nary(false, true, "", "")), "∑_()▒x");
|
||||
}
|
||||
@@ -30,10 +30,16 @@ fn page_number_type_is_kept_even_when_it_only_repeats_the_defaults() {
|
||||
|
||||
#[test]
|
||||
fn page_number_type_with_a_start_and_format_still_round_trips() {
|
||||
let body = r#"<w:p><w:r><w:t>One</w:t></w:r></w:p><w:sectPr><w:pgNumType w:fmt="upperRoman" w:start="3"/></w:sectPr>"#;
|
||||
let d = wordcraft_docx::read(&docx(body)).unwrap();
|
||||
let xml = document_xml(&wordcraft_docx::write(&d).unwrap());
|
||||
assert!(xml.contains(r#"w:fmt="upperRoman""#) && xml.contains(r#"w:start="3""#), "{xml}");
|
||||
for (format, start) in [("decimal", 0), ("upperRoman", 3)] {
|
||||
let page_number_type = format!(r#"<w:pgNumType w:fmt="{format}" w:start="{start}"/>"#);
|
||||
let body = format!(r#"<w:p><w:r><w:t>One</w:t></w:r></w:p><w:sectPr>{page_number_type}</w:sectPr>"#);
|
||||
let d = wordcraft_docx::read(&docx(&body)).unwrap();
|
||||
assert_eq!(d.last_section.page_num_start, Some(start));
|
||||
let saved = wordcraft_docx::write(&d).unwrap();
|
||||
let xml = document_xml(&saved);
|
||||
assert!(xml.contains(&page_number_type), "{xml}");
|
||||
assert_eq!(wordcraft_docx::read(&saved).unwrap().last_section.page_num_start, Some(start));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -3,36 +3,59 @@
|
||||
//! the same place for 0 pt and 12 pt before.
|
||||
|
||||
use super::lay;
|
||||
use crate::{Arc, Placed, StoryRef};
|
||||
use crate::{Arc, DocLayout, LayoutCache, LayoutOptions, Placed, StoryRef, ViewMode, layout};
|
||||
use wordcraft_doc::para::{Anchor, InlineObject};
|
||||
use wordcraft_doc::props::TableFloat;
|
||||
use wordcraft_doc::{Block, Document, Paragraph, Table, para_block};
|
||||
|
||||
/// Where the lines of body block `block` start on page one.
|
||||
fn line_y(d: &Document, block: u32) -> f32 {
|
||||
lay(d).pages[0]
|
||||
.items
|
||||
.iter()
|
||||
.find_map(|i| match i {
|
||||
Placed::Lines { story: StoryRef::Body, path, y, .. } if path.0 == [block] => Some(*y),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap()
|
||||
/// Positions are compared to a hundredth of a point; a kept 12 pt space before shows as at least
|
||||
/// 11.99 pt more.
|
||||
const EPS: f32 = 0.01;
|
||||
const KEPT: f32 = 12.0 - EPS;
|
||||
|
||||
/// What the paragraph after the table holds.
|
||||
enum Content<'a> {
|
||||
Empty,
|
||||
/// Nothing but this zero-width marker.
|
||||
Marker(&'a InlineObject),
|
||||
Text(&'static str),
|
||||
}
|
||||
|
||||
/// `table` (a floating one) or nothing, then a paragraph of `text` with `before`, then "After".
|
||||
fn document(table: bool, text: &str, before: f32) -> Document {
|
||||
let mut p = Paragraph::with_text(text, Default::default());
|
||||
// "#" stands for a bookmark, a zero-width marker.
|
||||
if text == "#" {
|
||||
p = Paragraph::new();
|
||||
p.insert_object(0, InlineObject::BookmarkStart { name: "_GoBack".into() }, &Default::default()).unwrap();
|
||||
}
|
||||
/// The page and top of body block `block`'s lines.
|
||||
fn line_at(l: &DocLayout, block: u32) -> (usize, f32) {
|
||||
l.pages
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find_map(|(pi, pg)| {
|
||||
pg.items.iter().find_map(|i| match i {
|
||||
Placed::Lines { story: StoryRef::Body, path, y, .. } if path.0 == [block] => Some((pi, *y)),
|
||||
_ => None,
|
||||
})
|
||||
})
|
||||
.unwrap_or_else(|| panic!("no lines laid out for body block {block}"))
|
||||
}
|
||||
|
||||
/// Where body block `block`'s lines start (on whatever page).
|
||||
fn line_y(d: &Document, block: u32) -> f32 {
|
||||
line_at(&lay(d), block).1
|
||||
}
|
||||
|
||||
/// A floating table `table` wide (or none), then a paragraph of `content` with `before`, then "After".
|
||||
fn document(table: Option<f32>, content: &Content<'_>, before: f32) -> Document {
|
||||
let mut p = match content {
|
||||
Content::Empty => Paragraph::new(),
|
||||
Content::Text(text) => Paragraph::with_text(text, Default::default()),
|
||||
Content::Marker(obj) => {
|
||||
let mut p = Paragraph::new();
|
||||
p.insert_object(0, (*obj).clone(), &Default::default()).unwrap();
|
||||
p
|
||||
}
|
||||
};
|
||||
p.props.space_before = Some(before);
|
||||
let mut d = Document::new();
|
||||
d.body = vec![para_block(p), para_block(Paragraph::with_text("After", Default::default()))];
|
||||
if table {
|
||||
let mut t = Table::new(1, 1, 200.0);
|
||||
if let Some(w) = table {
|
||||
let mut t = Table::new(1, 1, w);
|
||||
t.rows[0].cells[0].blocks = vec![para_block(Paragraph::with_text("Table", Default::default()))];
|
||||
t.props.float = Some(TableFloat { h_rel: Anchor::Margin, v_rel: Anchor::Paragraph, ..Default::default() });
|
||||
d.body.insert(0, Arc::new(Block::Table(t)));
|
||||
@@ -42,12 +65,55 @@ fn document(table: bool, text: &str, before: f32) -> Document {
|
||||
|
||||
#[test]
|
||||
fn an_empty_paragraph_after_a_floating_table_at_the_page_top_takes_no_space_before() {
|
||||
let empty = line_y(&document(true, "", 12.0), 2);
|
||||
let plain = line_y(&document(false, "", 0.0), 1);
|
||||
assert!((empty - plain).abs() < 0.01, "after the empty anchor at {empty}, with no table nor space before at {plain}");
|
||||
// A bookmark alone doesn't make it a paragraph with text.
|
||||
assert!((line_y(&document(true, "#", 12.0), 2) - plain).abs() < 0.01);
|
||||
let empty = line_y(&document(Some(200.0), &Content::Empty, 12.0), 2);
|
||||
let plain = line_y(&document(None, &Content::Empty, 0.0), 1);
|
||||
assert!((empty - plain).abs() < EPS, "after the empty anchor at {empty}, with no table nor space before at {plain}");
|
||||
// One with text keeps it, and so does an empty first paragraph with no table before it.
|
||||
assert!(line_y(&document(true, "Text", 12.0), 2) > empty + 11.99);
|
||||
assert!(line_y(&document(false, "", 12.0), 1) > plain + 11.99);
|
||||
assert!(line_y(&document(Some(200.0), &Content::Text("Text"), 12.0), 2) > empty + KEPT);
|
||||
assert!(line_y(&document(None, &Content::Empty, 12.0), 1) > plain + KEPT);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_paragraph_of_markers_only_after_the_leading_float_takes_no_space_before() {
|
||||
let empty = line_y(&document(Some(200.0), &Content::Empty, 12.0), 2);
|
||||
let markers = [
|
||||
InlineObject::BookmarkStart { name: "_GoBack".into() },
|
||||
InlineObject::CommentStart { id: 1 },
|
||||
InlineObject::PermStart { id: 1, group: "everyone".into(), editor: String::new() },
|
||||
];
|
||||
for m in markers {
|
||||
let y = line_y(&document(Some(200.0), &Content::Marker(&m), 12.0), 2);
|
||||
assert!((y - empty).abs() < EPS, "{m:?}: after it at {y}, after an empty anchor at {empty}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_paragraph_below_a_full_width_leading_float_takes_no_space_before() {
|
||||
// Not checked against Word (no fixture has it): the rule doesn't look at whether the paragraph
|
||||
// sits beside the table, so below one as wide as the text it drops its space before too.
|
||||
let full = |before| line_y(&document(Some(468.0), &Content::Empty, before), 2);
|
||||
assert!((full(12.0) - full(0.0)).abs() < EPS, "after it with 12 pt before at {}, with none at {}", full(12.0), full(0.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn web_view_and_a_floating_table_on_a_later_page_keep_the_space_before() {
|
||||
// Web view lays floating tables out in the text: the paragraph after one keeps its space before.
|
||||
let web = |before| {
|
||||
let opts = LayoutOptions { view: ViewMode::Web, web_width: 500.0, ..Default::default() };
|
||||
line_at(&layout(&document(Some(200.0), &Content::Empty, before), &mut LayoutCache::new(), &opts), 2).1
|
||||
};
|
||||
assert!(web(12.0) > web(0.0) + KEPT, "web view: after it with 12 pt before at {}, with none at {}", web(12.0), web(0.0));
|
||||
// A filler paragraph whose space after leaves less room than the table needs: the table goes to
|
||||
// the top of page two with its empty anchor, which isn't the document's leading float.
|
||||
let body_h = lay(&Document::new()).pages[0].body.h;
|
||||
let later = |before| {
|
||||
let mut d = document(Some(200.0), &Content::Empty, before);
|
||||
let mut filler = Paragraph::with_text("Filler", Default::default());
|
||||
filler.props.space_after = Some(body_h - 20.0);
|
||||
d.body.insert(0, para_block(filler));
|
||||
let l = lay(&d);
|
||||
assert_eq!(line_at(&l, 2).0, 1, "the empty anchor is on page two");
|
||||
line_at(&l, 3).1
|
||||
};
|
||||
assert!(later(12.0) > later(0.0) + KEPT, "later page: after it with 12 pt before at {}, with none at {}", later(12.0), later(0.0));
|
||||
}
|
||||
|
||||
@@ -240,7 +240,7 @@ impl PageNumbersForm {
|
||||
section: wordcraft_engine::cmd::page::section_index_of_focus(&app.session),
|
||||
format: sp.page_num_format.ooxml().to_string(),
|
||||
cont: sp.page_num_start.is_none(),
|
||||
start: sp.page_num_start.unwrap_or(1).clamp(1, 100_000),
|
||||
start: sp.page_num_start.unwrap_or(1).min(100_000),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -275,7 +275,7 @@ pub(super) fn page_numbers_ui(app: &mut WordApp, ui: &mut Ui, f: &mut PageNumber
|
||||
ui.radio_value(&mut f.cont, true, tl!("Continue from previous section"));
|
||||
ui.horizontal(|ui| {
|
||||
ui.radio_value(&mut f.cont, false, tl!("Start at:"));
|
||||
ui.add_enabled(!f.cont, egui::DragValue::new(&mut f.start).range(1..=100_000));
|
||||
ui.add_enabled(!f.cont, egui::DragValue::new(&mut f.start).range(0..=100_000));
|
||||
});
|
||||
});
|
||||
let (ok, cancel) = buttons(ui, "OK", true);
|
||||
|
||||
@@ -93,6 +93,72 @@ fn page_numbers_dialog_sets_the_format_and_start() {
|
||||
assert_eq!((f.section, f.format.as_str(), f.start), (1, "lowerRoman", 9));
|
||||
}
|
||||
|
||||
/// #4911: zero is an explicit start, distinct from continuing the previous section.
|
||||
#[test]
|
||||
fn page_numbers_dialog_preserves_zero_and_continue() {
|
||||
let mut a = app();
|
||||
a.run("ui.dialog", json!({"name": "pageNumbers"})).unwrap();
|
||||
let LayoutDialog::PageNumbers(f) = form(&mut a) else { panic!("page numbers") };
|
||||
assert!(f.cont);
|
||||
assert_eq!(f.start, 1, "unspecified numbering still defaults to one");
|
||||
for start in [0, 1, 100_000] {
|
||||
a.run("layout.pageNumberFormat", json!({"start": start})).unwrap();
|
||||
assert_eq!(a.session.doc.last_section.page_num_start, Some(start));
|
||||
a.run("ui.dialog", json!({"name": "pageNumbers"})).unwrap();
|
||||
let LayoutDialog::PageNumbers(f) = form(&mut a) else { panic!("page numbers") };
|
||||
assert!(!f.cont);
|
||||
assert_eq!(f.start, start);
|
||||
assert_eq!(f.params()["start"], json!(start));
|
||||
a.run("layout.pageNumberFormat", f.params()).unwrap();
|
||||
assert_eq!(a.session.doc.last_section.page_num_start, Some(start));
|
||||
a.run("ui.dialog", json!({"name": "pageNumbers"})).unwrap();
|
||||
let LayoutDialog::PageNumbers(mut f) = form(&mut a) else { panic!("page numbers") };
|
||||
assert!(!f.cont);
|
||||
assert_eq!(f.start, start, "reopening preserves the explicit start");
|
||||
f.cont = true;
|
||||
assert_eq!(f.params()["start"], serde_json::Value::Null);
|
||||
a.run("layout.pageNumberFormat", f.params()).unwrap();
|
||||
assert_eq!(a.session.doc.last_section.page_num_start, None);
|
||||
a.run("ui.dialog", json!({"name": "pageNumbers"})).unwrap();
|
||||
let LayoutDialog::PageNumbers(f) = form(&mut a) else { panic!("page numbers") };
|
||||
assert!(f.cont);
|
||||
assert_eq!(f.start, 1, "continuing does not become an explicit zero");
|
||||
}
|
||||
}
|
||||
|
||||
/// #4911: typing zero in Start at and pressing OK restarts numbering from zero.
|
||||
#[test]
|
||||
fn page_numbers_dialog_accepts_typed_zero() {
|
||||
use egui::accesskit::Role;
|
||||
use egui_kittest::kittest::Queryable;
|
||||
|
||||
let mut a = app();
|
||||
a.run("ui.language", json!({"value": "en"})).unwrap();
|
||||
a.run("ui.dialog", json!({"name": "pageNumbers"})).unwrap();
|
||||
// The dialog's existing numeric input and format picker lack accessible names.
|
||||
let mut h = egui_kittest::Harness::builder().with_accessibility_check(false).with_size(egui::vec2(1440.0, 900.0)).build_ui_state(
|
||||
|ui, app: &mut WordApp| {
|
||||
app.logic(&ui.ctx().clone());
|
||||
app.ui(ui);
|
||||
},
|
||||
a,
|
||||
);
|
||||
h.run_steps(4);
|
||||
h.get_by_label("Start at:").click();
|
||||
h.run_steps(2);
|
||||
h.get_by_role(Role::SpinButton).click();
|
||||
h.run_steps(2);
|
||||
h.get_by_role(Role::SpinButton).type_text("0");
|
||||
h.key_press(egui::Key::Enter);
|
||||
h.run_steps(2);
|
||||
assert_eq!(h.state().session.doc.last_section.page_num_start, None, "typing does not apply before OK");
|
||||
|
||||
h.get_by_role_and_label(Role::Button, "OK").click();
|
||||
h.run_steps(2);
|
||||
assert!(h.state().dialog.is_none(), "OK closes the dialog");
|
||||
assert_eq!(h.state().session.doc.last_section.page_num_start, Some(0));
|
||||
}
|
||||
|
||||
/// #755: the Line Numbers "From text" distance and the Hyphenation zone follow the
|
||||
/// File › Options unit (centimeters here); `params` sends points whatever the unit.
|
||||
#[test]
|
||||
|
||||
@@ -144,6 +144,8 @@ pub(crate) struct FileBrowser {
|
||||
listing: Result<Listing, String>,
|
||||
/// The highlighted entry.
|
||||
selected: Option<PathBuf>,
|
||||
/// The highlighted entry was a folder when it was selected.
|
||||
selected_is_dir: bool,
|
||||
note: Note,
|
||||
focus_name: bool,
|
||||
}
|
||||
@@ -182,6 +184,7 @@ impl FileBrowser {
|
||||
exports,
|
||||
listing: Ok(Listing::default()),
|
||||
selected: None,
|
||||
selected_is_dir: false,
|
||||
note: Note::None,
|
||||
focus_name: true,
|
||||
};
|
||||
@@ -199,6 +202,7 @@ impl FileBrowser {
|
||||
self.location = dir.to_string_lossy().into_owned();
|
||||
self.dir = dir;
|
||||
self.selected = None;
|
||||
self.selected_is_dir = false;
|
||||
self.note = Note::None;
|
||||
}
|
||||
|
||||
@@ -222,11 +226,13 @@ impl FileBrowser {
|
||||
/// pick. Save adds the chosen type's extension to a name without one (an export replaces a
|
||||
/// known document extension with the type's) and asks once more before replacing a file; Open wants a file that exists.
|
||||
pub(crate) fn accept(&mut self) -> Step {
|
||||
if let Some(dir) = self.selected.take().filter(|p| p.is_dir()) {
|
||||
if let Some(dir) = self.selected.clone().filter(|p| p.is_dir()) {
|
||||
self.navigate(dir);
|
||||
return Step::Stay;
|
||||
}
|
||||
let Some(mut path) = resolve(&self.dir, &self.name) else { return Step::Stay };
|
||||
// A removed folder remains a folder selection, not a proposed file name.
|
||||
let selected_file = self.selected.clone().filter(|_| !self.selected_is_dir);
|
||||
let Some(mut path) = selected_file.or_else(|| resolve(&self.dir, &self.name)) else { return Step::Stay };
|
||||
if path.is_dir() {
|
||||
self.name.clear();
|
||||
self.navigate(path);
|
||||
@@ -262,6 +268,7 @@ impl FileBrowser {
|
||||
/// A click on an entry: highlight it, and a file's name goes into the file name field.
|
||||
fn select(&mut self, e: &Entry) {
|
||||
self.selected = Some(e.path.clone());
|
||||
self.selected_is_dir = e.dir;
|
||||
if !e.dir {
|
||||
self.name = e.name.clone();
|
||||
}
|
||||
@@ -490,6 +497,7 @@ fn body(ui: &mut Ui, b: &mut FileBrowser, t: &Tokens) -> Option<Option<PathBuf>>
|
||||
}
|
||||
if r.changed() {
|
||||
b.selected = None;
|
||||
b.selected_is_dir = false;
|
||||
b.note = Note::None;
|
||||
}
|
||||
let before = b.filter;
|
||||
@@ -602,6 +610,74 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// A selected file keeps its exact name for Open and both steps of Save's replacement check.
|
||||
#[test]
|
||||
fn selecting_a_file_preserves_leading_spaces() {
|
||||
let dir = scratch("selected-path");
|
||||
let selected = dir.join(" report.docx");
|
||||
let sibling = dir.join("report.docx");
|
||||
std::fs::write(&selected, b"selected original").unwrap();
|
||||
std::fs::write(&sibling, b"sibling original").unwrap();
|
||||
for save in [false, true] {
|
||||
let req = if save {
|
||||
FileDialogRequest::Save { name: "Draft.docx".into(), dir: None }
|
||||
} else {
|
||||
FileDialogRequest::Open { purpose: "document".into() }
|
||||
};
|
||||
let mut browser = FileBrowser::new(req, AfterPick::OpenDocument { document: 0 }, dir.clone(), false);
|
||||
let entry = browser.listing.as_ref().unwrap().entries.iter().find(|entry| entry.path == selected).unwrap().clone();
|
||||
browser.select(&entry);
|
||||
if save {
|
||||
assert_eq!(browser.accept(), Step::Stay, "Save still asks before replacing the selected file");
|
||||
assert_eq!(browser.note, Note::Replace(selected.clone()));
|
||||
}
|
||||
assert_eq!(browser.accept(), Step::Pick(selected.clone()));
|
||||
assert_eq!(std::fs::read(&selected).unwrap(), b"selected original");
|
||||
assert_eq!(std::fs::read(&sibling).unwrap(), b"sibling original");
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// A folder selection keeps its kind even if the folder disappears before Save.
|
||||
#[test]
|
||||
fn a_selected_folder_never_replaces_the_typed_save_name() {
|
||||
for removed in [false, true] {
|
||||
let dir = scratch(if removed { "selected-folder-removed" } else { "selected-folder-live" });
|
||||
let folder = dir.join("Drafts");
|
||||
std::fs::create_dir(&folder).unwrap();
|
||||
let req = FileDialogRequest::Save { name: "chapter.docx".into(), dir: None };
|
||||
let mut browser = FileBrowser::new(req, AfterPick::OpenDocument { document: 0 }, dir.clone(), false);
|
||||
let entry = browser.listing.as_ref().unwrap().entries.iter().find(|entry| entry.path == folder).unwrap().clone();
|
||||
browser.select(&entry);
|
||||
assert_eq!(browser.name, "chapter.docx");
|
||||
if removed {
|
||||
std::fs::remove_dir(&folder).unwrap();
|
||||
assert_eq!(browser.accept(), Step::Pick(dir.join("chapter.docx")));
|
||||
assert!(!dir.join("Drafts.docx").exists());
|
||||
} else {
|
||||
assert_eq!(browser.accept(), Step::Stay);
|
||||
assert_eq!(browser.dir, folder);
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
|
||||
/// A selected file still names the exact Save target after external deletion.
|
||||
#[test]
|
||||
fn a_removed_selected_file_keeps_its_exact_save_path() {
|
||||
let dir = scratch("removed-selected-file");
|
||||
let selected = dir.join(" report.docx");
|
||||
std::fs::write(&selected, b"original").unwrap();
|
||||
let req = FileDialogRequest::Save { name: "Draft.docx".into(), dir: None };
|
||||
let mut browser = FileBrowser::new(req, AfterPick::OpenDocument { document: 0 }, dir.clone(), false);
|
||||
let entry = browser.listing.as_ref().unwrap().entries.iter().find(|entry| entry.path == selected).unwrap().clone();
|
||||
browser.select(&entry);
|
||||
std::fs::remove_file(&selected).unwrap();
|
||||
assert_eq!(browser.accept(), Step::Pick(selected));
|
||||
assert_eq!(browser.note, Note::None);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// The host can't show a system dialog (#576): Save As carries on in the built-in browser,
|
||||
/// and its answer saves the document.
|
||||
#[test]
|
||||
|
||||
@@ -180,13 +180,20 @@ fn font_key(name: &str, ppp: f32) -> String {
|
||||
}
|
||||
|
||||
impl Previews {
|
||||
/// Ensure the cached family lists and style preview textures match the current font generation.
|
||||
fn check_fonts(&mut self) {
|
||||
let generation = wordcraft_fonts::FontDb::global().generation();
|
||||
if self.font_gen != generation {
|
||||
self.fonts_changed();
|
||||
}
|
||||
}
|
||||
|
||||
/// The font list: installed families, each by its name in the interface language when it has
|
||||
/// one (宋体 for SimSun in Chinese, #505), like Word's.
|
||||
pub fn families(&mut self) -> Vec<String> {
|
||||
self.check_fonts();
|
||||
let lang = crate::i18n::current().code();
|
||||
let generation = wordcraft_fonts::FontDb::global().generation();
|
||||
if self.families.as_ref().is_none_or(|(l, _)| *l != lang) || self.font_gen != generation {
|
||||
self.font_gen = generation;
|
||||
if self.families.as_ref().is_none_or(|(l, _)| *l != lang) {
|
||||
let db = wordcraft_fonts::FontDb::global();
|
||||
let mut f: Vec<String> = db.families_in(lang).into_iter().filter(|f| !f.starts_with('.')).collect();
|
||||
for w in [wordcraft_doc::styles::BODY_FONT, wordcraft_doc::styles::HEADING_FONT] {
|
||||
@@ -237,11 +244,15 @@ impl Previews {
|
||||
self.english_names.get(shown).cloned().unwrap_or_else(|| shown.to_string())
|
||||
}
|
||||
|
||||
/// Fonts were added or removed: list the families again. The font menus' entries see the
|
||||
/// change themselves ([`FontAtlas`]), in every window.
|
||||
/// Fonts were added or removed: list the families again and clear cached style preview
|
||||
/// textures so any style rendered with a fallback face refreshes with newly available fonts.
|
||||
/// The font menus' entries see the change themselves ([`FontAtlas`]), in every window.
|
||||
pub fn fonts_changed(&mut self) {
|
||||
self.families = None;
|
||||
self.east_asian = None;
|
||||
// Style previews may have been rasterised with a fallback face before a font was installed.
|
||||
self.tex.clear();
|
||||
self.font_gen = wordcraft_fonts::FontDb::global().generation();
|
||||
}
|
||||
|
||||
/// A closure drawing a font-menu entry in its own face, from the fonts' atlas.
|
||||
@@ -299,7 +310,10 @@ impl Previews {
|
||||
})
|
||||
}
|
||||
|
||||
/// Look up a cached preview texture under `key`, or render and insert it if missing.
|
||||
/// Invalidates the cache first if the font generation has changed.
|
||||
pub(crate) fn get_or(&mut self, ctx: &egui::Context, key: &str, make: impl FnOnce() -> Option<egui::ColorImage>) -> Option<TextureHandle> {
|
||||
self.check_fonts();
|
||||
if let Some(t) = self.tex.get(key) {
|
||||
return Some(t.clone());
|
||||
}
|
||||
|
||||
@@ -64,6 +64,36 @@ fn gallery_style(name: &str) -> wordcraft_doc::styles::Style {
|
||||
wordcraft_doc::styles::Style { id: "Backfill".into(), name: name.into(), quick: true, priority: Some(0), ..Default::default() }
|
||||
}
|
||||
|
||||
/// Seeded style preview textures are invalidated when fonts change so that newly available
|
||||
/// faces replace fallback fonts used in earlier renders (#5957).
|
||||
#[test]
|
||||
fn font_changes_invalidate_cached_style_preview_textures() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut previews = Previews::default();
|
||||
let texture = ctx.load_texture("test-style-preview", egui::ColorImage::new([1, 1], vec![egui::Color32::BLACK]), egui::TextureOptions::LINEAR);
|
||||
previews.tex.insert("style:Normal:cached".into(), texture);
|
||||
previews.fonts_changed();
|
||||
assert!(previews.tex.is_empty(), "a new font can replace the fallback used by cached style previews");
|
||||
}
|
||||
|
||||
/// Previews instances (such as other open windows) that missed an active `watch_fonts` call
|
||||
/// invalidate their cached preview textures on their next lookup once the font generation advances (#5957).
|
||||
#[test]
|
||||
fn stale_previews_invalidate_on_next_lookup() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut previews = Previews::default();
|
||||
let texture = ctx.load_texture("test-style-preview", egui::ColorImage::new([1, 1], vec![egui::Color32::BLACK]), egui::TextureOptions::LINEAR);
|
||||
previews.tex.insert("style:Normal:cached".into(), texture);
|
||||
// Simulate this Previews instance having rendered at an older font generation.
|
||||
previews.font_gen = wordcraft_fonts::FontDb::global().generation().wrapping_sub(1);
|
||||
let mut made = false;
|
||||
let _ = previews.get_or(&ctx, "style:Normal:cached", || {
|
||||
made = true;
|
||||
Some(egui::ColorImage::new([1, 1], vec![egui::Color32::WHITE]))
|
||||
});
|
||||
assert!(made, "the cached preview was invalidated and re-rendered because the font generation changed");
|
||||
}
|
||||
|
||||
/// Regression test for #2: a style gallery label longer than its tile was painted at full width
|
||||
/// and ran into the neighbouring tiles; it is now cut with an ellipsis to fit the tile.
|
||||
#[test]
|
||||
|
||||
@@ -134,6 +134,7 @@ Page Setup's margin, paper-size and document-grid number boxes scroll long text
|
||||
| 2026-11-14 | trivial | Save As and Export (also the formats on the Print page) offer Browse in WordCraft… on Linux/BSD, as Open does; exporting there lists Export's formats as the types, whose extension replaces a known document extension on the name; an export refusal or failure shows on the Export page, as Save As's does (#3535) |
|
||||
| 2026-11-16 | trivial | Language dialog whole-document OK: Do not check sent only when clicked; `review.language` document scope returns the caret result with `scope`; glossary skipped while tracking formatting (result says `glossarySkipped`) (#3518) |
|
||||
| 2026-11-14 | trivial | Language dialog: Apply to the whole document sets the proofing language or Do not check for every story (body, headers, footers, notes, comments, text boxes, tables, glossary) in one undo step; `review.language` takes `scope: "document"` (#3518) |
|
||||
| 2026-11-10 | trivial | Home Styles gallery previews invalidate cached raster textures when the font database changes (#5957) |
|
||||
| 2026-10-26 | trivial | Dialog sections: 13 dialogs group related controls in theme-aware panels with indented settings; tall dialogs scroll only when needed (#4035, #4038) |
|
||||
| 2026-11-12 | trivial | Clipboard: a copied picture is published as PNG (and HTML where the host writes it); WordCraft's own paste still keeps the original fragment, and a copy of only shapes carries no HTML or RTF (#6205). Native external-app paste remains untested. |
|
||||
| 2026-11-07 | trivial | Vertical scrollbar track: clicking above the elevator moves one screen up and clicking below it moves one screen down, as in Word, instead of jumping to that position in the document (#5853) |
|
||||
|
||||
Reference in New Issue
Block a user