mathtex-editor-core 0.2.0

Headless core of the mathtex structural math editor: model, operations, navigation, selection, IR matching
Documentation
//! Host API for the guest and host boundary.

use crate::command::{ExitDir, Side};
use crate::doc::Document;

/// A rectangle in IR/render coordinate space.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rect {
    /// Horizontal position.
    pub x: f64,
    /// Vertical position.
    pub y: f64,
    /// Rectangle width.
    pub width: f64,
    /// Rectangle height.
    pub height: f64,
}

/// Inline layout metrics for the whole fragment.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Metrics {
    /// Inline layout width.
    pub width: f64,
    /// Inline layout height.
    pub height: f64,
    /// Inline layout baseline.
    pub baseline: f64,
}

/// Placement of one host object atom in the rendered fragment.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct HostObject {
    /// The host minted token identifying the object.
    pub token: u32,
    /// The object's box in IR/render coordinate space.
    pub rect: Rect,
}

/// The renderer neutral output the host paints.
pub struct RenderOutput {
    /// The mathtex IR fragment.
    pub ir: mathtex_ir::Fragment,
    /// The caret rectangle.
    pub caret: Rect,
    /// The selection rectangles.
    pub selection: Vec<Rect>,
    /// Empty slot placeholder rectangles.
    pub placeholders: Vec<Rect>,
    /// The fragment metrics.
    pub metrics: Metrics,
    /// The Backspace dropdown menu when one is open.
    pub menu: Option<crate::menu::MenuView>,
    /// Placements of host object atoms so the host can overlay their content.
    pub host_objects: Vec<HostObject>,
}

/// Callbacks implemented by the host.
pub trait Host {
    /// Typeset a LaTeX fragment to the mathtex IR.
    fn typeset(&self, latex: &str) -> mathtex_ir::Fragment;
    /// Handle changed content.
    fn on_change(&self, doc: &Document);
    /// Handle the caret leaving the math region at a boundary.
    fn on_exit(&self, dir: ExitDir);
    /// Request that math mode ends.
    fn request_close(&self);
    /// Request a repaint via `Editor::render`.
    fn on_render(&self);
    /// Handle horizontal caret motion into a host object, `true` consumes the motion.
    ///
    /// `side` is the direction of approach, `Before` when moving right and `After` when moving
    /// left. The default leaves the motion unconsumed, so the caret skips over the object.
    fn on_object_enter(&self, token: u32, side: Side) -> bool {
        let _ = (token, side);
        false
    }
}