diff options
author | 2023-02-22 21:23:04 +0100 | |
---|---|---|
committer | 2023-02-22 21:23:04 +0100 | |
commit | 4f41927155e7d4bc38497b0e298a0b23ccea6ca1 (patch) | |
tree | 70b7dbc1afdc62dfeaf42f0c62e8f4c01e407729 /core/src | |
parent | a35d6d2e4d59f71309f31c87ea5150959d639185 (diff) | |
parent | 666f3cd143047e49a010f0c97eabc7136f92aa35 (diff) | |
download | iced-4f41927155e7d4bc38497b0e298a0b23ccea6ca1.tar.gz iced-4f41927155e7d4bc38497b0e298a0b23ccea6ca1.tar.bz2 iced-4f41927155e7d4bc38497b0e298a0b23ccea6ca1.zip |
Merge branch 'iced-rs:master' into master
Diffstat (limited to 'core/src')
-rw-r--r-- | core/src/length.rs | 14 | ||||
-rw-r--r-- | core/src/lib.rs | 4 | ||||
-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, 90 insertions, 27 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..d3596b4d 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -7,7 +7,7 @@ //!  //! //! [Iced]: https://github.com/iced-rs/iced -//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.7/native +//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.8/native //! [`iced_web`]: https://github.com/iced-rs/iced_web #![doc( html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg" @@ -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(), } } |