Skip to main content

rdesktop_core/
app.rs

1use crate::config::{AppConfig, WindowConfig};
2use crate::ipc::IpcHandler;
3use crate::renderer::Renderer;
4
5/// Content to load in the main window.
6pub enum WindowContent {
7    /// Load a URL.
8    Url(String),
9    /// Load HTML directly.
10    Html(String),
11}
12
13/// Builder for creating an rdesktop application.
14pub struct AppBuilder {
15    config: AppConfig,
16    renderer: Option<Box<dyn Renderer>>,
17    ipc_handler: Option<Box<dyn IpcHandler>>,
18    content: Option<WindowContent>,
19    setup_fn: Option<Box<dyn FnOnce(&mut dyn Renderer) -> crate::Result<()>>>,
20}
21
22impl AppBuilder {
23    /// Create a new AppBuilder with the given configuration.
24    pub fn new(config: AppConfig) -> Self {
25        Self {
26            config,
27            renderer: None,
28            ipc_handler: None,
29            content: None,
30            setup_fn: None,
31        }
32    }
33
34    /// Set the renderer to use.
35    pub fn with_renderer(mut self, renderer: Box<dyn Renderer>) -> Self {
36        self.renderer = Some(renderer);
37        self
38    }
39
40    /// Set the IPC handler for frontend-to-backend communication.
41    pub fn with_ipc_handler(mut self, handler: Box<dyn IpcHandler>) -> Self {
42        self.ipc_handler = Some(handler);
43        self
44    }
45
46    /// Set the URL to load in the main window.
47    pub fn with_url(mut self, url: impl Into<String>) -> Self {
48        self.content = Some(WindowContent::Url(url.into()));
49        self
50    }
51
52    /// Set the HTML to load in the main window.
53    pub fn with_html(mut self, html: impl Into<String>) -> Self {
54        self.content = Some(WindowContent::Html(html.into()));
55        self
56    }
57
58    /// Set a setup function that runs after renderer initialization.
59    pub fn with_setup<F>(mut self, setup: F) -> Self
60    where
61        F: FnOnce(&mut dyn Renderer) -> crate::Result<()> + 'static,
62    {
63        self.setup_fn = Some(Box::new(setup));
64        self
65    }
66
67    /// Build the application.
68    pub fn build(self) -> crate::Result<App> {
69        let renderer = self
70            .renderer
71            .ok_or_else(|| {
72                crate::RdesktopError::RendererInit(
73                    "No renderer provided. Use with_renderer().".to_string(),
74                )
75            })?;
76
77        Ok(App {
78            config: self.config,
79            renderer,
80            ipc_handler: self.ipc_handler,
81            content: self.content,
82            setup_fn: self.setup_fn,
83        })
84    }
85}
86
87/// The main application struct.
88pub struct App {
89    config: AppConfig,
90    renderer: Box<dyn Renderer>,
91    ipc_handler: Option<Box<dyn IpcHandler>>,
92    content: Option<WindowContent>,
93    setup_fn: Option<Box<dyn FnOnce(&mut dyn Renderer) -> crate::Result<()>>>,
94}
95
96impl App {
97    /// Create a new AppBuilder.
98    pub fn builder(config: AppConfig) -> AppBuilder {
99        AppBuilder::new(config)
100    }
101
102    /// Run the application.
103    pub fn run(mut self) -> crate::Result<()> {
104        // Initialize the renderer
105        self.renderer.init()?;
106
107        // Set IPC handler if provided
108        if let Some(handler) = self.ipc_handler {
109            self.renderer.set_ipc_handler(handler);
110        }
111
112        // Create the main window
113        let window_config = WindowConfig {
114            title: self.config.name.clone(),
115            ..self.config.window.clone()
116        };
117        let handle = self.renderer.create_window(&window_config)?;
118
119        // Load content
120        match self.content {
121            Some(WindowContent::Url(url)) => {
122                self.renderer.load_url(handle, &url)?;
123            }
124            Some(WindowContent::Html(html)) => {
125                self.renderer.load_html(handle, &html)?;
126            }
127            None => {
128                // Load about:blank by default
129                self.renderer.load_url(handle, "about:blank")?;
130            }
131        }
132
133        // Run setup function if provided
134        if let Some(setup) = self.setup_fn {
135            setup(self.renderer.as_mut())?;
136        }
137
138        // Run the event loop (blocks until all windows are closed)
139        self.renderer.run()
140    }
141}