From eb614fc4a4351df65d65400b0431e8e37f270bce Mon Sep 17 00:00:00 2001 From: Stephen Broadley Date: Sun, 4 Aug 2024 20:46:21 +0100 Subject: [PATCH] add 'split_***' utility API to Rect --- helix-view/src/graphics.rs | 68 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/helix-view/src/graphics.rs b/helix-view/src/graphics.rs index a04394a0a..c5b6f9baf 100644 --- a/helix-view/src/graphics.rs +++ b/helix-view/src/graphics.rs @@ -254,6 +254,74 @@ impl Rect { && self.y < other.y + other.height && self.y + self.height > other.y } + + pub fn split_left(self, width: u16) -> (Rect, Rect) { + ( + Rect { width, ..self }, + Rect { + x: self.x + width, + width: self.width - width, + ..self + }, + ) + } + + pub fn split_right(self, width: u16) -> (Rect, Rect) { + ( + Rect { + width: self.width - width, + ..self + }, + Rect { + x: self.right() - width, + width, + ..self + }, + ) + } + + pub fn split_top(self, height: u16) -> (Rect, Rect) { + ( + Rect { height, ..self }, + Rect { + y: self.y + height, + height: self.height - height, + ..self + }, + ) + } + + pub fn split_bottom(self, height: u16) -> (Rect, Rect) { + ( + Rect { + height: self.height - height, + ..self + }, + Rect { + y: self.bottom() - height, + height, + ..self + }, + ) + } + + pub fn split_centre_vertical(self, width: u16) -> (Rect, Rect, Rect) { + let l = (self.width - width) / 2; + let r = self.width - width - l; + ( + Rect { width: l, ..self }, + Rect { + width, + x: self.x + l, + ..self + }, + Rect { + width: r, + x: self.right() - r, + ..self + }, + ) + } } #[derive(Debug, Clone, Copy, PartialEq, Eq)]