Skip to main content

itools_gui/
style.rs

1//! 样式系统模块
2//!
3//! 提供 GUI 样式管理功能,包括颜色、字体、布局样式等。
4
5use serde::{Deserialize, Serialize};
6
7/// 颜色结构体
8#[derive(Debug, Deserialize, Serialize, Clone, Copy)]
9pub struct Color {
10    /// 红色通道
11    pub r: u8,
12    /// 绿色通道
13    pub g: u8,
14    /// 蓝色通道
15    pub b: u8,
16    /// 透明度通道
17    pub a: f32,
18}
19
20/// 字体结构体
21#[derive(Debug, Deserialize, Serialize, Clone)]
22pub struct Font {
23    /// 字体名称
24    pub name: String,
25    /// 字体大小
26    pub size: f32,
27    /// 是否粗体
28    pub bold: bool,
29    /// 是否斜体
30    pub italic: bool,
31    /// 是否下划线
32    pub underline: bool,
33}
34
35/// 边距结构体
36#[derive(Debug, Deserialize, Serialize, Clone, Copy)]
37pub struct Padding {
38    /// 上内边距
39    pub top: f32,
40    /// 右内边距
41    pub right: f32,
42    /// 下内边距
43    pub bottom: f32,
44    /// 左内边距
45    pub left: f32,
46}
47
48/// 边框结构体
49#[derive(Debug, Deserialize, Serialize, Clone, Copy)]
50pub struct Border {
51    /// 边框宽度
52    pub width: f32,
53    /// 边框颜色
54    pub color: Color,
55    /// 边框圆角
56    pub radius: f32,
57}
58
59/// 样式结构体
60#[derive(Debug, Deserialize, Serialize, Clone)]
61pub struct Style {
62    /// 背景颜色
63    pub background: Option<Color>,
64    /// 前景颜色
65    pub foreground: Option<Color>,
66    /// 字体
67    pub font: Option<Font>,
68    /// 边距
69    pub padding: Option<Padding>,
70    /// 边框
71    pub border: Option<Border>,
72    /// 宽度
73    pub width: Option<f32>,
74    /// 高度
75    pub height: Option<f32>,
76    /// 水平对齐
77    pub horizontal_align: Option<Alignment>,
78    /// 垂直对齐
79    pub vertical_align: Option<Alignment>,
80}
81
82/// 对齐方式
83#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
84pub enum Alignment {
85    /// 左对齐/上对齐
86    Start,
87    /// 居中对齐
88    Center,
89    /// 右对齐/下对齐
90    End,
91}
92
93impl Color {
94    /// 创建新的颜色
95    ///
96    /// # 参数
97    /// - `r`: 红色通道 (0-255)
98    /// - `g`: 绿色通道 (0-255)
99    /// - `b`: 蓝色通道 (0-255)
100    /// - `a`: 透明度通道 (0.0-1.0)
101    ///
102    /// # 返回值
103    /// 颜色实例
104    pub fn new(r: u8, g: u8, b: u8, a: f32) -> Self {
105        Self { r, g, b, a: a.clamp(0.0, 1.0) }
106    }
107
108    /// 创建不透明颜色
109    ///
110    /// # 参数
111    /// - `r`: 红色通道 (0-255)
112    /// - `g`: 绿色通道 (0-255)
113    /// - `b`: 蓝色通道 (0-255)
114    ///
115    /// # 返回值
116    /// 不透明颜色实例
117    pub fn rgb(r: u8, g: u8, b: u8) -> Self {
118        Self::new(r, g, b, 1.0)
119    }
120
121    /// 创建透明颜色
122    ///
123    /// # 参数
124    /// - `r`: 红色通道 (0-255)
125    /// - `g`: 绿色通道 (0-255)
126    /// - `b`: 蓝色通道 (0-255)
127    /// - `a`: 透明度通道 (0.0-1.0)
128    ///
129    /// # 返回值
130    /// 透明颜色实例
131    pub fn rgba(r: u8, g: u8, b: u8, a: f32) -> Self {
132        Self::new(r, g, b, a)
133    }
134
135    /// 黑色
136    pub const BLACK: Self = Self { r: 0, g: 0, b: 0, a: 1.0 };
137
138    /// 白色
139    pub const WHITE: Self = Self { r: 255, g: 255, b: 255, a: 1.0 };
140
141    /// 红色
142    pub const RED: Self = Self { r: 255, g: 0, b: 0, a: 1.0 };
143
144    /// 绿色
145    pub const GREEN: Self = Self { r: 0, g: 255, b: 0, a: 1.0 };
146
147    /// 蓝色
148    pub const BLUE: Self = Self { r: 0, g: 0, b: 255, a: 1.0 };
149
150    /// 黄色
151    pub const YELLOW: Self = Self { r: 255, g: 255, b: 0, a: 1.0 };
152
153    /// 青色
154    pub const CYAN: Self = Self { r: 0, g: 255, b: 255, a: 1.0 };
155
156    /// 品红
157    pub const MAGENTA: Self = Self { r: 255, g: 0, b: 255, a: 1.0 };
158
159    /// 灰色
160    pub const GRAY: Self = Self { r: 128, g: 128, b: 128, a: 1.0 };
161
162    /// 浅灰色
163    pub const LIGHT_GRAY: Self = Self { r: 200, g: 200, b: 200, a: 1.0 };
164
165    /// 深灰色
166    pub const DARK_GRAY: Self = Self { r: 64, g: 64, b: 64, a: 1.0 };
167}
168
169impl Font {
170    /// 创建新的字体
171    ///
172    /// # 参数
173    /// - `name`: 字体名称
174    /// - `size`: 字体大小
175    /// - `bold`: 是否粗体
176    /// - `italic`: 是否斜体
177    /// - `underline`: 是否下划线
178    ///
179    /// # 返回值
180    /// 字体实例
181    pub fn new(name: &str, size: f32, bold: bool, italic: bool, underline: bool) -> Self {
182        Self { name: name.to_string(), size: size.max(1.0), bold, italic, underline }
183    }
184
185    /// 创建默认字体
186    ///
187    /// # 返回值
188    /// 默认字体实例
189    pub fn default() -> Self {
190        Self::new("Arial", 14.0, false, false, false)
191    }
192}
193
194impl Padding {
195    /// 创建新的边距
196    ///
197    /// # 参数
198    /// - `top`: 上内边距
199    /// - `right`: 右内边距
200    /// - `bottom`: 下内边距
201    /// - `left`: 左内边距
202    ///
203    /// # 返回值
204    /// 边距实例
205    pub fn new(top: f32, right: f32, bottom: f32, left: f32) -> Self {
206        Self { top: top.max(0.0), right: right.max(0.0), bottom: bottom.max(0.0), left: left.max(0.0) }
207    }
208
209    /// 创建对称边距
210    ///
211    /// # 参数
212    /// - `vertical`: 垂直边距(上和下)
213    /// - `horizontal`: 水平边距(左和右)
214    ///
215    /// # 返回值
216    /// 对称边距实例
217    pub fn symmetric(vertical: f32, horizontal: f32) -> Self {
218        Self::new(vertical, horizontal, vertical, horizontal)
219    }
220
221    /// 创建统一边距
222    ///
223    /// # 参数
224    /// - `all`: 所有方向的边距
225    ///
226    /// # 返回值
227    /// 统一边距实例
228    pub fn uniform(all: f32) -> Self {
229        Self::new(all, all, all, all)
230    }
231
232    /// 默认边距
233    pub const DEFAULT: Self = Self { top: 8.0, right: 8.0, bottom: 8.0, left: 8.0 };
234}
235
236impl Border {
237    /// 创建新的边框
238    ///
239    /// # 参数
240    /// - `width`: 边框宽度
241    /// - `color`: 边框颜色
242    /// - `radius`: 边框圆角
243    ///
244    /// # 返回值
245    /// 边框实例
246    pub fn new(width: f32, color: Color, radius: f32) -> Self {
247        Self { width: width.max(0.0), color, radius: radius.max(0.0) }
248    }
249
250    /// 创建默认边框
251    ///
252    /// # 返回值
253    /// 默认边框实例
254    pub fn default() -> Self {
255        Self::new(1.0, Color::GRAY, 0.0)
256    }
257}
258
259impl Style {
260    /// 创建新的样式
261    ///
262    /// # 返回值
263    /// 样式实例
264    pub fn new() -> Self {
265        Self {
266            background: None,
267            foreground: None,
268            font: None,
269            padding: None,
270            border: None,
271            width: None,
272            height: None,
273            horizontal_align: None,
274            vertical_align: None,
275        }
276    }
277
278    /// 设置背景颜色
279    ///
280    /// # 参数
281    /// - `color`: 背景颜色
282    ///
283    /// # 返回值
284    /// 样式实例(链式调用)
285    pub fn with_background(mut self, color: Color) -> Self {
286        self.background = Some(color);
287        self
288    }
289
290    /// 设置前景颜色
291    ///
292    /// # 参数
293    /// - `color`: 前景颜色
294    ///
295    /// # 返回值
296    /// 样式实例(链式调用)
297    pub fn with_foreground(mut self, color: Color) -> Self {
298        self.foreground = Some(color);
299        self
300    }
301
302    /// 设置字体
303    ///
304    /// # 参数
305    /// - `font`: 字体
306    ///
307    /// # 返回值
308    /// 样式实例(链式调用)
309    pub fn with_font(mut self, font: Font) -> Self {
310        self.font = Some(font);
311        self
312    }
313
314    /// 设置边距
315    ///
316    /// # 参数
317    /// - `padding`: 边距
318    ///
319    /// # 返回值
320    /// 样式实例(链式调用)
321    pub fn with_padding(mut self, padding: Padding) -> Self {
322        self.padding = Some(padding);
323        self
324    }
325
326    /// 设置边框
327    ///
328    /// # 参数
329    /// - `border`: 边框
330    ///
331    /// # 返回值
332    /// 样式实例(链式调用)
333    pub fn with_border(mut self, border: Border) -> Self {
334        self.border = Some(border);
335        self
336    }
337
338    /// 设置宽度
339    ///
340    /// # 参数
341    /// - `width`: 宽度
342    ///
343    /// # 返回值
344    /// 样式实例(链式调用)
345    pub fn with_width(mut self, width: f32) -> Self {
346        self.width = Some(width.max(0.0));
347        self
348    }
349
350    /// 设置高度
351    ///
352    /// # 参数
353    /// - `height`: 高度
354    ///
355    /// # 返回值
356    /// 样式实例(链式调用)
357    pub fn with_height(mut self, height: f32) -> Self {
358        self.height = Some(height.max(0.0));
359        self
360    }
361
362    /// 设置水平对齐
363    ///
364    /// # 参数
365    /// - `align`: 对齐方式
366    ///
367    /// # 返回值
368    /// 样式实例(链式调用)
369    pub fn with_horizontal_align(mut self, align: Alignment) -> Self {
370        self.horizontal_align = Some(align);
371        self
372    }
373
374    /// 设置垂直对齐
375    ///
376    /// # 参数
377    /// - `align`: 对齐方式
378    ///
379    /// # 返回值
380    /// 样式实例(链式调用)
381    pub fn with_vertical_align(mut self, align: Alignment) -> Self {
382        self.vertical_align = Some(align);
383        self
384    }
385
386    /// 默认样式
387    pub fn default() -> Self {
388        Self::new()
389    }
390}