diff --git a/src/client.rs b/src/client.rs index 80d49b8..6965a9e 100644 --- a/src/client.rs +++ b/src/client.rs @@ -88,8 +88,22 @@ impl Client { self.entered = false; } - pub fn move_by_delta( + pub fn move_self(&mut self, _point: isize) -> Option { + self.move_by_delta(true, _point, 0, None) + } + + pub fn move_other( + &mut self, + _point: isize, + _delta: isize, + _filename: Option, + ) -> Option { + self.move_by_delta(false, _point, _delta, _filename) + } + + fn move_by_delta( &mut self, + _sender_p: bool, _point: isize, _delta: isize, _filename: Option, @@ -119,7 +133,7 @@ impl Client { } } - user.move_by_delta(_point, _delta); + user.move_by_delta(_sender_p, _point, _delta); Some(filename) } diff --git a/src/handler/buffer.rs b/src/handler/buffer.rs index 82ff259..533aff7 100644 --- a/src/handler/buffer.rs +++ b/src/handler/buffer.rs @@ -25,7 +25,7 @@ pub mod update { fn predict_movement(addr: &SocketAddr, room: &mut Room, point: isize, delta: isize) { let client = room.get_client_mut(addr).unwrap(); - let filename = client.move_by_delta(point, delta, None); + let filename = client.move_self(point); let clients = room.get_clients_mut(); @@ -34,7 +34,7 @@ pub mod update { continue; } - _client.move_by_delta(point, delta, filename.clone()); + _client.move_other(point, delta, filename.clone()); } } @@ -50,18 +50,20 @@ pub mod update { // Predict movement. { - let delta = if add_or_delete == "delete" { - beg - end - } else { - end - beg - }; + let is_delete = add_or_delete == "delete"; + + // Predict the movement shift. + let delta = if is_delete { beg - end } else { end - beg }; + + // Predict the cursor final position. + let start = if is_delete { beg } else { end }; // Nothing has changed; return it. if delta == 0 { return; } - predict_movement(&addr, &mut room, beg, delta); + predict_movement(&addr, &mut room, start, delta); } // Update the buffer view. diff --git a/src/user.rs b/src/user.rs index 84f888a..cc65b58 100644 --- a/src/user.rs +++ b/src/user.rs @@ -61,9 +61,10 @@ impl User { /// /// # Arguments /// + /// * `_sender` - Boolean return true if the user is the sender. /// * `_point` - Only effect point after the origin. /// * `_delta` - Movement delta. - pub fn move_by_delta(&mut self, _point: isize, _delta: isize) { + pub fn move_by_delta(&mut self, _sender: bool, _point: isize, _delta: isize) { // Point must exists. if self.point.is_none() { return; @@ -73,7 +74,7 @@ impl User { // Shift the point. if _point <= point { - self.point = Some(point + _delta); + self.point = Some(if _sender { point } else { point + _delta }); // `region_beg`. and `region_end` must exists at the same time. if !self.region_beg.is_none() { @@ -81,8 +82,10 @@ impl User { let region_end = self.region_end.unwrap(); // Shift the region. - self.region_beg = Some(region_beg + _delta); - self.region_end = Some(region_end + _delta); + // + // If the sender, just set to the point. + self.region_beg = Some(if _sender { point } else { region_beg + _delta }); + self.region_end = Some(if _sender { point } else { region_end + _delta }); } } }