Skip to main content

itools_gui/
app.rs

1//! 应用程序模块
2//!
3//! 提供 GUI 应用程序的核心功能,包括应用初始化、配置和生命周期管理。
4
5use serde::{Deserialize, Serialize};
6use tracing::info;
7
8/// 应用程序配置
9#[derive(Debug, Deserialize, Serialize)]
10pub struct AppConfig {
11    /// 应用名称
12    pub name: String,
13    /// 应用版本
14    pub version: String,
15    /// 应用描述
16    pub description: Option<String>,
17    /// 应用作者
18    pub authors: Vec<String>,
19}
20
21/// 应用程序结构体
22#[derive(Debug)]
23pub struct App {
24    /// 应用配置
25    config: AppConfig,
26    /// 窗口列表
27    windows: Vec<Window>,
28}
29
30/// 窗口结构体
31#[derive(Debug)]
32pub struct Window {
33    /// 窗口标题
34    title: String,
35    /// 窗口宽度
36    width: u32,
37    /// 窗口高度
38    height: u32,
39}
40
41impl App {
42    /// 创建新的应用程序实例
43    ///
44    /// # 参数
45    /// - `config`: 应用程序配置
46    ///
47    /// # 返回值
48    /// 应用程序实例
49    pub fn new(config: AppConfig) -> Self {
50        info!("Creating new GUI app: {}", config.name);
51        Self { config, windows: Vec::new() }
52    }
53
54    /// 添加窗口
55    ///
56    /// # 参数
57    /// - `title`: 窗口标题
58    /// - `width`: 窗口宽度
59    /// - `height`: 窗口高度
60    ///
61    /// # 返回值
62    /// 窗口索引
63    pub fn add_window(&mut self, title: &str, width: u32, height: u32) -> usize {
64        let window = Window { title: title.to_string(), width, height };
65        self.windows.push(window);
66        self.windows.len() - 1
67    }
68
69    /// 运行应用程序
70    ///
71    /// # 返回值
72    /// 运行结果
73    pub fn run(&mut self) -> Result<(), Box<dyn std::error::Error>> {
74        info!("Running GUI app: {}", self.config.name);
75        // 这里将实现实际的应用运行逻辑
76        Ok(())
77    }
78
79    /// 获取应用配置
80    ///
81    /// # 返回值
82    /// 应用配置引用
83    pub fn config(&self) -> &AppConfig {
84        &self.config
85    }
86
87    /// 获取窗口列表
88    ///
89    /// # 返回值
90    /// 窗口列表引用
91    pub fn windows(&self) -> &Vec<Window> {
92        &self.windows
93    }
94}