Skip to main content

mathtex_editor_core/
host.rs

1//! Host API for the guest and host boundary.
2
3use crate::command::{ExitDir, Side};
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/// Placement of one host object atom in the rendered fragment.
31#[derive(Debug, Clone, Copy, PartialEq)]
32pub struct HostObject {
33    /// The host minted token identifying the object.
34    pub token: u32,
35    /// The object's box in IR/render coordinate space.
36    pub rect: Rect,
37}
38
39/// The renderer neutral output the host paints.
40pub struct RenderOutput {
41    /// The mathtex IR fragment.
42    pub ir: mathtex_ir::Fragment,
43    /// The caret rectangle.
44    pub caret: Rect,
45    /// The selection rectangles.
46    pub selection: Vec<Rect>,
47    /// Empty slot placeholder rectangles.
48    pub placeholders: Vec<Rect>,
49    /// The fragment metrics.
50    pub metrics: Metrics,
51    /// The Backspace dropdown menu when one is open.
52    pub menu: Option<crate::menu::MenuView>,
53    /// Placements of host object atoms so the host can overlay their content.
54    pub host_objects: Vec<HostObject>,
55}
56
57/// Callbacks implemented by the host.
58pub trait Host {
59    /// Typeset a LaTeX fragment to the mathtex IR.
60    fn typeset(&self, latex: &str) -> mathtex_ir::Fragment;
61    /// Handle changed content.
62    fn on_change(&self, doc: &Document);
63    /// Handle the caret leaving the math region at a boundary.
64    fn on_exit(&self, dir: ExitDir);
65    /// Request that math mode ends.
66    fn request_close(&self);
67    /// Request a repaint via `Editor::render`.
68    fn on_render(&self);
69    /// Handle horizontal caret motion into a host object, `true` consumes the motion.
70    ///
71    /// `side` is the direction of approach, `Before` when moving right and `After` when moving
72    /// left. The default leaves the motion unconsumed, so the caret skips over the object.
73    fn on_object_enter(&self, token: u32, side: Side) -> bool {
74        let _ = (token, side);
75        false
76    }
77}