Keep RTL paragraph marks inside the Story Editor text column (#3360)

* Exercise a full-width RTL paragraph mark in the Story Editor

* Keep RTL paragraph marks inside the Story Editor text column

* test: probe Story Editor marks at a narrow width

* Keep narrow Story Editor text visible with RTL marks

---------

Co-authored-by: skylartaylor <[email protected]>
This commit is contained in:
skylar taylor-barrickandskylartaylor authored and GitHub committed 2026-12-01 15:53:51 -06:00
1 parent 109c99d00f
commit 06938f5161
2 files changed
+112 -22

No files matched your search

+15 -7
View File
@@ -572,19 +572,26 @@ mod framework {
let mut options = TextOptions::new();
options.lines = Some(TEXT_ROWS);
options.size = Some(self.s.size);
// A paragraph that just fills the editor wraps while its mark shows, so the mark is drawn inside the field's
// width rather than in its margin or cut off.
// The text starts at the top of the editor, beside the style names and the ruler that follow its rows.
options.top = true;
// The editor has no edit menu of its own, as the native editor has none.
options.own_menu = true;
if let Some(value) = cx.value() {
// A paragraph that just fills the editor wraps while its mark shows, so the mark is drawn inside the
// field's width rather than in its margin or cut off.
let mut text_rect = g.text;
if self.s.marks {
text_rect.max.x = (text_rect.max.x - self.s.size.round()).max(text_rect.min.x);
let gutter = self.s.size.round();
// Keep the right gutter. Reserve space on the left only if a paragraph with a break can end there.
// The laid-out rows still decide where each mark goes, including neutral rows that follow RTL text.
let rtl_break = value.rsplit_once('\n').is_some_and(|(marked, _)| {
!marked.is_ascii() && marked.split('\n').any(|para| unicode_bidi::get_base_direction(para) == unicode_bidi::Direction::Rtl)
});
text_rect.max.x = (text_rect.max.x - gutter).max(text_rect.min.x);
if rtl_break {
// Leave a font-size worth of editing room when possible. In a narrower column,
// let the clip trim the optional mark rather than taking space from the text.
let left_gutter = (text_rect.width() - gutter).clamp(0.0, gutter);
text_rect.min.x += left_gutter;
}
}
cx.text(text_key(), text_rect, &PlainText, value, &options);
}
@@ -649,8 +656,9 @@ mod framework {
// A ¶ past each paragraph's last row, in the faint text, while the View menu shows them.
if self.s.marks {
let font = egui::FontId::proportional(self.s.size);
let marks = painter.with_clip_rect(g.text);
for (at, align) in super::paragraph_mark_spots(&layout.galley) {
painter.text(origin + at.to_vec2(), align, super::PARAGRAPH_MARK, font.clone(), t.text_dim);
marks.text(origin + at.to_vec2(), align, super::PARAGRAPH_MARK, font.clone(), t.text_dim);
}
}
// The overset line: across the text, and the ruler when it shows, above the first character that doesn't fit.
@@ -112,20 +112,30 @@ fn paragraph_break_marks_show_and_hide_from_the_view_menu() {
assert!(count(&h).is_empty(), "hiding them takes the marks away");
}
/// With both editor side columns visible, an RTL paragraph break must not paint into the depth ruler.
/// With both editor side columns visible, an RTL paragraph break must not paint into the depth ruler,
/// including when the terminal row fills the editor's wrap width.
#[test]
fn rtl_paragraph_break_marks_stay_inside_the_story_text_column() {
fn collect(shape: &egui::Shape, clip: egui::Rect, out: &mut Vec<(egui::Rect, egui::Rect)>) {
fn collect<'a>(shape: &'a egui::Shape, clip: egui::Rect, out: &mut Vec<(&'a egui::epaint::TextShape, egui::Rect)>) {
match shape {
egui::Shape::Vec(shapes) => shapes.iter().for_each(|s| collect(s, clip, out)),
egui::Shape::PaintChunk(chunk) => chunk.iter().for_each(|s| collect(&s.shape, clip.intersect(s.clip_rect), out)),
egui::Shape::Text(t) if t.galley.text() == super::PARAGRAPH_MARK => out.push((t.visual_bounding_rect(), clip)),
egui::Shape::Text(t) => out.push((t, clip)),
_ => {}
}
}
fn painted_text<'a>(h: &'a egui_kittest::Harness<'_, DesignApp>) -> Vec<(&'a egui::epaint::TextShape, egui::Rect)> {
let mut out = Vec::new();
for shape in &h.output().shapes {
collect(&shape.shape, shape.clip_rect, &mut out);
}
out
}
let mut app = DesignApp::new(designcraft_engine::Session::new(), Default::default());
app.run("file.new", json!({})).unwrap();
let text = format!("{}\nEnglish paragraph\nLast", "אבגדהוזחטיכלמנסעפצקרשת".repeat(2));
let rtl = "אבגדהוזחטיכלמנסעפצקרשת".repeat(2);
let text = format!("{rtl}\nEnglish paragraph\nLast");
let story = app.run("frame.create", json!({"rect": [72, 72, 372, 620], "content": "text", "text": text})).unwrap()["story"].clone();
app.run("app.storyEditor", json!({"story": story})).unwrap();
app.ui.story_editor_style_column = true;
@@ -146,16 +156,88 @@ fn rtl_paragraph_break_marks_stay_inside_the_story_text_column() {
);
h.input_mut().max_texture_side = Some(8192);
h.run_steps(4);
let divider = h.get_by_role_and_label(egui::accesskit::Role::Splitter, "Style name column divider").rect();
let left = divider.right() + super::RULER_WIDTH;
let mut marks = Vec::new();
for shape in &h.output().shapes {
collect(&shape.shape, shape.clip_rect, &mut marks);
let check_marks = |h: &egui_kittest::Harness<'_, DesignApp>| {
let divider = h.get_by_role_and_label(egui::accesskit::Role::Splitter, "Style name column divider").rect();
let left = divider.right() + super::RULER_WIDTH;
let marks: Vec<_> = painted_text(h)
.into_iter()
.filter(|(t, _)| t.galley.text() == super::PARAGRAPH_MARK)
.map(|(t, clip)| (t.visual_bounding_rect(), clip))
.collect();
assert_eq!(marks.len(), 2, "both newline-terminated paragraphs paint their mark");
assert!(marks[0].0.top() < marks[1].0.top(), "RTL paragraph mark is first");
for &(rect, clip) in &marks {
assert!(rect.left() >= left - 0.5, "mark at {rect:?} overlaps the side columns ending at {left}");
assert!(clip.contains_rect(rect), "mark at {rect:?} is clipped by {clip:?}");
}
(marks[0].0, marks[1].0)
};
check_marks(&h);
// Read the mounted editor's own wrap width and glyph metrics. Resize until the Hebrew paragraph fits on
// one row with only a few points to spare, regardless of the installed fonts or the space taken by gutters.
let wide = egui::vec2(900.0, 700.0);
let story_shape = |h: &egui_kittest::Harness<'_, DesignApp>| {
let (t, _) = painted_text(h).into_iter().find(|(t, _)| t.galley.text() == text).expect("painted story text");
(t.pos, t.galley.clone())
};
h.set_size(wide);
h.run_steps(2);
let (_, galley) = story_shape(&h);
let row = &galley.rows[0];
assert!(row.rtl_paragraph && row.ends_with_newline, "the wide probe lays out the Hebrew paragraph on one RTL row");
h.set_size(wide - egui::vec2(galley.job.wrap.max_width - row.rect().width() - 2.0, 0.0));
h.run_steps(2);
let (origin, galley) = story_shape(&h);
let row = &galley.rows[0];
let slack = galley.job.wrap.max_width - row.rect().width();
assert!(row.rtl_paragraph && row.ends_with_newline, "the full-width row must be RTL and end at the paragraph break");
assert!((0.0..=4.0).contains(&slack), "the RTL terminal row leaves {slack} points of unused wrap width");
let (rtl_mark, ltr_mark) = check_marks(&h);
let rtl_row = row.rect().translate(origin.to_vec2());
let ltr_row = galley.rows[1].rect().translate(origin.to_vec2());
assert!(rtl_mark.right() <= rtl_row.left(), "RTL mark {rtl_mark:?} must sit to the left of row {rtl_row:?}");
assert!(ltr_mark.left() >= ltr_row.right(), "LTR mark {ltr_mark:?} must sit to the right of row {ltr_row:?}");
}
#[test]
fn narrow_rtl_story_editor_keeps_painting_text_when_marks_show() {
fn visible_story(shape: &egui::Shape, clip: egui::Rect, story: &str) -> bool {
match shape {
egui::Shape::Vec(shapes) => shapes.iter().any(|s| visible_story(s, clip, story)),
egui::Shape::PaintChunk(chunk) => chunk.iter().any(|s| visible_story(&s.shape, clip.intersect(s.clip_rect), story)),
egui::Shape::Text(t) if t.galley.text() == story => t.visual_bounding_rect().intersect(clip).is_positive(),
_ => false,
}
}
assert_eq!(marks.len(), 2, "both newline-terminated paragraphs paint their mark");
assert!(marks[0].0.top() < marks[1].0.top(), "RTL paragraph mark is first");
for (rect, clip) in marks {
assert!(rect.left() >= left - 0.5, "mark at {rect:?} overlaps the side columns ending at {left}");
assert!(clip.contains_rect(rect), "mark at {rect:?} is clipped by {clip:?}");
}
let mut app = DesignApp::new(designcraft_engine::Session::new(), Default::default());
let story_text = "א\nx";
app.run("file.new", json!({})).unwrap();
let r = app.run("frame.create", json!({"rect": [72, 72, 372, 400], "content": "text", "text": story_text})).unwrap();
app.run("app.storyEditor", json!({"story": r["story"]})).unwrap();
app.ui.story_editor_style_column = true;
app.ui.story_editor_style_width = 400.0;
app.ui.story_editor_depth_ruler = true;
app.ui.story_editor_size = 36.0;
let mut ready = false;
let mut h = crate::test_window::builder::<DesignApp>().with_size(egui::vec2(515.0, 700.0)).with_accessibility_check(false).build_ui_state(
move |ui, app: &mut DesignApp| {
if !ready {
crate::theme::install_fonts(ui.ctx(), &app.ui.language);
crate::theme::apply(ui.ctx(), &crate::theme::Tokens::for_brightness(Default::default()));
ready = true;
return;
}
super::body(app, ui);
},
app,
);
h.input_mut().max_texture_side = Some(8192);
h.run_steps(4);
let visible = |h: &egui_kittest::Harness<'_, DesignApp>| h.output().shapes.iter().any(|s| visible_story(&s.shape, s.clip_rect, story_text));
assert!(visible(&h), "the narrow editor starts with visible story text");
h.state_mut().run("view.storyEditorParagraphMarks", json!({})).unwrap();
h.run_steps(4);
assert!(visible(&h), "enabling paragraph marks must not make all editable story text disappear");
}