iced_core/
rotation.rs

1//! Control the rotation of some content (like an image) within a space.
2use crate::{Degrees, Radians, Size};
3
4/// The strategy used to rotate the content.
5///
6/// This is used to control the behavior of the layout when the content is rotated
7/// by a certain angle.
8#[derive(Debug, Clone, Copy, PartialEq)]
9pub enum Rotation {
10    /// The element will float while rotating. The layout will be kept exactly as it was
11    /// before the rotation.
12    ///
13    /// This is especially useful when used for animations, as it will avoid the
14    /// layout being shifted or resized when smoothly i.e. an icon.
15    ///
16    /// This is the default.
17    Floating(Radians),
18    /// The element will be solid while rotating. The layout will be adjusted to fit
19    /// the rotated content.
20    ///
21    /// This allows you to rotate an image and have the layout adjust to fit the new
22    /// size of the image.
23    Solid(Radians),
24}
25
26impl Rotation {
27    /// Returns the angle of the [`Rotation`] in [`Radians`].
28    pub fn radians(self) -> Radians {
29        match self {
30            Rotation::Floating(radians) | Rotation::Solid(radians) => radians,
31        }
32    }
33
34    /// Returns a mutable reference to the angle of the [`Rotation`] in [`Radians`].
35    pub fn radians_mut(&mut self) -> &mut Radians {
36        match self {
37            Rotation::Floating(radians) | Rotation::Solid(radians) => radians,
38        }
39    }
40
41    /// Returns the angle of the [`Rotation`] in [`Degrees`].
42    pub fn degrees(self) -> Degrees {
43        Degrees(self.radians().0.to_degrees())
44    }
45
46    /// Applies the [`Rotation`] to the given [`Size`], returning
47    /// the minimum [`Size`] containing the rotated one.
48    pub fn apply(self, size: Size) -> Size {
49        match self {
50            Self::Floating(_) => size,
51            Self::Solid(rotation) => size.rotate(rotation),
52        }
53    }
54}
55
56impl Default for Rotation {
57    fn default() -> Self {
58        Self::Floating(Radians(0.0))
59    }
60}
61
62impl From<Radians> for Rotation {
63    fn from(radians: Radians) -> Self {
64        Self::Floating(radians)
65    }
66}
67
68impl From<f32> for Rotation {
69    fn from(radians: f32) -> Self {
70        Self::Floating(Radians(radians))
71    }
72}