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, 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 let _ = user_interfaces.insert(
650 id,
651 build_user_interface(
652 &program,
653 user_interface::Cache::default(),
654 &mut window.renderer,
655 logical_size,
656 id,
657 ),
658 );
659 let _ = ui_caches.insert(id, user_interface::Cache::default());
660
661 if make_visible {
662 window.raw.set_visible(true);
663 }
664
665 events.push((
666 id,
667 core::Event::Window(window::Event::Opened {
668 position: window.position(),
669 size: window.logical_size(),
670 }),
671 ));
672
673 if clipboard.window_id().is_none() {
674 clipboard = Clipboard::connect(window.raw.clone());
675 }
676
677 let _ = on_open.send(id);
678 is_window_opening = false;
679 }
680 Event::EventLoopAwakened(event) => {
681 match event {
682 event::Event::NewEvents(event::StartCause::Init) => {
683 for (_id, window) in window_manager.iter_mut() {
684 window.raw.request_redraw();
685 }
686 }
687 event::Event::NewEvents(event::StartCause::ResumeTimeReached { .. }) => {
688 let now = Instant::now();
689
690 for (_id, window) in window_manager.iter_mut() {
691 if let Some(redraw_at) = window.redraw_at
692 && redraw_at <= now
693 {
694 window.raw.request_redraw();
695 window.redraw_at = None;
696 }
697 }
698
699 if let Some(redraw_at) = window_manager.redraw_at() {
700 let _ = control_sender
701 .start_send(Control::ChangeFlow(ControlFlow::WaitUntil(redraw_at)));
702 } else {
703 let _ =
704 control_sender.start_send(Control::ChangeFlow(ControlFlow::Wait));
705 }
706 }
707 event::Event::PlatformSpecific(event::PlatformSpecific::MacOS(
708 event::MacOS::ReceivedUrl(url),
709 )) => {
710 runtime.broadcast(subscription::Event::PlatformSpecific(
711 subscription::PlatformSpecific::MacOS(
712 subscription::MacOS::ReceivedUrl(url),
713 ),
714 ));
715 }
716 event::Event::UserEvent(action) => {
717 run_action(
718 action,
719 &program,
720 &mut runtime,
721 &mut compositor,
722 &mut events,
723 &mut messages,
724 &mut clipboard,
725 &mut control_sender,
726 &mut user_interfaces,
727 &mut window_manager,
728 &mut ui_caches,
729 &mut is_window_opening,
730 &mut system_theme,
731 );
732 actions += 1;
733 }
734 event::Event::WindowEvent {
735 window_id: id,
736 event: event::WindowEvent::RedrawRequested,
737 ..
738 } => {
739 let Some(mut current_compositor) = compositor.as_mut() else {
740 continue;
741 };
742
743 let Some((id, mut window)) = window_manager.get_mut_alias(id) else {
744 continue;
745 };
746
747 let physical_size = window.state.physical_size();
748 let mut logical_size = window.state.logical_size();
749
750 if physical_size.width == 0 || physical_size.height == 0 {
751 continue;
752 }
753
754 if window.surface_version != window.state.surface_version() {
756 let ui = user_interfaces.remove(&id).expect("Remove user interface");
757
758 let layout_span = debug::layout(id);
759 let _ = user_interfaces
760 .insert(id, ui.relayout(logical_size, &mut window.renderer));
761 layout_span.finish();
762
763 current_compositor.configure_surface(
764 &mut window.surface,
765 physical_size.width,
766 physical_size.height,
767 );
768
769 window.surface_version = window.state.surface_version();
770 }
771
772 let redraw_event =
773 core::Event::Window(window::Event::RedrawRequested(Instant::now()));
774
775 let cursor = window.state.cursor();
776
777 let mut interface =
778 user_interfaces.get_mut(&id).expect("Get user interface");
779
780 let interact_span = debug::interact(id);
781 let mut redraw_count = 0;
782
783 let state = loop {
784 let message_count = messages.len();
785 let (state, _) = interface.update(
786 slice::from_ref(&redraw_event),
787 cursor,
788 &mut window.renderer,
789 &mut clipboard,
790 &mut messages,
791 );
792
793 if message_count == messages.len() && !state.has_layout_changed() {
794 break state;
795 }
796
797 if redraw_count >= 2 {
798 log::warn!(
799 "More than 3 consecutive RedrawRequested events \
800 produced layout invalidation"
801 );
802
803 break state;
804 }
805
806 redraw_count += 1;
807
808 if !messages.is_empty() {
809 let caches: FxHashMap<_, _> =
810 ManuallyDrop::into_inner(user_interfaces)
811 .into_iter()
812 .map(|(id, interface)| (id, interface.into_cache()))
813 .collect();
814
815 let actions = update(&mut program, &mut runtime, &mut messages);
816
817 user_interfaces = ManuallyDrop::new(build_user_interfaces(
818 &program,
819 &mut window_manager,
820 caches,
821 ));
822
823 for action in actions {
824 if let Action::Window(_) = action {
827 proxy.send_action(action);
828 continue;
829 }
830
831 run_action(
832 action,
833 &program,
834 &mut runtime,
835 &mut compositor,
836 &mut events,
837 &mut messages,
838 &mut clipboard,
839 &mut control_sender,
840 &mut user_interfaces,
841 &mut window_manager,
842 &mut ui_caches,
843 &mut is_window_opening,
844 &mut system_theme,
845 );
846 }
847
848 for (window_id, window) in window_manager.iter_mut() {
849 if window_id == id {
851 continue;
852 }
853
854 window.raw.request_redraw();
855 }
856
857 let Some(next_compositor) = compositor.as_mut() else {
858 continue 'next_event;
859 };
860
861 current_compositor = next_compositor;
862 window = window_manager.get_mut(id).unwrap();
863
864 if logical_size != window.state.logical_size() {
866 logical_size = window.state.logical_size();
867
868 log::debug!(
869 "Window scale factor changed during a redraw request"
870 );
871
872 let ui =
873 user_interfaces.remove(&id).expect("Remove user interface");
874
875 let layout_span = debug::layout(id);
876 let _ = user_interfaces.insert(
877 id,
878 ui.relayout(logical_size, &mut window.renderer),
879 );
880 layout_span.finish();
881 }
882
883 interface = user_interfaces.get_mut(&id).unwrap();
884 }
885 };
886 interact_span.finish();
887
888 let draw_span = debug::draw(id);
889 interface.draw(
890 &mut window.renderer,
891 window.state.theme(),
892 &renderer::Style {
893 text_color: window.state.text_color(),
894 },
895 cursor,
896 );
897 draw_span.finish();
898
899 if let user_interface::State::Updated {
900 redraw_request,
901 input_method,
902 mouse_interaction,
903 ..
904 } = state
905 {
906 window.request_redraw(redraw_request);
907 window.request_input_method(input_method);
908 window.update_mouse(mouse_interaction);
909 }
910
911 runtime.broadcast(subscription::Event::Interaction {
912 window: id,
913 event: redraw_event,
914 status: core::event::Status::Ignored,
915 });
916
917 window.draw_preedit();
918
919 let present_span = debug::present(id);
920 match current_compositor.present(
921 &mut window.renderer,
922 &mut window.surface,
923 window.state.viewport(),
924 window.state.background_color(),
925 || window.raw.pre_present_notify(),
926 ) {
927 Ok(()) => {
928 present_span.finish();
929 }
930 Err(error) => match error {
931 compositor::SurfaceError::OutOfMemory => {
932 panic!("{error:?}");
934 }
935 compositor::SurfaceError::Outdated
936 | compositor::SurfaceError::Lost => {
937 present_span.finish();
938
939 let physical_size = window.state.physical_size();
941
942 if error == compositor::SurfaceError::Lost {
943 window.surface = current_compositor.create_surface(
944 window.raw.clone(),
945 physical_size.width,
946 physical_size.height,
947 );
948 } else {
949 current_compositor.configure_surface(
950 &mut window.surface,
951 physical_size.width,
952 physical_size.height,
953 );
954 }
955
956 window.raw.request_redraw();
957 }
958 _ => {
959 present_span.finish();
960
961 log::error!(
962 "Error {error:?} when \
963 presenting surface."
964 );
965
966 for (_id, window) in window_manager.iter_mut() {
968 window.raw.request_redraw();
969 }
970 }
971 },
972 }
973 }
974 event::Event::WindowEvent {
975 event: window_event,
976 window_id,
977 } => {
978 if !is_daemon
979 && matches!(window_event, winit::event::WindowEvent::Destroyed)
980 && !is_window_opening
981 && window_manager.is_empty()
982 {
983 control_sender
984 .start_send(Control::Exit)
985 .expect("Send control action");
986
987 continue;
988 }
989
990 let Some((id, window)) = window_manager.get_mut_alias(window_id) else {
991 continue;
992 };
993
994 match window_event {
995 winit::event::WindowEvent::Resized(_) => {
996 window.raw.request_redraw();
997 }
998 winit::event::WindowEvent::ThemeChanged(theme) => {
999 let mode = conversion::theme_mode(theme);
1000
1001 if mode != system_theme {
1002 system_theme = mode;
1003
1004 runtime
1005 .broadcast(subscription::Event::SystemThemeChanged(mode));
1006 }
1007 }
1008 _ => {}
1009 }
1010
1011 if matches!(window_event, winit::event::WindowEvent::CloseRequested)
1012 && window.exit_on_close_request
1013 {
1014 run_action(
1015 Action::Window(runtime::window::Action::Close(id)),
1016 &program,
1017 &mut runtime,
1018 &mut compositor,
1019 &mut events,
1020 &mut messages,
1021 &mut clipboard,
1022 &mut control_sender,
1023 &mut user_interfaces,
1024 &mut window_manager,
1025 &mut ui_caches,
1026 &mut is_window_opening,
1027 &mut system_theme,
1028 );
1029 } else {
1030 window.state.update(&program, &window.raw, &window_event);
1031
1032 if let Some(event) = conversion::window_event(
1033 window_event,
1034 window.state.scale_factor(),
1035 window.state.modifiers(),
1036 ) {
1037 events.push((id, event));
1038 }
1039 }
1040 }
1041 event::Event::AboutToWait => {
1042 if actions > 0 {
1043 proxy.free_slots(actions);
1044 actions = 0;
1045 }
1046
1047 if events.is_empty() && messages.is_empty() && window_manager.is_idle() {
1048 continue;
1049 }
1050
1051 let mut uis_stale = false;
1052
1053 for (id, window) in window_manager.iter_mut() {
1054 let interact_span = debug::interact(id);
1055 let mut window_events = vec![];
1056
1057 events.retain(|(window_id, event)| {
1058 if *window_id == id {
1059 window_events.push(event.clone());
1060 false
1061 } else {
1062 true
1063 }
1064 });
1065
1066 if window_events.is_empty() && messages.is_empty() {
1067 continue;
1068 }
1069
1070 let (ui_state, statuses) = user_interfaces
1071 .get_mut(&id)
1072 .expect("Get user interface")
1073 .update(
1074 &window_events,
1075 window.state.cursor(),
1076 &mut window.renderer,
1077 &mut clipboard,
1078 &mut messages,
1079 );
1080
1081 #[cfg(feature = "unconditional-rendering")]
1082 window.request_redraw(window::RedrawRequest::NextFrame);
1083
1084 match ui_state {
1085 user_interface::State::Updated {
1086 redraw_request: _redraw_request,
1087 mouse_interaction,
1088 ..
1089 } => {
1090 window.update_mouse(mouse_interaction);
1091
1092 #[cfg(not(feature = "unconditional-rendering"))]
1093 window.request_redraw(_redraw_request);
1094 }
1095 user_interface::State::Outdated => {
1096 uis_stale = true;
1097 }
1098 }
1099
1100 for (event, status) in
1101 window_events.into_iter().zip(statuses.into_iter())
1102 {
1103 runtime.broadcast(subscription::Event::Interaction {
1104 window: id,
1105 event,
1106 status,
1107 });
1108 }
1109
1110 interact_span.finish();
1111 }
1112
1113 for (id, event) in events.drain(..) {
1114 runtime.broadcast(subscription::Event::Interaction {
1115 window: id,
1116 event,
1117 status: core::event::Status::Ignored,
1118 });
1119 }
1120
1121 if !messages.is_empty() || uis_stale {
1122 let cached_interfaces: FxHashMap<_, _> =
1123 ManuallyDrop::into_inner(user_interfaces)
1124 .into_iter()
1125 .map(|(id, ui)| (id, ui.into_cache()))
1126 .collect();
1127
1128 let actions = update(&mut program, &mut runtime, &mut messages);
1129
1130 user_interfaces = ManuallyDrop::new(build_user_interfaces(
1131 &program,
1132 &mut window_manager,
1133 cached_interfaces,
1134 ));
1135
1136 for action in actions {
1137 run_action(
1138 action,
1139 &program,
1140 &mut runtime,
1141 &mut compositor,
1142 &mut events,
1143 &mut messages,
1144 &mut clipboard,
1145 &mut control_sender,
1146 &mut user_interfaces,
1147 &mut window_manager,
1148 &mut ui_caches,
1149 &mut is_window_opening,
1150 &mut system_theme,
1151 );
1152 }
1153
1154 for (_id, window) in window_manager.iter_mut() {
1155 window.raw.request_redraw();
1156 }
1157 }
1158
1159 if let Some(redraw_at) = window_manager.redraw_at() {
1160 let _ = control_sender
1161 .start_send(Control::ChangeFlow(ControlFlow::WaitUntil(redraw_at)));
1162 } else {
1163 let _ =
1164 control_sender.start_send(Control::ChangeFlow(ControlFlow::Wait));
1165 }
1166 }
1167 _ => {}
1168 }
1169 }
1170 Event::Exit => break,
1171 }
1172 }
1173
1174 let _ = ManuallyDrop::into_inner(user_interfaces);
1175}
1176
1177fn build_user_interface<'a, P: Program>(
1179 program: &'a program::Instance<P>,
1180 cache: user_interface::Cache,
1181 renderer: &mut P::Renderer,
1182 size: Size,
1183 id: window::Id,
1184) -> UserInterface<'a, P::Message, P::Theme, P::Renderer>
1185where
1186 P::Theme: theme::Base,
1187{
1188 let view_span = debug::view(id);
1189 let view = program.view(id);
1190 view_span.finish();
1191
1192 let layout_span = debug::layout(id);
1193 let user_interface = UserInterface::build(view, size, cache, renderer);
1194 layout_span.finish();
1195
1196 user_interface
1197}
1198
1199fn update<P: Program, E: Executor>(
1200 program: &mut program::Instance<P>,
1201 runtime: &mut Runtime<E, Proxy<P::Message>, Action<P::Message>>,
1202 messages: &mut Vec<P::Message>,
1203) -> Vec<Action<P::Message>>
1204where
1205 P::Theme: theme::Base,
1206{
1207 use futures::futures;
1208
1209 let mut actions = Vec::new();
1210
1211 for message in messages.drain(..) {
1212 let task = runtime.enter(|| program.update(message));
1213
1214 if let Some(mut stream) = runtime::task::into_stream(task) {
1215 let waker = futures::task::noop_waker_ref();
1216 let mut context = futures::task::Context::from_waker(waker);
1217
1218 loop {
1220 match runtime.enter(|| stream.poll_next_unpin(&mut context)) {
1221 futures::task::Poll::Ready(Some(action)) => {
1222 actions.push(action);
1223 }
1224 futures::task::Poll::Ready(None) => {
1225 break;
1226 }
1227 futures::task::Poll::Pending => {
1228 runtime.run(stream);
1229 break;
1230 }
1231 }
1232 }
1233 }
1234 }
1235
1236 let subscription = runtime.enter(|| program.subscription());
1237 let recipes = subscription::into_recipes(subscription.map(Action::Output));
1238
1239 runtime.track(recipes);
1240
1241 actions
1242}
1243
1244fn run_action<'a, P, C>(
1245 action: Action<P::Message>,
1246 program: &'a program::Instance<P>,
1247 runtime: &mut Runtime<P::Executor, Proxy<P::Message>, Action<P::Message>>,
1248 compositor: &mut Option<C>,
1249 events: &mut Vec<(window::Id, core::Event)>,
1250 messages: &mut Vec<P::Message>,
1251 clipboard: &mut Clipboard,
1252 control_sender: &mut mpsc::UnboundedSender<Control>,
1253 interfaces: &mut FxHashMap<window::Id, UserInterface<'a, P::Message, P::Theme, P::Renderer>>,
1254 window_manager: &mut WindowManager<P, C>,
1255 ui_caches: &mut FxHashMap<window::Id, user_interface::Cache>,
1256 is_window_opening: &mut bool,
1257 system_theme: &mut theme::Mode,
1258) where
1259 P: Program,
1260 C: Compositor<Renderer = P::Renderer> + 'static,
1261 P::Theme: theme::Base,
1262{
1263 use crate::runtime::clipboard;
1264 use crate::runtime::window;
1265
1266 match action {
1267 Action::Output(message) => {
1268 messages.push(message);
1269 }
1270 Action::Clipboard(action) => match action {
1271 clipboard::Action::Read { target, channel } => {
1272 let _ = channel.send(clipboard.read(target));
1273 }
1274 clipboard::Action::Write { target, contents } => {
1275 clipboard.write(target, contents);
1276 }
1277 },
1278 Action::Window(action) => match action {
1279 window::Action::Open(id, settings, channel) => {
1280 let monitor = window_manager.last_monitor();
1281
1282 control_sender
1283 .start_send(Control::CreateWindow {
1284 id,
1285 settings,
1286 title: program.title(id),
1287 scale_factor: program.scale_factor(id),
1288 monitor,
1289 on_open: channel,
1290 })
1291 .expect("Send control action");
1292
1293 *is_window_opening = true;
1294 }
1295 window::Action::Close(id) => {
1296 let _ = ui_caches.remove(&id);
1297 let _ = interfaces.remove(&id);
1298
1299 if let Some(window) = window_manager.remove(id) {
1300 if clipboard.window_id() == Some(window.raw.id()) {
1301 *clipboard = window_manager
1302 .first()
1303 .map(|window| window.raw.clone())
1304 .map(Clipboard::connect)
1305 .unwrap_or_else(Clipboard::unconnected);
1306 }
1307
1308 events.push((id, core::Event::Window(core::window::Event::Closed)));
1309 }
1310
1311 if window_manager.is_empty() {
1312 *compositor = None;
1313 }
1314 }
1315 window::Action::GetOldest(channel) => {
1316 let id = window_manager.iter_mut().next().map(|(id, _window)| id);
1317
1318 let _ = channel.send(id);
1319 }
1320 window::Action::GetLatest(channel) => {
1321 let id = window_manager.iter_mut().last().map(|(id, _window)| id);
1322
1323 let _ = channel.send(id);
1324 }
1325 window::Action::Drag(id) => {
1326 if let Some(window) = window_manager.get_mut(id) {
1327 let _ = window.raw.drag_window();
1328 }
1329 }
1330 window::Action::DragResize(id, direction) => {
1331 if let Some(window) = window_manager.get_mut(id) {
1332 let _ = window
1333 .raw
1334 .drag_resize_window(conversion::resize_direction(direction));
1335 }
1336 }
1337 window::Action::Resize(id, size) => {
1338 if let Some(window) = window_manager.get_mut(id) {
1339 let _ = window.raw.request_inner_size(
1340 winit::dpi::LogicalSize {
1341 width: size.width,
1342 height: size.height,
1343 }
1344 .to_physical::<f32>(f64::from(window.state.scale_factor())),
1345 );
1346 }
1347 }
1348 window::Action::SetMinSize(id, size) => {
1349 if let Some(window) = window_manager.get_mut(id) {
1350 window.raw.set_min_inner_size(size.map(|size| {
1351 winit::dpi::LogicalSize {
1352 width: size.width,
1353 height: size.height,
1354 }
1355 .to_physical::<f32>(f64::from(window.state.scale_factor()))
1356 }));
1357 }
1358 }
1359 window::Action::SetMaxSize(id, size) => {
1360 if let Some(window) = window_manager.get_mut(id) {
1361 window.raw.set_max_inner_size(size.map(|size| {
1362 winit::dpi::LogicalSize {
1363 width: size.width,
1364 height: size.height,
1365 }
1366 .to_physical::<f32>(f64::from(window.state.scale_factor()))
1367 }));
1368 }
1369 }
1370 window::Action::SetResizeIncrements(id, increments) => {
1371 if let Some(window) = window_manager.get_mut(id) {
1372 window.raw.set_resize_increments(increments.map(|size| {
1373 winit::dpi::LogicalSize {
1374 width: size.width,
1375 height: size.height,
1376 }
1377 .to_physical::<f32>(f64::from(window.state.scale_factor()))
1378 }));
1379 }
1380 }
1381 window::Action::SetResizable(id, resizable) => {
1382 if let Some(window) = window_manager.get_mut(id) {
1383 window.raw.set_resizable(resizable);
1384 }
1385 }
1386 window::Action::GetSize(id, channel) => {
1387 if let Some(window) = window_manager.get_mut(id) {
1388 let size = window.logical_size();
1389 let _ = channel.send(Size::new(size.width, size.height));
1390 }
1391 }
1392 window::Action::GetMaximized(id, channel) => {
1393 if let Some(window) = window_manager.get_mut(id) {
1394 let _ = channel.send(window.raw.is_maximized());
1395 }
1396 }
1397 window::Action::Maximize(id, maximized) => {
1398 if let Some(window) = window_manager.get_mut(id) {
1399 window.raw.set_maximized(maximized);
1400 }
1401 }
1402 window::Action::GetMinimized(id, channel) => {
1403 if let Some(window) = window_manager.get_mut(id) {
1404 let _ = channel.send(window.raw.is_minimized());
1405 }
1406 }
1407 window::Action::Minimize(id, minimized) => {
1408 if let Some(window) = window_manager.get_mut(id) {
1409 window.raw.set_minimized(minimized);
1410 }
1411 }
1412 window::Action::GetPosition(id, channel) => {
1413 if let Some(window) = window_manager.get(id) {
1414 let position = window
1415 .raw
1416 .outer_position()
1417 .map(|position| {
1418 let position = position.to_logical::<f32>(window.raw.scale_factor());
1419
1420 Point::new(position.x, position.y)
1421 })
1422 .ok();
1423
1424 let _ = channel.send(position);
1425 }
1426 }
1427 window::Action::GetScaleFactor(id, channel) => {
1428 if let Some(window) = window_manager.get_mut(id) {
1429 let scale_factor = window.raw.scale_factor();
1430
1431 let _ = channel.send(scale_factor as f32);
1432 }
1433 }
1434 window::Action::Move(id, position) => {
1435 if let Some(window) = window_manager.get_mut(id) {
1436 window.raw.set_outer_position(winit::dpi::LogicalPosition {
1437 x: position.x,
1438 y: position.y,
1439 });
1440 }
1441 }
1442 window::Action::SetMode(id, mode) => {
1443 if let Some(window) = window_manager.get_mut(id) {
1444 window.raw.set_visible(conversion::visible(mode));
1445 window
1446 .raw
1447 .set_fullscreen(conversion::fullscreen(window.raw.current_monitor(), mode));
1448 }
1449 }
1450 window::Action::SetIcon(id, icon) => {
1451 if let Some(window) = window_manager.get_mut(id) {
1452 window.raw.set_window_icon(conversion::icon(icon));
1453 }
1454 }
1455 window::Action::GetMode(id, channel) => {
1456 if let Some(window) = window_manager.get_mut(id) {
1457 let mode = if window.raw.is_visible().unwrap_or(true) {
1458 conversion::mode(window.raw.fullscreen())
1459 } else {
1460 core::window::Mode::Hidden
1461 };
1462
1463 let _ = channel.send(mode);
1464 }
1465 }
1466 window::Action::ToggleMaximize(id) => {
1467 if let Some(window) = window_manager.get_mut(id) {
1468 window.raw.set_maximized(!window.raw.is_maximized());
1469 }
1470 }
1471 window::Action::ToggleDecorations(id) => {
1472 if let Some(window) = window_manager.get_mut(id) {
1473 window.raw.set_decorations(!window.raw.is_decorated());
1474 }
1475 }
1476 window::Action::RequestUserAttention(id, attention_type) => {
1477 if let Some(window) = window_manager.get_mut(id) {
1478 window
1479 .raw
1480 .request_user_attention(attention_type.map(conversion::user_attention));
1481 }
1482 }
1483 window::Action::GainFocus(id) => {
1484 if let Some(window) = window_manager.get_mut(id) {
1485 window.raw.focus_window();
1486 }
1487 }
1488 window::Action::SetLevel(id, level) => {
1489 if let Some(window) = window_manager.get_mut(id) {
1490 window.raw.set_window_level(conversion::window_level(level));
1491 }
1492 }
1493 window::Action::ShowSystemMenu(id) => {
1494 if let Some(window) = window_manager.get_mut(id)
1495 && let mouse::Cursor::Available(point) = window.state.cursor()
1496 {
1497 window.raw.show_window_menu(winit::dpi::LogicalPosition {
1498 x: point.x,
1499 y: point.y,
1500 });
1501 }
1502 }
1503 window::Action::GetRawId(id, channel) => {
1504 if let Some(window) = window_manager.get_mut(id) {
1505 let _ = channel.send(window.raw.id().into());
1506 }
1507 }
1508 window::Action::Run(id, f) => {
1509 if let Some(window) = window_manager.get_mut(id) {
1510 f(window);
1511 }
1512 }
1513 window::Action::Screenshot(id, channel) => {
1514 if let Some(window) = window_manager.get_mut(id)
1515 && let Some(compositor) = compositor
1516 {
1517 let bytes = compositor.screenshot(
1518 &mut window.renderer,
1519 window.state.viewport(),
1520 window.state.background_color(),
1521 );
1522
1523 let _ = channel.send(core::window::Screenshot::new(
1524 bytes,
1525 window.state.physical_size(),
1526 window.state.scale_factor(),
1527 ));
1528 }
1529 }
1530 window::Action::EnableMousePassthrough(id) => {
1531 if let Some(window) = window_manager.get_mut(id) {
1532 let _ = window.raw.set_cursor_hittest(false);
1533 }
1534 }
1535 window::Action::DisableMousePassthrough(id) => {
1536 if let Some(window) = window_manager.get_mut(id) {
1537 let _ = window.raw.set_cursor_hittest(true);
1538 }
1539 }
1540 window::Action::GetMonitorSize(id, channel) => {
1541 if let Some(window) = window_manager.get(id) {
1542 let size = window.raw.current_monitor().map(|monitor| {
1543 let scale = window.state.scale_factor();
1544 let size = monitor.size().to_logical(f64::from(scale));
1545
1546 Size::new(size.width, size.height)
1547 });
1548
1549 let _ = channel.send(size);
1550 }
1551 }
1552 window::Action::SetAllowAutomaticTabbing(enabled) => {
1553 control_sender
1554 .start_send(Control::SetAutomaticWindowTabbing(enabled))
1555 .expect("Send control action");
1556 }
1557 window::Action::RedrawAll => {
1558 for (_id, window) in window_manager.iter_mut() {
1559 window.raw.request_redraw();
1560 }
1561 }
1562 window::Action::RelayoutAll => {
1563 for (id, window) in window_manager.iter_mut() {
1564 if let Some(ui) = interfaces.remove(&id) {
1565 let _ = interfaces.insert(
1566 id,
1567 ui.relayout(window.state.logical_size(), &mut window.renderer),
1568 );
1569 }
1570
1571 window.raw.request_redraw();
1572 }
1573 }
1574 },
1575 Action::System(action) => match action {
1576 system::Action::GetInformation(_channel) => {
1577 #[cfg(feature = "sysinfo")]
1578 {
1579 if let Some(compositor) = compositor {
1580 let graphics_info = compositor.information();
1581
1582 let _ = std::thread::spawn(move || {
1583 let information = system_information(graphics_info);
1584
1585 let _ = _channel.send(information);
1586 });
1587 }
1588 }
1589 }
1590 system::Action::GetTheme(channel) => {
1591 let _ = channel.send(*system_theme);
1592 }
1593 system::Action::NotifyTheme(mode) => {
1594 if mode != *system_theme {
1595 *system_theme = mode;
1596
1597 runtime.broadcast(subscription::Event::SystemThemeChanged(mode));
1598 }
1599
1600 let Some(theme) = conversion::window_theme(mode) else {
1601 return;
1602 };
1603
1604 for (_id, window) in window_manager.iter_mut() {
1605 window.state.update(
1606 program,
1607 &window.raw,
1608 &winit::event::WindowEvent::ThemeChanged(theme),
1609 );
1610 }
1611 }
1612 },
1613 Action::Widget(operation) => {
1614 let mut current_operation = Some(operation);
1615
1616 while let Some(mut operation) = current_operation.take() {
1617 for (id, ui) in interfaces.iter_mut() {
1618 if let Some(window) = window_manager.get_mut(*id) {
1619 ui.operate(&window.renderer, operation.as_mut());
1620 }
1621 }
1622
1623 match operation.finish() {
1624 operation::Outcome::None => {}
1625 operation::Outcome::Some(()) => {}
1626 operation::Outcome::Chain(next) => {
1627 current_operation = Some(next);
1628 }
1629 }
1630 }
1631 }
1632 Action::Image(action) => match action {
1633 image::Action::Allocate(handle, sender) => {
1634 use core::Renderer as _;
1635
1636 if let Some((_id, window)) = window_manager.iter_mut().next() {
1638 window.renderer.allocate_image(&handle, move |allocation| {
1639 let _ = sender.send(allocation);
1640 });
1641 }
1642 }
1643 },
1644 Action::LoadFont { bytes, channel } => {
1645 if let Some(compositor) = compositor {
1646 compositor.load_font(bytes.clone());
1648
1649 let _ = channel.send(Ok(()));
1650 }
1651 }
1652 Action::Reload => {
1653 for (id, window) in window_manager.iter_mut() {
1654 let Some(ui) = interfaces.remove(&id) else {
1655 continue;
1656 };
1657
1658 let cache = ui.into_cache();
1659 let size = window.logical_size();
1660
1661 let _ = interfaces.insert(
1662 id,
1663 build_user_interface(program, cache, &mut window.renderer, size, id),
1664 );
1665
1666 window.raw.request_redraw();
1667 }
1668 }
1669 Action::Exit => {
1670 control_sender
1671 .start_send(Control::Exit)
1672 .expect("Send control action");
1673 }
1674 }
1675}
1676
1677pub fn build_user_interfaces<'a, P: Program, C>(
1679 program: &'a program::Instance<P>,
1680 window_manager: &mut WindowManager<P, C>,
1681 mut cached_user_interfaces: FxHashMap<window::Id, user_interface::Cache>,
1682) -> FxHashMap<window::Id, UserInterface<'a, P::Message, P::Theme, P::Renderer>>
1683where
1684 C: Compositor<Renderer = P::Renderer>,
1685 P::Theme: theme::Base,
1686{
1687 for (id, window) in window_manager.iter_mut() {
1688 window.state.synchronize(program, id, &window.raw);
1689 }
1690
1691 debug::theme_changed(|| {
1692 window_manager
1693 .first()
1694 .and_then(|window| theme::Base::palette(window.state.theme()))
1695 });
1696
1697 cached_user_interfaces
1698 .drain()
1699 .filter_map(|(id, cache)| {
1700 let window = window_manager.get_mut(id)?;
1701
1702 Some((
1703 id,
1704 build_user_interface(
1705 program,
1706 cache,
1707 &mut window.renderer,
1708 window.state.logical_size(),
1709 id,
1710 ),
1711 ))
1712 })
1713 .collect()
1714}
1715
1716pub fn user_force_quit(
1719 event: &winit::event::WindowEvent,
1720 _modifiers: winit::keyboard::ModifiersState,
1721) -> bool {
1722 match event {
1723 #[cfg(target_os = "macos")]
1724 winit::event::WindowEvent::KeyboardInput {
1725 event:
1726 winit::event::KeyEvent {
1727 logical_key: winit::keyboard::Key::Character(c),
1728 state: winit::event::ElementState::Pressed,
1729 ..
1730 },
1731 ..
1732 } if c == "q" && _modifiers.super_key() => true,
1733 _ => false,
1734 }
1735}
1736
1737#[cfg(feature = "sysinfo")]
1738fn system_information(graphics: compositor::Information) -> system::Information {
1739 use sysinfo::{Process, System};
1740
1741 let mut system = System::new_all();
1742 system.refresh_all();
1743
1744 let cpu_brand = system
1745 .cpus()
1746 .first()
1747 .map(|cpu| cpu.brand().to_string())
1748 .unwrap_or_default();
1749
1750 let memory_used = sysinfo::get_current_pid()
1751 .and_then(|pid| system.process(pid).ok_or("Process not found"))
1752 .map(Process::memory)
1753 .ok();
1754
1755 system::Information {
1756 system_name: System::name(),
1757 system_kernel: System::kernel_version(),
1758 system_version: System::long_os_version(),
1759 system_short_version: System::os_version(),
1760 cpu_brand,
1761 cpu_cores: system.physical_core_count(),
1762 memory_total: system.total_memory(),
1763 memory_used,
1764 graphics_adapter: graphics.adapter,
1765 graphics_backend: graphics.backend,
1766 }
1767}