2024-09-04 21:37:35 +02:00
|
|
|
//! ### Cursor Controller
|
|
|
|
//! A [Controller] implementation for [Cursor] actions in a [crate::Workspace]
|
|
|
|
|
2024-08-05 19:13:48 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-09-01 03:08:43 +02:00
|
|
|
use tokio::sync::{mpsc, oneshot, watch};
|
2023-08-16 23:09:47 +02:00
|
|
|
|
2024-09-01 02:46:03 +02:00
|
|
|
use crate::{api::{controller::ControllerCallback, Controller, Cursor}, errors::ControllerResult};
|
2024-09-05 23:59:05 +02:00
|
|
|
use codemp_proto::{cursor::{CursorPosition, RowCol}, files::BufferNode};
|
2024-09-04 21:37:35 +02:00
|
|
|
|
2024-09-05 01:45:48 +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].
|
2024-08-05 19:13:48 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2024-09-13 22:21:53 +02:00
|
|
|
#[cfg_attr(feature = "py", pyo3::pyclass)]
|
2024-08-07 23:06:33 +02:00
|
|
|
#[cfg_attr(feature = "js", napi_derive::napi)]
|
2024-08-08 02:27:06 +02:00
|
|
|
pub struct CursorController(pub(crate) Arc<CursorControllerInner>);
|
2024-08-05 19:13:48 +02:00
|
|
|
|
2023-08-21 02:47:52 +02:00
|
|
|
#[derive(Debug)]
|
2024-08-08 02:27:06 +02:00
|
|
|
pub(crate) struct CursorControllerInner {
|
2024-08-15 19:31:34 +02:00
|
|
|
pub(crate) op: mpsc::Sender<CursorPosition>,
|
2024-09-01 03:08:43 +02:00
|
|
|
pub(crate) stream: mpsc::Sender<oneshot::Sender<Option<Cursor>>>,
|
|
|
|
pub(crate) poll: mpsc::UnboundedSender<oneshot::Sender<()>>,
|
2024-08-15 20:41:53 +02:00
|
|
|
pub(crate) callback: watch::Sender<Option<ControllerCallback<CursorController>>>,
|
2024-08-15 19:31:34 +02:00
|
|
|
pub(crate) stop: mpsc::UnboundedSender<()>,
|
2023-08-17 00:04:37 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 23:26:56 +02:00
|
|
|
#[cfg_attr(feature = "async-trait", async_trait::async_trait)]
|
2024-08-05 19:13:48 +02:00
|
|
|
impl Controller<Cursor> for CursorController {
|
2024-09-01 02:46:03 +02:00
|
|
|
async fn send(&self, mut cursor: Cursor) -> ControllerResult<()> {
|
2024-01-25 03:25:45 +01:00
|
|
|
if cursor.start > cursor.end {
|
2023-09-10 23:20:07 +02:00
|
|
|
std::mem::swap(&mut cursor.start, &mut cursor.end);
|
|
|
|
}
|
2024-09-05 23:59:05 +02:00
|
|
|
Ok(self.0.op.send(CursorPosition {
|
|
|
|
buffer: BufferNode { path: cursor.buffer },
|
|
|
|
start: RowCol { row: cursor.start.0, col: cursor.start.1 },
|
|
|
|
end: RowCol { row: cursor.end.0, col: cursor.end.1 },
|
|
|
|
}).await?)
|
2023-08-16 23:09:47 +02:00
|
|
|
}
|
|
|
|
|
2024-09-01 02:46:03 +02:00
|
|
|
async fn try_recv(&self) -> ControllerResult<Option<Cursor>> {
|
2024-09-01 03:08:43 +02:00
|
|
|
let (tx, rx) = oneshot::channel();
|
|
|
|
self.0.stream.send(tx).await?;
|
|
|
|
Ok(rx.await?)
|
2023-08-16 23:09:47 +02:00
|
|
|
}
|
2023-08-21 02:35:56 +02:00
|
|
|
|
2024-09-01 02:46:03 +02:00
|
|
|
async fn poll(&self) -> ControllerResult<()> {
|
2024-09-01 03:08:43 +02:00
|
|
|
let (tx, rx) = oneshot::channel();
|
|
|
|
self.0.poll.send(tx)?;
|
|
|
|
rx.await?;
|
|
|
|
Ok(())
|
2023-08-21 02:35:56 +02:00
|
|
|
}
|
2024-08-08 00:28:15 +02:00
|
|
|
|
2024-08-15 22:47:49 +02:00
|
|
|
fn callback(&self, cb: impl Into<ControllerCallback<CursorController>>) {
|
|
|
|
if self.0.callback.send(Some(cb.into())).is_err() {
|
2024-08-15 19:32:01 +02:00
|
|
|
// 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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-08 00:28:15 +02:00
|
|
|
fn stop(&self) -> bool {
|
|
|
|
self.0.stop.send(()).is_ok()
|
|
|
|
}
|
2023-08-16 23:09:47 +02:00
|
|
|
}
|