codemp/src/cursor/controller.rs

93 lines
2.7 KiB
Rust
Raw Normal View History

2024-09-04 21:37:35 +02:00
//! ### Cursor Controller
2024-09-18 16:11:12 +02:00
//! A [Controller] implementation for [crate::api::Cursor] actions in a [crate::Workspace]
2024-09-04 21:37:35 +02:00
use std::sync::Arc;
use tokio::sync::{mpsc, oneshot, watch};
use crate::{
2024-10-03 04:05:58 +02:00
api::{
2024-10-10 12:14:44 +02:00
controller::{AsyncReceiver, AsyncSender, ControllerCallback},
2024-10-10 12:46:56 +02:00
Controller, Cursor, Selection,
2024-10-03 04:05:58 +02:00
},
errors::ControllerResult,
};
use codemp_proto::{
cursor::{CursorPosition, RowCol},
files::BufferNode,
};
2024-09-04 21:37:35 +02:00
/// A [Controller] for asynchronously sending and receiving [Cursor] event.
2023-08-20 00:46:55 +02:00
///
2024-09-04 21:37:35 +02:00
/// An unique [CursorController] exists for each active [crate::Workspace].
#[derive(Debug, Clone)]
#[cfg_attr(any(feature = "py", feature = "py-noabi"), pyo3::pyclass)]
#[cfg_attr(feature = "js", napi_derive::napi)]
pub struct CursorController(pub(crate) Arc<CursorControllerInner>);
2023-08-21 02:47:52 +02:00
#[derive(Debug)]
pub(crate) struct CursorControllerInner {
pub(crate) op: mpsc::UnboundedSender<CursorPosition>,
pub(crate) stream: mpsc::Sender<oneshot::Sender<Option<Cursor>>>,
pub(crate) poll: mpsc::UnboundedSender<oneshot::Sender<()>>,
pub(crate) callback: watch::Sender<Option<ControllerCallback<CursorController>>>,
}
#[cfg_attr(feature = "async-trait", async_trait::async_trait)]
impl Controller<Selection, Cursor> for CursorController {}
#[cfg_attr(feature = "async-trait", async_trait::async_trait)]
impl AsyncSender<Selection> for CursorController {
fn send(&self, mut cursor: Selection) -> ControllerResult<()> {
2024-10-10 12:46:56 +02:00
if cursor.start_row > cursor.end_row
|| (cursor.start_row == cursor.end_row && cursor.start_col > cursor.end_col)
{
std::mem::swap(&mut cursor.start_row, &mut cursor.end_row);
std::mem::swap(&mut cursor.start_col, &mut cursor.end_col);
}
2024-10-03 03:17:30 +02:00
Ok(self.0.op.send(CursorPosition {
buffer: BufferNode {
path: cursor.buffer,
},
start: RowCol {
row: cursor.start_row,
col: cursor.start_col,
2024-10-03 03:17:30 +02:00
},
end: RowCol {
row: cursor.end_row,
col: cursor.end_col,
2024-10-03 03:17:30 +02:00
},
})?)
}
}
#[cfg_attr(feature = "async-trait", async_trait::async_trait)]
impl AsyncReceiver<Cursor> for CursorController {
async fn try_recv(&self) -> ControllerResult<Option<Cursor>> {
let (tx, rx) = oneshot::channel();
self.0.stream.send(tx).await?;
Ok(rx.await?)
}
async fn poll(&self) -> ControllerResult<()> {
let (tx, rx) = oneshot::channel();
self.0.poll.send(tx)?;
rx.await?;
Ok(())
}
fn callback(&self, cb: impl Into<ControllerCallback<CursorController>>) {
if self.0.callback.send(Some(cb.into())).is_err() {
// TODO should we panic? we failed what we were supposed to do
tracing::error!("no active cursor worker to run registered callback!");
}
}
fn clear_callback(&self) {
if self.0.callback.send(None).is_err() {
tracing::warn!("no active cursor worker to clear callback");
}
}
}