use crate::ecs::components::{buffer::ViewEntity, cursor::ByteIndex};
use bevy::prelude::Message;
pub use super::error::CursorMoveRejectionReason;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CursorCell {
byte_index: ByteIndex,
}
impl CursorCell {
#[must_use]
pub const fn new(byte_index: ByteIndex) -> Self {
Self { byte_index }
}
#[must_use]
pub const fn byte_index(self) -> ByteIndex {
self.byte_index
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct InsertCaret {
byte_index: ByteIndex,
}
impl InsertCaret {
#[must_use]
pub const fn new(byte_index: ByteIndex) -> Self {
Self { byte_index }
}
#[must_use]
pub const fn byte_index(self) -> ByteIndex {
self.byte_index
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CursorMoveDestination {
CursorCell(CursorCell),
InsertCaret(InsertCaret),
}
impl CursorMoveDestination {
#[must_use]
pub const fn cursor_cell(byte_index: ByteIndex) -> Self {
Self::CursorCell(CursorCell::new(byte_index))
}
#[must_use]
pub const fn insert_caret(byte_index: ByteIndex) -> Self {
Self::InsertCaret(InsertCaret::new(byte_index))
}
#[must_use]
pub const fn byte_index(self) -> ByteIndex {
match self {
Self::CursorCell(cell) => cell.byte_index(),
Self::InsertCaret(caret) => caret.byte_index(),
}
}
}
#[derive(Clone, Copy, Debug, Eq, Message, PartialEq)]
pub struct CursorMoveRequested {
pub target: ViewEntity,
pub destination: CursorMoveDestination,
pub desired_column: Option<usize>,
}
impl CursorMoveRequested {
#[must_use]
pub const fn cursor_cell(
target: ViewEntity,
byte_index: ByteIndex,
desired_column: Option<usize>,
) -> Self {
Self {
target,
destination: CursorMoveDestination::cursor_cell(byte_index),
desired_column,
}
}
#[must_use]
pub const fn insert_caret(
target: ViewEntity,
byte_index: ByteIndex,
desired_column: Option<usize>,
) -> Self {
Self {
target,
destination: CursorMoveDestination::insert_caret(byte_index),
desired_column,
}
}
}
#[derive(Clone, Copy, Debug, Eq, Message, PartialEq)]
pub struct CursorMoved {
pub target: ViewEntity,
pub previous_byte_index: ByteIndex,
pub current_byte_index: ByteIndex,
pub desired_column: Option<usize>,
}
#[derive(Clone, Copy, Debug, Eq, Message, PartialEq)]
pub struct CursorMoveRejected {
pub target: ViewEntity,
pub rejected: CursorMoveRequestShape,
pub reason: CursorMoveRejectionReason,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CursorMoveRequestShape {
pub destination: CursorMoveDestination,
pub byte_index: ByteIndex,
pub desired_column: Option<usize>,
}
impl From<&CursorMoveRequested> for CursorMoveRequestShape {
fn from(request: &CursorMoveRequested) -> Self {
Self {
destination: request.destination,
byte_index: request.destination.byte_index(),
desired_column: request.desired_column,
}
}
}