1#![allow(deprecated)] use std::cell::{Cell, RefCell};
10use std::rc::Rc;
11use std::sync::Arc;
12use std::time::Instant;
13
14use agg_gui::{App, Modifiers};
15use agg_gui_wgpu::{CopySrc, Gpu, GpuConfig};
16use winit::event_loop::EventLoop;
17use winit::window::{Icon, Window, WindowAttributes};
18
19use crate::bounds::{
20 sanitize_restored_window_size, BoundsAutoSave, SavedBounds, WindowedSizeTracker,
21};
22use crate::config::{ShellConfig, WindowIcon, WindowSize};
23use crate::host::{ExitAction, ShellHost};
24use crate::paint::{PaintRequest, Painter};
25use crate::shell_loop::{ShellInit, ShellLoop};
26use crate::waker::HostWakerGuard;
27use crate::ShellError;
28
29pub fn run<H, B>(config: ShellConfig, build: B) -> Result<(), ShellError>
58where
59 H: ShellHost + 'static,
60 B: FnOnce(&ShellInit<'_>) -> Result<(App, H), ShellError>,
61{
62 let event_loop = EventLoop::new().map_err(ShellError::EventLoop)?;
63
64 let restored_bounds = config.bounds_store.as_ref().and_then(|s| s.load());
65 let (window, restored_size) = create_window(&event_loop, &config, restored_bounds)?;
66 agg_gui::set_device_scale(window.scale_factor());
67 if config.os_tooltip_timings {
68 if let Some(timings) = crate::tooltip::os_tooltip_timings() {
69 agg_gui::set_tooltip_timings(timings);
70 }
71 }
72
73 let init_size = window.inner_size();
77 let gpu_config = GpuConfig::new(config.device_label)
78 .with_present_mode(config.present_mode)
79 .with_copy_src(if config.screenshot.is_some() {
82 CopySrc::Required
83 } else {
84 config.copy_src
85 });
86 let gpu = Gpu::new(
87 Arc::clone(&window),
88 (init_size.width.max(1), init_size.height.max(1)),
89 gpu_config,
90 )
91 .map_err(ShellError::Gpu)?;
92
93 let (app, host) = build(&ShellInit {
94 window: &window,
95 gpu: &gpu,
96 restored_bounds,
97 })?;
98
99 let waker_guard = HostWakerGuard::install(event_loop.create_proxy());
102
103 let painter = Painter::new(&gpu);
104 let maximized = window.is_maximized();
105 let windowed_size =
109 WindowedSizeTracker::new(restored_size, (gpu.config().width, gpu.config().height));
110 let mut bounds_auto = BoundsAutoSave::default();
111 bounds_auto.seed(restored_bounds);
112
113 let exit = Rc::new(Cell::new(None::<ExitAction>));
114 let error = Rc::new(RefCell::new(None::<ShellError>));
115
116 let mut shell = ShellLoop {
117 window: Arc::clone(&window),
118 gpu: Some(gpu),
119 gpu_config,
120 painter,
121 app,
122 host,
123 policy: config.redraw_policy,
124 screenshot: config.screenshot.clone(),
125 screenshot_done: false,
126 screenshot_last_paint: Instant::now(),
127 cursor: (0.0, 0.0),
128 mods: Modifiers::default(),
129 mouse_buttons_down: 0,
130 input_since_frame: false,
131 pending_resize: None,
132 bounds_store: config.bounds_store,
133 bounds_auto,
134 windowed_size,
135 maximized,
136 exit: Rc::clone(&exit),
137 error: Rc::clone(&error),
138 finished: false,
139 _waker_guard: waker_guard,
140 };
141
142 if let Some(gpu) = shell.gpu.as_mut() {
147 shell.painter.paint(
148 gpu,
149 &window,
150 &mut shell.app,
151 &mut shell.host,
152 PaintRequest {
153 pending_resize: &mut None,
154 input_since_last_frame: false,
155 capture: None,
156 },
157 )?;
158 }
159 window.set_visible(true);
160
161 event_loop
162 .run(move |event, elwt| shell.handle(event, elwt))
163 .map_err(ShellError::EventLoop)?;
164
165 if let Some(e) = error.borrow_mut().take() {
166 return Err(e);
167 }
168 if exit.get() == Some(ExitAction::Relaunch) {
169 relaunch()?;
170 }
171 Ok(())
172}
173
174fn relaunch() -> Result<(), ShellError> {
177 let exe = std::env::current_exe().map_err(ShellError::Relaunch)?;
178 std::process::Command::new(exe)
179 .spawn()
180 .map(|_| ())
181 .map_err(ShellError::Relaunch)
182}
183
184fn create_window(
189 event_loop: &EventLoop<()>,
190 config: &ShellConfig,
191 restored: Option<SavedBounds>,
192) -> Result<(Arc<Window>, Option<(u32, u32)>), ShellError> {
193 let saved = restored.map(|b| (b.width, b.height));
201 let mut restored_size = saved.map(|_| sanitize_restored_window_size(saved, None));
202 let size = match restored_size {
203 Some((w, h)) => WindowSize::Physical(w, h),
204 None => config.size,
205 };
206
207 let mut attributes = WindowAttributes::default()
208 .with_title(config.title.clone())
209 .with_inner_size(size.to_winit())
210 .with_maximized(restored.map(|b| b.maximized).unwrap_or(config.maximized))
211 .with_visible(false);
213 if let Some(min) = config.min_size {
214 attributes = attributes.with_min_inner_size(min.to_winit());
215 }
216 if let Some(icon) = config.icon.as_ref() {
217 attributes = attributes.with_window_icon(window_icon(icon));
218 }
219 if config.fullscreen {
220 attributes = attributes.with_fullscreen(Some(winit::window::Fullscreen::Borderless(None)));
221 agg_gui::fullscreen::set_active(true);
222 }
223
224 let window = Arc::new(
225 event_loop
226 .create_window(attributes)
227 .map_err(ShellError::CreateWindow)?,
228 );
229
230 if saved.is_some() {
235 if let Some(monitor) = window
236 .primary_monitor()
237 .or_else(|| window.current_monitor())
238 {
239 let m = monitor.size();
240 let refined = sanitize_restored_window_size(saved, Some((m.width, m.height)));
241 restored_size = Some(refined);
242 if WindowSize::Physical(refined.0, refined.1) != size {
243 let _ =
244 window.request_inner_size(winit::dpi::PhysicalSize::new(refined.0, refined.1));
245 }
246 }
247 }
248 Ok((window, restored_size))
249}
250
251fn window_icon(icon: &WindowIcon) -> Option<Icon> {
254 match Icon::from_rgba(icon.rgba.clone(), icon.width, icon.height) {
255 Ok(icon) => Some(icon),
256 Err(err) => {
257 log::warn!("agg-gui-shell: ignoring invalid window icon: {err}");
258 None
259 }
260 }
261}