From 7ca632a10d97126623b38aee71368a695bae6716 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Fri, 16 Mar 2018 16:09:47 -0700 Subject: [PATCH] Move Margins to `view` module --- src/vec.rs | 131 ------------------------------------------ src/view/margins.rs | 135 ++++++++++++++++++++++++++++++++++++++++++++ src/view/mod.rs | 2 + src/views/dialog.rs | 4 +- 4 files changed, 139 insertions(+), 133 deletions(-) create mode 100644 src/view/margins.rs diff --git a/src/vec.rs b/src/vec.rs index 5f8716e..c08b539 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -240,137 +240,6 @@ impl Mul for XY { } } -/// Four values representing each direction. -#[derive(Clone, Copy)] -pub struct Margins { - /// Left margin - pub left: usize, - /// Right margin - pub right: usize, - /// Top margin - pub top: usize, - /// Bottom margin - pub bottom: usize, -} - -impl Margins { - /// Creates a new Margins. - pub fn new(left: usize, right: usize, top: usize, bottom: usize) -> Self { - Margins { - left: left, - right: right, - top: top, - bottom: bottom, - } - } - - /// Returns left + right. - pub fn horizontal(&self) -> usize { - self.left + self.right - } - - /// Returns top + bottom. - pub fn vertical(&self) -> usize { - self.top + self.bottom - } - - /// Returns (left+right, top+bottom). - pub fn combined(&self) -> Vec2 { - Vec2::new(self.horizontal(), self.vertical()) - } - - /// Returns (left, top). - pub fn top_left(&self) -> Vec2 { - Vec2::new(self.left, self.top) - } - - /// Returns (right, bottom). - pub fn bot_right(&self) -> Vec2 { - Vec2::new(self.right, self.bottom) - } -} - -impl From<(usize, usize, usize, usize)> for Margins { - fn from((left, right, top, bottom): (usize, usize, usize, usize)) -> Margins { - Margins::new(left, right, top, bottom) - } -} - -impl From<(i32, i32, i32, i32)> for Margins { - fn from((left, right, top, bottom): (i32, i32, i32, i32)) -> Margins { - (left as usize, right as usize, top as usize, bottom as usize).into() - } -} - -impl From<((i32, i32), (i32, i32))> for Margins { - fn from(((left, right), (top, bottom)): ((i32, i32), (i32, i32))) -> Margins { - (left, right, top, bottom).into() - } -} -impl From<((usize, usize), (usize, usize))> for Margins { - fn from( - ((left, right), (top, bottom)): ((usize, usize), (usize, usize)) - ) -> Margins { - (left, right, top, bottom).into() - } -} - -impl> Add for Margins { - type Output = Margins; - - fn add(self, other: T) -> Margins { - let ov = other.into(); - - Margins { - left: self.left + ov.left, - right: self.right + ov.right, - top: self.top + ov.top, - bottom: self.bottom + ov.bottom, - } - } -} - -impl> Sub for Margins { - type Output = Margins; - - fn sub(self, other: T) -> Margins { - let ov = other.into(); - - Margins { - left: self.left - ov.left, - right: self.right - ov.right, - top: self.top - ov.top, - bottom: self.bottom - ov.bottom, - } - } -} - -impl Div for Margins { - type Output = Margins; - - fn div(self, other: usize) -> Margins { - Margins { - left: self.left / other, - right: self.right / other, - top: self.top / other, - bottom: self.bottom / other, - } - } -} - -impl Mul for Margins { - type Output = Margins; - - fn mul(self, other: usize) -> Margins { - Margins { - left: self.left * other, - right: self.right * other, - top: self.top * other, - bottom: self.bottom * other, - } - } -} - #[cfg(test)] mod tests { use super::Vec2; diff --git a/src/view/margins.rs b/src/view/margins.rs new file mode 100644 index 0000000..4772b7e --- /dev/null +++ b/src/view/margins.rs @@ -0,0 +1,135 @@ +use vec::Vec2; +use std::ops::{Add, Div, Mul, Sub}; + +/// Four values representing each direction. +#[derive(Clone, Copy)] +pub struct Margins { + /// Left margin + pub left: usize, + /// Right margin + pub right: usize, + /// Top margin + pub top: usize, + /// Bottom margin + pub bottom: usize, +} + +impl Margins { + /// Creates a new Margins. + pub fn new(left: usize, right: usize, top: usize, bottom: usize) -> Self { + Margins { + left: left, + right: right, + top: top, + bottom: bottom, + } + } + + /// Returns left + right. + pub fn horizontal(&self) -> usize { + self.left + self.right + } + + /// Returns top + bottom. + pub fn vertical(&self) -> usize { + self.top + self.bottom + } + + /// Returns (left+right, top+bottom). + pub fn combined(&self) -> Vec2 { + Vec2::new(self.horizontal(), self.vertical()) + } + + /// Returns (left, top). + pub fn top_left(&self) -> Vec2 { + Vec2::new(self.left, self.top) + } + + /// Returns (right, bottom). + pub fn bot_right(&self) -> Vec2 { + Vec2::new(self.right, self.bottom) + } +} + +impl From<(usize, usize, usize, usize)> for Margins { + fn from((left, right, top, bottom): (usize, usize, usize, usize)) -> Margins { + Margins::new(left, right, top, bottom) + } +} + +impl From<(i32, i32, i32, i32)> for Margins { + fn from((left, right, top, bottom): (i32, i32, i32, i32)) -> Margins { + (left as usize, right as usize, top as usize, bottom as usize).into() + } +} + +impl From<((i32, i32), (i32, i32))> for Margins { + fn from(((left, right), (top, bottom)): ((i32, i32), (i32, i32))) -> Margins { + (left, right, top, bottom).into() + } +} +impl From<((usize, usize), (usize, usize))> for Margins { + fn from( + ((left, right), (top, bottom)): ((usize, usize), (usize, usize)) + ) -> Margins { + (left, right, top, bottom).into() + } +} + +impl> Add for Margins { + type Output = Margins; + + fn add(self, other: T) -> Margins { + let ov = other.into(); + + Margins { + left: self.left + ov.left, + right: self.right + ov.right, + top: self.top + ov.top, + bottom: self.bottom + ov.bottom, + } + } +} + +impl> Sub for Margins { + type Output = Margins; + + fn sub(self, other: T) -> Margins { + let ov = other.into(); + + Margins { + left: self.left - ov.left, + right: self.right - ov.right, + top: self.top - ov.top, + bottom: self.bottom - ov.bottom, + } + } +} + +impl Div for Margins { + type Output = Margins; + + fn div(self, other: usize) -> Margins { + Margins { + left: self.left / other, + right: self.right / other, + top: self.top / other, + bottom: self.bottom / other, + } + } +} + +impl Mul for Margins { + type Output = Margins; + + fn mul(self, other: usize) -> Margins { + Margins { + left: self.left * other, + right: self.right * other, + top: self.top * other, + bottom: self.bottom * other, + } + } +} + + diff --git a/src/view/mod.rs b/src/view/mod.rs index 16f8bf1..d04f1e0 100644 --- a/src/view/mod.rs +++ b/src/view/mod.rs @@ -42,6 +42,7 @@ mod view_wrapper; mod any; mod finder; mod position; +mod margins; mod size_cache; mod size_constraint; mod view_path; @@ -59,6 +60,7 @@ pub use self::boxable::Boxable; pub use self::finder::{Finder, Selector}; pub use self::identifiable::Identifiable; pub use self::into_boxed_view::IntoBoxedView; +pub use self::margins::Margins; pub use self::position::{Offset, Position}; pub use self::scroll::{ScrollBase, ScrollStrategy}; pub use self::size_cache::SizeCache; diff --git a/src/views/dialog.rs b/src/views/dialog.rs index 063982b..7222678 100644 --- a/src/views/dialog.rs +++ b/src/views/dialog.rs @@ -9,8 +9,8 @@ use std::cell::Cell; use std::cmp::max; use theme::ColorStyle; use unicode_width::UnicodeWidthStr; -use vec::{Vec2, Margins}; -use view::{Selector, View}; +use vec::Vec2; +use view::{Margins, Selector, View}; use views::{Button, DummyView, SizedView, TextView, ViewBox}; /// Identifies currently focused element in [`Dialog`].