| 1 | use super::ParticipantId; |
| 2 | use serde::{Deserialize, Serialize}; |
| 3 | |
| 4 | #[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)] |
| 5 | pub enum GridType { |
| 6 | Prompt, |
| 7 | /// Right side prompt |
| 8 | Rprompt, |
| 9 | Output, |
| 10 | /// Combined prompt/command grid, used for same-line prompt |
| 11 | PromptAndCommand, |
| 12 | } |
| 13 | |
| 14 | /// A point in a grid. |
| 15 | #[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)] |
| 16 | pub struct Point { |
| 17 | pub row: usize, |
| 18 | pub col: usize, |
| 19 | } |
| 20 | |
| 21 | /// A point in a grid within a block. |
| 22 | #[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)] |
| 23 | pub struct BlockPoint { |
| 24 | pub block_id: BlockId, |
| 25 | pub grid_type: GridType, |
| 26 | pub point: Point, |
| 27 | } |
| 28 | |
| 29 | /// An ID for a block that is unique only within a single shared session. |
| 30 | #[derive(Clone, Debug, Default, Hash, PartialEq, Eq, Serialize, Deserialize)] |
| 31 | pub struct BlockId(String); |
| 32 | |
| 33 | impl std::fmt::Display for BlockId { |
| 34 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 35 | write!(f, "{}", self.0) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl From<String> for BlockId { |
| 40 | fn from(value: String) -> Self { |
| 41 | Self(value) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// What a shared session participant has selected. |
| 46 | #[derive(Clone, Debug, Default, Deserialize, PartialEq, Eq, Serialize)] |
| 47 | pub enum Selection { |
| 48 | #[default] |
| 49 | None, |
| 50 | Blocks { |
| 51 | block_ids: Vec<BlockId>, |
| 52 | }, |
| 53 | /// Start is always before end. |
| 54 | BlockText { |
| 55 | start: BlockPoint, |
| 56 | end: BlockPoint, |
| 57 | /// If true, the user selected from the end point to the start point (useful for knowing where the cursor should be) |
| 58 | is_reversed: bool, |
| 59 | }, |
| 60 | /// Start is always before end |
| 61 | AltScreenText { |
| 62 | start: Point, |
| 63 | end: Point, |
| 64 | /// If true, the user selected from the end point to the start point (useful for knowing where the cursor should be) |
| 65 | is_reversed: bool, |
| 66 | }, |
| 67 | } |
| 68 | |
| 69 | #[derive( |
| 70 | Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize, |
| 71 | )] |
| 72 | pub struct SelectionEventNo(usize); |
| 73 | |
| 74 | impl From<usize> for SelectionEventNo { |
| 75 | fn from(value: usize) -> Self { |
| 76 | Self(value) |
| 77 | } |
| 78 | } |
| 79 | #[derive(Clone, Debug, Deserialize, Serialize)] |
| 80 | pub struct SelectionUpdate { |
| 81 | pub selection: Selection, |
| 82 | pub event_no: SelectionEventNo, |
| 83 | } |
| 84 | |
| 85 | #[derive(Clone, Debug, Deserialize, Serialize)] |
| 86 | pub enum PresenceUpdate { |
| 87 | Selection(Selection), |
| 88 | // other stuff in the future, like scroll state |
| 89 | } |
| 90 | |
| 91 | #[derive(Clone, Debug, Deserialize, Serialize)] |
| 92 | pub struct ParticipantPresenceUpdate { |
| 93 | pub participant_id: ParticipantId, |
| 94 | pub update: PresenceUpdate, |
| 95 | } |
| 96 | |
| 97 | /// One participant's current selection. |
| 98 | #[derive(Clone, Debug, Default, Deserialize, Serialize)] |
| 99 | pub struct ParticipantSelection { |
| 100 | pub id: ParticipantId, |
| 101 | pub selection: Selection, |
| 102 | } |