mathtex-editor-core 0.1.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;
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,
}

/// 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>,
}

/// 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);
}