termrs_core 0.3.0

The core library of termrs
Documentation
use crate::{
    input::{Event, EventStatus},
    render::{Position, RenderContext, Size},
};

mod event_context;

pub use copyrs::clipboard;
pub use copyrs::{Clipboard, ClipboardBinaryKind, ClipboardContent, ClipboardTextKind, Result};
pub use event_context::*;

pub trait LayoutInfo {
    /// Returns the bounding box size.
    fn bounds(&self) -> Size;
}

impl LayoutInfo for Size {
    fn bounds(&self) -> Size {
        *self
    }
}

/// Information about [`Widget`] rendering.
pub trait RenderInfo {
    /// Returns bounding box size
    /// of the rendered content
    fn bounds(&self) -> Size;

    /// Returns top-left position
    /// of the bounding box.
    fn position(&self) -> Position;
}

pub struct SimpleRenderInfo {
    pub bounds: Size,
    pub position: Position,
}

impl RenderInfo for SimpleRenderInfo {
    fn bounds(&self) -> Size {
        self.bounds
    }

    fn position(&self) -> Position {
        self.position
    }
}

impl SimpleRenderInfo {
    pub fn new(bounds: Size, position: Position) -> Self {
        Self { bounds, position }
    }
}

/// An object which can render itself to the [`RenderContext`]
/// and can handle input.
pub trait Widget<Message> {
    type LayoutInfo: LayoutInfo;
    type RenderInfo: RenderInfo;

    /// Renders itself to the given context
    /// based on the state.
    fn render(
        &self,
        position: Position,
        layout_info: &Self::LayoutInfo,
        context: &mut dyn RenderContext,
    ) -> std::io::Result<Self::RenderInfo>;

    /// Calculates and returns layout information
    /// based on the available size.
    fn layout(&self, available_size: Size) -> Self::LayoutInfo;

    /// Handled the given event.
    fn on_event(
        &mut self,
        event: &Event,
        render_info: &Self::RenderInfo,
        event_context: &mut dyn EventContext<Message>,
    ) -> EventStatus {
        let _ = event;
        let _ = render_info;
        let _ = event_context;

        EventStatus::Ignored
    }
}