Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-view/src/graphics.rs')
-rw-r--r--helix-view/src/graphics.rs74
1 files changed, 34 insertions, 40 deletions
diff --git a/helix-view/src/graphics.rs b/helix-view/src/graphics.rs
index 046db86a..a26823b9 100644
--- a/helix-view/src/graphics.rs
+++ b/helix-view/src/graphics.rs
@@ -25,62 +25,52 @@ impl Default for CursorKind {
}
}
-#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Margin {
- pub left: u16,
- pub right: u16,
- pub top: u16,
- pub bottom: u16,
+ pub horizontal: u16,
+ pub vertical: u16,
}
impl Margin {
pub fn none() -> Self {
Self {
- left: 0,
- right: 0,
- top: 0,
- bottom: 0,
+ horizontal: 0,
+ vertical: 0,
}
}
/// Set uniform margin for all sides.
- pub fn all(value: u16) -> Self {
+ pub const fn all(value: u16) -> Self {
Self {
- left: value,
- right: value,
- top: value,
- bottom: value,
+ horizontal: value,
+ vertical: value,
}
}
/// Set the margin of left and right sides to specified value.
- pub fn horizontal(value: u16) -> Self {
+ pub const fn horizontal(value: u16) -> Self {
Self {
- left: value,
- right: value,
- top: 0,
- bottom: 0,
+ horizontal: value,
+ vertical: 0,
}
}
/// Set the margin of top and bottom sides to specified value.
- pub fn vertical(value: u16) -> Self {
+ pub const fn vertical(value: u16) -> Self {
Self {
- left: 0,
- right: 0,
- top: value,
- bottom: value,
+ horizontal: 0,
+ vertical: value,
}
}
/// Get the total width of the margin (left + right)
- pub fn width(&self) -> u16 {
- self.left + self.right
+ pub const fn width(&self) -> u16 {
+ self.horizontal * 2
}
/// Get the total height of the margin (top + bottom)
- pub fn height(&self) -> u16 {
- self.top + self.bottom
+ pub const fn height(&self) -> u16 {
+ self.vertical * 2
}
}
@@ -181,13 +171,13 @@ impl Rect {
Self::new(self.x, self.y, width, self.height)
}
- pub fn inner(self, margin: &Margin) -> Rect {
+ pub fn inner(self, margin: Margin) -> Rect {
if self.width < margin.width() || self.height < margin.height() {
Rect::default()
} else {
Rect {
- x: self.x + margin.left,
- y: self.y + margin.top,
+ x: self.x + margin.horizontal,
+ y: self.y + margin.vertical,
width: self.width - margin.width(),
height: self.height - margin.height(),
}
@@ -459,7 +449,13 @@ pub struct Style {
}
impl Default for Style {
- fn default() -> Style {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl Style {
+ pub const fn new() -> Self {
Style {
fg: None,
bg: None,
@@ -469,12 +465,10 @@ impl Default for Style {
sub_modifier: Modifier::empty(),
}
}
-}
-impl Style {
/// Returns a `Style` resetting all properties.
- pub fn reset() -> Style {
- Style {
+ pub const fn reset() -> Self {
+ Self {
fg: Some(Color::Reset),
bg: Some(Color::Reset),
underline_color: None,
@@ -494,7 +488,7 @@ impl Style {
/// let diff = Style::default().fg(Color::Red);
/// assert_eq!(style.patch(diff), Style::default().fg(Color::Red));
/// ```
- pub fn fg(mut self, color: Color) -> Style {
+ pub const fn fg(mut self, color: Color) -> Style {
self.fg = Some(color);
self
}
@@ -509,7 +503,7 @@ impl Style {
/// let diff = Style::default().bg(Color::Red);
/// assert_eq!(style.patch(diff), Style::default().bg(Color::Red));
/// ```
- pub fn bg(mut self, color: Color) -> Style {
+ pub const fn bg(mut self, color: Color) -> Style {
self.bg = Some(color);
self
}
@@ -524,7 +518,7 @@ impl Style {
/// let diff = Style::default().underline_color(Color::Red);
/// assert_eq!(style.patch(diff), Style::default().underline_color(Color::Red));
/// ```
- pub fn underline_color(mut self, color: Color) -> Style {
+ pub const fn underline_color(mut self, color: Color) -> Style {
self.underline_color = Some(color);
self
}
@@ -539,7 +533,7 @@ impl Style {
/// let diff = Style::default().underline_style(UnderlineStyle::Curl);
/// assert_eq!(style.patch(diff), Style::default().underline_style(UnderlineStyle::Curl));
/// ```
- pub fn underline_style(mut self, style: UnderlineStyle) -> Style {
+ pub const fn underline_style(mut self, style: UnderlineStyle) -> Style {
self.underline_style = Some(style);
self
}