diff options
author | 2024-05-23 13:29:45 +0200 | |
---|---|---|
committer | 2024-05-23 13:29:45 +0200 | |
commit | d8ba6b0673a33724a177f3a1ba59705527280142 (patch) | |
tree | 89482c8d1e3a03e00b3a8151abbb81e30ae5898c /core/src/rotation.rs | |
parent | 72ed8bcc8def9956e25f3720a3095fc96bb2eef0 (diff) | |
parent | 468794d918eb06c1dbebb33c32b10017ad335f05 (diff) | |
download | iced-d8ba6b0673a33724a177f3a1ba59705527280142.tar.gz iced-d8ba6b0673a33724a177f3a1ba59705527280142.tar.bz2 iced-d8ba6b0673a33724a177f3a1ba59705527280142.zip |
Merge branch 'master' into feat/text-macro
Diffstat (limited to 'core/src/rotation.rs')
-rw-r--r-- | core/src/rotation.rs | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/core/src/rotation.rs b/core/src/rotation.rs new file mode 100644 index 00000000..afa8d79e --- /dev/null +++ b/core/src/rotation.rs @@ -0,0 +1,72 @@ +//! Control the rotation of some content (like an image) within a space. +use crate::{Degrees, Radians, Size}; + +/// The strategy used to rotate the content. +/// +/// This is used to control the behavior of the layout when the content is rotated +/// by a certain angle. +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Rotation { + /// The element will float while rotating. The layout will be kept exactly as it was + /// before the rotation. + /// + /// This is especially useful when used for animations, as it will avoid the + /// layout being shifted or resized when smoothly i.e. an icon. + /// + /// This is the default. + Floating(Radians), + /// The element will be solid while rotating. The layout will be adjusted to fit + /// the rotated content. + /// + /// This allows you to rotate an image and have the layout adjust to fit the new + /// size of the image. + Solid(Radians), +} + +impl Rotation { + /// Returns the angle of the [`Rotation`] in [`Radians`]. + pub fn radians(self) -> Radians { + match self { + Rotation::Floating(radians) | Rotation::Solid(radians) => radians, + } + } + + /// Returns a mutable reference to the angle of the [`Rotation`] in [`Radians`]. + pub fn radians_mut(&mut self) -> &mut Radians { + match self { + Rotation::Floating(radians) | Rotation::Solid(radians) => radians, + } + } + + /// Returns the angle of the [`Rotation`] in [`Degrees`]. + pub fn degrees(self) -> Degrees { + Degrees(self.radians().0.to_degrees()) + } + + /// Applies the [`Rotation`] to the given [`Size`], returning + /// the minimum [`Size`] containing the rotated one. + pub fn apply(self, size: Size) -> Size { + match self { + Self::Floating(_) => size, + Self::Solid(rotation) => size.rotate(rotation), + } + } +} + +impl Default for Rotation { + fn default() -> Self { + Self::Floating(Radians(0.0)) + } +} + +impl From<Radians> for Rotation { + fn from(radians: Radians) -> Self { + Self::Floating(radians) + } +} + +impl From<f32> for Rotation { + fn from(radians: f32) -> Self { + Self::Floating(Radians(radians)) + } +} |