diff options
author | wongjiahau | 2024-05-01 20:42:31 +0000 |
---|---|---|
committer | JJ | 2024-05-01 23:06:08 +0000 |
commit | 3bff36ab90aba7de8bb5bff7dbb8230d81cdf582 (patch) | |
tree | 733bdb4ff30a35c2212910d48a635ddf4c23b2e3 /helix-view/src/graphics.rs | |
parent | 2cadec0b1182332338a5a1cc3062776f834d8835 (diff) |
Add file explorer and tree helper
ref: https://github.com/helix-editor/helix/issues/200
ref: https://github.com/helix-editor/helix/pull/2377
ref: https://github.com/helix-editor/helix/pull/5566
ref: https://github.com/helix-editor/helix/pull/5768
Co-authored-by: cossonleo <cossonleo@foxmail.com>
Co-authored-by: JJ <git@toki.la>
Co-authored-by: Quan Tong <quantonganh@gmail.com>
Diffstat (limited to 'helix-view/src/graphics.rs')
-rw-r--r-- | helix-view/src/graphics.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/helix-view/src/graphics.rs b/helix-view/src/graphics.rs index 046db86a..fbfde635 100644 --- a/helix-view/src/graphics.rs +++ b/helix-view/src/graphics.rs @@ -248,6 +248,34 @@ impl Rect { && self.y < other.y + other.height && self.y + self.height > other.y } + + /// Returns a smaller `Rect` with a margin of 5% on each side, and an additional 2 rows at the bottom + pub fn overlaid(self) -> Rect { + self.clip_bottom(2).clip_relative(90, 90) + } + + /// Returns a smaller `Rect` with width and height clipped to the given `percent_horizontal` + /// and `percent_vertical`. + /// + /// Value of `percent_horizontal` and `percent_vertical` is from 0 to 100. + pub fn clip_relative(self, percent_horizontal: u8, percent_vertical: u8) -> Rect { + fn mul_and_cast(size: u16, factor: u8) -> u16 { + ((size as u32) * (factor as u32) / 100).try_into().unwrap() + } + + let inner_w = mul_and_cast(self.width, percent_horizontal); + let inner_h = mul_and_cast(self.height, percent_vertical); + + let offset_x = self.width.saturating_sub(inner_w) / 2; + let offset_y = self.height.saturating_sub(inner_h) / 2; + + Rect { + x: self.x + offset_x, + y: self.y + offset_y, + width: inner_w, + height: inner_h, + } + } } #[derive(Debug, Clone, Copy, PartialEq, Eq)] |