From 5812dea19a1507639833d1150e30392c21f09849 Mon Sep 17 00:00:00 2001 From: alemi Date: Sun, 10 Sep 2023 23:20:07 +0200 Subject: [PATCH] feat: swap cursor start/end if needed when sending --- src/cursor/controller.rs | 6 +++++- src/cursor/mod.rs | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/cursor/controller.rs b/src/cursor/controller.rs index 53f8ffe..443574e 100644 --- a/src/cursor/controller.rs +++ b/src/cursor/controller.rs @@ -50,7 +50,11 @@ impl Controller for CursorController { type Input = CursorPosition; /// enqueue a cursor event to be broadcast to current workspace - fn send(&self, cursor: CursorPosition) -> Result<(), Error> { + /// will automatically invert cursor start/end if they are inverted + fn send(&self, mut cursor: CursorPosition) -> Result<(), Error> { + if cursor.start() < cursor.end() { + std::mem::swap(&mut cursor.start, &mut cursor.end); + } Ok(self.op.send(CursorEvent { user: self.uid.clone(), position: Some(cursor), diff --git a/src/cursor/mod.rs b/src/cursor/mod.rs index b362837..16d0950 100644 --- a/src/cursor/mod.rs +++ b/src/cursor/mod.rs @@ -37,3 +37,13 @@ impl CursorPosition { self.end.clone().unwrap_or((0, 0).into()) } } + +impl PartialOrd for RowCol { + fn partial_cmp(&self, other: &Self) -> Option { + match self.row.partial_cmp(&other.row) { + Some(core::cmp::Ordering::Equal) => {} + ord => return ord, + } + self.col.partial_cmp(&other.col) + } +}