red-fw-gallery: SpellingMarks stories use the framework's text layout; TextEditor story on the panel surface

Written by a Codex red fixer; committed by the lead (gallery gate).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Dylan M. TaylorandClaude Opus 5.5 committed 2026-11-27 07:32:54 +00:00
1 parent bc81d34dc5
commit f2e3439ccd
2 files changed
+49 -35

No files matched your search

+6 -5
View File
@@ -12,7 +12,7 @@ use craft_ui::contract::{Bind, Show as _, ShowContainer as _, Validity};
use craft_ui::runtime::env::{Env, Message};
use craft_ui::testing::canvas_story::{FontOutline, StoryPicture, paint_shapes, show_document, text_shapes};
use craft_ui::testing::{Drive, StoryData, StoryOutput, stage_fitting, story};
use egui::{Color32, Pos2, Rect, Ui, Vec2};
use egui::{Pos2, Rect, Ui, Vec2};
use super::{CaretLine, EditKind, EditorMode, HostLine, HostRun, TextEditor, TextEditorHost, TextSelection};
@@ -237,7 +237,9 @@ fn over_canvas(ui: &mut Ui, text: &mut String, mut bind: Bind<'_, TextSelection>
return StoryOutput::default().readout(error.to_string());
}
};
let ink = craft_ui::style::rgba_color(Env::of(ui).tokens.color.text.normal);
let env = Env::of(ui);
let ink = craft_ui::style::rgba_color(env.tokens.color.text.normal);
let surface = craft_ui::style::rgba_color(env.tokens.color.surface.panel);
let shown = Canvas::new("Document").view(view).show_at(ui, room, |ui, _| {
let bounds = Rect::from_min_size(view.to_screen(Point::ZERO), host.extent_with_caret_cell());
let rows = host.row_texts();
@@ -247,10 +249,9 @@ fn over_canvas(ui: &mut Ui, text: &mut String, mut bind: Bind<'_, TextSelection>
Ok(shapes) => shapes,
Err(error) => return StoryOutput::default().readout(error.to_string()),
};
let revision = super::fingerprint(&format!("{content}:{ink:?}")).1;
let revision = super::fingerprint(&format!("{content}:{ink:?}:{surface:?}")).1;
let extent = host.extent_with_caret_cell();
let picture =
StoryPicture::new(extent.x.ceil() as u32, extent.y.ceil() as u32, |p| paint_shapes(&shapes, Color32::TRANSPARENT, p)).revision(revision);
let picture = StoryPicture::new(extent.x.ceil() as u32, extent.y.ceil() as u32, |p| paint_shapes(&shapes, surface, p)).revision(revision);
show_document(ui, room, &picture);
let editor = TextEditor::new("Text", &mut host, bind.reborrowed()).mode(mode).click_runs(click_runs);
StoryOutput::from(if masked { editor.masked() } else { editor }.show_at(ui, bounds))
@@ -1,43 +1,56 @@
//! Stories of spelling marks in document coordinates.
use super::SpellingMarks;
use crate::canvas::testing::canvas_story::{StoryDocument, StoryPicture, on_canvas_view, paint_shapes, text_shapes};
use crate::canvas::canvas_space;
use crate::canvas::testing::canvas_story::{StoryDocument, on_canvas_view};
use crate::contract::Show as _;
use crate::primitives::FlagMark;
use crate::runtime::env::with_env;
use crate::fonts::FontRef;
use crate::primitives::{DocRun, DocText, FlagMark};
use crate::runtime::env::{Env, with_env};
use crate::style::{Corners, DataColor, SurfaceKind};
use crate::testing::{Drive, StoryData, StoryOutput, story};
use craft_behavior::kurbo::{Point, Vec2};
use egui::{Color32, FontFamily, Ui};
use craft_behavior::kurbo::Vec2;
use egui::Ui;
/// The document's glyphs and marks share fixed cells, so zoom and rotation exercise the same mapping.
/// The marks follow the rows and advances of the framework's document text layout.
fn words(ui: &mut Ui, wrapped: bool, angle: f64, zoom: f64) -> StoryOutput {
let definitions = crate::fonts::deflated_default_definitions();
let data = definitions.families.get(&FontFamily::Proportional).and_then(|names| names.first()).and_then(|name| definitions.font_data.get(name));
let Some(data) = data else {
return crate::display::Text::new("The story font is unavailable").show(ui).into();
};
let Ok(face) = craft_font_catalog::FontOutline::from_bytes(data.bytes(), data.index, &[]) else {
return crate::display::Text::new("The story font could not be read").show(ui).into();
};
let text = if wrapped { "mispelled\nwrds" } else { "mispelled" };
let origin = Point::new(220.0, 160.0);
let cell = Vec2::new(22.0, 44.0);
let Ok(shapes) = text_shapes(&face, text, origin, cell, 30.0, Color32::from_gray(40)) else {
return crate::display::Text::new("The story glyphs could not be drawn").show(ui).into();
};
let picture = StoryPicture::new(640, 400, move |p| paint_shapes(&shapes, Color32::TRANSPARENT, p));
let segments: Vec<_> = text
.lines()
.enumerate()
.map(|(row, word)| {
let y = origin.y + (row + 1) as f64 * cell.y;
(Point::new(origin.x, y), Point::new(origin.x + word.chars().count() as f64 * cell.x, y))
})
.collect();
on_canvas_view(
ui,
|room, ppp| StoryDocument::STANDARD.view(room, ppp, zoom).rotation(angle),
Some(&picture),
|ui, at, _| SpellingMarks::new(&segments).show_at(ui, at),
None,
|ui, at, _| {
let env = Env::of(ui);
let Some(space) = canvas_space(&env.view()) else { return StoryOutput::default() };
let view = space.view;
let runs = [DocRun::new(
text,
FontRef::proportional(),
env.tokens.typography.heading.size * view.zoom as f32,
DataColor::new(crate::style::rgba_color(env.tokens.color.text.normal)),
)];
let mut layout = DocText::new(&runs).layout(ui.ctx(), &env.tokens, env.direction);
let origin = view.center - Vec2::new(f64::from(layout.size().x) / view.zoom, f64::from(layout.size().y) / view.zoom) / 2.0;
let segments: Vec<_> = layout
.galley
.rows
.iter()
.filter_map(|row| {
let first = row.glyphs.first()?;
let last = row.glyphs.last()?;
let y = f64::from(row.pos.y + first.logical_rect().bottom()) / view.zoom;
Some((
origin + Vec2::new(f64::from(row.pos.x + first.pos.x) / view.zoom, y),
origin + Vec2::new(f64::from(row.pos.x + last.max_x()) / view.zoom, y),
))
})
.collect();
layout.rotation = view.rotation.to_radians() as f32;
space.paint_overlay(ui, |draw, _| {
draw.surface(at, SurfaceKind::Panel, Corners::All);
draw.galley(view.to_screen(origin), &layout);
});
SpellingMarks::new(&segments).show_at(ui, at).into()
},
)
}