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