termrs_core 0.3.0

The core library of termrs
Documentation
use std::marker::PhantomData;

use super::Clipboard;

use crate::render::Position;

pub trait EventContext<Message> {
    /// Forces the widget to be rerendered.
    fn invalidate_render(&mut self);

    /// Forces the widget's layout to be recalculated
    /// and so the widget to be rerendered.
    fn invalidate_layout(&mut self);

    /// Emits the message for application to handle.
    fn emit_message(&mut self, message: Message);

    /// Returns reference to the clipboard.
    fn clipboard(&self) -> &dyn Clipboard;

    /// Returns mutable reference to the clipboard.
    fn clipboard_mut(&mut self) -> &mut dyn Clipboard;

    /// Gets position of the cursor
    /// in global cooridnates.
    fn cursor_position(&self) -> Position;

    /// Sets position of the cursor
    /// in global coordinates.
    ///
    /// There is only one cursor per terminal, but
    /// any widget in the tree can move it, so be careful.
    fn set_cursor_position(&mut self, value: Position);

    /// Returns keyboard focus state of this widget.
    ///
    /// When a widget has keyboard focus
    /// all key event routed to it.
    fn keyboard_focus(&self) -> bool;

    /// Sets the keyboard focus.
    fn set_keyboard_focus(&mut self, value: bool);

    /// Gets value indicating whether this widget
    /// captures mouse or not.
    ///
    /// When widget has mouse focus
    /// all mouse events roted to it.
    fn mouse_focus(&self) -> bool;

    /// Sets the mouse focus.
    fn set_mouse_focus(&mut self, value: bool);
}

pub struct WrappedEventContext<'a, Message, E>
where
    E: EventContext<Message> + ?Sized,
{
    context: &'a mut E,
    phantom_message: PhantomData<Message>,

    mouse_focus: bool,
    keyboard_focus: bool,
}

impl<'a, Message, E> WrappedEventContext<'a, Message, E>
where
    E: EventContext<Message> + ?Sized,
{
    pub fn new(context: &'a mut E, mouse_focus: bool, keyboard_focus: bool) -> Self {
        Self {
            context,
            phantom_message: PhantomData,
            mouse_focus,
            keyboard_focus,
        }
    }
}

impl<'a, Message, E> EventContext<Message> for WrappedEventContext<'a, Message, E>
where
    E: EventContext<Message> + ?Sized,
{
    fn invalidate_render(&mut self) {
        self.context.invalidate_render()
    }

    fn invalidate_layout(&mut self) {
        self.context.invalidate_layout()
    }

    fn emit_message(&mut self, message: Message) {
        self.context.emit_message(message)
    }

    fn cursor_position(&self) -> Position {
        self.context.cursor_position()
    }

    fn set_cursor_position(&mut self, value: Position) {
        self.context.set_cursor_position(value)
    }

    fn keyboard_focus(&self) -> bool {
        self.keyboard_focus
    }

    fn set_keyboard_focus(&mut self, value: bool) {
        self.keyboard_focus = value;
    }

    fn mouse_focus(&self) -> bool {
        self.mouse_focus
    }

    fn set_mouse_focus(&mut self, value: bool) {
        self.mouse_focus = value;
    }

    fn clipboard(&self) -> &dyn Clipboard {
        self.context.clipboard()
    }

    fn clipboard_mut(&mut self) -> &mut dyn Clipboard {
        self.context.clipboard_mut()
    }
}