aboutsummaryrefslogtreecommitdiff
path: root/helix-tui/src/layout.rs
diff options
context:
space:
mode:
authorKeith Simmons2021-06-25 03:58:15 +0000
committerGitHub2021-06-25 03:58:15 +0000
commit4418e17547562e211952b9e8585916e04b858b3b (patch)
tree8427958809392969b035967c1c69e9507207d00e /helix-tui/src/layout.rs
parent74cc4b4a49e05462b2b543737ad4dc32e2265794 (diff)
reverse the dependency between helix-tui and helix-view (#366)
* reverse the dependency between helix-tui and helix-view by moving a fiew types to view * fix tests * clippy and format fixes Co-authored-by: Keith Simmons <keithsim@microsoft.com>
Diffstat (limited to 'helix-tui/src/layout.rs')
-rw-r--r--helix-tui/src/layout.rs167
1 files changed, 4 insertions, 163 deletions
diff --git a/helix-tui/src/layout.rs b/helix-tui/src/layout.rs
index 5248f7bf..e6a84aa0 100644
--- a/helix-tui/src/layout.rs
+++ b/helix-tui/src/layout.rs
@@ -1,11 +1,12 @@
use std::cell::RefCell;
-use std::cmp::{max, min};
use std::collections::HashMap;
use cassowary::strength::{REQUIRED, WEAK};
use cassowary::WeightedRelation::*;
use cassowary::{Constraint as CassowaryConstraint, Expression, Solver, Variable};
+use helix_view::graphics::{Margin, Rect};
+
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
pub enum Corner {
TopLeft,
@@ -45,12 +46,6 @@ impl Constraint {
}
}
-#[derive(Debug, Clone, PartialEq, Eq, Hash)]
-pub struct Margin {
- pub vertical: u16,
- pub horizontal: u16,
-}
-
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Alignment {
Left,
@@ -119,7 +114,8 @@ impl Layout {
///
/// # Examples
/// ```
- /// # use helix_tui::layout::{Rect, Constraint, Direction, Layout};
+ /// # use helix_tui::layout::{Constraint, Direction, Layout};
+ /// # use helix_view::graphics::Rect;
/// let chunks = Layout::default()
/// .direction(Direction::Vertical)
/// .constraints([Constraint::Length(5), Constraint::Min(0)].as_ref())
@@ -348,117 +344,6 @@ impl Element {
}
}
-/// A simple rectangle used in the computation of the layout and to give widgets an hint about the
-/// area they are supposed to render to.
-#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
-pub struct Rect {
- pub x: u16,
- pub y: u16,
- pub width: u16,
- pub height: u16,
-}
-
-impl Default for Rect {
- fn default() -> Rect {
- Rect {
- x: 0,
- y: 0,
- width: 0,
- height: 0,
- }
- }
-}
-
-impl Rect {
- /// Creates a new rect, with width and height limited to keep the area under max u16.
- /// If clipped, aspect ratio will be preserved.
- pub fn new(x: u16, y: u16, width: u16, height: u16) -> Rect {
- let max_area = u16::max_value();
- let (clipped_width, clipped_height) =
- if u32::from(width) * u32::from(height) > u32::from(max_area) {
- let aspect_ratio = f64::from(width) / f64::from(height);
- let max_area_f = f64::from(max_area);
- let height_f = (max_area_f / aspect_ratio).sqrt();
- let width_f = height_f * aspect_ratio;
- (width_f as u16, height_f as u16)
- } else {
- (width, height)
- };
- Rect {
- x,
- y,
- width: clipped_width,
- height: clipped_height,
- }
- }
-
- pub fn area(self) -> u16 {
- self.width * self.height
- }
-
- pub fn left(self) -> u16 {
- self.x
- }
-
- pub fn right(self) -> u16 {
- self.x.saturating_add(self.width)
- }
-
- pub fn top(self) -> u16 {
- self.y
- }
-
- pub fn bottom(self) -> u16 {
- self.y.saturating_add(self.height)
- }
-
- pub fn inner(self, margin: &Margin) -> Rect {
- if self.width < 2 * margin.horizontal || self.height < 2 * margin.vertical {
- Rect::default()
- } else {
- Rect {
- x: self.x + margin.horizontal,
- y: self.y + margin.vertical,
- width: self.width - 2 * margin.horizontal,
- height: self.height - 2 * margin.vertical,
- }
- }
- }
-
- pub fn union(self, other: Rect) -> Rect {
- let x1 = min(self.x, other.x);
- let y1 = min(self.y, other.y);
- let x2 = max(self.x + self.width, other.x + other.width);
- let y2 = max(self.y + self.height, other.y + other.height);
- Rect {
- x: x1,
- y: y1,
- width: x2 - x1,
- height: y2 - y1,
- }
- }
-
- pub fn intersection(self, other: Rect) -> Rect {
- let x1 = max(self.x, other.x);
- let y1 = max(self.y, other.y);
- let x2 = min(self.x + self.width, other.x + other.width);
- let y2 = min(self.y + self.height, other.y + other.height);
- Rect {
- x: x1,
- y: y1,
- width: x2 - x1,
- height: y2 - y1,
- }
- }
-
- pub fn intersects(self, other: Rect) -> bool {
- self.x < other.x + other.width
- && self.x + self.width > other.x
- && self.y < other.y + other.height
- && self.y + self.height > other.y
- }
-}
-
#[cfg(test)]
mod tests {
use super::*;
@@ -487,48 +372,4 @@ mod tests {
assert_eq!(target.height, chunks.iter().map(|r| r.height).sum::<u16>());
chunks.windows(2).for_each(|w| assert!(w[0].y <= w[1].y));
}
-
- #[test]
- fn test_rect_size_truncation() {
- for width in 256u16..300u16 {
- for height in 256u16..300u16 {
- let rect = Rect::new(0, 0, width, height);
- rect.area(); // Should not panic.
- assert!(rect.width < width || rect.height < height);
- // The target dimensions are rounded down so the math will not be too precise
- // but let's make sure the ratios don't diverge crazily.
- assert!(
- (f64::from(rect.width) / f64::from(rect.height)
- - f64::from(width) / f64::from(height))
- .abs()
- < 1.0
- )
- }
- }
-
- // One dimension below 255, one above. Area above max u16.
- let width = 900;
- let height = 100;
- let rect = Rect::new(0, 0, width, height);
- assert_ne!(rect.width, 900);
- assert_ne!(rect.height, 100);
- assert!(rect.width < width || rect.height < height);
- }
-
- #[test]
- fn test_rect_size_preservation() {
- for width in 0..256u16 {
- for height in 0..256u16 {
- let rect = Rect::new(0, 0, width, height);
- rect.area(); // Should not panic.
- assert_eq!(rect.width, width);
- assert_eq!(rect.height, height);
- }
- }
-
- // One dimension below 255, one above. Area below max u16.
- let rect = Rect::new(0, 0, 300, 100);
- assert_eq!(rect.width, 300);
- assert_eq!(rect.height, 100);
- }
}