diff options
| author | 2023-02-17 11:45:34 -0800 | |
|---|---|---|
| committer | 2023-02-17 11:45:34 -0800 | |
| commit | 744cef5608a91fe55cbbe1adb73a9a0b5e266668 (patch) | |
| tree | f88ca6ae3c481e2de74178bb3f0d1f1b685e1740 /core | |
| parent | 8da098330b58542cc929f4f24d02e26bd654bae4 (diff) | |
| parent | 7dc1fb488ddbd12519571b51d75ae0c28875911d (diff) | |
| download | iced-744cef5608a91fe55cbbe1adb73a9a0b5e266668.tar.gz iced-744cef5608a91fe55cbbe1adb73a9a0b5e266668.tar.bz2 iced-744cef5608a91fe55cbbe1adb73a9a0b5e266668.zip | |
Merge remote-tracking branch 'origin/master' into feat/multi-window-support
# Conflicts:
#	winit/src/window.rs
Diffstat (limited to '')
| -rw-r--r-- | core/src/length.rs | 14 | ||||
| -rw-r--r-- | core/src/lib.rs | 2 | ||||
| -rw-r--r-- | core/src/padding.rs | 73 | ||||
| -rw-r--r-- | core/src/pixels.rs | 22 | ||||
| -rw-r--r-- | core/src/size.rs | 4 | 
5 files changed, 89 insertions, 26 deletions
| diff --git a/core/src/length.rs b/core/src/length.rs index 95ea6e0e..bb925c4b 100644 --- a/core/src/length.rs +++ b/core/src/length.rs @@ -1,5 +1,5 @@  /// The strategy used to fill space in a specific dimension. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq)]  pub enum Length {      /// Fill all the remaining space      Fill, @@ -17,7 +17,7 @@ pub enum Length {      Shrink,      /// Fill a fixed amount of space -    Units(u16), +    Fixed(f32),  }  impl Length { @@ -31,13 +31,19 @@ impl Length {              Length::Fill => 1,              Length::FillPortion(factor) => *factor,              Length::Shrink => 0, -            Length::Units(_) => 0, +            Length::Fixed(_) => 0,          }      }  } +impl From<f32> for Length { +    fn from(amount: f32) -> Self { +        Length::Fixed(amount) +    } +} +  impl From<u16> for Length {      fn from(units: u16) -> Self { -        Length::Units(units) +        Length::Fixed(f32::from(units))      }  } diff --git a/core/src/lib.rs b/core/src/lib.rs index 3aa5defe..31280a05 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -35,6 +35,7 @@ mod content_fit;  mod font;  mod length;  mod padding; +mod pixels;  mod point;  mod rectangle;  mod size; @@ -47,6 +48,7 @@ pub use content_fit::ContentFit;  pub use font::Font;  pub use length::Length;  pub use padding::Padding; +pub use pixels::Pixels;  pub use point::Point;  pub use rectangle::Rectangle;  pub use size::Size; diff --git a/core/src/padding.rs b/core/src/padding.rs index 140ad8ee..752b2b86 100644 --- a/core/src/padding.rs +++ b/core/src/padding.rs @@ -33,29 +33,29 @@ use crate::Size;  /// let widget = Widget::new().padding([10, 20]);        // top/bottom, left/right  /// let widget = Widget::new().padding([5, 10, 15, 20]); // top, right, bottom, left  /// ``` -#[derive(Debug, Hash, Copy, Clone)] +#[derive(Debug, Copy, Clone)]  pub struct Padding {      /// Top padding -    pub top: u16, +    pub top: f32,      /// Right padding -    pub right: u16, +    pub right: f32,      /// Bottom padding -    pub bottom: u16, +    pub bottom: f32,      /// Left padding -    pub left: u16, +    pub left: f32,  }  impl Padding {      /// Padding of zero      pub const ZERO: Padding = Padding { -        top: 0, -        right: 0, -        bottom: 0, -        left: 0, +        top: 0.0, +        right: 0.0, +        bottom: 0.0, +        left: 0.0,      };      /// Create a Padding that is equal on all sides -    pub const fn new(padding: u16) -> Padding { +    pub const fn new(padding: f32) -> Padding {          Padding {              top: padding,              right: padding, @@ -65,12 +65,12 @@ impl Padding {      }      /// Returns the total amount of vertical [`Padding`]. -    pub fn vertical(self) -> u16 { +    pub fn vertical(self) -> f32 {          self.top + self.bottom      }      /// Returns the total amount of horizontal [`Padding`]. -    pub fn horizontal(self) -> u16 { +    pub fn horizontal(self) -> f32 {          self.left + self.right      } @@ -79,10 +79,10 @@ impl Padding {          let available = (outer - inner).max(Size::ZERO);          Padding { -            top: self.top.min((available.height as u16) / 2), -            right: self.right.min((available.width as u16) / 2), -            bottom: self.bottom.min((available.height as u16) / 2), -            left: self.left.min((available.width as u16) / 2), +            top: self.top.min(available.height / 2.0), +            right: self.right.min(available.width / 2.0), +            bottom: self.bottom.min(available.height / 2.0), +            left: self.left.min(available.width / 2.0),          }      }  } @@ -90,6 +90,39 @@ impl Padding {  impl From<u16> for Padding {      fn from(p: u16) -> Self {          Padding { +            top: f32::from(p), +            right: f32::from(p), +            bottom: f32::from(p), +            left: f32::from(p), +        } +    } +} + +impl From<[u16; 2]> for Padding { +    fn from(p: [u16; 2]) -> Self { +        Padding { +            top: f32::from(p[0]), +            right: f32::from(p[1]), +            bottom: f32::from(p[0]), +            left: f32::from(p[1]), +        } +    } +} + +impl From<[u16; 4]> for Padding { +    fn from(p: [u16; 4]) -> Self { +        Padding { +            top: f32::from(p[0]), +            right: f32::from(p[1]), +            bottom: f32::from(p[2]), +            left: f32::from(p[3]), +        } +    } +} + +impl From<f32> for Padding { +    fn from(p: f32) -> Self { +        Padding {              top: p,              right: p,              bottom: p, @@ -98,8 +131,8 @@ impl From<u16> for Padding {      }  } -impl From<[u16; 2]> for Padding { -    fn from(p: [u16; 2]) -> Self { +impl From<[f32; 2]> for Padding { +    fn from(p: [f32; 2]) -> Self {          Padding {              top: p[0],              right: p[1], @@ -109,8 +142,8 @@ impl From<[u16; 2]> for Padding {      }  } -impl From<[u16; 4]> for Padding { -    fn from(p: [u16; 4]) -> Self { +impl From<[f32; 4]> for Padding { +    fn from(p: [f32; 4]) -> Self {          Padding {              top: p[0],              right: p[1], diff --git a/core/src/pixels.rs b/core/src/pixels.rs new file mode 100644 index 00000000..e42cd9f9 --- /dev/null +++ b/core/src/pixels.rs @@ -0,0 +1,22 @@ +/// An amount of logical pixels. +/// +/// Normally used to represent an amount of space, or the size of something. +/// +/// This type is normally asked as an argument in a generic way +/// (e.g. `impl Into<Pixels>`) and, since `Pixels` implements `From` both for +/// `f32` and `u16`, you should be able to provide both integers and float +/// literals as needed. +#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] +pub struct Pixels(pub f32); + +impl From<f32> for Pixels { +    fn from(amount: f32) -> Self { +        Self(amount) +    } +} + +impl From<u16> for Pixels { +    fn from(amount: u16) -> Self { +        Self(f32::from(amount)) +    } +} diff --git a/core/src/size.rs b/core/src/size.rs index a2c72926..fbe940ef 100644 --- a/core/src/size.rs +++ b/core/src/size.rs @@ -29,8 +29,8 @@ impl Size {      /// Increments the [`Size`] to account for the given padding.      pub fn pad(&self, padding: Padding) -> Self {          Size { -            width: self.width + padding.horizontal() as f32, -            height: self.height + padding.vertical() as f32, +            width: self.width + padding.horizontal(), +            height: self.height + padding.vertical(),          }      } | 
