Skip to main content

rustigram_types/
background.rs

1use serde::{Deserialize, Serialize};
2
3use crate::file::Document;
4
5/// How a chat background is filled with colour.
6#[derive(Debug, Clone, Serialize, Deserialize)]
7#[serde(tag = "type", rename_all = "snake_case")]
8pub enum BackgroundFill {
9    /// A single colour.
10    Solid(BackgroundFillSolid),
11    /// A two-colour gradient.
12    Gradient(BackgroundFillGradient),
13    /// A freeform gradient rotating between three or four colours.
14    FreeformGradient(BackgroundFillFreeformGradient),
15}
16
17/// A background filled with a single colour.
18#[derive(Debug, Clone, Default, Serialize, Deserialize)]
19#[non_exhaustive]
20pub struct BackgroundFillSolid {
21    /// The fill colour in RGB24 format.
22    pub color: u32,
23}
24
25/// A background filled with a two-colour gradient.
26#[derive(Debug, Clone, Default, Serialize, Deserialize)]
27#[non_exhaustive]
28pub struct BackgroundFillGradient {
29    /// Top colour of the gradient in RGB24 format.
30    pub top_color: u32,
31    /// Bottom colour of the gradient in RGB24 format.
32    pub bottom_color: u32,
33    /// Clockwise rotation angle of the background fill, in degrees (0–359).
34    pub rotation_angle: u16,
35}
36
37/// A background filled with a freeform gradient that rotates after every message.
38#[derive(Debug, Clone, Default, Serialize, Deserialize)]
39#[non_exhaustive]
40pub struct BackgroundFillFreeformGradient {
41    /// Three or four base colours used to generate the gradient, in RGB24 format.
42    pub colors: Vec<u32>,
43}
44
45/// The type of a chat background.
46#[derive(Debug, Clone, Serialize, Deserialize)]
47#[serde(tag = "type", rename_all = "snake_case")]
48pub enum BackgroundType {
49    /// Automatically filled based on colours.
50    Fill(BackgroundTypeFill),
51    /// A wallpaper image in JPEG format.
52    Wallpaper(BackgroundTypeWallpaper),
53    /// A PNG or TGV pattern filled with a colour.
54    Pattern(BackgroundTypePattern),
55    /// One of the default chat themes.
56    ChatTheme(BackgroundTypeChatTheme),
57}
58
59/// A background automatically filled based on colours.
60#[derive(Debug, Clone, Serialize, Deserialize)]
61#[non_exhaustive]
62pub struct BackgroundTypeFill {
63    /// The background fill.
64    pub fill: BackgroundFill,
65    /// Dimming of the background in dark themes, as a percentage (0–100).
66    pub dark_theme_dimming: u8,
67}
68
69/// A background that is a wallpaper image in JPEG format.
70#[derive(Debug, Clone, Serialize, Deserialize)]
71#[non_exhaustive]
72pub struct BackgroundTypeWallpaper {
73    /// Document with the wallpaper.
74    pub document: Document,
75    /// Dimming of the background in dark themes, as a percentage (0–100).
76    pub dark_theme_dimming: u8,
77    /// `true` if the wallpaper is downscaled to fit in a 450×450 square and
78    /// then box-blurred with a radius of 12.
79    #[serde(skip_serializing_if = "Option::is_none")]
80    pub is_blurred: Option<bool>,
81    /// `true` if the background moves slightly when the device is tilted.
82    #[serde(skip_serializing_if = "Option::is_none")]
83    pub is_moving: Option<bool>,
84}
85
86/// A background that is a PNG or TGV pattern filled with a colour.
87#[derive(Debug, Clone, Serialize, Deserialize)]
88#[non_exhaustive]
89pub struct BackgroundTypePattern {
90    /// Document with the pattern.
91    pub document: Document,
92    /// The background fill used to combine with the pattern.
93    pub fill: BackgroundFill,
94    /// Intensity of the pattern when it is shown above the filled background,
95    /// as a percentage (0–100).
96    pub intensity: u8,
97    /// `true` if the background fill must be applied only to the pattern itself.
98    /// All other pixels are black in this case.
99    #[serde(skip_serializing_if = "Option::is_none")]
100    pub is_inverted: Option<bool>,
101    /// `true` if the background moves slightly when the device is tilted.
102    #[serde(skip_serializing_if = "Option::is_none")]
103    pub is_moving: Option<bool>,
104}
105
106/// A background taken from one of the default chat themes.
107#[derive(Debug, Clone, Default, Serialize, Deserialize)]
108#[non_exhaustive]
109pub struct BackgroundTypeChatTheme {
110    /// Name of the chat theme, which is usually an emoji.
111    pub theme_name: String,
112}
113
114/// A chat background.
115///
116/// Sealed without [`Default`]: its required `type` field is a
117/// [`BackgroundType`], and each variant describes a genuinely different kind of
118/// background, so nominating one as the default would invent a value Telegram
119/// never sends.
120#[derive(Debug, Clone, Serialize, Deserialize)]
121#[non_exhaustive]
122pub struct ChatBackground {
123    /// Type of the background.
124    #[serde(rename = "type")]
125    pub kind: BackgroundType,
126}