polyhorn_ui/styles/image.rs
1use strum_macros::EnumString;
2
3use super::ViewStyle;
4use crate::color::Color;
5
6/// Defines the method for fitting objects that do not match the dimensions of
7/// their container.
8#[derive(Copy, Clone, Debug, Eq, PartialEq, EnumString)]
9pub enum ObjectFit {
10 /// Fills the container without respecting the aspect ratio of the object if
11 /// the dimensions of the container isn't a multiple of the dimensions of
12 /// the object.
13 #[strum(serialize = "fill")]
14 Fill,
15
16 /// Applies no scaling to the object.
17 #[strum(serialize = "none")]
18 None,
19
20 /// Scales the object so that it is completely visible while maintaining its
21 /// aspect ratio. If after scaling, one of the dimensions is smaller than
22 /// the corresponding dimension of the container, the object is centered
23 /// within its container.
24 #[strum(serialize = "contain")]
25 Contain,
26
27 /// Scales the object so that it completely covers its container, while
28 /// maintaining its aspect ratio. If after scaling, one of the dimensions is
29 /// larger than the corresponding dimension of the container, the object is
30 /// centered within its container and the invisible area is split evenly
31 /// across both ends of the relevant dimension.
32 #[strum(serialize = "cover")]
33 Cover,
34}
35
36impl Default for ObjectFit {
37 fn default() -> Self {
38 ObjectFit::Fill
39 }
40}
41
42/// Controls the appearance of an Image.
43#[derive(Copy, Clone, Debug, Default, PartialEq)]
44pub struct ImageStyle {
45 /// Controls the method for fitting images that do not match the dimensions
46 /// of their container.
47 pub object_fit: ObjectFit,
48
49 /// If present, controls the color that this image is rendered in. Only the
50 /// alpha channel of the original image is kept: all other channels are
51 /// replaced by the given tint color. If this tint color exists in a
52 /// different color space than the original image, the resulting image is
53 /// drawn using the color space of the tint color.
54 pub tint_color: Option<Color>,
55}
56
57/// This is a union style of the Image and View styles.
58#[derive(Copy, Clone, Debug, Default, PartialEq)]
59pub struct ImageViewStyle {
60 /// This style contains the properties that are only applicable to Images.
61 pub image: ImageStyle,
62
63 /// This style contains the properties that are only applicable to Views.
64 pub view: ViewStyle,
65}
66
67impl From<ImageStyle> for ImageViewStyle {
68 fn from(style: ImageStyle) -> Self {
69 ImageViewStyle {
70 image: style,
71 ..Default::default()
72 }
73 }
74}
75
76impl From<ViewStyle> for ImageViewStyle {
77 fn from(style: ViewStyle) -> Self {
78 ImageViewStyle {
79 view: style,
80 ..Default::default()
81 }
82 }
83}