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_optional_features(config.optional_features)
80 .with_copy_src(if config.screenshot.is_some() {
83 CopySrc::Required
84 } else {
85 config.copy_src
86 });
87 let gpu = Gpu::new(
88 Arc::clone(&window),
89 (init_size.width.max(1), init_size.height.max(1)),
90 gpu_config,
91 )
92 .map_err(ShellError::Gpu)?;
93
94 let (app, host) = build(&ShellInit {
95 window: &window,
96 gpu: &gpu,
97 restored_bounds,
98 })?;
99
100 let waker_guard = HostWakerGuard::install(event_loop.create_proxy());
103
104 let painter = Painter::new(&gpu);
105 let maximized = window.is_maximized();
106 let windowed_size =
110 WindowedSizeTracker::new(restored_size, (gpu.config().width, gpu.config().height));
111 let mut bounds_auto = BoundsAutoSave::default();
112 bounds_auto.seed(restored_bounds);
113
114 let exit = Rc::new(Cell::new(None::<ExitAction>));
115 let error = Rc::new(RefCell::new(None::<ShellError>));
116
117 let mut shell = ShellLoop {
118 window: Arc::clone(&window),
119 gpu: Some(gpu),
120 gpu_config,
121 painter,
122 app,
123 host,
124 policy: config.redraw_policy,
125 screenshot: config.screenshot.clone(),
126 screenshot_done: false,
127 screenshot_last_paint: Instant::now(),
128 cursor: (0.0, 0.0),
129 mods: Modifiers::default(),
130 mouse_buttons_down: 0,
131 input_since_frame: false,
132 pending_resize: None,
133 surface_retry_at: None,
134 bounds_store: config.bounds_store,
135 bounds_auto,
136 windowed_size,
137 maximized,
138 exit: Rc::clone(&exit),
139 error: Rc::clone(&error),
140 finished: false,
141 _waker_guard: waker_guard,
142 };
143
144 if let Some(gpu) = shell.gpu.as_mut() {
149 shell.painter.paint(
150 gpu,
151 &window,
152 &mut shell.app,
153 &mut shell.host,
154 PaintRequest {
155 pending_resize: &mut None,
156 input_since_last_frame: false,
157 capture: None,
158 },
159 )?;
160 }
161 window.set_visible(true);
162
163 event_loop
164 .run(move |event, elwt| shell.handle(event, elwt))
165 .map_err(ShellError::EventLoop)?;
166
167 if let Some(e) = error.borrow_mut().take() {
168 return Err(e);
169 }
170 if exit.get() == Some(ExitAction::Relaunch) {
171 relaunch()?;
172 }
173 Ok(())
174}
175
176fn relaunch() -> Result<(), ShellError> {
179 let exe = std::env::current_exe().map_err(ShellError::Relaunch)?;
180 std::process::Command::new(exe)
181 .spawn()
182 .map(|_| ())
183 .map_err(ShellError::Relaunch)
184}
185
186fn create_window(
191 event_loop: &EventLoop<()>,
192 config: &ShellConfig,
193 restored: Option<SavedBounds>,
194) -> Result<(Arc<Window>, Option<(u32, u32)>), ShellError> {
195 let saved = restored.map(|b| (b.width, b.height));
203 let mut restored_size = saved.map(|_| sanitize_restored_window_size(saved, None));
204 let size = match restored_size {
205 Some((w, h)) => WindowSize::Physical(w, h),
206 None => config.size,
207 };
208
209 let mut attributes = WindowAttributes::default()
210 .with_title(config.title.clone())
211 .with_inner_size(size.to_winit())
212 .with_maximized(restored.map(|b| b.maximized).unwrap_or(config.maximized))
213 .with_visible(false);
215 if let Some(min) = config.min_size {
216 attributes = attributes.with_min_inner_size(min.to_winit());
217 }
218 if let Some(icon) = config.icon.as_ref() {
219 attributes = attributes.with_window_icon(window_icon(icon));
220 }
221 if config.fullscreen {
222 attributes = attributes.with_fullscreen(Some(winit::window::Fullscreen::Borderless(None)));
223 agg_gui::fullscreen::set_active(true);
224 }
225
226 let window = Arc::new(
227 event_loop
228 .create_window(attributes)
229 .map_err(ShellError::CreateWindow)?,
230 );
231
232 if saved.is_some() {
237 if let Some(monitor) = window
238 .primary_monitor()
239 .or_else(|| window.current_monitor())
240 {
241 let m = monitor.size();
242 let refined = sanitize_restored_window_size(saved, Some((m.width, m.height)));
243 restored_size = Some(refined);
244 if WindowSize::Physical(refined.0, refined.1) != size {
245 let _ =
246 window.request_inner_size(winit::dpi::PhysicalSize::new(refined.0, refined.1));
247 }
248 }
249 }
250 Ok((window, restored_size))
251}
252
253fn window_icon(icon: &WindowIcon) -> Option<Icon> {
256 match Icon::from_rgba(icon.rgba.clone(), icon.width, icon.height) {
257 Ok(icon) => Some(icon),
258 Err(err) => {
259 log::warn!("agg-gui-shell: ignoring invalid window icon: {err}");
260 None
261 }
262 }
263}