iced_chat_widget/
style.rs

1use iced::Color;
2use iced::Theme;
3use iced::border::Radius;
4
5#[derive(Debug, Clone)]
6pub struct ChatTheme {
7    own_message_style: MessageStyle,
8    other_message_style: MessageStyle,
9    background_color: Color,
10    spacing: f32,
11    padding: f32,
12}
13
14#[derive(Debug, Clone)]
15pub struct MessageStyle {
16    background: Color,
17    text_color: Color,
18    border_radius: Radius,
19    padding: f32,
20    author_text_size: f32,
21    content_text_size: f32,
22    time_stamp_text_color: Color,
23    timestamp_text_size: f32,
24    time_stamp_format: String,
25}
26
27impl MessageStyle {
28    pub fn background(&self) -> Color {
29        self.background
30    }
31
32    pub fn text_color(&self) -> Color {
33        self.text_color
34    }
35
36    pub fn time_stamp_text_color(&self) -> Color {
37        self.time_stamp_text_color
38    }
39
40    pub fn border_radius(&self) -> Radius {
41        self.border_radius
42    }
43
44    pub fn padding(&self) -> f32 {
45        self.padding
46    }
47
48    pub fn author_text_size(&self) -> f32 {
49        self.author_text_size
50    }
51
52    pub fn content_text_size(&self) -> f32 {
53        self.content_text_size
54    }
55
56    pub fn timestamp_text_size(&self) -> f32 {
57        self.timestamp_text_size
58    }
59
60    pub fn time_stamp_format(&self) -> &str {
61        &self.time_stamp_format
62    }
63}
64
65impl ChatTheme {
66    pub fn get_default(iced_theme: &Theme) -> Self {
67        // Define default styles
68        let own_message_style = MessageStyle {
69            background: iced_theme.extended_palette().background.strong.color,
70            text_color: iced_theme.extended_palette().background.strong.text,
71            time_stamp_text_color: iced_theme.extended_palette().background.strong.text,
72            border_radius: Radius::new(8),
73            padding: 10.0,
74            author_text_size: 14.0,
75            content_text_size: 16.0,
76            timestamp_text_size: 10.0,
77            time_stamp_format: String::from("%H:%M:%S"),
78        };
79
80        let other_message_style = MessageStyle {
81            background: iced_theme.extended_palette().background.weak.color,
82            text_color: iced_theme.extended_palette().background.weak.text,
83            time_stamp_text_color: iced_theme.extended_palette().background.weak.text,
84            border_radius: Radius::new(8),
85            padding: 10.0,
86            author_text_size: 14.0,
87            content_text_size: 16.0,
88            timestamp_text_size: 10.0,
89            time_stamp_format: String::from("%H:%M:%S"),
90        };
91
92        ChatTheme {
93            own_message_style,
94            other_message_style,
95            background_color: iced_theme.extended_palette().background.base.color,
96            spacing: 10.0,
97            padding: 10.0,
98        }
99    }
100
101    pub fn own_message_style(&self) -> &MessageStyle {
102        &self.own_message_style
103    }
104
105    pub fn other_message_style(&self) -> &MessageStyle {
106        &self.other_message_style
107    }
108
109    pub fn background_color(&self) -> Color {
110        self.background_color
111    }
112
113    pub fn spacing(&self) -> f32 {
114        self.spacing
115    }
116
117    pub fn padding(&self) -> f32 {
118        self.padding
119    }
120}