1#![doc(html_favicon_url = "https://zng-ui.github.io/res/zng-logo-icon.png")]
2#![doc(html_logo_url = "https://zng-ui.github.io/res/zng-logo.png")]
3#![doc = include_str!(concat!("../", std::env!("CARGO_PKG_README")))]
88#![doc(test(no_crate_inject))]
89#![warn(missing_docs)]
90#![warn(unused_extern_crates)]
91
92use std::{
93 fmt, mem,
94 path::PathBuf,
95 thread,
96 time::{Duration, Instant},
97};
98
99use extensions::ViewExtensions;
100use gl::GlContextManager;
101use image_cache::ImageCache;
102use keyboard::KeyLocation;
103use util::WinitToPx;
104use winit::{
105 event::{DeviceEvent, WindowEvent},
106 event_loop::{ActiveEventLoop, EventLoop, EventLoopProxy},
107 keyboard::ModifiersState,
108 monitor::MonitorHandle,
109};
110
111#[cfg(not(target_os = "android"))]
112use winit::platform::modifier_supplement::KeyEventExtModifierSupplement;
113
114#[cfg(target_os = "android")]
115use winit::platform::android::EventLoopBuilderExtAndroid;
116
117mod config;
118mod display_list;
119mod gl;
120mod image_cache;
121#[cfg(windows)]
122mod input_device_info;
123mod low_memory;
124mod px_wr;
125mod surface;
126mod util;
127mod window;
128
129use surface::*;
130
131pub mod extensions;
132
133pub mod platform;
134
135#[doc(no_inline)]
137pub use webrender;
138
139#[doc(no_inline)]
141pub use gleam;
142
143use webrender::api::*;
144use window::Window;
145use zng_txt::Txt;
146use zng_unit::{Dip, DipPoint, DipRect, DipSideOffsets, DipSize, Factor, Px, PxPoint, PxRect, PxToDip};
147use zng_view_api::{
148 Inited,
149 api_extension::{ApiExtensionId, ApiExtensionPayload},
150 dialog::{DialogId, FileDialog, MsgDialog, MsgDialogResponse},
151 drag_drop::*,
152 font::{FontFaceId, FontId, FontOptions, FontVariationName},
153 image::{ImageId, ImageLoadedData, ImageMaskMode, ImageRequest, ImageTextureId},
154 ipc::{IpcBytes, IpcBytesReceiver},
155 keyboard::{Key, KeyCode, KeyState},
156 mouse::ButtonId,
157 raw_input::{InputDeviceCapability, InputDeviceEvent, InputDeviceId, InputDeviceInfo},
158 touch::{TouchId, TouchUpdate},
159 window::{
160 CursorIcon, CursorImage, EventCause, EventFrameRendered, FocusIndicator, FrameRequest, FrameUpdateRequest, FrameWaitId,
161 HeadlessOpenData, HeadlessRequest, MonitorId, MonitorInfo, VideoMode, WindowChanged, WindowId, WindowOpenData, WindowRequest,
162 WindowState, WindowStateAll,
163 },
164 *,
165};
166
167use rustc_hash::FxHashMap;
168
169#[cfg(ipc)]
170zng_env::on_process_start!(|args| {
171 if std::env::var("ZNG_VIEW_NO_INIT_START").is_err() {
172 if args.yield_count == 0 {
173 return args.yield_once();
175 }
176
177 view_process_main();
178 }
179});
180
181#[cfg(ipc)]
189pub fn view_process_main() {
190 let config = match ViewConfig::from_env() {
191 Some(c) => c,
192 None => return,
193 };
194
195 zng_env::set_process_name("view-process");
196
197 std::panic::set_hook(Box::new(init_abort));
198 config.assert_version(false);
199 let c = ipc::connect_view_process(config.server_name).expect("failed to connect to app-process");
200
201 let mut ext = ViewExtensions::new();
202 for e in extensions::VIEW_EXTENSIONS {
203 e(&mut ext);
204 }
205
206 if config.headless {
207 App::run_headless(c, ext);
208 } else {
209 App::run_headed(c, ext);
210 }
211
212 zng_env::exit(0)
213}
214
215#[cfg(ipc)]
216#[doc(hidden)]
217#[unsafe(no_mangle)] pub extern "C" fn extern_view_process_main(patch: &StaticPatch) {
219 std::panic::set_hook(Box::new(ffi_abort));
220
221 unsafe {
224 patch.install();
225 }
226
227 view_process_main()
228}
229
230pub fn run_same_process(run_app: impl FnOnce() + Send + 'static) {
251 run_same_process_extended(run_app, ViewExtensions::new)
252}
253
254pub fn run_same_process_extended(run_app: impl FnOnce() + Send + 'static, ext: fn() -> ViewExtensions) {
258 let app_thread = thread::Builder::new()
259 .name("app".to_owned())
260 .spawn(move || {
261 if let Err(e) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(run_app)) {
263 thread::Builder::new()
264 .name("ensure-exit".into())
265 .stack_size(256 * 1024)
266 .spawn(|| {
267 thread::sleep(std::time::Duration::from_secs(5));
272 eprintln!("run_same_process did not exit after 5s of a fatal panic, exiting now");
273 zng_env::exit(101);
274 })
275 .expect("failed to spawn thread");
276 std::panic::resume_unwind(e);
278 }
279 })
280 .unwrap();
281
282 let config = ViewConfig::wait_same_process();
283 config.assert_version(true);
284
285 let c = ipc::connect_view_process(config.server_name).expect("failed to connect to app in same process");
286
287 let mut ext = ext();
288 for e in extensions::VIEW_EXTENSIONS {
289 e(&mut ext);
290 }
291
292 if config.headless {
293 App::run_headless(c, ext);
294 } else {
295 App::run_headed(c, ext);
296 }
297
298 if let Err(p) = app_thread.join() {
299 std::panic::resume_unwind(p);
300 }
301}
302
303#[cfg(ipc)]
304#[doc(hidden)]
305#[unsafe(no_mangle)] pub extern "C" fn extern_run_same_process(patch: &StaticPatch, run_app: extern "C" fn()) {
307 std::panic::set_hook(Box::new(ffi_abort));
308
309 unsafe {
312 patch.install();
313 }
314
315 #[expect(clippy::redundant_closure)] run_same_process(move || run_app())
317}
318#[cfg(ipc)]
319fn init_abort(info: &std::panic::PanicHookInfo) {
320 panic_hook(info, "note: aborting to respawn");
321}
322#[cfg(ipc)]
323fn ffi_abort(info: &std::panic::PanicHookInfo) {
324 panic_hook(info, "note: aborting to avoid unwind across FFI");
325}
326#[cfg(ipc)]
327fn panic_hook(info: &std::panic::PanicHookInfo, details: &str) {
328 let panic = util::SuppressedPanic::from_hook(info, std::backtrace::Backtrace::force_capture());
331
332 if crate::util::suppress_panic() {
333 crate::util::set_suppressed_panic(panic);
334 } else {
335 eprintln!("{panic}\n{details}");
336 zng_env::exit(101) }
338}
339
340pub(crate) struct App {
342 headless: bool,
343
344 exts: ViewExtensions,
345
346 gl_manager: GlContextManager,
347 winit_loop: util::WinitEventLoop,
348 idle: IdleTrace,
349 app_sender: AppEventSender,
350 request_recv: flume::Receiver<RequestEvent>,
351
352 response_sender: ipc::ResponseSender,
353 event_sender: ipc::EventSender,
354 image_cache: ImageCache,
355
356 generation: ViewProcessGen,
357 device_events_filter: DeviceEventsFilter,
358
359 windows: Vec<Window>,
360 surfaces: Vec<Surface>,
361
362 monitor_id_gen: MonitorId,
363 pub monitors: Vec<(MonitorId, MonitorHandle)>,
364
365 device_id_gen: InputDeviceId,
366 devices: Vec<(InputDeviceId, winit::event::DeviceId, InputDeviceInfo)>,
367
368 dialog_id_gen: DialogId,
369
370 resize_frame_wait_id_gen: FrameWaitId,
371
372 coalescing_event: Option<(Event, Instant)>,
373 cursor_entered_expect_move: Vec<WindowId>,
379
380 #[cfg(windows)]
381 skip_ralt: bool,
382
383 pressed_modifiers: FxHashMap<(Key, KeyLocation), (InputDeviceId, KeyCode)>,
384 pending_modifiers_update: Option<ModifiersState>,
385 pending_modifiers_focus_clear: bool,
386
387 #[cfg(not(any(windows, target_os = "android")))]
388 arboard: Option<arboard::Clipboard>,
389
390 low_memory_watcher: Option<low_memory::LowMemoryWatcher>,
391
392 config_listener_exit: Option<Box<dyn FnOnce()>>,
393
394 app_state: AppState,
395 drag_drop_hovered: Option<(WindowId, DipPoint)>,
396 drag_drop_next_move: Option<(Instant, PathBuf)>,
397 exited: bool,
398}
399impl fmt::Debug for App {
400 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
401 f.debug_struct("HeadlessBackend")
402 .field("app_state", &self.app_state)
403 .field("generation", &self.generation)
404 .field("device_events_filter", &self.device_events_filter)
405 .field("windows", &self.windows)
406 .field("surfaces", &self.surfaces)
407 .finish_non_exhaustive()
408 }
409}
410impl winit::application::ApplicationHandler<AppEvent> for App {
411 fn resumed(&mut self, winit_loop: &ActiveEventLoop) {
412 if let AppState::Suspended = self.app_state {
413 let mut winit_loop_guard = self.winit_loop.set(winit_loop);
414
415 self.exts.resumed();
416 self.generation = self.generation.next();
417
418 self.init(self.generation.next(), true, self.headless);
419
420 winit_loop_guard.unset(&mut self.winit_loop);
421 } else {
422 self.exts.init(&self.app_sender);
423 }
424 self.app_state = AppState::Resumed;
425
426 self.update_memory_watcher(winit_loop);
427 }
428
429 fn window_event(&mut self, winit_loop: &ActiveEventLoop, window_id: winit::window::WindowId, event: WindowEvent) {
430 let i = if let Some((i, _)) = self.windows.iter_mut().enumerate().find(|(_, w)| w.window_id() == window_id) {
431 i
432 } else {
433 return;
434 };
435
436 let _s = tracing::trace_span!("on_window_event", ?event).entered();
437
438 let mut winit_loop_guard = self.winit_loop.set(winit_loop);
439
440 self.windows[i].on_window_event(&event);
441
442 let id = self.windows[i].id();
443 let scale_factor = self.windows[i].scale_factor();
444
445 #[cfg(any(
448 target_os = "linux",
449 target_os = "dragonfly",
450 target_os = "freebsd",
451 target_os = "netbsd",
452 target_os = "openbsd"
453 ))]
454 let modal_dialog_active = self.windows[i].modal_dialog_active();
455 #[cfg(any(
456 target_os = "linux",
457 target_os = "dragonfly",
458 target_os = "freebsd",
459 target_os = "netbsd",
460 target_os = "openbsd"
461 ))]
462 macro_rules! linux_modal_dialog_bail {
463 () => {
464 if modal_dialog_active {
465 winit_loop_guard.unset(&mut self.winit_loop);
466 return;
467 }
468 };
469 }
470 #[cfg(not(any(
471 target_os = "linux",
472 target_os = "dragonfly",
473 target_os = "freebsd",
474 target_os = "netbsd",
475 target_os = "openbsd"
476 )))]
477 macro_rules! linux_modal_dialog_bail {
478 () => {};
479 }
480
481 match event {
482 WindowEvent::RedrawRequested => self.windows[i].redraw(),
483 WindowEvent::Resized(_) => {
484 let size = if let Some(size) = self.windows[i].resized() {
485 size
486 } else {
487 winit_loop_guard.unset(&mut self.winit_loop);
488 return;
489 };
490
491 let deadline = Instant::now() + Duration::from_millis(300);
495
496 if self.windows[i].is_rendering_frame() {
498 tracing::debug!("resize requested while still rendering");
499
500 while let Ok(req) = self.request_recv.recv_deadline(deadline) {
502 match req {
503 RequestEvent::Request(req) => {
504 let rsp = self.respond(req);
505 if rsp.must_be_send() {
506 let _ = self.response_sender.send(rsp);
507 }
508 }
509 RequestEvent::FrameReady(id, msg) => {
510 self.on_frame_ready(id, msg);
511 if id == self.windows[i].id() {
512 break;
513 }
514 }
515 }
516 }
517 }
518
519 if let Some(state) = self.windows[i].state_change() {
520 self.notify(Event::WindowChanged(WindowChanged::state_changed(id, state, EventCause::System)));
521 }
522
523 if let Some(handle) = self.windows[i].monitor_change() {
524 let m_id = self.monitor_handle_to_id(&handle);
525
526 self.notify(Event::WindowChanged(WindowChanged::monitor_changed(id, m_id, EventCause::System)));
527 }
528
529 let wait_id = Some(self.resize_frame_wait_id_gen.incr());
530
531 self.notify(Event::WindowChanged(WindowChanged::resized(id, size, EventCause::System, wait_id)));
533
534 self.flush_coalesced();
535
536 let mut received_frame = false;
538 loop {
539 match self.request_recv.recv_deadline(deadline) {
540 Ok(req) => {
541 match req {
542 RequestEvent::Request(req) => {
543 received_frame = req.is_frame(id, wait_id);
544 if received_frame || req.affects_window_rect(id) {
545 let rsp = self.respond(req);
547 if rsp.must_be_send() {
548 let _ = self.response_sender.send(rsp);
549 }
550 break;
551 } else {
552 let rsp = self.respond(req);
554 if rsp.must_be_send() {
555 let _ = self.response_sender.send(rsp);
556 }
557 }
558 }
559 RequestEvent::FrameReady(id, msg) => self.on_frame_ready(id, msg),
560 }
561 }
562
563 Err(flume::RecvTimeoutError::Timeout) => {
564 break;
566 }
567 Err(flume::RecvTimeoutError::Disconnected) => {
568 winit_loop_guard.unset(&mut self.winit_loop);
569 unreachable!()
570 }
571 }
572 }
573
574 if received_frame && deadline > Instant::now() {
576 while let Ok(req) = self.request_recv.recv_deadline(deadline) {
578 match req {
579 RequestEvent::Request(req) => {
580 let rsp = self.respond(req);
581 if rsp.must_be_send() {
582 let _ = self.response_sender.send(rsp);
583 }
584 }
585 RequestEvent::FrameReady(id, msg) => {
586 self.on_frame_ready(id, msg);
587 if id == self.windows[i].id() {
588 break;
589 }
590 }
591 }
592 }
593 }
594 }
595 WindowEvent::Moved(_) => {
596 let (global_position, position) = if let Some(p) = self.windows[i].moved() {
597 p
598 } else {
599 winit_loop_guard.unset(&mut self.winit_loop);
600 return;
601 };
602
603 if let Some(state) = self.windows[i].state_change() {
604 self.notify(Event::WindowChanged(WindowChanged::state_changed(id, state, EventCause::System)));
605 }
606
607 self.notify(Event::WindowChanged(WindowChanged::moved(
608 id,
609 global_position,
610 position,
611 EventCause::System,
612 )));
613
614 if let Some(handle) = self.windows[i].monitor_change() {
615 let m_id = self.monitor_handle_to_id(&handle);
616
617 self.notify(Event::WindowChanged(WindowChanged::monitor_changed(id, m_id, EventCause::System)));
618 }
619 }
620 WindowEvent::CloseRequested => {
621 linux_modal_dialog_bail!();
622 self.notify(Event::WindowCloseRequested(id))
623 }
624 WindowEvent::Destroyed => {
625 self.windows.remove(i);
626 self.notify(Event::WindowClosed(id));
627 }
628 WindowEvent::HoveredFile(file) => {
629 linux_modal_dialog_bail!();
630
631 if self.device_events_filter.input.is_empty() {
635 winit_loop.listen_device_events(winit::event_loop::DeviceEvents::Always);
636 }
637 self.drag_drop_hovered = Some((id, DipPoint::splat(Dip::new(-1000))));
638 self.notify(Event::DragHovered {
639 window: id,
640 data: vec![DragDropData::Path(file)],
641 allowed: DragDropEffect::all(),
642 });
643 }
644 WindowEvent::DroppedFile(file) => {
645 linux_modal_dialog_bail!();
646
647 if self.device_events_filter.input.is_empty() {
648 winit_loop.listen_device_events(winit::event_loop::DeviceEvents::Never);
649 }
650
651 let mut delay_to_next_move = true;
652
653 if let Some(position) = self.windows[i].drag_drop_cursor_pos() {
655 self.notify(Event::DragMoved {
656 window: id,
657 coalesced_pos: vec![],
658 position,
659 });
660 delay_to_next_move = false;
661 } else if let Some((_, pos)) = self.drag_drop_hovered {
662 delay_to_next_move = pos.x < Dip::new(0);
663 }
664
665 if delay_to_next_move {
666 self.drag_drop_next_move = Some((Instant::now(), file));
667 } else {
668 self.notify(Event::DragDropped {
669 window: id,
670 data: vec![DragDropData::Path(file)],
671 allowed: DragDropEffect::all(),
672 drop_id: DragDropId(0),
673 });
674 }
675 }
676 WindowEvent::HoveredFileCancelled => {
677 linux_modal_dialog_bail!();
678
679 self.drag_drop_hovered = None;
680 if self.device_events_filter.input.is_empty() {
681 winit_loop.listen_device_events(winit::event_loop::DeviceEvents::Never);
682 }
683
684 if self.drag_drop_next_move.is_none() {
685 self.notify(Event::DragCancelled { window: id });
687 }
688 }
689 WindowEvent::Focused(mut focused) => {
690 if self.windows[i].focused_changed(&mut focused) {
691 if focused {
692 self.notify(Event::FocusChanged { prev: None, new: Some(id) });
693
694 if let Some(state) = self.windows[i].state_change() {
696 self.notify(Event::WindowChanged(WindowChanged::state_changed(id, state, EventCause::System)));
697 }
698 } else {
699 self.pending_modifiers_focus_clear = true;
700 self.notify(Event::FocusChanged { prev: Some(id), new: None });
701 }
702 }
703 }
704 WindowEvent::KeyboardInput {
705 device_id,
706 event,
707 is_synthetic,
708 } => {
709 linux_modal_dialog_bail!();
710
711 if !is_synthetic && self.windows[i].is_focused() {
712 #[cfg(windows)]
714 if self.skip_ralt
715 && let winit::keyboard::PhysicalKey::Code(winit::keyboard::KeyCode::AltRight) = event.physical_key
716 {
717 winit_loop_guard.unset(&mut self.winit_loop);
718 return;
719 }
720
721 let state = util::element_state_to_key_state(event.state);
722 #[cfg(not(target_os = "android"))]
723 let key = util::winit_key_to_key(event.key_without_modifiers());
724 let key_modified = util::winit_key_to_key(event.logical_key);
725 #[cfg(target_os = "android")]
726 let key = key_modified.clone();
727 let key_code = util::winit_physical_key_to_key_code(event.physical_key);
728 let key_location = util::winit_key_location_to_zng(event.location);
729 let d_id = self.input_device_id(device_id, InputDeviceCapability::KEY);
730
731 let mut send_event = true;
732
733 if key.is_modifier() {
734 match state {
735 KeyState::Pressed => {
736 send_event = self
737 .pressed_modifiers
738 .insert((key.clone(), key_location), (d_id, key_code))
739 .is_none();
740 }
741 KeyState::Released => send_event = self.pressed_modifiers.remove(&(key.clone(), key_location)).is_some(),
742 }
743 }
744
745 if send_event {
746 self.notify(Event::KeyboardInput {
747 window: id,
748 device: d_id,
749 key_code,
750 key_location,
751 state,
752 text: match event.text {
753 Some(s) => Txt::from_str(s.as_str()),
754 #[cfg(target_os = "android")]
755 None => match (state, &key) {
756 (KeyState::Pressed, Key::Char(c)) => Txt::from(*c),
757 (KeyState::Pressed, Key::Str(s)) => s.clone(),
758 _ => Txt::default(),
759 },
760 #[cfg(not(target_os = "android"))]
761 None => Txt::default(),
762 },
763 key,
764 key_modified,
765 });
766 }
767 }
768 }
769 WindowEvent::ModifiersChanged(m) => {
770 linux_modal_dialog_bail!();
771 if self.windows[i].is_focused() {
772 self.pending_modifiers_update = Some(m.state());
773 }
774 }
775 WindowEvent::CursorMoved { device_id, position, .. } => {
776 linux_modal_dialog_bail!();
777
778 let px_p = position.to_px();
779 let p = px_p.to_dip(scale_factor);
780 let d_id = self.input_device_id(device_id, InputDeviceCapability::POINTER_MOTION);
781
782 let mut is_after_cursor_enter = false;
783 if let Some(i) = self.cursor_entered_expect_move.iter().position(|&w| w == id) {
784 self.cursor_entered_expect_move.remove(i);
785 is_after_cursor_enter = true;
786 }
787
788 if self.windows[i].cursor_moved(p, d_id) || is_after_cursor_enter {
789 self.notify(Event::MouseMoved {
790 window: id,
791 device: d_id,
792 coalesced_pos: vec![],
793 position: p,
794 });
795 }
796
797 if let Some((drop_moment, file)) = self.drag_drop_next_move.take()
798 && drop_moment.elapsed() < Duration::from_millis(300)
799 {
800 let window_id = self.windows[i].id();
801 self.notify(Event::DragMoved {
802 window: window_id,
803 coalesced_pos: vec![],
804 position: p,
805 });
806 self.notify(Event::DragDropped {
807 window: window_id,
808 data: vec![DragDropData::Path(file)],
809 allowed: DragDropEffect::all(),
810 drop_id: DragDropId(0),
811 });
812 }
813 }
814 WindowEvent::CursorEntered { device_id } => {
815 linux_modal_dialog_bail!();
816 if self.windows[i].cursor_entered() {
817 let d_id = self.input_device_id(device_id, InputDeviceCapability::POINTER_MOTION);
818 self.notify(Event::MouseEntered { window: id, device: d_id });
819 self.cursor_entered_expect_move.push(id);
820 }
821 }
822 WindowEvent::CursorLeft { device_id } => {
823 linux_modal_dialog_bail!();
824 if self.windows[i].cursor_left() {
825 let d_id = self.input_device_id(device_id, InputDeviceCapability::POINTER_MOTION);
826 self.notify(Event::MouseLeft { window: id, device: d_id });
827
828 if let Some(i) = self.cursor_entered_expect_move.iter().position(|&w| w == id) {
830 self.cursor_entered_expect_move.remove(i);
831 }
832 }
833 }
834 WindowEvent::MouseWheel {
835 device_id, delta, phase, ..
836 } => {
837 linux_modal_dialog_bail!();
838 let d_id = self.input_device_id(device_id, InputDeviceCapability::SCROLL_MOTION);
839 self.notify(Event::MouseWheel {
840 window: id,
841 device: d_id,
842 delta: util::winit_mouse_wheel_delta_to_zng(delta),
843 phase: util::winit_touch_phase_to_zng(phase),
844 });
845 }
846 WindowEvent::MouseInput {
847 device_id, state, button, ..
848 } => {
849 linux_modal_dialog_bail!();
850 let d_id = self.input_device_id(device_id, InputDeviceCapability::BUTTON);
851 self.notify(Event::MouseInput {
852 window: id,
853 device: d_id,
854 state: util::element_state_to_button_state(state),
855 button: util::winit_mouse_button_to_zng(button),
856 });
857 }
858 WindowEvent::TouchpadPressure {
859 device_id,
860 pressure,
861 stage,
862 } => {
863 linux_modal_dialog_bail!();
864 let d_id = self.input_device_id(device_id, InputDeviceCapability::empty());
865 self.notify(Event::TouchpadPressure {
866 window: id,
867 device: d_id,
868 pressure,
869 stage,
870 });
871 }
872 WindowEvent::AxisMotion { device_id, axis, value } => {
873 linux_modal_dialog_bail!();
874 let d_id = self.input_device_id(device_id, InputDeviceCapability::AXIS_MOTION);
875 self.notify(Event::AxisMotion {
876 window: id,
877 device: d_id,
878 axis: AxisId(axis),
879 value,
880 });
881 }
882 WindowEvent::Touch(t) => {
883 let d_id = self.input_device_id(t.device_id, InputDeviceCapability::empty());
884 let position = t.location.to_px().to_dip(scale_factor);
885
886 let notify = match t.phase {
887 winit::event::TouchPhase::Moved => self.windows[i].touch_moved(position, d_id, t.id),
888 winit::event::TouchPhase::Started => true,
889 winit::event::TouchPhase::Ended | winit::event::TouchPhase::Cancelled => {
890 self.windows[i].touch_end(d_id, t.id);
891 true
892 }
893 };
894
895 if notify {
896 self.notify(Event::Touch {
897 window: id,
898 device: d_id,
899 touches: vec![TouchUpdate::new(
900 TouchId(t.id),
901 util::winit_touch_phase_to_zng(t.phase),
902 position,
903 t.force.map(util::winit_force_to_zng),
904 )],
905 });
906 }
907 }
908 WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
909 let monitor;
910 let mut is_monitor_change = false;
911
912 if let Some(new_monitor) = self.windows[i].monitor_change() {
913 monitor = Some(new_monitor);
914 is_monitor_change = true;
915 } else {
916 monitor = self.windows[i].monitor();
917 }
918
919 let monitor = if let Some(handle) = monitor {
920 self.monitor_handle_to_id(&handle)
921 } else {
922 MonitorId::INVALID
923 };
924
925 if is_monitor_change {
926 self.notify(Event::WindowChanged(WindowChanged::monitor_changed(
927 id,
928 monitor,
929 EventCause::System,
930 )));
931 }
932 self.notify(Event::ScaleFactorChanged {
933 monitor,
934 windows: vec![id],
935 scale_factor: scale_factor as f32,
936 });
937
938 if let Some(size) = self.windows[i].resized() {
939 self.notify(Event::WindowChanged(WindowChanged::resized(id, size, EventCause::System, None)));
940 }
941 }
942 WindowEvent::Ime(ime) => {
943 linux_modal_dialog_bail!();
944
945 match ime {
946 winit::event::Ime::Preedit(s, c) => {
947 let caret = c.unwrap_or((s.len(), s.len()));
948 let ime = Ime::Preview(s.into(), caret);
949 self.notify(Event::Ime { window: id, ime });
950 }
951 winit::event::Ime::Commit(s) => {
952 let ime = Ime::Commit(s.into());
953 self.notify(Event::Ime { window: id, ime });
954 }
955 winit::event::Ime::Enabled => {}
956 winit::event::Ime::Disabled => {}
957 }
958 }
959 WindowEvent::ThemeChanged(_) => {}
960 WindowEvent::Occluded(_) => {}
961 WindowEvent::ActivationTokenDone { .. } => {}
962 WindowEvent::PinchGesture { .. } => {}
963 WindowEvent::RotationGesture { .. } => {}
964 WindowEvent::DoubleTapGesture { .. } => {}
965 WindowEvent::PanGesture { .. } => {}
966 }
967
968 winit_loop_guard.unset(&mut self.winit_loop);
969 }
970
971 fn new_events(&mut self, _winit_loop: &ActiveEventLoop, _cause: winit::event::StartCause) {
972 self.idle.exit();
973
974 #[cfg(windows)]
975 if let winit::event::StartCause::ResumeTimeReached { .. } = _cause {
976 self.update_memory_watcher(_winit_loop);
977 }
978 }
979
980 fn user_event(&mut self, winit_loop: &ActiveEventLoop, ev: AppEvent) {
981 let mut winit_loop_guard = self.winit_loop.set(winit_loop);
982 match ev {
983 AppEvent::Request => {
984 while let Ok(req) = self.request_recv.try_recv() {
985 match req {
986 RequestEvent::Request(req) => {
987 let rsp = self.respond(req);
988 if rsp.must_be_send() && self.response_sender.send(rsp).is_err() {
989 self.exited = true;
991 self.winit_loop.exit();
992 }
993 }
994 RequestEvent::FrameReady(wid, msg) => self.on_frame_ready(wid, msg),
995 }
996 }
997 }
998 AppEvent::Notify(ev) => self.notify(ev),
999 AppEvent::WinitFocused(window_id, focused) => self.window_event(winit_loop, window_id, WindowEvent::Focused(focused)),
1000 AppEvent::RefreshMonitors => self.refresh_monitors(),
1001 AppEvent::ParentProcessExited => {
1002 self.exited = true;
1003 self.winit_loop.exit();
1004 }
1005 AppEvent::ImageLoaded(data) => {
1006 self.image_cache.loaded(data);
1007 }
1008 AppEvent::MonitorPowerChanged => {
1009 for w in &mut self.windows {
1011 w.redraw();
1012 }
1013 }
1014 AppEvent::SetDeviceEventsFilter(filter) => {
1015 self.set_device_events_filter(filter, Some(winit_loop));
1016 }
1017 }
1018 winit_loop_guard.unset(&mut self.winit_loop);
1019 }
1020
1021 fn device_event(&mut self, winit_loop: &ActiveEventLoop, device_id: winit::event::DeviceId, event: DeviceEvent) {
1022 let filter = self.device_events_filter.input;
1023
1024 if !filter.is_empty() {
1025 let _s = tracing::trace_span!("on_device_event", ?event);
1026
1027 let mut winit_loop_guard = self.winit_loop.set(winit_loop);
1028
1029 match &event {
1030 DeviceEvent::Added => {
1031 let _ = self.input_device_id(device_id, InputDeviceCapability::empty());
1032 }
1034 DeviceEvent::Removed => {
1035 if let Some(i) = self.devices.iter().position(|(_, id, _)| *id == device_id) {
1036 self.devices.remove(i);
1037 self.notify_input_devices_changed();
1038 }
1039 }
1040 DeviceEvent::MouseMotion { delta } => {
1041 let cap = InputDeviceCapability::POINTER_MOTION;
1042 if filter.contains(cap) {
1043 let d_id = self.input_device_id(device_id, cap);
1044 self.notify(Event::InputDeviceEvent {
1045 device: d_id,
1046 event: InputDeviceEvent::PointerMotion {
1047 delta: euclid::vec2(delta.0, delta.1),
1048 },
1049 });
1050 }
1051 }
1052 DeviceEvent::MouseWheel { delta } => {
1053 let cap = InputDeviceCapability::SCROLL_MOTION;
1054 if filter.contains(cap) {
1055 let d_id = self.input_device_id(device_id, cap);
1056 self.notify(Event::InputDeviceEvent {
1057 device: d_id,
1058 event: InputDeviceEvent::ScrollMotion {
1059 delta: util::winit_mouse_wheel_delta_to_zng(*delta),
1060 },
1061 });
1062 }
1063 }
1064 DeviceEvent::Motion { axis, value } => {
1065 let cap = InputDeviceCapability::AXIS_MOTION;
1066 if filter.contains(cap) {
1067 let d_id = self.input_device_id(device_id, cap);
1068 self.notify(Event::InputDeviceEvent {
1069 device: d_id,
1070 event: InputDeviceEvent::AxisMotion {
1071 axis: AxisId(*axis),
1072 value: *value,
1073 },
1074 });
1075 }
1076 }
1077 DeviceEvent::Button { button, state } => {
1078 let cap = InputDeviceCapability::BUTTON;
1079 if filter.contains(cap) {
1080 let d_id = self.input_device_id(device_id, cap);
1081 self.notify(Event::InputDeviceEvent {
1082 device: d_id,
1083 event: InputDeviceEvent::Button {
1084 button: ButtonId(*button),
1085 state: util::element_state_to_button_state(*state),
1086 },
1087 });
1088 }
1089 }
1090 DeviceEvent::Key(k) => {
1091 let cap = InputDeviceCapability::KEY;
1092 if filter.contains(cap) {
1093 let d_id = self.input_device_id(device_id, cap);
1094 self.notify(Event::InputDeviceEvent {
1095 device: d_id,
1096 event: InputDeviceEvent::Key {
1097 key_code: util::winit_physical_key_to_key_code(k.physical_key),
1098 state: util::element_state_to_key_state(k.state),
1099 },
1100 });
1101 }
1102 }
1103 }
1104
1105 winit_loop_guard.unset(&mut self.winit_loop);
1106 }
1107
1108 if let Some((id, pos)) = &mut self.drag_drop_hovered
1109 && let DeviceEvent::MouseMotion { .. } = &event
1110 && let Some(win) = self.windows.iter().find(|w| w.id() == *id)
1111 && let Some(new_pos) = win.drag_drop_cursor_pos()
1112 && *pos != new_pos
1113 {
1114 *pos = new_pos;
1115 let event = Event::DragMoved {
1116 window: *id,
1117 coalesced_pos: vec![],
1118 position: *pos,
1119 };
1120 self.notify(event);
1121 }
1122 }
1123
1124 fn about_to_wait(&mut self, winit_loop: &ActiveEventLoop) {
1125 let mut winit_loop_guard = self.winit_loop.set(winit_loop);
1126
1127 self.finish_cursor_entered_move();
1128 self.update_modifiers();
1129 self.flush_coalesced();
1130 #[cfg(windows)]
1131 {
1132 self.skip_ralt = false;
1133 }
1134 self.idle.enter();
1135
1136 winit_loop_guard.unset(&mut self.winit_loop);
1137 }
1138
1139 fn suspended(&mut self, _: &ActiveEventLoop) {
1140 #[cfg(target_os = "android")]
1141 if let Some(w) = &self.windows.first() {
1142 self.notify(Event::FocusChanged {
1143 prev: Some(w.id()),
1144 new: None,
1145 });
1146 }
1147
1148 self.app_state = AppState::Suspended;
1149 self.windows.clear();
1150 self.surfaces.clear();
1151 self.image_cache.clear();
1152 self.exts.suspended();
1153
1154 self.notify(Event::Suspended);
1155 }
1156
1157 fn exiting(&mut self, event_loop: &ActiveEventLoop) {
1158 let _ = event_loop;
1159 if let Some(t) = self.config_listener_exit.take() {
1160 t();
1161 }
1162 }
1163
1164 fn memory_warning(&mut self, winit_loop: &ActiveEventLoop) {
1165 let mut winit_loop_guard = self.winit_loop.set(winit_loop);
1166
1167 self.image_cache.on_low_memory();
1168 for w in &mut self.windows {
1169 w.on_low_memory();
1170 }
1171 for s in &mut self.surfaces {
1172 s.on_low_memory();
1173 }
1174 self.exts.on_low_memory();
1175 self.notify(Event::LowMemory);
1176
1177 winit_loop_guard.unset(&mut self.winit_loop);
1178 }
1179}
1180#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1181enum AppState {
1182 PreInitSuspended,
1183 Resumed,
1184 Suspended,
1185}
1186
1187struct IdleTrace(Option<tracing::span::EnteredSpan>);
1188impl IdleTrace {
1189 pub fn enter(&mut self) {
1190 self.0 = Some(tracing::trace_span!("<winit-idle>").entered());
1191 }
1192 pub fn exit(&mut self) {
1193 self.0 = None;
1194 }
1195}
1196impl App {
1197 fn set_device_events_filter(&mut self, filter: DeviceEventsFilter, t: Option<&ActiveEventLoop>) {
1198 self.device_events_filter = filter;
1199
1200 if let Some(t) = t {
1201 if !self.device_events_filter.input.is_empty() {
1202 t.listen_device_events(winit::event_loop::DeviceEvents::Always);
1203 } else {
1204 t.listen_device_events(winit::event_loop::DeviceEvents::Never);
1205 }
1206 }
1207 }
1208
1209 pub fn run_headless(ipc: ipc::ViewChannels, ext: ViewExtensions) {
1210 tracing::info!("running headless view-process");
1211
1212 gl::warmup();
1213
1214 let (app_sender, app_receiver) = flume::unbounded();
1215 let (request_sender, request_receiver) = flume::unbounded();
1216 let mut app = App::new(
1217 AppEventSender::Headless(app_sender, request_sender),
1218 ipc.response_sender,
1219 ipc.event_sender,
1220 request_receiver,
1221 ext,
1222 );
1223 app.headless = true;
1224
1225 let winit_span = tracing::trace_span!("winit::EventLoop::new").entered();
1226 #[cfg(not(target_os = "android"))]
1227 let event_loop = EventLoop::builder().build().unwrap();
1228 #[cfg(target_os = "android")]
1229 let event_loop = EventLoop::builder()
1230 .with_android_app(platform::android::android_app())
1231 .build()
1232 .unwrap();
1233 drop(winit_span);
1234
1235 let mut app = HeadlessApp {
1236 app,
1237 request_receiver: Some(ipc.request_receiver),
1238 app_receiver,
1239 };
1240 if let Err(e) = event_loop.run_app(&mut app) {
1241 if app.app.exited {
1242 tracing::error!("winit event loop error after app exit, {e}");
1248 } else {
1249 panic!("winit event loop error, {e}");
1250 }
1251 }
1252
1253 struct HeadlessApp {
1254 app: App,
1255 request_receiver: Option<ipc::RequestReceiver>,
1256 app_receiver: flume::Receiver<AppEvent>,
1257 }
1258 impl winit::application::ApplicationHandler<()> for HeadlessApp {
1259 fn resumed(&mut self, winit_loop: &ActiveEventLoop) {
1260 let mut winit_loop_guard = self.app.winit_loop.set(winit_loop);
1261
1262 self.app.resumed(winit_loop);
1263 self.app.start_receiving(self.request_receiver.take().unwrap());
1264
1265 'app_loop: while !self.app.exited {
1266 match self.app_receiver.recv() {
1267 Ok(app_ev) => match app_ev {
1268 AppEvent::Request => {
1269 while let Ok(request) = self.app.request_recv.try_recv() {
1270 match request {
1271 RequestEvent::Request(request) => {
1272 let response = self.app.respond(request);
1273 if response.must_be_send() && self.app.response_sender.send(response).is_err() {
1274 self.app.exited = true;
1275 break 'app_loop;
1276 }
1277 }
1278 RequestEvent::FrameReady(id, msg) => {
1279 let r = if let Some(s) = self.app.surfaces.iter_mut().find(|s| s.id() == id) {
1280 Some(s.on_frame_ready(msg, &mut self.app.image_cache))
1281 } else {
1282 None
1283 };
1284 if let Some((frame_id, image)) = r {
1285 self.app.notify(Event::FrameRendered(EventFrameRendered::new(id, frame_id, image)));
1286 }
1287 }
1288 }
1289 }
1290 }
1291 AppEvent::Notify(ev) => {
1292 if self.app.event_sender.send(ev).is_err() {
1293 self.app.exited = true;
1294 break 'app_loop;
1295 }
1296 }
1297 AppEvent::RefreshMonitors => {
1298 panic!("no monitor info in headless mode")
1299 }
1300 AppEvent::WinitFocused(_, _) => {
1301 panic!("no winit event loop in headless mode")
1302 }
1303 AppEvent::ParentProcessExited => {
1304 self.app.exited = true;
1305 break 'app_loop;
1306 }
1307 AppEvent::ImageLoaded(data) => {
1308 self.app.image_cache.loaded(data);
1309 }
1310 AppEvent::MonitorPowerChanged => {} AppEvent::SetDeviceEventsFilter(filter) => {
1312 self.app.set_device_events_filter(filter, None);
1313 }
1314 },
1315 Err(_) => {
1316 self.app.exited = true;
1317 break 'app_loop;
1318 }
1319 }
1320 }
1321
1322 self.app.winit_loop.exit();
1323
1324 winit_loop_guard.unset(&mut self.app.winit_loop);
1325 }
1326
1327 fn window_event(&mut self, _: &ActiveEventLoop, _: winit::window::WindowId, _: WindowEvent) {}
1328
1329 fn suspended(&mut self, event_loop: &ActiveEventLoop) {
1330 self.app.suspended(event_loop);
1331 }
1332 }
1333 }
1334
1335 pub fn run_headed(ipc: ipc::ViewChannels, ext: ViewExtensions) {
1336 tracing::info!("running headed view-process");
1337
1338 gl::warmup();
1339
1340 let winit_span = tracing::trace_span!("winit::EventLoop::new").entered();
1341 #[cfg(not(target_os = "android"))]
1342 let event_loop = EventLoop::with_user_event().build().unwrap();
1343 #[cfg(target_os = "android")]
1344 let event_loop = EventLoop::with_user_event()
1345 .with_android_app(platform::android::android_app())
1346 .build()
1347 .unwrap();
1348 drop(winit_span);
1349 let app_sender = event_loop.create_proxy();
1350
1351 let (request_sender, request_receiver) = flume::unbounded();
1352 let mut app = App::new(
1353 AppEventSender::Headed(app_sender, request_sender),
1354 ipc.response_sender,
1355 ipc.event_sender,
1356 request_receiver,
1357 ext,
1358 );
1359 app.start_receiving(ipc.request_receiver);
1360
1361 app.config_listener_exit = config::spawn_listener(app.app_sender.clone());
1362
1363 if let Err(e) = event_loop.run_app(&mut app) {
1364 if app.exited {
1365 tracing::error!("winit event loop error after app exit, {e}");
1366 } else {
1367 panic!("winit event loop error, {e}");
1368 }
1369 }
1370 }
1371
1372 fn new(
1373 app_sender: AppEventSender,
1374 response_sender: ipc::ResponseSender,
1375 event_sender: ipc::EventSender,
1376 request_recv: flume::Receiver<RequestEvent>,
1377 mut exts: ViewExtensions,
1378 ) -> Self {
1379 exts.renderer("zng-view.webrender_debug", extensions::RendererDebugExt::new);
1380 #[cfg(windows)]
1381 {
1382 exts.window("zng-view.prefer_angle", extensions::PreferAngleExt::new);
1383 }
1384 let mut idle = IdleTrace(None);
1385 idle.enter();
1386 App {
1387 headless: false,
1388 exts,
1389 idle,
1390 gl_manager: GlContextManager::default(),
1391 image_cache: ImageCache::new(app_sender.clone()),
1392 app_sender,
1393 request_recv,
1394 response_sender,
1395 event_sender,
1396 winit_loop: util::WinitEventLoop::default(),
1397 generation: ViewProcessGen::INVALID,
1398 device_events_filter: DeviceEventsFilter::empty(),
1399 windows: vec![],
1400 surfaces: vec![],
1401 monitors: vec![],
1402 monitor_id_gen: MonitorId::INVALID,
1403 devices: vec![],
1404 device_id_gen: InputDeviceId::INVALID,
1405 dialog_id_gen: DialogId::INVALID,
1406 resize_frame_wait_id_gen: FrameWaitId::INVALID,
1407 coalescing_event: None,
1408 cursor_entered_expect_move: Vec::with_capacity(1),
1409 app_state: AppState::PreInitSuspended,
1410 exited: false,
1411 #[cfg(windows)]
1412 skip_ralt: false,
1413 pressed_modifiers: FxHashMap::default(),
1414 pending_modifiers_update: None,
1415 pending_modifiers_focus_clear: false,
1416 config_listener_exit: None,
1417 drag_drop_hovered: None,
1418 drag_drop_next_move: None,
1419 #[cfg(not(any(windows, target_os = "android")))]
1420 arboard: None,
1421 low_memory_watcher: low_memory::LowMemoryWatcher::new(),
1422 }
1423 }
1424
1425 fn start_receiving(&mut self, mut request_recv: ipc::RequestReceiver) {
1426 let app_sender = self.app_sender.clone();
1427 thread::Builder::new()
1428 .name("request-recv".into())
1429 .stack_size(256 * 1024)
1430 .spawn(move || {
1431 while let Ok(r) = request_recv.recv() {
1432 if let Err(ipc::ViewChannelError::Disconnected) = app_sender.request(r) {
1433 break;
1434 }
1435 }
1436 let _ = app_sender.send(AppEvent::ParentProcessExited);
1437 })
1438 .expect("failed to spawn thread");
1439 }
1440
1441 fn monitor_handle_to_id(&mut self, handle: &MonitorHandle) -> MonitorId {
1442 if let Some((id, _)) = self.monitors.iter().find(|(_, h)| h == handle) {
1443 *id
1444 } else {
1445 self.refresh_monitors();
1446 if let Some((id, _)) = self.monitors.iter().find(|(_, h)| h == handle) {
1447 *id
1448 } else {
1449 MonitorId::INVALID
1450 }
1451 }
1452 }
1453
1454 fn update_modifiers(&mut self) {
1455 if mem::take(&mut self.pending_modifiers_focus_clear) && self.windows.iter().all(|w| !w.is_focused()) {
1462 self.pressed_modifiers.clear();
1463 }
1464
1465 if let Some(m) = self.pending_modifiers_update.take()
1466 && let Some(id) = self.windows.iter().find(|w| w.is_focused()).map(|w| w.id())
1467 {
1468 let mut notify = vec![];
1469 self.pressed_modifiers.retain(|(key, location), (d_id, s_code)| {
1470 let mut retain = true;
1471 if matches!(key, Key::Super) && !m.super_key() {
1472 retain = false;
1473 notify.push(Event::KeyboardInput {
1474 window: id,
1475 device: *d_id,
1476 key_code: *s_code,
1477 state: KeyState::Released,
1478 key: key.clone(),
1479 key_location: *location,
1480 key_modified: key.clone(),
1481 text: Txt::from_str(""),
1482 });
1483 }
1484 if matches!(key, Key::Shift) && !m.shift_key() {
1485 retain = false;
1486 notify.push(Event::KeyboardInput {
1487 window: id,
1488 device: *d_id,
1489 key_code: *s_code,
1490 state: KeyState::Released,
1491 key: key.clone(),
1492 key_location: *location,
1493 key_modified: key.clone(),
1494 text: Txt::from_str(""),
1495 });
1496 }
1497 if matches!(key, Key::Alt | Key::AltGraph) && !m.alt_key() {
1498 retain = false;
1499 notify.push(Event::KeyboardInput {
1500 window: id,
1501 device: *d_id,
1502 key_code: *s_code,
1503 state: KeyState::Released,
1504 key: key.clone(),
1505 key_location: *location,
1506 key_modified: key.clone(),
1507 text: Txt::from_str(""),
1508 });
1509 }
1510 if matches!(key, Key::Ctrl) && !m.control_key() {
1511 retain = false;
1512 notify.push(Event::KeyboardInput {
1513 window: id,
1514 device: *d_id,
1515 key_code: *s_code,
1516 state: KeyState::Released,
1517 key: key.clone(),
1518 key_location: *location,
1519 key_modified: key.clone(),
1520 text: Txt::from_str(""),
1521 });
1522 }
1523 retain
1524 });
1525
1526 for ev in notify {
1527 self.notify(ev);
1528 }
1529 }
1530 }
1531
1532 fn refresh_monitors(&mut self) {
1533 let mut monitors = Vec::with_capacity(self.monitors.len());
1534
1535 let mut changed = false;
1536
1537 for (fresh_handle, (id, handle)) in self.winit_loop.available_monitors().zip(&self.monitors) {
1538 let id = if &fresh_handle == handle {
1539 *id
1540 } else {
1541 changed = true;
1542 self.monitor_id_gen.incr()
1543 };
1544 monitors.push((id, fresh_handle))
1545 }
1546
1547 if changed {
1548 self.monitors = monitors;
1549
1550 let monitors = self.available_monitors();
1551 self.notify(Event::MonitorsChanged(monitors));
1552 }
1553 }
1554
1555 fn on_frame_ready(&mut self, window_id: WindowId, msg: FrameReadyMsg) {
1556 let _s = tracing::trace_span!("on_frame_ready").entered();
1557
1558 if let Some(w) = self.windows.iter_mut().find(|w| w.id() == window_id) {
1559 let r = w.on_frame_ready(msg, &mut self.image_cache);
1560
1561 let _ = self
1562 .event_sender
1563 .send(Event::FrameRendered(EventFrameRendered::new(window_id, r.frame_id, r.image)));
1564
1565 if r.first_frame {
1566 let size = w.size();
1567 self.notify(Event::WindowChanged(WindowChanged::resized(window_id, size, EventCause::App, None)));
1568 }
1569 } else if let Some(s) = self.surfaces.iter_mut().find(|w| w.id() == window_id) {
1570 let (frame_id, image) = s.on_frame_ready(msg, &mut self.image_cache);
1571
1572 self.notify(Event::FrameRendered(EventFrameRendered::new(window_id, frame_id, image)))
1573 }
1574 }
1575
1576 pub(crate) fn notify(&mut self, event: Event) {
1577 let now = Instant::now();
1578 if let Some((mut coal, timestamp)) = self.coalescing_event.take() {
1579 let r = if now.saturating_duration_since(timestamp) >= Duration::from_millis(16) {
1580 Err(event)
1581 } else {
1582 coal.coalesce(event)
1583 };
1584 match r {
1585 Ok(()) => self.coalescing_event = Some((coal, timestamp)),
1586 Err(event) => match (&mut coal, event) {
1587 (
1588 Event::KeyboardInput {
1589 window,
1590 device,
1591 state,
1592 text,
1593 ..
1594 },
1595 Event::KeyboardInput {
1596 window: n_window,
1597 device: n_device,
1598 text: n_text,
1599 ..
1600 },
1601 ) if !n_text.is_empty() && *window == n_window && *device == n_device && *state == KeyState::Pressed => {
1602 if text.is_empty() {
1604 *text = n_text;
1605 } else {
1606 text.push_str(&n_text);
1607 };
1608 self.coalescing_event = Some((coal, now));
1609 }
1610 (_, event) => {
1611 let mut error = self.event_sender.send(coal).is_err();
1612 error |= self.event_sender.send(event).is_err();
1613
1614 if error {
1615 let _ = self.app_sender.send(AppEvent::ParentProcessExited);
1616 }
1617 }
1618 },
1619 }
1620 } else {
1621 self.coalescing_event = Some((event, now));
1622 }
1623
1624 if self.headless {
1625 self.flush_coalesced();
1626 }
1627 }
1628
1629 pub(crate) fn finish_cursor_entered_move(&mut self) {
1630 let mut moves = vec![];
1631 for window_id in self.cursor_entered_expect_move.drain(..) {
1632 if let Some(w) = self.windows.iter().find(|w| w.id() == window_id) {
1633 let (position, device) = w.last_cursor_pos();
1634 moves.push(Event::MouseMoved {
1635 window: w.id(),
1636 device,
1637 coalesced_pos: vec![],
1638 position,
1639 });
1640 }
1641 }
1642 for ev in moves {
1643 self.notify(ev);
1644 }
1645 }
1646
1647 pub(crate) fn flush_coalesced(&mut self) {
1649 if let Some((coal, _)) = self.coalescing_event.take()
1650 && self.event_sender.send(coal).is_err()
1651 {
1652 let _ = self.app_sender.send(AppEvent::ParentProcessExited);
1653 }
1654 }
1655
1656 #[track_caller]
1657 fn assert_resumed(&self) {
1658 assert_eq!(self.app_state, AppState::Resumed);
1659 }
1660
1661 fn with_window<R>(&mut self, id: WindowId, action: impl FnOnce(&mut Window) -> R, not_found: impl FnOnce() -> R) -> R {
1662 self.assert_resumed();
1663 self.windows.iter_mut().find(|w| w.id() == id).map(action).unwrap_or_else(|| {
1664 tracing::error!("headed window `{id:?}` not found, will return fallback result");
1665 not_found()
1666 })
1667 }
1668
1669 fn monitor_id(&mut self, handle: &MonitorHandle) -> MonitorId {
1670 if let Some((id, _)) = self.monitors.iter().find(|(_, h)| h == handle) {
1671 *id
1672 } else {
1673 let id = self.monitor_id_gen.incr();
1674 self.monitors.push((id, handle.clone()));
1675 id
1676 }
1677 }
1678
1679 fn notify_input_devices_changed(&mut self) {
1680 let devices = self.devices.iter().map(|(id, _, info)| (*id, info.clone())).collect();
1681 self.notify(Event::InputDevicesChanged(devices));
1682 }
1683
1684 fn input_device_id(&mut self, device_id: winit::event::DeviceId, capability: InputDeviceCapability) -> InputDeviceId {
1686 if let Some((id, _, info)) = self.devices.iter_mut().find(|(_, id, _)| *id == device_id) {
1687 let id = *id;
1688 if !self.device_events_filter.input.is_empty() && !capability.is_empty() && !info.capabilities.contains(capability) {
1689 info.capabilities |= capability;
1690 self.notify_input_devices_changed();
1691 }
1692 id
1693 } else {
1694 let id = self.device_id_gen.incr();
1695
1696 #[cfg(not(windows))]
1697 let info = InputDeviceInfo::new("Winit Device", InputDeviceCapability::empty());
1698 #[cfg(windows)]
1699 let info = {
1700 use winit::platform::windows::DeviceIdExtWindows as _;
1701 if !self.device_events_filter.input.is_empty()
1702 && let Some(device_path) = device_id.persistent_identifier()
1703 {
1704 input_device_info::get(&device_path)
1705 } else {
1706 InputDeviceInfo::new("Winit Device", InputDeviceCapability::empty())
1707 }
1708 };
1709
1710 self.devices.push((id, device_id, info));
1711
1712 if !self.device_events_filter.input.is_empty() {
1713 self.notify_input_devices_changed();
1714 }
1715
1716 id
1717 }
1718 }
1719
1720 fn available_monitors(&mut self) -> Vec<(MonitorId, MonitorInfo)> {
1721 let _span = tracing::trace_span!("available_monitors").entered();
1722
1723 let primary = self.winit_loop.primary_monitor();
1724 self.winit_loop
1725 .available_monitors()
1726 .map(|m| {
1727 let id = self.monitor_id(&m);
1728 let is_primary = primary.as_ref().map(|h| h == &m).unwrap_or(false);
1729 let mut info = util::monitor_handle_to_info(&m);
1730 info.is_primary = is_primary;
1731 (id, info)
1732 })
1733 .collect()
1734 }
1735
1736 fn update_memory_watcher(&mut self, _winit_loop: &ActiveEventLoop) {
1737 if let Some(m) = &mut self.low_memory_watcher {
1738 if m.notify() {
1739 use winit::application::ApplicationHandler as _;
1740 self.memory_warning(_winit_loop);
1741 }
1742 _winit_loop.set_control_flow(winit::event_loop::ControlFlow::wait_duration(Duration::from_secs(5)));
1743 }
1744 }
1745}
1746macro_rules! with_window_or_surface {
1747 ($self:ident, $id:ident, |$el:ident|$action:expr, ||$fallback:expr) => {
1748 if let Some($el) = $self.windows.iter_mut().find(|w| w.id() == $id) {
1749 $action
1750 } else if let Some($el) = $self.surfaces.iter_mut().find(|w| w.id() == $id) {
1751 $action
1752 } else {
1753 tracing::error!("window `{:?}` not found, will return fallback result", $id);
1754 $fallback
1755 }
1756 };
1757}
1758impl Drop for App {
1759 fn drop(&mut self) {
1760 if let Some(f) = self.config_listener_exit.take() {
1761 f();
1762 }
1763 }
1764}
1765impl App {
1766 fn open_headless_impl(&mut self, config: HeadlessRequest) -> HeadlessOpenData {
1767 self.assert_resumed();
1768 let surf = Surface::open(
1769 self.generation,
1770 config,
1771 &self.winit_loop,
1772 &mut self.gl_manager,
1773 self.exts.new_window(),
1774 self.exts.new_renderer(),
1775 self.app_sender.clone(),
1776 );
1777 let render_mode = surf.render_mode();
1778
1779 self.surfaces.push(surf);
1780
1781 HeadlessOpenData::new(render_mode)
1782 }
1783
1784 #[cfg(not(any(windows, target_os = "android")))]
1785 fn arboard(&mut self) -> Result<&mut arboard::Clipboard, clipboard::ClipboardError> {
1786 if self.arboard.is_none() {
1787 match arboard::Clipboard::new() {
1788 Ok(c) => self.arboard = Some(c),
1789 Err(e) => return Err(util::arboard_to_clip(e)),
1790 }
1791 }
1792 Ok(self.arboard.as_mut().unwrap())
1793 }
1794}
1795
1796impl Api for App {
1797 fn init(&mut self, vp_gen: ViewProcessGen, is_respawn: bool, headless: bool) {
1798 if self.exited {
1799 panic!("cannot restart exited");
1800 }
1801
1802 self.generation = vp_gen;
1803 self.headless = headless;
1804
1805 self.notify(Event::Inited(Inited::new(vp_gen, is_respawn, self.exts.api_extensions())));
1806
1807 let available_monitors = self.available_monitors();
1808 self.notify(Event::MonitorsChanged(available_monitors));
1809
1810 let cfg = config::multi_click_config();
1811 if is_respawn || cfg != zng_view_api::config::MultiClickConfig::default() {
1812 self.notify(Event::MultiClickConfigChanged(cfg));
1813 }
1814
1815 let cfg = config::key_repeat_config();
1816 if is_respawn || cfg != zng_view_api::config::KeyRepeatConfig::default() {
1817 self.notify(Event::KeyRepeatConfigChanged(cfg));
1818 }
1819
1820 let cfg = config::touch_config();
1821 if is_respawn || cfg != zng_view_api::config::TouchConfig::default() {
1822 self.notify(Event::TouchConfigChanged(cfg));
1823 }
1824
1825 let cfg = config::font_aa();
1826 if is_respawn || cfg != zng_view_api::config::FontAntiAliasing::default() {
1827 self.notify(Event::FontAaChanged(cfg));
1828 }
1829
1830 let cfg = config::animations_config();
1831 if is_respawn || cfg != zng_view_api::config::AnimationsConfig::default() {
1832 self.notify(Event::AnimationsConfigChanged(cfg));
1833 }
1834
1835 let cfg = config::locale_config();
1836 if is_respawn || cfg != zng_view_api::config::LocaleConfig::default() {
1837 self.notify(Event::LocaleChanged(cfg));
1838 }
1839
1840 let cfg = config::colors_config();
1841 if is_respawn || cfg != zng_view_api::config::ColorsConfig::default() {
1842 self.notify(Event::ColorsConfigChanged(cfg));
1843 }
1844
1845 let cfg = config::chrome_config();
1846 if is_respawn || cfg != zng_view_api::config::ChromeConfig::default() {
1847 self.notify(Event::ChromeConfigChanged(cfg));
1848 }
1849 }
1850
1851 fn exit(&mut self) {
1852 self.assert_resumed();
1853 self.exited = true;
1854 if let Some(t) = self.config_listener_exit.take() {
1855 t();
1856 }
1857 let _ = self.app_sender.send(AppEvent::ParentProcessExited);
1859 }
1860
1861 fn set_device_events_filter(&mut self, filter: DeviceEventsFilter) {
1862 let _ = self.app_sender.send(AppEvent::SetDeviceEventsFilter(filter));
1863 }
1864
1865 fn open_window(&mut self, mut config: WindowRequest) {
1866 let _s = tracing::debug_span!("open", ?config).entered();
1867
1868 config.state.clamp_size();
1869 config.enforce_kiosk();
1870
1871 if self.headless {
1872 let id = config.id;
1873 let data = self.open_headless_impl(HeadlessRequest::new(
1874 config.id,
1875 Factor(1.0),
1876 config.state.restore_rect.size,
1877 config.render_mode,
1878 config.extensions,
1879 ));
1880 let msg = WindowOpenData::new(
1881 WindowStateAll::new(
1882 WindowState::Fullscreen,
1883 PxPoint::zero(),
1884 DipRect::from_size(config.state.restore_rect.size),
1885 WindowState::Fullscreen,
1886 DipSize::zero(),
1887 DipSize::new(Dip::MAX, Dip::MAX),
1888 false,
1889 ),
1890 None,
1891 (PxPoint::zero(), DipPoint::zero()),
1892 config.state.restore_rect.size,
1893 Factor(1.0),
1894 data.render_mode,
1895 DipSideOffsets::zero(),
1896 );
1897
1898 self.notify(Event::WindowOpened(id, msg));
1899 } else {
1900 self.assert_resumed();
1901
1902 #[cfg(target_os = "android")]
1903 if !self.windows.is_empty() {
1904 tracing::error!("android can only have one window");
1905 return;
1906 }
1907
1908 let id = config.id;
1909 let win = Window::open(
1910 self.generation,
1911 config.icon.and_then(|i| self.image_cache.get(i)).and_then(|i| i.icon()),
1912 config
1913 .cursor_image
1914 .and_then(|(i, h)| self.image_cache.get(i).and_then(|i| i.cursor(h, &self.winit_loop))),
1915 config,
1916 &self.winit_loop,
1917 &mut self.gl_manager,
1918 self.exts.new_window(),
1919 self.exts.new_renderer(),
1920 self.app_sender.clone(),
1921 );
1922
1923 let msg = WindowOpenData::new(
1924 win.state(),
1925 win.monitor().map(|h| self.monitor_id(&h)),
1926 win.inner_position(),
1927 win.size(),
1928 win.scale_factor(),
1929 win.render_mode(),
1930 win.safe_padding(),
1931 );
1932
1933 self.windows.push(win);
1934
1935 self.notify(Event::WindowOpened(id, msg));
1936
1937 #[cfg(target_os = "android")]
1939 {
1940 self.windows.last_mut().unwrap().focused_changed(&mut true);
1941 self.notify(Event::FocusChanged { prev: None, new: Some(id) });
1942 }
1943 }
1944 }
1945
1946 fn open_headless(&mut self, config: HeadlessRequest) {
1947 let _s = tracing::debug_span!("open_headless", ?config).entered();
1948
1949 let id = config.id;
1950 let msg = self.open_headless_impl(config);
1951
1952 self.notify(Event::HeadlessOpened(id, msg));
1953 }
1954
1955 fn close(&mut self, id: WindowId) {
1956 let _s = tracing::debug_span!("close_window", ?id);
1957
1958 self.assert_resumed();
1959 if let Some(i) = self.windows.iter().position(|w| w.id() == id) {
1960 let _ = self.windows.swap_remove(i);
1961 }
1962 if let Some(i) = self.surfaces.iter().position(|w| w.id() == id) {
1963 let _ = self.surfaces.swap_remove(i);
1964 }
1965 }
1966
1967 fn set_title(&mut self, id: WindowId, title: Txt) {
1968 self.with_window(id, |w| w.set_title(title), || ())
1969 }
1970
1971 fn set_visible(&mut self, id: WindowId, visible: bool) {
1972 self.with_window(id, |w| w.set_visible(visible), || ())
1973 }
1974
1975 fn set_always_on_top(&mut self, id: WindowId, always_on_top: bool) {
1976 self.with_window(id, |w| w.set_always_on_top(always_on_top), || ())
1977 }
1978
1979 fn set_movable(&mut self, id: WindowId, movable: bool) {
1980 self.with_window(id, |w| w.set_movable(movable), || ())
1981 }
1982
1983 fn set_resizable(&mut self, id: WindowId, resizable: bool) {
1984 self.with_window(id, |w| w.set_resizable(resizable), || ())
1985 }
1986
1987 fn set_taskbar_visible(&mut self, id: WindowId, visible: bool) {
1988 self.with_window(id, |w| w.set_taskbar_visible(visible), || ())
1989 }
1990
1991 fn bring_to_top(&mut self, id: WindowId) {
1992 self.with_window(id, |w| w.bring_to_top(), || ())
1993 }
1994
1995 fn set_state(&mut self, id: WindowId, state: WindowStateAll) {
1996 if let Some(w) = self.windows.iter_mut().find(|w| w.id() == id)
1997 && w.set_state(state.clone())
1998 {
1999 let mut change = WindowChanged::state_changed(id, state, EventCause::App);
2000
2001 change.size = w.resized();
2002 change.position = w.moved();
2003 if let Some(handle) = w.monitor_change() {
2004 let monitor = self.monitor_handle_to_id(&handle);
2005 change.monitor = Some(monitor);
2006 }
2007
2008 let _ = self.app_sender.send(AppEvent::Notify(Event::WindowChanged(change)));
2009 }
2010 }
2011
2012 fn set_headless_size(&mut self, renderer: WindowId, size: DipSize, scale_factor: Factor) {
2013 self.assert_resumed();
2014 if let Some(surf) = self.surfaces.iter_mut().find(|s| s.id() == renderer) {
2015 surf.set_size(size, scale_factor)
2016 }
2017 }
2018
2019 fn set_video_mode(&mut self, id: WindowId, mode: VideoMode) {
2020 self.with_window(id, |w| w.set_video_mode(mode), || ())
2021 }
2022
2023 fn set_icon(&mut self, id: WindowId, icon: Option<ImageId>) {
2024 let icon = icon.and_then(|i| self.image_cache.get(i)).and_then(|i| i.icon());
2025 self.with_window(id, |w| w.set_icon(icon), || ())
2026 }
2027
2028 fn set_focus_indicator(&mut self, id: WindowId, request: Option<FocusIndicator>) {
2029 self.with_window(id, |w| w.set_focus_request(request), || ())
2030 }
2031
2032 fn focus(&mut self, id: WindowId) -> FocusResult {
2033 #[cfg(windows)]
2034 {
2035 let (r, s) = self.with_window(id, |w| w.focus(), || (FocusResult::Requested, false));
2036 self.skip_ralt = s;
2037 r
2038 }
2039
2040 #[cfg(not(windows))]
2041 {
2042 self.with_window(id, |w| w.focus(), || FocusResult::Requested)
2043 }
2044 }
2045
2046 fn drag_move(&mut self, id: WindowId) {
2047 self.with_window(id, |w| w.drag_move(), || ())
2048 }
2049
2050 fn drag_resize(&mut self, id: WindowId, direction: zng_view_api::window::ResizeDirection) {
2051 self.with_window(id, |w| w.drag_resize(direction), || ())
2052 }
2053
2054 fn set_enabled_buttons(&mut self, id: WindowId, buttons: zng_view_api::window::WindowButton) {
2055 self.with_window(id, |w| w.set_enabled_buttons(buttons), || ())
2056 }
2057
2058 fn open_title_bar_context_menu(&mut self, id: WindowId, position: DipPoint) {
2059 self.with_window(id, |w| w.open_title_bar_context_menu(position), || ())
2060 }
2061
2062 fn set_cursor(&mut self, id: WindowId, icon: Option<CursorIcon>) {
2063 self.with_window(id, |w| w.set_cursor(icon), || ())
2064 }
2065
2066 fn set_cursor_image(&mut self, id: WindowId, icon: Option<CursorImage>) {
2067 let icon = icon.and_then(|img| self.image_cache.get(img.img).and_then(|i| i.cursor(img.hotspot, &self.winit_loop)));
2068 self.with_window(id, |w| w.set_cursor_image(icon), || ());
2069 }
2070
2071 fn set_ime_area(&mut self, id: WindowId, area: Option<DipRect>) {
2072 self.with_window(id, |w| w.set_ime_area(area), || ())
2073 }
2074
2075 fn image_decoders(&mut self) -> Vec<Txt> {
2076 image_cache::DECODERS.iter().map(|&s| Txt::from_static(s)).collect()
2077 }
2078
2079 fn image_encoders(&mut self) -> Vec<Txt> {
2080 image_cache::ENCODERS.iter().map(|&s| Txt::from_static(s)).collect()
2081 }
2082
2083 fn add_image(&mut self, request: ImageRequest<IpcBytes>) -> ImageId {
2084 self.image_cache.add(request)
2085 }
2086
2087 fn add_image_pro(&mut self, request: ImageRequest<IpcBytesReceiver>) -> ImageId {
2088 self.image_cache.add_pro(request)
2089 }
2090
2091 fn forget_image(&mut self, id: ImageId) {
2092 self.image_cache.forget(id)
2093 }
2094
2095 fn encode_image(&mut self, id: ImageId, format: Txt) {
2096 self.image_cache.encode(id, format)
2097 }
2098
2099 fn use_image(&mut self, id: WindowId, image_id: ImageId) -> ImageTextureId {
2100 if let Some(img) = self.image_cache.get(image_id) {
2101 with_window_or_surface!(self, id, |w| w.use_image(img), || ImageTextureId::INVALID)
2102 } else {
2103 ImageTextureId::INVALID
2104 }
2105 }
2106
2107 fn update_image_use(&mut self, id: WindowId, texture_id: ImageTextureId, image_id: ImageId) {
2108 if let Some(img) = self.image_cache.get(image_id) {
2109 with_window_or_surface!(self, id, |w| w.update_image(texture_id, img), || ())
2110 }
2111 }
2112
2113 fn delete_image_use(&mut self, id: WindowId, texture_id: ImageTextureId) {
2114 with_window_or_surface!(self, id, |w| w.delete_image(texture_id), || ())
2115 }
2116
2117 fn add_audio(&mut self, _request: audio::AudioRequest<IpcBytes>) -> audio::AudioId {
2118 unimplemented!()
2119 }
2120
2121 fn add_audio_pro(&mut self, _request: audio::AudioRequest<IpcBytesReceiver>) -> audio::AudioId {
2122 unimplemented!()
2123 }
2124
2125 fn audio_decoders(&mut self) -> Vec<Txt> {
2126 unimplemented!()
2127 }
2128
2129 fn forget_audio(&mut self, _id: audio::AudioId) {
2130 unimplemented!()
2131 }
2132
2133 fn playback(&mut self, _request: audio::PlaybackRequest) -> audio::PlaybackId {
2134 unimplemented!()
2135 }
2136
2137 fn playback_update(&mut self, _id: audio::PlaybackId, _request: audio::PlaybackUpdateRequest) {
2138 unimplemented!()
2139 }
2140
2141 fn add_font_face(&mut self, id: WindowId, bytes: font::IpcFontBytes, index: u32) -> FontFaceId {
2142 with_window_or_surface!(self, id, |w| w.add_font_face(bytes, index), || FontFaceId::INVALID)
2143 }
2144
2145 fn delete_font_face(&mut self, id: WindowId, font_face_id: FontFaceId) {
2146 with_window_or_surface!(self, id, |w| w.delete_font_face(font_face_id), || ())
2147 }
2148
2149 fn add_font(
2150 &mut self,
2151 id: WindowId,
2152 font_face_id: FontFaceId,
2153 glyph_size: Px,
2154 options: FontOptions,
2155 variations: Vec<(FontVariationName, f32)>,
2156 ) -> FontId {
2157 with_window_or_surface!(self, id, |w| w.add_font(font_face_id, glyph_size, options, variations), || {
2158 FontId::INVALID
2159 })
2160 }
2161
2162 fn delete_font(&mut self, id: WindowId, font_id: FontId) {
2163 with_window_or_surface!(self, id, |w| w.delete_font(font_id), || ())
2164 }
2165
2166 fn set_capture_mode(&mut self, id: WindowId, enabled: bool) {
2167 self.with_window(id, |w| w.set_capture_mode(enabled), || ())
2168 }
2169
2170 fn frame_image(&mut self, id: WindowId, mask: Option<ImageMaskMode>) -> ImageId {
2171 with_window_or_surface!(self, id, |w| w.frame_image(&mut self.image_cache, mask), || ImageId::INVALID)
2172 }
2173
2174 fn frame_image_rect(&mut self, id: WindowId, rect: PxRect, mask: Option<ImageMaskMode>) -> ImageId {
2175 with_window_or_surface!(self, id, |w| w.frame_image_rect(&mut self.image_cache, rect, mask), || {
2176 ImageId::INVALID
2177 })
2178 }
2179
2180 fn render(&mut self, id: WindowId, frame: FrameRequest) {
2181 with_window_or_surface!(self, id, |w| w.render(frame), || ())
2182 }
2183
2184 fn render_update(&mut self, id: WindowId, frame: FrameUpdateRequest) {
2185 with_window_or_surface!(self, id, |w| w.render_update(frame), || ())
2186 }
2187
2188 fn access_update(&mut self, id: WindowId, update: access::AccessTreeUpdate) {
2189 if let Some(s) = self.windows.iter_mut().find(|s| s.id() == id) {
2190 s.access_update(update, &self.app_sender);
2191 }
2192 }
2193
2194 fn message_dialog(&mut self, id: WindowId, dialog: MsgDialog) -> DialogId {
2195 let r_id = self.dialog_id_gen.incr();
2196 if let Some(s) = self.windows.iter_mut().find(|s| s.id() == id) {
2197 s.message_dialog(dialog, r_id, self.app_sender.clone());
2198 } else {
2199 let r = MsgDialogResponse::Error(Txt::from_static("window not found"));
2200 let _ = self.app_sender.send(AppEvent::Notify(Event::MsgDialogResponse(r_id, r)));
2201 }
2202 r_id
2203 }
2204
2205 fn file_dialog(&mut self, id: WindowId, dialog: FileDialog) -> DialogId {
2206 let r_id = self.dialog_id_gen.incr();
2207 if let Some(s) = self.windows.iter_mut().find(|s| s.id() == id) {
2208 s.file_dialog(dialog, r_id, self.app_sender.clone());
2209 } else {
2210 let r = MsgDialogResponse::Error(Txt::from_static("window not found"));
2211 let _ = self.app_sender.send(AppEvent::Notify(Event::MsgDialogResponse(r_id, r)));
2212 };
2213 r_id
2214 }
2215
2216 #[cfg(windows)]
2217 fn read_clipboard(&mut self, data_type: clipboard::ClipboardType) -> Result<clipboard::ClipboardData, clipboard::ClipboardError> {
2218 match data_type {
2219 clipboard::ClipboardType::Text => {
2220 let _clip = clipboard_win::Clipboard::new_attempts(10).map_err(util::clipboard_win_to_clip)?;
2221
2222 clipboard_win::get(clipboard_win::formats::Unicode)
2223 .map_err(util::clipboard_win_to_clip)
2224 .map(|s: String| clipboard::ClipboardData::Text(Txt::from_str(&s)))
2225 }
2226 clipboard::ClipboardType::Image => {
2227 let _clip = clipboard_win::Clipboard::new_attempts(10).map_err(util::clipboard_win_to_clip)?;
2228
2229 let bitmap = clipboard_win::get(clipboard_win::formats::Bitmap).map_err(util::clipboard_win_to_clip)?;
2230
2231 let id = self.image_cache.add(ImageRequest::new(
2232 image::ImageDataFormat::FileExtension(Txt::from_str("bmp")),
2233 IpcBytes::from_vec(bitmap),
2234 u64::MAX,
2235 None,
2236 None,
2237 ));
2238 Ok(clipboard::ClipboardData::Image(id))
2239 }
2240 clipboard::ClipboardType::FileList => {
2241 let _clip = clipboard_win::Clipboard::new_attempts(10).map_err(util::clipboard_win_to_clip)?;
2242
2243 clipboard_win::get(clipboard_win::formats::FileList)
2244 .map_err(util::clipboard_win_to_clip)
2245 .map(clipboard::ClipboardData::FileList)
2246 }
2247 clipboard::ClipboardType::Extension(_) => Err(clipboard::ClipboardError::NotSupported),
2248 _ => Err(clipboard::ClipboardError::NotSupported),
2249 }
2250 }
2251
2252 #[cfg(windows)]
2253 fn write_clipboard(&mut self, data: clipboard::ClipboardData) -> Result<(), clipboard::ClipboardError> {
2254 use zng_txt::formatx;
2255
2256 match data {
2257 clipboard::ClipboardData::Text(t) => {
2258 let _clip = clipboard_win::Clipboard::new_attempts(10).map_err(util::clipboard_win_to_clip)?;
2259
2260 clipboard_win::set(clipboard_win::formats::Unicode, t).map_err(util::clipboard_win_to_clip)
2261 }
2262 clipboard::ClipboardData::Image(id) => {
2263 let _clip = clipboard_win::Clipboard::new_attempts(10).map_err(util::clipboard_win_to_clip)?;
2264
2265 if let Some(img) = self.image_cache.get(id) {
2266 let mut bmp = vec![];
2267 img.encode(::image::ImageFormat::Bmp, &mut bmp)
2268 .map_err(|e| clipboard::ClipboardError::Other(formatx!("{e:?}")))?;
2269 clipboard_win::set(clipboard_win::formats::Bitmap, bmp).map_err(util::clipboard_win_to_clip)
2270 } else {
2271 Err(clipboard::ClipboardError::Other(Txt::from_str("image not found")))
2272 }
2273 }
2274 clipboard::ClipboardData::FileList(l) => {
2275 use clipboard_win::Setter;
2276 let _clip = clipboard_win::Clipboard::new_attempts(10).map_err(util::clipboard_win_to_clip)?;
2277
2278 let strs = l.into_iter().map(|p| p.display().to_string()).collect::<Vec<String>>();
2280 clipboard_win::formats::FileList
2281 .write_clipboard(&strs)
2282 .map_err(util::clipboard_win_to_clip)
2283 }
2284 clipboard::ClipboardData::Extension { .. } => Err(clipboard::ClipboardError::NotSupported),
2285 _ => Err(clipboard::ClipboardError::NotSupported),
2286 }
2287 }
2288
2289 #[cfg(not(any(windows, target_os = "android")))]
2290 fn read_clipboard(&mut self, data_type: clipboard::ClipboardType) -> Result<clipboard::ClipboardData, clipboard::ClipboardError> {
2291 match data_type {
2292 clipboard::ClipboardType::Text => self
2293 .arboard()?
2294 .get_text()
2295 .map_err(util::arboard_to_clip)
2296 .map(|s| clipboard::ClipboardData::Text(zng_txt::Txt::from(s))),
2297 clipboard::ClipboardType::Image => {
2298 let bitmap = self.arboard()?.get_image().map_err(util::arboard_to_clip)?;
2299 let mut data = bitmap.bytes.into_owned();
2300 for rgba in data.chunks_exact_mut(4) {
2301 rgba.swap(0, 2); }
2303 let id = self.image_cache.add(image::ImageRequest::new(
2304 image::ImageDataFormat::Bgra8 {
2305 size: zng_unit::PxSize::new(Px(bitmap.width as _), Px(bitmap.height as _)),
2306 density: None,
2307 },
2308 IpcBytes::from_vec(data),
2309 u64::MAX,
2310 None,
2311 None,
2312 ));
2313 Ok(clipboard::ClipboardData::Image(id))
2314 }
2315 clipboard::ClipboardType::FileList => self
2316 .arboard()?
2317 .get()
2318 .file_list()
2319 .map_err(util::arboard_to_clip)
2320 .map(clipboard::ClipboardData::FileList),
2321 clipboard::ClipboardType::Extension(_) => Err(clipboard::ClipboardError::NotSupported),
2322 _ => Err(clipboard::ClipboardError::NotSupported),
2323 }
2324 }
2325
2326 #[cfg(not(any(windows, target_os = "android")))]
2327 fn write_clipboard(&mut self, data: clipboard::ClipboardData) -> Result<(), clipboard::ClipboardError> {
2328 match data {
2329 clipboard::ClipboardData::Text(t) => self.arboard()?.set_text(t).map_err(util::arboard_to_clip),
2330 clipboard::ClipboardData::Image(id) => {
2331 self.arboard()?;
2332 if let Some(img) = self.image_cache.get(id) {
2333 let size = img.size();
2334 let mut data = img.pixels().clone().to_vec();
2335 for rgba in data.chunks_exact_mut(4) {
2336 rgba.swap(0, 2); }
2338 let board = self.arboard()?;
2339 let _ = board.set_image(arboard::ImageData {
2340 width: size.width.0 as _,
2341 height: size.height.0 as _,
2342 bytes: std::borrow::Cow::Owned(data),
2343 });
2344 Ok(())
2345 } else {
2346 Err(clipboard::ClipboardError::Other(zng_txt::Txt::from_static("image not found")))
2347 }
2348 }
2349 clipboard::ClipboardData::FileList(_) => Err(clipboard::ClipboardError::NotSupported),
2350 clipboard::ClipboardData::Extension { .. } => Err(clipboard::ClipboardError::NotSupported),
2351 _ => Err(clipboard::ClipboardError::NotSupported),
2352 }
2353 }
2354
2355 #[cfg(target_os = "android")]
2356 fn read_clipboard(&mut self, data_type: clipboard::ClipboardType) -> Result<clipboard::ClipboardData, clipboard::ClipboardError> {
2357 let _ = data_type;
2358 Err(clipboard::ClipboardError::Other(Txt::from_static(
2359 "clipboard not implemented for Android",
2360 )))
2361 }
2362
2363 #[cfg(target_os = "android")]
2364 fn write_clipboard(&mut self, data: clipboard::ClipboardData) -> Result<(), clipboard::ClipboardError> {
2365 let _ = data;
2366 Err(clipboard::ClipboardError::Other(Txt::from_static(
2367 "clipboard not implemented for Android",
2368 )))
2369 }
2370
2371 fn start_drag_drop(
2372 &mut self,
2373 id: WindowId,
2374 data: Vec<DragDropData>,
2375 allowed_effects: DragDropEffect,
2376 ) -> Result<DragDropId, DragDropError> {
2377 let _ = (id, data, allowed_effects); Err(DragDropError::NotSupported)
2379 }
2380
2381 fn cancel_drag_drop(&mut self, id: WindowId, drag_id: DragDropId) {
2382 let _ = (id, drag_id);
2383 }
2384
2385 fn drag_dropped(&mut self, id: WindowId, drop_id: DragDropId, applied: DragDropEffect) {
2386 let _ = (id, drop_id, applied); }
2388
2389 fn set_system_shutdown_warn(&mut self, id: WindowId, reason: Txt) {
2390 self.with_window(id, move |w| w.set_system_shutdown_warn(reason), || ())
2391 }
2392
2393 fn third_party_licenses(&mut self) -> Vec<zng_tp_licenses::LicenseUsed> {
2394 #[cfg(feature = "bundle_licenses")]
2395 {
2396 zng_tp_licenses::include_bundle!()
2397 }
2398 #[cfg(not(feature = "bundle_licenses"))]
2399 {
2400 vec![]
2401 }
2402 }
2403
2404 fn app_extension(&mut self, extension_id: ApiExtensionId, extension_request: ApiExtensionPayload) -> ApiExtensionPayload {
2405 self.exts.call_command(extension_id, extension_request)
2406 }
2407
2408 fn window_extension(
2409 &mut self,
2410 id: WindowId,
2411 extension_id: ApiExtensionId,
2412 extension_request: ApiExtensionPayload,
2413 ) -> ApiExtensionPayload {
2414 self.with_window(
2415 id,
2416 |w| w.window_extension(extension_id, extension_request),
2417 || ApiExtensionPayload::invalid_request(extension_id, "window not found"),
2418 )
2419 }
2420
2421 fn render_extension(
2422 &mut self,
2423 id: WindowId,
2424 extension_id: ApiExtensionId,
2425 extension_request: ApiExtensionPayload,
2426 ) -> ApiExtensionPayload {
2427 with_window_or_surface!(self, id, |w| w.render_extension(extension_id, extension_request), || {
2428 ApiExtensionPayload::invalid_request(extension_id, "renderer not found")
2429 })
2430 }
2431
2432 fn ping(&mut self, count: u16) -> u16 {
2433 self.notify(Event::Pong(count));
2434 count
2435 }
2436}
2437
2438#[derive(Debug)]
2440#[allow(clippy::large_enum_variant)]
2441pub(crate) enum AppEvent {
2442 Request,
2444 Notify(Event),
2446 #[cfg_attr(not(windows), allow(unused))]
2448 RefreshMonitors,
2449
2450 #[cfg_attr(not(windows), allow(unused))]
2452 WinitFocused(winit::window::WindowId, bool),
2453
2454 ParentProcessExited,
2456
2457 ImageLoaded(ImageLoadedData),
2459
2460 SetDeviceEventsFilter(DeviceEventsFilter),
2462
2463 #[allow(unused)]
2465 MonitorPowerChanged,
2466}
2467
2468#[allow(clippy::large_enum_variant)] #[derive(Debug)]
2474enum RequestEvent {
2475 Request(Request),
2477 FrameReady(WindowId, FrameReadyMsg),
2479}
2480
2481#[derive(Debug)]
2482pub(crate) struct FrameReadyMsg {
2483 pub composite_needed: bool,
2484}
2485
2486#[derive(Clone)]
2488pub(crate) enum AppEventSender {
2489 Headed(EventLoopProxy<AppEvent>, flume::Sender<RequestEvent>),
2490 Headless(flume::Sender<AppEvent>, flume::Sender<RequestEvent>),
2491}
2492impl AppEventSender {
2493 fn send(&self, ev: AppEvent) -> Result<(), ipc::ViewChannelError> {
2495 match self {
2496 AppEventSender::Headed(p, _) => p.send_event(ev).map_err(|_| ipc::ViewChannelError::Disconnected),
2497 AppEventSender::Headless(p, _) => p.send(ev).map_err(|_| ipc::ViewChannelError::Disconnected),
2498 }
2499 }
2500
2501 fn request(&self, req: Request) -> Result<(), ipc::ViewChannelError> {
2503 match self {
2504 AppEventSender::Headed(_, p) => p.send(RequestEvent::Request(req)).map_err(|_| ipc::ViewChannelError::Disconnected),
2505 AppEventSender::Headless(_, p) => p.send(RequestEvent::Request(req)).map_err(|_| ipc::ViewChannelError::Disconnected),
2506 }?;
2507 self.send(AppEvent::Request)
2508 }
2509
2510 fn frame_ready(&self, window_id: WindowId, msg: FrameReadyMsg) -> Result<(), ipc::ViewChannelError> {
2512 match self {
2513 AppEventSender::Headed(_, p) => p
2514 .send(RequestEvent::FrameReady(window_id, msg))
2515 .map_err(|_| ipc::ViewChannelError::Disconnected),
2516 AppEventSender::Headless(_, p) => p
2517 .send(RequestEvent::FrameReady(window_id, msg))
2518 .map_err(|_| ipc::ViewChannelError::Disconnected),
2519 }?;
2520 self.send(AppEvent::Request)
2521 }
2522}
2523
2524pub(crate) struct WrNotifier {
2526 id: WindowId,
2527 sender: AppEventSender,
2528}
2529impl WrNotifier {
2530 pub fn create(id: WindowId, sender: AppEventSender) -> Box<dyn RenderNotifier> {
2531 Box::new(WrNotifier { id, sender })
2532 }
2533}
2534impl RenderNotifier for WrNotifier {
2535 fn clone(&self) -> Box<dyn RenderNotifier> {
2536 Box::new(Self {
2537 id: self.id,
2538 sender: self.sender.clone(),
2539 })
2540 }
2541
2542 fn wake_up(&self, _: bool) {}
2543
2544 fn new_frame_ready(&self, _: DocumentId, _: FramePublishId, params: &FrameReadyParams) {
2545 let msg = FrameReadyMsg {
2547 composite_needed: params.render,
2548 };
2549 let _ = self.sender.frame_ready(self.id, msg);
2550 }
2551}
2552
2553#[cfg(target_arch = "wasm32")]
2554compile_error!("zng-view does not support Wasm");