Skip to main content

iced_winit/
lib.rs

1//! A windowing shell for Iced, on top of [`winit`].
2//!
3//! ![The native path of the Iced ecosystem](https://github.com/iced-rs/iced/blob/0525d76ff94e828b7b21634fa94a747022001c83/docs/graphs/native.png?raw=true)
4//!
5//! `iced_winit` offers some convenient abstractions on top of [`iced_runtime`]
6//! to quickstart development when using [`winit`].
7//!
8//! It exposes a renderer-agnostic [`Program`] trait that can be implemented
9//! and then run with a simple call. The use of this trait is optional.
10//!
11//! Additionally, a [`conversion`] module is available for users that decide to
12//! implement a custom event loop.
13//!
14//! [`iced_runtime`]: https://github.com/iced-rs/iced/tree/master/runtime
15//! [`winit`]: https://github.com/rust-windowing/winit
16//! [`conversion`]: crate::conversion
17#![doc(
18    html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg"
19)]
20#![cfg_attr(docsrs, feature(doc_cfg))]
21pub use iced_debug as debug;
22pub use iced_program as program;
23pub use iced_runtime as runtime;
24pub use program::core;
25pub use program::graphics;
26pub use runtime::futures;
27pub use winit;
28
29pub mod clipboard;
30pub mod conversion;
31
32mod error;
33mod proxy;
34mod window;
35
36pub use clipboard::Clipboard;
37pub use error::Error;
38pub use proxy::Proxy;
39
40use crate::core::backend;
41use crate::core::mouse;
42use crate::core::renderer;
43use crate::core::theme;
44use crate::core::time::Instant;
45use crate::core::widget::operation;
46use crate::core::{Point, Renderer, Size};
47use crate::futures::futures::channel::mpsc;
48use crate::futures::futures::channel::oneshot;
49use crate::futures::futures::task;
50use crate::futures::futures::{Future, StreamExt};
51use crate::futures::subscription;
52use crate::futures::{Executor, Runtime};
53use crate::graphics::{Compositor, Shell, compositor};
54use crate::runtime::font;
55use crate::runtime::image;
56use crate::runtime::system;
57use crate::runtime::user_interface::{self, UserInterface};
58use crate::runtime::{Action, Task};
59
60use program::Program;
61
62use rustc_hash::FxHashMap;
63use std::borrow::Cow;
64use std::mem::ManuallyDrop;
65use std::slice;
66use std::sync::Arc;
67
68/// Runs a [`Program`] with the provided settings.
69pub fn run<P>(program: P) -> Result<(), Error>
70where
71    P: Program + 'static,
72    P::Theme: theme::Base,
73{
74    use winit::event_loop::EventLoop;
75
76    let boot_span = debug::boot();
77    let settings = program.settings();
78    let window_settings = program.window();
79
80    let event_loop = EventLoop::with_user_event()
81        .build()
82        .expect("Create event loop");
83
84    let backend_settings = backend::Settings::from(&settings);
85    let renderer_settings = renderer::Settings::from(&settings);
86    let display_handle = event_loop.owned_display_handle();
87
88    let (proxy, worker) = Proxy::new(event_loop.create_proxy());
89
90    #[cfg(feature = "debug")]
91    {
92        let proxy = proxy.clone();
93
94        debug::on_hotpatch(move || {
95            proxy.send_action(Action::Reload);
96        });
97    }
98
99    let mut runtime = {
100        let executor = P::Executor::new().map_err(Error::ExecutorCreationFailed)?;
101        executor.spawn(worker);
102
103        Runtime::new(executor, proxy.clone())
104    };
105
106    let (program, task) = runtime.enter(|| program::Instance::new(program));
107    let is_daemon = window_settings.is_none();
108
109    let task = if let Some(window_settings) = window_settings {
110        let mut task = Some(task);
111
112        let (_id, open) = runtime::window::open(window_settings);
113
114        open.then(move |_| task.take().unwrap_or_else(Task::none))
115    } else {
116        task
117    };
118
119    if let Some(stream) = runtime::task::into_stream(task) {
120        runtime.run(stream);
121    }
122
123    runtime.track(subscription::into_recipes(
124        runtime.enter(|| program.subscription().map(Action::Output)),
125    ));
126
127    let (event_sender, event_receiver) = mpsc::unbounded();
128    let (control_sender, control_receiver) = mpsc::unbounded();
129    let (system_theme_sender, system_theme_receiver) = oneshot::channel();
130
131    let instance = Box::pin(run_instance::<P>(
132        program,
133        runtime,
134        proxy.clone(),
135        event_receiver,
136        control_sender,
137        display_handle,
138        is_daemon,
139        backend_settings,
140        renderer_settings,
141        settings.fonts,
142        system_theme_receiver,
143    ));
144
145    let context = task::Context::from_waker(task::noop_waker_ref());
146
147    struct Runner<Message: 'static, F> {
148        instance: std::pin::Pin<Box<F>>,
149        context: task::Context<'static>,
150        id: Option<String>,
151        sender: mpsc::UnboundedSender<Event<Action<Message>>>,
152        receiver: mpsc::UnboundedReceiver<Control>,
153        error: Option<Error>,
154        system_theme: Option<oneshot::Sender<theme::Mode>>,
155
156        #[cfg(target_arch = "wasm32")]
157        canvas: Option<web_sys::HtmlCanvasElement>,
158    }
159
160    let runner = Runner {
161        instance,
162        context,
163        id: settings.id,
164        sender: event_sender,
165        receiver: control_receiver,
166        error: None,
167        system_theme: Some(system_theme_sender),
168
169        #[cfg(target_arch = "wasm32")]
170        canvas: None,
171    };
172
173    boot_span.finish();
174
175    impl<Message, F> winit::application::ApplicationHandler<Action<Message>> for Runner<Message, F>
176    where
177        F: Future<Output = ()>,
178    {
179        fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
180            if let Some(sender) = self.system_theme.take() {
181                let _ = sender.send(
182                    event_loop
183                        .system_theme()
184                        .map(conversion::theme_mode)
185                        .unwrap_or_default(),
186                );
187            }
188        }
189
190        fn new_events(
191            &mut self,
192            event_loop: &winit::event_loop::ActiveEventLoop,
193            cause: winit::event::StartCause,
194        ) {
195            self.process_event(
196                event_loop,
197                Event::EventLoopAwakened(winit::event::Event::NewEvents(cause)),
198            );
199        }
200
201        fn window_event(
202            &mut self,
203            event_loop: &winit::event_loop::ActiveEventLoop,
204            window_id: winit::window::WindowId,
205            event: winit::event::WindowEvent,
206        ) {
207            #[cfg(target_os = "windows")]
208            let is_move_or_resize = matches!(
209                event,
210                winit::event::WindowEvent::Resized(_) | winit::event::WindowEvent::Moved(_)
211            );
212
213            self.process_event(
214                event_loop,
215                Event::EventLoopAwakened(winit::event::Event::WindowEvent { window_id, event }),
216            );
217
218            // TODO: Remove when unnecessary
219            // On Windows, we emulate an `AboutToWait` event after every `Resized` event
220            // since the event loop does not resume during resize interaction.
221            // More details: https://github.com/rust-windowing/winit/issues/3272
222            #[cfg(target_os = "windows")]
223            {
224                if is_move_or_resize {
225                    self.process_event(
226                        event_loop,
227                        Event::EventLoopAwakened(winit::event::Event::AboutToWait),
228                    );
229                }
230            }
231        }
232
233        fn user_event(
234            &mut self,
235            event_loop: &winit::event_loop::ActiveEventLoop,
236            action: Action<Message>,
237        ) {
238            self.process_event(
239                event_loop,
240                Event::EventLoopAwakened(winit::event::Event::UserEvent(action)),
241            );
242        }
243
244        fn received_url(&mut self, event_loop: &winit::event_loop::ActiveEventLoop, url: String) {
245            self.process_event(
246                event_loop,
247                Event::EventLoopAwakened(winit::event::Event::PlatformSpecific(
248                    winit::event::PlatformSpecific::MacOS(winit::event::MacOS::ReceivedUrl(url)),
249                )),
250            );
251        }
252
253        fn about_to_wait(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
254            self.process_event(
255                event_loop,
256                Event::EventLoopAwakened(winit::event::Event::AboutToWait),
257            );
258        }
259    }
260
261    impl<Message, F> Runner<Message, F>
262    where
263        F: Future<Output = ()>,
264    {
265        fn process_event(
266            &mut self,
267            event_loop: &winit::event_loop::ActiveEventLoop,
268            event: Event<Action<Message>>,
269        ) {
270            if event_loop.exiting() {
271                return;
272            }
273
274            self.sender.start_send(event).expect("Send event");
275
276            loop {
277                let poll = self.instance.as_mut().poll(&mut self.context);
278
279                match poll {
280                    task::Poll::Pending => match self.receiver.try_recv() {
281                        Ok(control) => match control {
282                            Control::ChangeFlow(flow) => {
283                                use winit::event_loop::ControlFlow;
284
285                                match (event_loop.control_flow(), flow) {
286                                    (
287                                        ControlFlow::WaitUntil(current),
288                                        ControlFlow::WaitUntil(new),
289                                    ) if current < new => {}
290                                    (ControlFlow::WaitUntil(target), ControlFlow::Wait)
291                                        if target > Instant::now() => {}
292                                    _ => {
293                                        event_loop.set_control_flow(flow);
294                                    }
295                                }
296                            }
297                            Control::CreateWindow {
298                                id,
299                                settings,
300                                title,
301                                scale_factor,
302                                monitor,
303                                on_open,
304                            } => {
305                                let exit_on_close_request = settings.exit_on_close_request;
306
307                                let visible = settings.visible;
308
309                                #[cfg(target_arch = "wasm32")]
310                                let target = settings.platform_specific.target.clone();
311
312                                let window_attributes = conversion::window_attributes(
313                                    settings,
314                                    &title,
315                                    scale_factor,
316                                    monitor.or(event_loop.primary_monitor()),
317                                    self.id.clone(),
318                                )
319                                .with_visible(false);
320
321                                #[cfg(target_arch = "wasm32")]
322                                let window_attributes = {
323                                    use winit::platform::web::WindowAttributesExtWebSys;
324                                    window_attributes.with_canvas(self.canvas.take())
325                                };
326
327                                log::info!(
328                                    "Window attributes for id `{id:#?}`: {window_attributes:#?}"
329                                );
330
331                                // On macOS, the `position` in `WindowAttributes` represents the "inner"
332                                // position of the window; while on other platforms it's the "outer" position.
333                                // We fix the inconsistency on macOS by positioning the window after creation.
334                                #[cfg(target_os = "macos")]
335                                let mut window_attributes = window_attributes;
336
337                                #[cfg(target_os = "macos")]
338                                let position = window_attributes.position.take();
339
340                                let window = event_loop
341                                    .create_window(window_attributes)
342                                    .expect("Create window");
343
344                                #[cfg(target_os = "macos")]
345                                if let Some(position) = position {
346                                    window.set_outer_position(position);
347                                }
348
349                                #[cfg(target_arch = "wasm32")]
350                                {
351                                    use winit::platform::web::WindowExtWebSys;
352
353                                    let canvas = window.canvas().expect("Get window canvas");
354
355                                    let _ = canvas.set_attribute(
356                                        "style",
357                                        "display: block; width: 100%; height: 100%",
358                                    );
359
360                                    let window = web_sys::window().unwrap();
361                                    let document = window.document().unwrap();
362                                    let body = document.body().unwrap();
363
364                                    let target = target.and_then(|target| {
365                                        body.query_selector(&format!("#{target}"))
366                                            .ok()
367                                            .unwrap_or(None)
368                                    });
369
370                                    match target {
371                                        Some(node) => {
372                                            let _ = node.replace_with_with_node_1(&canvas).expect(
373                                                &format!("Could not replace #{}", node.id()),
374                                            );
375                                        }
376                                        None => {
377                                            let _ = body
378                                                .append_child(&canvas)
379                                                .expect("Append canvas to HTML body");
380                                        }
381                                    };
382                                }
383
384                                self.process_event(
385                                    event_loop,
386                                    Event::WindowCreated {
387                                        id,
388                                        window: Arc::new(window),
389                                        exit_on_close_request,
390                                        make_visible: visible,
391                                        on_open,
392                                    },
393                                );
394                            }
395                            Control::Exit => {
396                                self.process_event(event_loop, Event::Exit);
397                                event_loop.exit();
398                                break;
399                            }
400                            Control::Crash(error) => {
401                                self.error = Some(error);
402                                event_loop.exit();
403                            }
404                            Control::SetAutomaticWindowTabbing(_enabled) => {
405                                #[cfg(target_os = "macos")]
406                                {
407                                    use winit::platform::macos::ActiveEventLoopExtMacOS;
408                                    event_loop.set_allows_automatic_window_tabbing(_enabled);
409                                }
410                            }
411                        },
412                        _ => {
413                            break;
414                        }
415                    },
416                    task::Poll::Ready(_) => {
417                        event_loop.exit();
418                        break;
419                    }
420                };
421            }
422        }
423    }
424
425    #[cfg(not(target_arch = "wasm32"))]
426    {
427        let mut runner = runner;
428        let _ = event_loop.run_app(&mut runner);
429
430        runner.error.map(Err).unwrap_or(Ok(()))
431    }
432
433    #[cfg(target_arch = "wasm32")]
434    {
435        use winit::platform::web::EventLoopExtWebSys;
436        let _ = event_loop.spawn_app(runner);
437
438        Ok(())
439    }
440}
441
442#[derive(Debug)]
443enum Event<Message: 'static> {
444    WindowCreated {
445        id: window::Id,
446        window: Arc<winit::window::Window>,
447        exit_on_close_request: bool,
448        make_visible: bool,
449        on_open: oneshot::Sender<window::Id>,
450    },
451    EventLoopAwakened(winit::event::Event<Message>),
452    Exit,
453}
454
455#[derive(Debug)]
456enum Control {
457    ChangeFlow(winit::event_loop::ControlFlow),
458    Exit,
459    Crash(Error),
460    CreateWindow {
461        id: window::Id,
462        settings: window::Settings,
463        title: String,
464        monitor: Option<winit::monitor::MonitorHandle>,
465        on_open: oneshot::Sender<window::Id>,
466        scale_factor: f32,
467    },
468    SetAutomaticWindowTabbing(bool),
469}
470
471async fn run_instance<P>(
472    mut program: program::Instance<P>,
473    mut runtime: Runtime<P::Executor, Proxy<P::Message>, Action<P::Message>>,
474    mut proxy: Proxy<P::Message>,
475    mut event_receiver: mpsc::UnboundedReceiver<Event<Action<P::Message>>>,
476    mut control_sender: mpsc::UnboundedSender<Control>,
477    display_handle: winit::event_loop::OwnedDisplayHandle,
478    is_daemon: bool,
479    backend_settings: backend::Settings,
480    mut renderer_settings: renderer::Settings,
481    default_fonts: Vec<Cow<'static, [u8]>>,
482    mut _system_theme: oneshot::Receiver<theme::Mode>,
483) where
484    P: Program + 'static,
485    P::Theme: theme::Base,
486{
487    use winit::event;
488    use winit::event_loop::ControlFlow;
489
490    let mut window_manager = window::Manager::new();
491    let mut is_window_opening = !is_daemon;
492
493    let mut compositor = None;
494    let mut events = Vec::new();
495    let mut messages = Vec::new();
496    let mut actions = 0;
497
498    let mut ui_caches = FxHashMap::default();
499    let mut user_interfaces = ManuallyDrop::new(FxHashMap::default());
500    let mut clipboard = Clipboard::new();
501
502    #[cfg(all(feature = "linux-theme-detection", target_os = "linux"))]
503    let mut system_theme = {
504        let to_mode = |color_scheme| match color_scheme {
505            mundy::ColorScheme::NoPreference => theme::Mode::None,
506            mundy::ColorScheme::Light => theme::Mode::Light,
507            mundy::ColorScheme::Dark => theme::Mode::Dark,
508        };
509
510        runtime.run(
511            mundy::Preferences::stream(mundy::Interest::ColorScheme)
512                .map(move |preferences| {
513                    Action::System(system::Action::NotifyTheme(to_mode(
514                        preferences.color_scheme,
515                    )))
516                })
517                .boxed(),
518        );
519
520        runtime
521            .enter(|| {
522                mundy::Preferences::once_blocking(
523                    mundy::Interest::ColorScheme,
524                    core::time::Duration::from_millis(200),
525                )
526            })
527            .map(|preferences| to_mode(preferences.color_scheme))
528            .unwrap_or_default()
529    };
530
531    #[cfg(not(all(feature = "linux-theme-detection", target_os = "linux")))]
532    let mut system_theme = _system_theme.try_recv().ok().flatten().unwrap_or_default();
533
534    log::info!("System theme: {system_theme:?}");
535
536    'next_event: loop {
537        // Empty the queue if possible
538        let event = if let Ok(event) = event_receiver.try_recv() {
539            Some(event)
540        } else {
541            event_receiver.next().await
542        };
543
544        let Some(event) = event else {
545            break;
546        };
547
548        match event {
549            Event::WindowCreated {
550                id,
551                window,
552                exit_on_close_request,
553                make_visible,
554                on_open,
555            } => {
556                if compositor.is_none() {
557                    let (compositor_sender, compositor_receiver) = oneshot::channel();
558
559                    let create_compositor = {
560                        let window = window.clone();
561                        let backend_settings = backend_settings.clone();
562                        let display_handle = display_handle.clone();
563                        let proxy = proxy.clone();
564                        let default_fonts = default_fonts.clone();
565
566                        async move {
567                            let shell = Shell::new(proxy.clone());
568
569                            let mut compositor =
570                                <P::Renderer as compositor::Default>::Compositor::new(
571                                    backend_settings,
572                                    display_handle,
573                                    window,
574                                    shell,
575                                )
576                                .await;
577
578                            if let Ok(compositor) = &mut compositor {
579                                for font in default_fonts {
580                                    compositor.load_font(font.clone());
581                                }
582                            }
583
584                            compositor_sender
585                                .send(compositor)
586                                .ok()
587                                .expect("Send compositor");
588
589                            // HACK! Send a proxy event on completion to trigger
590                            // a runtime re-poll
591                            // TODO: Send compositor through proxy (?)
592                            {
593                                let (sender, _receiver) = oneshot::channel();
594
595                                proxy.send_action(Action::Window(
596                                    runtime::window::Action::GetLatest(sender),
597                                ));
598                            }
599                        }
600                    };
601
602                    #[cfg(target_arch = "wasm32")]
603                    wasm_bindgen_futures::spawn_local(create_compositor);
604
605                    #[cfg(not(target_arch = "wasm32"))]
606                    runtime.block_on(create_compositor);
607
608                    match compositor_receiver.await.expect("Wait for compositor") {
609                        Ok(new_compositor) => {
610                            compositor = Some(new_compositor);
611                        }
612                        Err(error) => {
613                            let _ = control_sender.start_send(Control::Crash(error.into()));
614                            continue;
615                        }
616                    }
617                }
618
619                let window_theme = window
620                    .theme()
621                    .map(conversion::theme_mode)
622                    .unwrap_or_default();
623
624                if system_theme != window_theme {
625                    system_theme = window_theme;
626
627                    runtime.broadcast(subscription::Event::SystemThemeChanged(window_theme));
628                }
629
630                let is_first = window_manager.is_empty();
631                let window = window_manager.insert(
632                    id,
633                    window,
634                    &program,
635                    compositor.as_mut().expect("Compositor must be initialized"),
636                    proxy.clone(),
637                    renderer_settings,
638                    exit_on_close_request,
639                    system_theme,
640                );
641
642                window
643                    .raw
644                    .set_theme(conversion::window_theme(window.state.theme_mode()));
645
646                debug::theme_changed(|| {
647                    if is_first {
648                        theme::Base::seed(window.state.theme())
649                    } else {
650                        None
651                    }
652                });
653
654                let logical_size = window.state.logical_size();
655
656                #[cfg(feature = "hinting")]
657                window.renderer.hint(window.state.scale_factor());
658
659                let _ = user_interfaces.insert(
660                    id,
661                    build_user_interface(
662                        &program,
663                        user_interface::Cache::default(),
664                        &mut window.renderer,
665                        logical_size,
666                        id,
667                    ),
668                );
669                let _ = ui_caches.insert(id, user_interface::Cache::default());
670
671                if make_visible {
672                    window.raw.set_visible(true);
673                }
674
675                events.push((
676                    id,
677                    core::Event::Window(window::Event::Opened {
678                        position: window.position(),
679                        size: window.state.logical_size(),
680                        scale_factor: window.raw.scale_factor() as f32,
681                    }),
682                ));
683
684                let _ = on_open.send(id);
685                is_window_opening = false;
686            }
687            Event::EventLoopAwakened(event) => {
688                match event {
689                    event::Event::NewEvents(event::StartCause::Init) => {
690                        for (_id, window) in window_manager.iter_mut() {
691                            window.raw.request_redraw();
692                        }
693                    }
694                    event::Event::NewEvents(event::StartCause::ResumeTimeReached { .. }) => {
695                        let now = Instant::now();
696
697                        for (_id, window) in window_manager.iter_mut() {
698                            if let Some(redraw_at) = window.redraw_at
699                                && redraw_at <= now
700                            {
701                                window.raw.request_redraw();
702                                window.redraw_at = None;
703                            }
704                        }
705
706                        if let Some(redraw_at) = window_manager.redraw_at() {
707                            let _ = control_sender
708                                .start_send(Control::ChangeFlow(ControlFlow::WaitUntil(redraw_at)));
709                        } else {
710                            let _ =
711                                control_sender.start_send(Control::ChangeFlow(ControlFlow::Wait));
712                        }
713                    }
714                    event::Event::PlatformSpecific(event::PlatformSpecific::MacOS(
715                        event::MacOS::ReceivedUrl(url),
716                    )) => {
717                        runtime.broadcast(subscription::Event::PlatformSpecific(
718                            subscription::PlatformSpecific::MacOS(
719                                subscription::MacOS::ReceivedUrl(url),
720                            ),
721                        ));
722                    }
723                    event::Event::UserEvent(action) => {
724                        run_action(
725                            action,
726                            &program,
727                            &proxy,
728                            &mut runtime,
729                            &mut compositor,
730                            &mut events,
731                            &mut messages,
732                            &mut clipboard,
733                            &mut control_sender,
734                            &mut user_interfaces,
735                            &mut window_manager,
736                            &mut ui_caches,
737                            &mut is_window_opening,
738                            &mut system_theme,
739                            &mut renderer_settings,
740                        );
741                        actions += 1;
742                    }
743                    event::Event::WindowEvent {
744                        window_id: id,
745                        event: event::WindowEvent::RedrawRequested,
746                        ..
747                    } => {
748                        let Some(mut current_compositor) = compositor.as_mut() else {
749                            continue;
750                        };
751
752                        let Some((id, mut window)) = window_manager.get_mut_alias(id) else {
753                            continue;
754                        };
755
756                        let physical_size = window.state.physical_size();
757                        let mut logical_size = window.state.logical_size();
758
759                        if physical_size.width == 0 || physical_size.height == 0 {
760                            continue;
761                        }
762
763                        // Window was resized between redraws
764                        if window.surface_version != window.state.surface_version() {
765                            #[cfg(feature = "hinting")]
766                            window.renderer.hint(window.state.scale_factor());
767
768                            let ui = user_interfaces.remove(&id).expect("Remove user interface");
769
770                            let layout_span = debug::layout(id);
771                            let _ = user_interfaces
772                                .insert(id, ui.relayout(logical_size, &mut window.renderer));
773                            layout_span.finish();
774
775                            current_compositor.configure_surface(
776                                &mut window.surface,
777                                physical_size.width,
778                                physical_size.height,
779                            );
780
781                            window.surface_version = window.state.surface_version();
782                        }
783
784                        let redraw_event =
785                            core::Event::Window(window::Event::RedrawRequested(Instant::now()));
786
787                        let cursor = window.state.cursor();
788
789                        let mut interface =
790                            user_interfaces.get_mut(&id).expect("Get user interface");
791
792                        let interact_span = debug::interact(id);
793                        let mut redraw_count = 0;
794
795                        let state = loop {
796                            let message_count = messages.len();
797                            let (state, _) = interface.update(
798                                &window.raw,
799                                &window.waker,
800                                slice::from_ref(&redraw_event),
801                                cursor,
802                                &mut window.renderer,
803                                &mut messages,
804                            );
805
806                            if message_count == messages.len() && !state.has_layout_changed() {
807                                break state;
808                            }
809
810                            if redraw_count >= 2 {
811                                log::warn!(
812                                    "More than 3 consecutive RedrawRequested events \
813                                    produced layout invalidation"
814                                );
815
816                                break state;
817                            }
818
819                            redraw_count += 1;
820
821                            if !messages.is_empty() {
822                                let caches: FxHashMap<_, _> =
823                                    ManuallyDrop::into_inner(user_interfaces)
824                                        .into_iter()
825                                        .map(|(id, interface)| (id, interface.into_cache()))
826                                        .collect();
827
828                                let actions = update(&mut program, &mut runtime, &mut messages);
829
830                                user_interfaces = ManuallyDrop::new(build_user_interfaces(
831                                    &program,
832                                    &mut window_manager,
833                                    caches,
834                                ));
835
836                                for action in actions {
837                                    // Defer all window actions to avoid compositor
838                                    // race conditions while redrawing
839                                    if let Action::Window(_) = action {
840                                        proxy.send_action(action);
841                                        continue;
842                                    }
843
844                                    run_action(
845                                        action,
846                                        &program,
847                                        &proxy,
848                                        &mut runtime,
849                                        &mut compositor,
850                                        &mut events,
851                                        &mut messages,
852                                        &mut clipboard,
853                                        &mut control_sender,
854                                        &mut user_interfaces,
855                                        &mut window_manager,
856                                        &mut ui_caches,
857                                        &mut is_window_opening,
858                                        &mut system_theme,
859                                        &mut renderer_settings,
860                                    );
861                                }
862
863                                for (window_id, window) in window_manager.iter_mut() {
864                                    // We are already redrawing this window
865                                    if window_id == id {
866                                        continue;
867                                    }
868
869                                    window.raw.request_redraw();
870                                }
871
872                                let Some(next_compositor) = compositor.as_mut() else {
873                                    continue 'next_event;
874                                };
875
876                                current_compositor = next_compositor;
877                                window = window_manager.get_mut(id).unwrap();
878
879                                // Window scale factor changed during a redraw request
880                                if logical_size != window.state.logical_size() {
881                                    logical_size = window.state.logical_size();
882
883                                    log::debug!(
884                                        "Window scale factor changed during a redraw request"
885                                    );
886
887                                    let ui =
888                                        user_interfaces.remove(&id).expect("Remove user interface");
889
890                                    let layout_span = debug::layout(id);
891                                    let _ = user_interfaces.insert(
892                                        id,
893                                        ui.relayout(logical_size, &mut window.renderer),
894                                    );
895                                    layout_span.finish();
896                                }
897
898                                interface = user_interfaces.get_mut(&id).unwrap();
899                            }
900                        };
901                        interact_span.finish();
902
903                        let draw_span = debug::draw(id);
904                        interface.draw(
905                            &mut window.renderer,
906                            window.state.theme(),
907                            &renderer::Style {
908                                text_color: window.state.text_color(),
909                            },
910                            cursor,
911                        );
912                        draw_span.finish();
913
914                        if let user_interface::State::Updated {
915                            redraw_request,
916                            input_method,
917                            mouse_interaction,
918                            clipboard: clipboard_requests,
919                            ..
920                        } = state
921                        {
922                            window.request_redraw(redraw_request);
923                            window.request_input_method(input_method);
924                            window.update_mouse(mouse_interaction);
925
926                            run_clipboard(&mut proxy, &mut clipboard, clipboard_requests, id);
927                        }
928
929                        runtime.broadcast(subscription::Event::Interaction {
930                            window: id,
931                            event: redraw_event,
932                            status: core::event::Status::Ignored,
933                        });
934
935                        window.draw_preedit();
936
937                        let present_span = debug::present(id);
938                        match current_compositor.present(
939                            &mut window.renderer,
940                            &mut window.surface,
941                            window.state.viewport(),
942                            window.state.background_color(),
943                            || window.raw.pre_present_notify(),
944                        ) {
945                            Ok(()) => {
946                                present_span.finish();
947                            }
948                            Err(error) => match error {
949                                compositor::SurfaceError::OutOfMemory => {
950                                    // This is an unrecoverable error.
951                                    panic!("{error:?}");
952                                }
953                                compositor::SurfaceError::Outdated
954                                | compositor::SurfaceError::Lost => {
955                                    present_span.finish();
956
957                                    // Reconfigure surface and try redrawing
958                                    let physical_size = window.state.physical_size();
959
960                                    if error == compositor::SurfaceError::Lost {
961                                        window.surface = current_compositor.create_surface(
962                                            window.raw.clone(),
963                                            physical_size.width,
964                                            physical_size.height,
965                                        );
966                                    } else {
967                                        current_compositor.configure_surface(
968                                            &mut window.surface,
969                                            physical_size.width,
970                                            physical_size.height,
971                                        );
972                                    }
973
974                                    window.raw.request_redraw();
975                                }
976                                compositor::SurfaceError::Occluded => {
977                                    present_span.finish();
978
979                                    // Do nothing and wait for window to become visible again
980                                }
981                                _ => {
982                                    present_span.finish();
983
984                                    log::error!("Error {error:?} when presenting surface.");
985
986                                    // Try rendering all windows again next frame.
987                                    for (_id, window) in window_manager.iter_mut() {
988                                        window.raw.request_redraw();
989                                    }
990                                }
991                            },
992                        }
993                    }
994                    event::Event::WindowEvent {
995                        event: window_event,
996                        window_id,
997                    } => {
998                        if !is_daemon
999                            && matches!(window_event, winit::event::WindowEvent::Destroyed)
1000                            && !is_window_opening
1001                            && window_manager.is_empty()
1002                        {
1003                            control_sender
1004                                .start_send(Control::Exit)
1005                                .expect("Send control action");
1006
1007                            continue;
1008                        }
1009
1010                        let Some((id, window)) = window_manager.get_mut_alias(window_id) else {
1011                            continue;
1012                        };
1013
1014                        match window_event {
1015                            winit::event::WindowEvent::Resized(_)
1016                            | winit::event::WindowEvent::Occluded(false) => {
1017                                window.raw.request_redraw();
1018                            }
1019                            winit::event::WindowEvent::ThemeChanged(theme) => {
1020                                let mode = conversion::theme_mode(theme);
1021
1022                                if mode != system_theme {
1023                                    system_theme = mode;
1024
1025                                    runtime
1026                                        .broadcast(subscription::Event::SystemThemeChanged(mode));
1027                                }
1028                            }
1029                            _ => {}
1030                        }
1031
1032                        if matches!(window_event, winit::event::WindowEvent::CloseRequested)
1033                            && window.exit_on_close_request
1034                        {
1035                            run_action(
1036                                Action::Window(runtime::window::Action::Close(id)),
1037                                &program,
1038                                &proxy,
1039                                &mut runtime,
1040                                &mut compositor,
1041                                &mut events,
1042                                &mut messages,
1043                                &mut clipboard,
1044                                &mut control_sender,
1045                                &mut user_interfaces,
1046                                &mut window_manager,
1047                                &mut ui_caches,
1048                                &mut is_window_opening,
1049                                &mut system_theme,
1050                                &mut renderer_settings,
1051                            );
1052                        } else {
1053                            window.state.update(&program, &window.raw, &window_event);
1054
1055                            if let Some(event) = conversion::window_event(
1056                                window_event,
1057                                window.state.scale_factor(),
1058                                window.state.modifiers(),
1059                            ) {
1060                                events.push((id, event));
1061                            }
1062                        }
1063                    }
1064                    event::Event::AboutToWait => {
1065                        if actions > 0 {
1066                            proxy.free_slots(actions);
1067                            actions = 0;
1068                        }
1069
1070                        if events.is_empty() && messages.is_empty() && window_manager.is_idle() {
1071                            continue;
1072                        }
1073
1074                        let mut uis_stale = false;
1075
1076                        for (id, window) in window_manager.iter_mut() {
1077                            let interact_span = debug::interact(id);
1078                            let mut window_events = vec![];
1079
1080                            events.retain(|(window_id, event)| {
1081                                if *window_id == id {
1082                                    window_events.push(event.clone());
1083                                    false
1084                                } else {
1085                                    true
1086                                }
1087                            });
1088
1089                            if window_events.is_empty() {
1090                                continue;
1091                            }
1092
1093                            let (ui_state, statuses) = user_interfaces
1094                                .get_mut(&id)
1095                                .expect("Get user interface")
1096                                .update(
1097                                    &window.raw,
1098                                    &window.waker,
1099                                    &window_events,
1100                                    window.state.cursor(),
1101                                    &mut window.renderer,
1102                                    &mut messages,
1103                                );
1104
1105                            #[cfg(feature = "unconditional-rendering")]
1106                            window.request_redraw(window::RedrawRequest::NextFrame);
1107
1108                            match ui_state {
1109                                user_interface::State::Updated {
1110                                    redraw_request: _redraw_request,
1111                                    mouse_interaction,
1112                                    clipboard: clipboard_requests,
1113                                    ..
1114                                } => {
1115                                    window.update_mouse(mouse_interaction);
1116
1117                                    #[cfg(not(feature = "unconditional-rendering"))]
1118                                    window.request_redraw(_redraw_request);
1119
1120                                    run_clipboard(
1121                                        &mut proxy,
1122                                        &mut clipboard,
1123                                        clipboard_requests,
1124                                        id,
1125                                    );
1126                                }
1127                                user_interface::State::Outdated => {
1128                                    uis_stale = true;
1129                                }
1130                            }
1131
1132                            for (event, status) in window_events.into_iter().zip(statuses) {
1133                                runtime.broadcast(subscription::Event::Interaction {
1134                                    window: id,
1135                                    event,
1136                                    status,
1137                                });
1138                            }
1139
1140                            interact_span.finish();
1141                        }
1142
1143                        for (id, event) in events.drain(..) {
1144                            runtime.broadcast(subscription::Event::Interaction {
1145                                window: id,
1146                                event,
1147                                status: core::event::Status::Ignored,
1148                            });
1149                        }
1150
1151                        if !messages.is_empty() || uis_stale {
1152                            let cached_interfaces: FxHashMap<_, _> =
1153                                ManuallyDrop::into_inner(user_interfaces)
1154                                    .into_iter()
1155                                    .map(|(id, ui)| (id, ui.into_cache()))
1156                                    .collect();
1157
1158                            let actions = update(&mut program, &mut runtime, &mut messages);
1159
1160                            user_interfaces = ManuallyDrop::new(build_user_interfaces(
1161                                &program,
1162                                &mut window_manager,
1163                                cached_interfaces,
1164                            ));
1165
1166                            for action in actions {
1167                                run_action(
1168                                    action,
1169                                    &program,
1170                                    &proxy,
1171                                    &mut runtime,
1172                                    &mut compositor,
1173                                    &mut events,
1174                                    &mut messages,
1175                                    &mut clipboard,
1176                                    &mut control_sender,
1177                                    &mut user_interfaces,
1178                                    &mut window_manager,
1179                                    &mut ui_caches,
1180                                    &mut is_window_opening,
1181                                    &mut system_theme,
1182                                    &mut renderer_settings,
1183                                );
1184                            }
1185
1186                            for (_id, window) in window_manager.iter_mut() {
1187                                window.raw.request_redraw();
1188                            }
1189                        }
1190
1191                        if let Some(redraw_at) = window_manager.redraw_at() {
1192                            let _ = control_sender
1193                                .start_send(Control::ChangeFlow(ControlFlow::WaitUntil(redraw_at)));
1194                        } else {
1195                            let _ =
1196                                control_sender.start_send(Control::ChangeFlow(ControlFlow::Wait));
1197                        }
1198                    }
1199                    _ => {}
1200                }
1201            }
1202            Event::Exit => break,
1203        }
1204    }
1205
1206    let _ = ManuallyDrop::into_inner(user_interfaces);
1207}
1208
1209/// Builds a window's [`UserInterface`] for the [`Program`].
1210fn build_user_interface<'a, P: Program>(
1211    program: &'a program::Instance<P>,
1212    cache: user_interface::Cache,
1213    renderer: &mut P::Renderer,
1214    size: Size,
1215    id: window::Id,
1216) -> UserInterface<'a, P::Message, P::Theme, P::Renderer>
1217where
1218    P::Theme: theme::Base,
1219{
1220    let view_span = debug::view(id);
1221    let view = program.view(id);
1222    view_span.finish();
1223
1224    let layout_span = debug::layout(id);
1225    let user_interface = UserInterface::build(view, size, cache, renderer);
1226    layout_span.finish();
1227
1228    user_interface
1229}
1230
1231fn update<P: Program, E: Executor>(
1232    program: &mut program::Instance<P>,
1233    runtime: &mut Runtime<E, Proxy<P::Message>, Action<P::Message>>,
1234    messages: &mut Vec<P::Message>,
1235) -> Vec<Action<P::Message>>
1236where
1237    P::Theme: theme::Base,
1238{
1239    use futures::futures;
1240
1241    let mut actions = Vec::new();
1242    let mut outputs = Vec::new();
1243
1244    while !messages.is_empty() {
1245        for message in messages.drain(..) {
1246            let task = runtime.enter(|| program.update(message));
1247
1248            if let Some(mut stream) = runtime::task::into_stream(task) {
1249                let waker = futures::task::noop_waker_ref();
1250                let mut context = futures::task::Context::from_waker(waker);
1251
1252                // Run immediately available actions synchronously (e.g. widget operations)
1253                loop {
1254                    match runtime.enter(|| stream.poll_next_unpin(&mut context)) {
1255                        futures::task::Poll::Ready(Some(Action::Output(output))) => {
1256                            outputs.push(output);
1257                        }
1258                        futures::task::Poll::Ready(Some(action)) => {
1259                            actions.push(action);
1260                        }
1261                        futures::task::Poll::Ready(None) => {
1262                            break;
1263                        }
1264                        futures::task::Poll::Pending => {
1265                            runtime.run(stream);
1266                            break;
1267                        }
1268                    }
1269                }
1270            }
1271        }
1272
1273        messages.append(&mut outputs);
1274    }
1275
1276    let subscription = runtime.enter(|| program.subscription());
1277    let recipes = subscription::into_recipes(subscription.map(Action::Output));
1278
1279    runtime.track(recipes);
1280
1281    actions
1282}
1283
1284fn run_action<'a, P, C>(
1285    action: Action<P::Message>,
1286    program: &'a program::Instance<P>,
1287    _proxy: &Proxy<P::Message>,
1288    runtime: &mut Runtime<P::Executor, Proxy<P::Message>, Action<P::Message>>,
1289    compositor: &mut Option<C>,
1290    events: &mut Vec<(window::Id, core::Event)>,
1291    messages: &mut Vec<P::Message>,
1292    clipboard: &mut Clipboard,
1293    control_sender: &mut mpsc::UnboundedSender<Control>,
1294    interfaces: &mut FxHashMap<window::Id, UserInterface<'a, P::Message, P::Theme, P::Renderer>>,
1295    window_manager: &mut window::Manager<P, C>,
1296    ui_caches: &mut FxHashMap<window::Id, user_interface::Cache>,
1297    is_window_opening: &mut bool,
1298    system_theme: &mut theme::Mode,
1299    renderer_settings: &mut renderer::Settings,
1300) where
1301    P: Program,
1302    C: Compositor<Renderer = P::Renderer> + 'static,
1303    P::Theme: theme::Base,
1304{
1305    use crate::core::Renderer as _;
1306    use crate::runtime::backend;
1307    use crate::runtime::clipboard;
1308    use crate::runtime::window;
1309
1310    match action {
1311        Action::Output(message) => {
1312            messages.push(message);
1313        }
1314        Action::Clipboard(action) => match action {
1315            clipboard::Action::Read { kind, channel } => {
1316                clipboard.read(kind, move |result| {
1317                    let _ = channel.send(result);
1318                });
1319            }
1320            clipboard::Action::Write { content, channel } => {
1321                clipboard.write(content, move |result| {
1322                    let _ = channel.send(result);
1323                });
1324            }
1325        },
1326        Action::Window(action) => match action {
1327            window::Action::Open(id, settings, channel) => {
1328                let monitor = window_manager.last_monitor();
1329
1330                control_sender
1331                    .start_send(Control::CreateWindow {
1332                        id,
1333                        settings,
1334                        title: program.title(id),
1335                        scale_factor: program.scale_factor(id),
1336                        monitor,
1337                        on_open: channel,
1338                    })
1339                    .expect("Send control action");
1340
1341                *is_window_opening = true;
1342            }
1343            window::Action::Close(id) => {
1344                let _ = ui_caches.remove(&id);
1345                let _ = interfaces.remove(&id);
1346
1347                if window_manager.remove(id).is_some() {
1348                    events.push((id, core::Event::Window(core::window::Event::Closed)));
1349                }
1350
1351                if window_manager.is_empty() {
1352                    *compositor = None;
1353                }
1354            }
1355            window::Action::GetOldest(channel) => {
1356                let id = window_manager.iter_mut().next().map(|(id, _window)| id);
1357
1358                let _ = channel.send(id);
1359            }
1360            window::Action::GetLatest(channel) => {
1361                let id = window_manager.iter_mut().last().map(|(id, _window)| id);
1362
1363                let _ = channel.send(id);
1364            }
1365            window::Action::Drag(id) => {
1366                if let Some(window) = window_manager.get_mut(id) {
1367                    let _ = window.raw.drag_window();
1368                }
1369            }
1370            window::Action::DragResize(id, direction) => {
1371                if let Some(window) = window_manager.get_mut(id) {
1372                    let _ = window
1373                        .raw
1374                        .drag_resize_window(conversion::resize_direction(direction));
1375                }
1376            }
1377            window::Action::Resize(id, size) => {
1378                if let Some(window) = window_manager.get_mut(id) {
1379                    let _ = window.raw.request_inner_size(
1380                        winit::dpi::LogicalSize {
1381                            width: size.width,
1382                            height: size.height,
1383                        }
1384                        .to_physical::<f32>(f64::from(window.state.scale_factor())),
1385                    );
1386                }
1387            }
1388            window::Action::SetMinSize(id, size) => {
1389                if let Some(window) = window_manager.get_mut(id) {
1390                    window.raw.set_min_inner_size(size.map(|size| {
1391                        winit::dpi::LogicalSize {
1392                            width: size.width,
1393                            height: size.height,
1394                        }
1395                        .to_physical::<f32>(f64::from(window.state.scale_factor()))
1396                    }));
1397                }
1398            }
1399            window::Action::SetMaxSize(id, size) => {
1400                if let Some(window) = window_manager.get_mut(id) {
1401                    window.raw.set_max_inner_size(size.map(|size| {
1402                        winit::dpi::LogicalSize {
1403                            width: size.width,
1404                            height: size.height,
1405                        }
1406                        .to_physical::<f32>(f64::from(window.state.scale_factor()))
1407                    }));
1408                }
1409            }
1410            window::Action::SetResizeIncrements(id, increments) => {
1411                if let Some(window) = window_manager.get_mut(id) {
1412                    window.raw.set_resize_increments(increments.map(|size| {
1413                        winit::dpi::LogicalSize {
1414                            width: size.width,
1415                            height: size.height,
1416                        }
1417                        .to_physical::<f32>(f64::from(window.state.scale_factor()))
1418                    }));
1419                }
1420            }
1421            window::Action::SetResizable(id, resizable) => {
1422                if let Some(window) = window_manager.get_mut(id) {
1423                    window.raw.set_resizable(resizable);
1424                }
1425            }
1426            window::Action::GetSize(id, channel) => {
1427                if let Some(window) = window_manager.get_mut(id) {
1428                    let size = window.state.logical_size();
1429                    let _ = channel.send(Size::new(size.width, size.height));
1430                }
1431            }
1432            window::Action::GetMaximized(id, channel) => {
1433                if let Some(window) = window_manager.get_mut(id) {
1434                    let _ = channel.send(window.raw.is_maximized());
1435                }
1436            }
1437            window::Action::Maximize(id, maximized) => {
1438                if let Some(window) = window_manager.get_mut(id) {
1439                    window.raw.set_maximized(maximized);
1440                }
1441            }
1442            window::Action::GetMinimized(id, channel) => {
1443                if let Some(window) = window_manager.get_mut(id) {
1444                    let _ = channel.send(window.raw.is_minimized());
1445                }
1446            }
1447            window::Action::Minimize(id, minimized) => {
1448                if let Some(window) = window_manager.get_mut(id) {
1449                    window.raw.set_minimized(minimized);
1450                }
1451            }
1452            window::Action::GetPosition(id, channel) => {
1453                if let Some(window) = window_manager.get(id) {
1454                    let position = window
1455                        .raw
1456                        .outer_position()
1457                        .map(|position| {
1458                            let position = position.to_logical::<f32>(window.raw.scale_factor());
1459
1460                            Point::new(position.x, position.y)
1461                        })
1462                        .ok();
1463
1464                    let _ = channel.send(position);
1465                }
1466            }
1467            window::Action::GetScaleFactor(id, channel) => {
1468                if let Some(window) = window_manager.get_mut(id) {
1469                    let scale_factor = window.raw.scale_factor();
1470
1471                    let _ = channel.send(scale_factor as f32);
1472                }
1473            }
1474            window::Action::Move(id, position) => {
1475                if let Some(window) = window_manager.get_mut(id) {
1476                    window.raw.set_outer_position(winit::dpi::LogicalPosition {
1477                        x: position.x,
1478                        y: position.y,
1479                    });
1480                }
1481            }
1482            window::Action::SetMode(id, mode) => {
1483                if let Some(window) = window_manager.get_mut(id) {
1484                    window.raw.set_visible(conversion::visible(mode));
1485                    window
1486                        .raw
1487                        .set_fullscreen(conversion::fullscreen(window.raw.current_monitor(), mode));
1488                }
1489            }
1490            window::Action::SetIcon(id, icon) => {
1491                if let Some(window) = window_manager.get_mut(id) {
1492                    window.raw.set_window_icon(conversion::icon(icon));
1493                }
1494            }
1495            window::Action::GetMode(id, channel) => {
1496                if let Some(window) = window_manager.get_mut(id) {
1497                    let mode = if window.raw.is_visible().unwrap_or(true) {
1498                        conversion::mode(window.raw.fullscreen())
1499                    } else {
1500                        core::window::Mode::Hidden
1501                    };
1502
1503                    let _ = channel.send(mode);
1504                }
1505            }
1506            window::Action::ToggleMaximize(id) => {
1507                if let Some(window) = window_manager.get_mut(id) {
1508                    window.raw.set_maximized(!window.raw.is_maximized());
1509                }
1510            }
1511            window::Action::ToggleDecorations(id) => {
1512                if let Some(window) = window_manager.get_mut(id) {
1513                    window.raw.set_decorations(!window.raw.is_decorated());
1514                }
1515            }
1516            window::Action::RequestUserAttention(id, attention_type) => {
1517                if let Some(window) = window_manager.get_mut(id) {
1518                    window
1519                        .raw
1520                        .request_user_attention(attention_type.map(conversion::user_attention));
1521                }
1522            }
1523            window::Action::GainFocus(id) => {
1524                if let Some(window) = window_manager.get_mut(id) {
1525                    window.raw.focus_window();
1526                }
1527            }
1528            window::Action::SetLevel(id, level) => {
1529                if let Some(window) = window_manager.get_mut(id) {
1530                    window.raw.set_window_level(conversion::window_level(level));
1531                }
1532            }
1533            window::Action::ShowSystemMenu(id) => {
1534                if let Some(window) = window_manager.get_mut(id)
1535                    && let mouse::Cursor::Available(point) = window.state.cursor()
1536                {
1537                    window.raw.show_window_menu(winit::dpi::LogicalPosition {
1538                        x: point.x,
1539                        y: point.y,
1540                    });
1541                }
1542            }
1543            window::Action::GetRawId(id, channel) => {
1544                if let Some(window) = window_manager.get_mut(id) {
1545                    let _ = channel.send(window.raw.id().into());
1546                }
1547            }
1548            window::Action::Run(id, f) => {
1549                if let Some(window) = window_manager.get_mut(id) {
1550                    f(&window.raw);
1551                }
1552            }
1553            window::Action::Screenshot(id, channel) => {
1554                if let Some(window) = window_manager.get_mut(id)
1555                    && let Some(compositor) = compositor
1556                {
1557                    let bytes = compositor.screenshot(
1558                        &mut window.renderer,
1559                        window.state.viewport(),
1560                        window.state.background_color(),
1561                    );
1562
1563                    let _ = channel.send(core::window::Screenshot::new(
1564                        bytes,
1565                        window.state.physical_size(),
1566                        window.state.scale_factor(),
1567                    ));
1568                }
1569            }
1570            window::Action::EnableMousePassthrough(id) => {
1571                if let Some(window) = window_manager.get_mut(id) {
1572                    let _ = window.raw.set_cursor_hittest(false);
1573                }
1574            }
1575            window::Action::DisableMousePassthrough(id) => {
1576                if let Some(window) = window_manager.get_mut(id) {
1577                    let _ = window.raw.set_cursor_hittest(true);
1578                }
1579            }
1580            window::Action::GetMonitorSize(id, channel) => {
1581                if let Some(window) = window_manager.get(id) {
1582                    let size = window.raw.current_monitor().map(|monitor| {
1583                        let scale = window.state.scale_factor();
1584                        let size = monitor.size().to_logical(f64::from(scale));
1585
1586                        Size::new(size.width, size.height)
1587                    });
1588
1589                    let _ = channel.send(size);
1590                }
1591            }
1592            window::Action::SetAllowAutomaticTabbing(enabled) => {
1593                control_sender
1594                    .start_send(Control::SetAutomaticWindowTabbing(enabled))
1595                    .expect("Send control action");
1596            }
1597            window::Action::RedrawAll => {
1598                for (_id, window) in window_manager.iter_mut() {
1599                    window.raw.request_redraw();
1600                }
1601            }
1602            window::Action::RelayoutAll => {
1603                for (id, window) in window_manager.iter_mut() {
1604                    if let Some(ui) = interfaces.remove(&id) {
1605                        let _ = interfaces.insert(
1606                            id,
1607                            ui.relayout(window.state.logical_size(), &mut window.renderer),
1608                        );
1609                    }
1610
1611                    window.raw.request_redraw();
1612                }
1613            }
1614        },
1615        Action::System(action) => match action {
1616            system::Action::GetInformation(_channel) => {
1617                #[cfg(feature = "sysinfo")]
1618                {
1619                    if let Some(compositor) = compositor {
1620                        let graphics_info = compositor.information();
1621
1622                        let _ = std::thread::spawn(move || {
1623                            let information = system_information(graphics_info);
1624
1625                            let _ = _channel.send(information);
1626                        });
1627                    }
1628                }
1629            }
1630            system::Action::GetTheme(channel) => {
1631                let _ = channel.send(*system_theme);
1632            }
1633            system::Action::NotifyTheme(mode) => {
1634                if mode != *system_theme {
1635                    *system_theme = mode;
1636
1637                    runtime.broadcast(subscription::Event::SystemThemeChanged(mode));
1638                }
1639
1640                let Some(theme) = conversion::window_theme(mode) else {
1641                    return;
1642                };
1643
1644                for (_id, window) in window_manager.iter_mut() {
1645                    window.state.update(
1646                        program,
1647                        &window.raw,
1648                        &winit::event::WindowEvent::ThemeChanged(theme),
1649                    );
1650                }
1651            }
1652        },
1653        Action::Font(action) => match action {
1654            font::Action::Load { bytes, channel } => {
1655                if let Some(compositor) = compositor {
1656                    let result = compositor.load_font(bytes.clone());
1657                    let _ = channel.send(result);
1658                }
1659            }
1660            font::Action::List { channel } => {
1661                if let Some(compositor) = compositor {
1662                    let fonts = compositor.list_fonts();
1663                    let _ = channel.send(fonts);
1664                }
1665            }
1666            font::Action::SetDefaults { font, text_size } => {
1667                renderer_settings.default_font = font;
1668                renderer_settings.default_text_size = text_size;
1669
1670                let Some(compositor) = compositor else {
1671                    return;
1672                };
1673
1674                // Recreate renderers and relayout all windows
1675                for (id, window) in window_manager.iter_mut() {
1676                    window.renderer = compositor.create_renderer(*renderer_settings);
1677
1678                    let Some(ui) = interfaces.remove(&id) else {
1679                        continue;
1680                    };
1681
1682                    let size = window.state.logical_size();
1683                    let ui = ui.relayout(size, &mut window.renderer);
1684                    let _ = interfaces.insert(id, ui);
1685
1686                    window.raw.request_redraw();
1687                }
1688            }
1689        },
1690        Action::Widget(operation) => {
1691            let mut current_operation = Some(operation);
1692
1693            while let Some(mut operation) = current_operation.take() {
1694                for (id, ui) in interfaces.iter_mut() {
1695                    if let Some(window) = window_manager.get_mut(*id) {
1696                        ui.operate(&window.renderer, operation.as_mut());
1697                    }
1698                }
1699
1700                match operation.finish() {
1701                    operation::Outcome::None => {}
1702                    operation::Outcome::Some(()) => {}
1703                    operation::Outcome::Chain(next) => {
1704                        current_operation = Some(next);
1705                    }
1706                }
1707            }
1708
1709            // Redraw all windows
1710            for (_, window) in window_manager.iter_mut() {
1711                window.raw.request_redraw();
1712            }
1713        }
1714        Action::Image(action) => match action {
1715            image::Action::Allocate(handle, sender) => {
1716                // TODO: Shared image cache in compositor
1717                if let Some((_id, window)) = window_manager.iter_mut().next() {
1718                    window.renderer.allocate_image(&handle, move |allocation| {
1719                        let _ = sender.send(allocation);
1720                    });
1721                }
1722            }
1723        },
1724        Action::Backend(action) => match action {
1725            #[cfg(not(target_arch = "wasm32"))]
1726            backend::Action::Configure(settings, sender) => {
1727                let shell = Shell::new(_proxy.clone());
1728
1729                let mut new_compositor = if let Some(window) = window_manager.first() {
1730                    match runtime.block_on(C::new(
1731                        settings,
1732                        window.raw.clone(),
1733                        window.raw.clone(),
1734                        shell,
1735                    )) {
1736                        Ok(compositor) => compositor,
1737                        Err(error) => {
1738                            let _ = sender.send(Err(error));
1739                            return;
1740                        }
1741                    }
1742                } else {
1743                    return;
1744                };
1745
1746                graphics::cache::invalidate_all();
1747
1748                window_manager.replace_with(|mut window| {
1749                    let size = window.state.physical_size();
1750
1751                    drop(window.renderer);
1752                    drop(window.surface);
1753
1754                    window.renderer = new_compositor.create_renderer(*renderer_settings);
1755                    window.surface =
1756                        new_compositor.create_surface(window.raw.clone(), size.width, size.height);
1757
1758                    window
1759                });
1760
1761                *compositor = Some(new_compositor);
1762
1763                let _ = sender.send(Ok(()));
1764            }
1765            #[cfg(target_arch = "wasm32")]
1766            backend::Action::Configure(_, _) => {}
1767        },
1768        Action::Event { window, event } => {
1769            events.push((window, event));
1770        }
1771        Action::Tick => {
1772            for (_id, window) in window_manager.iter_mut() {
1773                window.renderer.tick();
1774            }
1775        }
1776        Action::Reload => {
1777            for (id, window) in window_manager.iter_mut() {
1778                let Some(ui) = interfaces.remove(&id) else {
1779                    continue;
1780                };
1781
1782                let cache = ui.into_cache();
1783                let size = window.state.logical_size();
1784
1785                let _ = interfaces.insert(
1786                    id,
1787                    build_user_interface(program, cache, &mut window.renderer, size, id),
1788                );
1789
1790                window.raw.request_redraw();
1791            }
1792        }
1793        Action::Exit => {
1794            control_sender
1795                .start_send(Control::Exit)
1796                .expect("Send control action");
1797        }
1798    }
1799}
1800
1801/// Build the user interface for every window.
1802pub fn build_user_interfaces<'a, P: Program, C>(
1803    program: &'a program::Instance<P>,
1804    window_manager: &mut window::Manager<P, C>,
1805    mut cached_user_interfaces: FxHashMap<window::Id, user_interface::Cache>,
1806) -> FxHashMap<window::Id, UserInterface<'a, P::Message, P::Theme, P::Renderer>>
1807where
1808    C: Compositor<Renderer = P::Renderer>,
1809    P::Theme: theme::Base,
1810{
1811    for (id, window) in window_manager.iter_mut() {
1812        window.state.synchronize(program, id, &window.raw);
1813
1814        #[cfg(feature = "hinting")]
1815        window.renderer.hint(window.state.scale_factor());
1816    }
1817
1818    debug::theme_changed(|| {
1819        window_manager
1820            .first()
1821            .and_then(|window| theme::Base::seed(window.state.theme()))
1822    });
1823
1824    cached_user_interfaces
1825        .drain()
1826        .filter_map(|(id, cache)| {
1827            let window = window_manager.get_mut(id)?;
1828
1829            Some((
1830                id,
1831                build_user_interface(
1832                    program,
1833                    cache,
1834                    &mut window.renderer,
1835                    window.state.logical_size(),
1836                    id,
1837                ),
1838            ))
1839        })
1840        .collect()
1841}
1842
1843/// Returns true if the provided event should cause a [`Program`] to
1844/// exit.
1845pub fn user_force_quit(
1846    event: &winit::event::WindowEvent,
1847    _modifiers: winit::keyboard::ModifiersState,
1848) -> bool {
1849    match event {
1850        #[cfg(target_os = "macos")]
1851        winit::event::WindowEvent::KeyboardInput {
1852            event:
1853                winit::event::KeyEvent {
1854                    logical_key: winit::keyboard::Key::Character(c),
1855                    state: winit::event::ElementState::Pressed,
1856                    ..
1857                },
1858            ..
1859        } if c == "q" && _modifiers.super_key() => true,
1860        _ => false,
1861    }
1862}
1863
1864#[cfg(feature = "sysinfo")]
1865fn system_information(graphics: compositor::Information) -> system::Information {
1866    use sysinfo::{Process, System};
1867
1868    let mut system = System::new_all();
1869    system.refresh_all();
1870
1871    let cpu_brand = system
1872        .cpus()
1873        .first()
1874        .map(|cpu| cpu.brand().to_string())
1875        .unwrap_or_default();
1876
1877    let memory_used = sysinfo::get_current_pid()
1878        .and_then(|pid| system.process(pid).ok_or("Process not found"))
1879        .map(Process::memory)
1880        .ok();
1881
1882    system::Information {
1883        system_name: System::name(),
1884        system_kernel: System::kernel_version(),
1885        system_version: System::long_os_version(),
1886        system_short_version: System::os_version(),
1887        cpu_brand,
1888        cpu_cores: system.physical_core_count(),
1889        memory_total: system.total_memory(),
1890        memory_used,
1891        graphics_adapter: graphics.adapter,
1892        graphics_backend: graphics.backend,
1893    }
1894}
1895
1896fn run_clipboard<Message: Send>(
1897    proxy: &mut Proxy<Message>,
1898    clipboard: &mut Clipboard,
1899    requests: core::Clipboard,
1900    window: window::Id,
1901) {
1902    for kind in requests.reads {
1903        let proxy = proxy.clone();
1904
1905        clipboard.read(kind, move |result| {
1906            proxy.send_action(Action::Event {
1907                window,
1908                event: core::Event::Clipboard(core::clipboard::Event::Read(result.map(Arc::new))),
1909            });
1910        });
1911    }
1912
1913    if let Some(content) = requests.write {
1914        let proxy = proxy.clone();
1915
1916        clipboard.write(content, move |result| {
1917            proxy.send_action(Action::Event {
1918                window,
1919                event: core::Event::Clipboard(core::clipboard::Event::Written(result)),
1920            });
1921        });
1922    }
1923}