mathtex_editor_core/host.rs
1//! Host API for the guest and host boundary.
2
3use crate::command::ExitDir;
4use crate::doc::Document;
5
6/// A rectangle in IR/render coordinate space.
7#[derive(Debug, Clone, Copy, PartialEq)]
8pub struct Rect {
9 /// Horizontal position.
10 pub x: f64,
11 /// Vertical position.
12 pub y: f64,
13 /// Rectangle width.
14 pub width: f64,
15 /// Rectangle height.
16 pub height: f64,
17}
18
19/// Inline layout metrics for the whole fragment.
20#[derive(Debug, Clone, Copy, PartialEq)]
21pub struct Metrics {
22 /// Inline layout width.
23 pub width: f64,
24 /// Inline layout height.
25 pub height: f64,
26 /// Inline layout baseline.
27 pub baseline: f64,
28}
29
30/// The renderer neutral output the host paints.
31pub struct RenderOutput {
32 /// The mathtex IR fragment.
33 pub ir: mathtex_ir::Fragment,
34 /// The caret rectangle.
35 pub caret: Rect,
36 /// The selection rectangles.
37 pub selection: Vec<Rect>,
38 /// Empty slot placeholder rectangles.
39 pub placeholders: Vec<Rect>,
40 /// The fragment metrics.
41 pub metrics: Metrics,
42 /// The Backspace dropdown menu when one is open.
43 pub menu: Option<crate::menu::MenuView>,
44}
45
46/// Callbacks implemented by the host.
47pub trait Host {
48 /// Typeset a LaTeX fragment to the mathtex IR.
49 fn typeset(&self, latex: &str) -> mathtex_ir::Fragment;
50 /// Handle changed content.
51 fn on_change(&self, doc: &Document);
52 /// Handle the caret leaving the math region at a boundary.
53 fn on_exit(&self, dir: ExitDir);
54 /// Request that math mode ends.
55 fn request_close(&self);
56 /// Request a repaint via `Editor::render`.
57 fn on_render(&self);
58}