Move Margins to view module

This commit is contained in:
Alexandre Bury 2018-03-16 16:09:47 -07:00
parent 92c1d1a5b2
commit 7ca632a10d
4 changed files with 139 additions and 133 deletions

View File

@ -240,137 +240,6 @@ impl Mul<usize> for XY<usize> {
} }
} }
/// 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<T: Into<Margins>> Add<T> 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<T: Into<Margins>> Sub<T> 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<usize> 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<usize> 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)] #[cfg(test)]
mod tests { mod tests {
use super::Vec2; use super::Vec2;

135
src/view/margins.rs Normal file
View File

@ -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<T: Into<Margins>> Add<T> 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<T: Into<Margins>> Sub<T> 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<usize> 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<usize> 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,
}
}
}

View File

@ -42,6 +42,7 @@ mod view_wrapper;
mod any; mod any;
mod finder; mod finder;
mod position; mod position;
mod margins;
mod size_cache; mod size_cache;
mod size_constraint; mod size_constraint;
mod view_path; mod view_path;
@ -59,6 +60,7 @@ pub use self::boxable::Boxable;
pub use self::finder::{Finder, Selector}; pub use self::finder::{Finder, Selector};
pub use self::identifiable::Identifiable; pub use self::identifiable::Identifiable;
pub use self::into_boxed_view::IntoBoxedView; pub use self::into_boxed_view::IntoBoxedView;
pub use self::margins::Margins;
pub use self::position::{Offset, Position}; pub use self::position::{Offset, Position};
pub use self::scroll::{ScrollBase, ScrollStrategy}; pub use self::scroll::{ScrollBase, ScrollStrategy};
pub use self::size_cache::SizeCache; pub use self::size_cache::SizeCache;

View File

@ -9,8 +9,8 @@ use std::cell::Cell;
use std::cmp::max; use std::cmp::max;
use theme::ColorStyle; use theme::ColorStyle;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
use vec::{Vec2, Margins}; use vec::Vec2;
use view::{Selector, View}; use view::{Margins, Selector, View};
use views::{Button, DummyView, SizedView, TextView, ViewBox}; use views::{Button, DummyView, SizedView, TextView, ViewBox};
/// Identifies currently focused element in [`Dialog`]. /// Identifies currently focused element in [`Dialog`].