1use crate::config::{AppConfig, WindowConfig};
2use crate::ipc::IpcHandler;
3use crate::renderer::Renderer;
4
5pub enum WindowContent {
7 Url(String),
9 Html(String),
11}
12
13pub 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 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 pub fn with_renderer(mut self, renderer: Box<dyn Renderer>) -> Self {
36 self.renderer = Some(renderer);
37 self
38 }
39
40 pub fn with_ipc_handler(mut self, handler: Box<dyn IpcHandler>) -> Self {
42 self.ipc_handler = Some(handler);
43 self
44 }
45
46 pub fn with_url(mut self, url: impl Into<String>) -> Self {
48 self.content = Some(WindowContent::Url(url.into()));
49 self
50 }
51
52 pub fn with_html(mut self, html: impl Into<String>) -> Self {
54 self.content = Some(WindowContent::Html(html.into()));
55 self
56 }
57
58 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 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
87pub 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 pub fn builder(config: AppConfig) -> AppBuilder {
99 AppBuilder::new(config)
100 }
101
102 pub fn run(mut self) -> crate::Result<()> {
104 self.renderer.init()?;
106
107 if let Some(handler) = self.ipc_handler {
109 self.renderer.set_ipc_handler(handler);
110 }
111
112 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 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 self.renderer.load_url(handle, "about:blank")?;
130 }
131 }
132
133 if let Some(setup) = self.setup_fn {
135 setup(self.renderer.as_mut())?;
136 }
137
138 self.renderer.run()
140 }
141}