1use crate::window_modifiers::WindowModifiers;
2use glutin::context::GlProfile;
3use vizia_core::context::TreeProps;
4use vizia_window::{AnchorTarget, WindowDescription};
5#[cfg(target_os = "windows")]
6use winit::platform::windows::WindowExtWindows;
7#[cfg(target_os = "windows")]
8use winit::{platform::windows::WindowAttributesExtWindows, raw_window_handle::RawWindowHandle};
9
10use crate::convert::cursor_icon_to_cursor_icon;
11use hashbrown::HashMap;
12use std::error::Error;
13use std::num::NonZeroU32;
14use std::{ffi::CString, sync::Arc};
15use winit::raw_window_handle::HasWindowHandle;
16
17use gl_rs as gl;
18use glutin::config::Config;
19use glutin_winit::DisplayBuilder;
20
21use gl::types::*;
22
23use glutin::{
24 config::ConfigTemplateBuilder,
25 context::{ContextApi, ContextAttributesBuilder},
26 display::GetGlDisplay,
27 prelude::*,
28 surface::{SurfaceAttributesBuilder, WindowSurface},
29};
30
31use skia_safe::{
32 gpu::{
33 self, backend_render_targets, ganesh::context_options, gl::FramebufferInfo, ContextOptions,
34 SurfaceOrigin,
35 },
36 ColorSpace, ColorType, PixelGeometry, Surface, SurfaceProps, SurfacePropsFlags,
37};
38
39use vizia_core::prelude::*;
40use winit::event_loop::ActiveEventLoop;
41use winit::window::{CursorGrabMode, CursorIcon, CustomCursor, WindowAttributes, WindowLevel};
42use winit::{dpi::*, window::WindowId};
43
44pub struct WinState {
45 pub entity: Entity,
46 gl_config: Config,
47 gl_context: glutin::context::PossiblyCurrentContext,
48 pub gl_surface: glutin::surface::Surface<glutin::surface::WindowSurface>,
49 pub id: WindowId,
50 pub gr_context: skia_safe::gpu::DirectContext,
51 pub window: Arc<winit::window::Window>,
52 pub surface: skia_safe::Surface,
53 pub dirty_surface: skia_safe::Surface,
54 pub should_close: bool,
55 #[cfg(target_os = "windows")]
56 pub is_initially_cloaked: bool,
57}
58
59impl Drop for WinState {
60 fn drop(&mut self) {
61 self.gl_context.make_current(&self.gl_surface).unwrap();
62 }
63}
64
65impl WinState {
66 pub fn new(
67 event_loop: &ActiveEventLoop,
68 entity: Entity,
69 #[allow(unused_mut)] mut window_attributes: WindowAttributes,
70 #[allow(unused_variables)] owner: Option<Arc<winit::window::Window>>,
71 ) -> Result<Self, Box<dyn Error>> {
72 #[cfg(target_os = "windows")]
73 let (window, gl_config) = {
74 if let Some(owner) = owner {
75 let RawWindowHandle::Win32(handle) = owner.window_handle().unwrap().as_raw() else {
76 unreachable!();
77 };
78 window_attributes = window_attributes.with_owner_window(handle.hwnd.get());
79 }
80
81 let window_attributes = window_attributes.with_visible(false);
84
85 let (window, config) = build_window(event_loop, window_attributes);
86
87 let window = window.expect("Could not create window with OpenGL context");
88 set_cloak(&window, true);
93
94 (window, config)
95 };
96
97 #[cfg(not(target_os = "windows"))]
98 let (window, gl_config) = {
99 let (window, config) = build_window(event_loop, window_attributes);
100 let window = window.expect("Could not create window with OpenGL context");
101 (window, config)
102 };
103
104 window.set_ime_allowed(true);
105
106 let raw_window_handle = window.window_handle().unwrap().as_raw();
107
108 let gl_display = gl_config.display();
109
110 let context_attributes = ContextAttributesBuilder::new()
111 .with_profile(GlProfile::Core)
112 .with_context_api(ContextApi::OpenGl(None))
113 .build(Some(raw_window_handle));
114
115 let fallback_context_attributes = ContextAttributesBuilder::new()
116 .with_profile(GlProfile::Core)
117 .with_context_api(ContextApi::Gles(None))
118 .build(Some(raw_window_handle));
119
120 let not_current_gl_context = unsafe {
121 gl_display.create_context(&gl_config, &context_attributes).unwrap_or_else(|_| {
122 gl_display
123 .create_context(&gl_config, &fallback_context_attributes)
124 .expect("failed to create context")
125 })
126 };
127
128 let (width, height): (u32, u32) = window.inner_size().into();
129
130 let attrs = SurfaceAttributesBuilder::<WindowSurface>::new().with_srgb(Some(true)).build(
131 raw_window_handle,
132 NonZeroU32::new(width.max(1)).unwrap(),
133 NonZeroU32::new(height.max(1)).unwrap(),
134 );
135
136 let gl_surface =
137 unsafe { gl_config.display().create_window_surface(&gl_config, &attrs).unwrap() };
138
139 let gl_context = not_current_gl_context.make_current(&gl_surface).unwrap();
140
141 gl::load_with(|s| {
149 gl_config.display().get_proc_address(CString::new(s).unwrap().as_c_str())
150 });
151
152 let interface = skia_safe::gpu::gl::Interface::new_load_with(|name| {
153 if name == "eglGetCurrentDisplay" {
154 return std::ptr::null();
155 }
156 gl_config.display().get_proc_address(CString::new(name).unwrap().as_c_str())
157 })
158 .expect("Could not create interface");
159
160 let mut context_options = ContextOptions::new();
162 context_options.skip_gl_error_checks = context_options::Enable::Yes;
163
164 let mut gr_context = skia_safe::gpu::direct_contexts::make_gl(interface, &context_options)
165 .expect("Could not create direct context");
166
167 let fb_info = {
168 let mut fboid: GLint = 0;
169 unsafe { gl::GetIntegerv(gl::FRAMEBUFFER_BINDING, &mut fboid) };
170
171 FramebufferInfo {
172 fboid: fboid.try_into().unwrap(),
173 format: skia_safe::gpu::gl::Format::RGBA8.into(),
174 ..Default::default()
175 }
176 };
177
178 let num_samples = gl_config.num_samples() as usize;
179 let stencil_size = gl_config.stencil_size() as usize;
180
181 let mut surface =
182 create_surface(&window, fb_info, &mut gr_context, num_samples, stencil_size);
183
184 let inner_size = window.inner_size();
185
186 let dirty_surface = surface
187 .new_surface_with_dimensions((inner_size.width as i32, inner_size.height as i32))
188 .unwrap();
189
190 Ok(WinState {
192 entity,
193 gl_config,
194 gl_context,
195 id: window.id(),
196 gr_context,
197 gl_surface,
198 window: Arc::new(window),
199 surface,
200 dirty_surface,
201 should_close: false,
202 #[cfg(target_os = "windows")]
203 is_initially_cloaked: true,
204 })
205 }
206
207 pub fn window(&self) -> &winit::window::Window {
209 &self.window
210 }
211
212 pub fn make_current(&mut self) {
213 self.gl_context.make_current(&self.gl_surface).unwrap();
214 }
215
216 pub fn resize(&mut self, size: PhysicalSize<u32>) {
217 self.gl_context.make_current(&self.gl_surface).unwrap();
218 let (width, height): (u32, u32) = size.into();
219
220 if width == 0 || height == 0 {
221 return;
222 }
223
224 let fb_info = {
225 let mut fboid: GLint = 0;
226 unsafe { gl::GetIntegerv(gl::FRAMEBUFFER_BINDING, &mut fboid) };
227
228 FramebufferInfo {
229 fboid: fboid.try_into().unwrap(),
230 format: skia_safe::gpu::gl::Format::RGBA8.into(),
231 ..Default::default()
232 }
233 };
234
235 self.surface = create_surface(
236 &self.window,
237 fb_info,
238 &mut self.gr_context,
239 self.gl_config.num_samples() as usize,
240 self.gl_config.stencil_size() as usize,
241 );
242
243 self.dirty_surface = self
244 .surface
245 .new_surface_with_dimensions((width.max(1) as i32, height.max(1) as i32))
246 .unwrap();
247
248 self.gl_surface.resize(
249 &self.gl_context,
250 NonZeroU32::new(width.max(1)).unwrap(),
251 NonZeroU32::new(height.max(1)).unwrap(),
252 );
253 }
254
255 pub fn swap_buffers(&mut self) {
256 self.gr_context.flush_and_submit();
257 self.gl_surface.swap_buffers(&self.gl_context).expect("Failed to swap buffers");
258 }
259}
260
261fn build_window(
262 event_loop: &ActiveEventLoop,
263 window_attributes: WindowAttributes,
264) -> (Option<winit::window::Window>, Config) {
265 let template = ConfigTemplateBuilder::new().with_alpha_size(8).with_transparency(true);
266 let display_builder = DisplayBuilder::new().with_window_attributes(Some(window_attributes));
267
268 display_builder
269 .build(event_loop, template, |configs| {
270 configs
273 .reduce(|accum, config| {
274 let transparency_check = config.supports_transparency().unwrap_or(false)
275 & !accum.supports_transparency().unwrap_or(false);
276
277 if transparency_check || config.num_samples() < accum.num_samples() {
278 config
279 } else {
280 accum
281 }
282 })
283 .unwrap()
284 })
285 .unwrap()
286}
287
288#[cfg(target_os = "windows")]
293pub fn set_cloak(window: &winit::window::Window, state: bool) -> bool {
294 use windows_sys::Win32::{
295 Foundation::{BOOL, FALSE, HWND, TRUE},
296 Graphics::Dwm::{DwmSetWindowAttribute, DWMWA_CLOAK},
297 };
298
299 let RawWindowHandle::Win32(handle) = window.window_handle().unwrap().as_raw() else {
300 unreachable!();
301 };
302
303 let value = if state { TRUE } else { FALSE };
304
305 let result = unsafe {
306 DwmSetWindowAttribute(
307 handle.hwnd.get() as HWND,
308 DWMWA_CLOAK as u32,
309 std::ptr::from_ref(&value).cast(),
310 std::mem::size_of::<BOOL>() as u32,
311 )
312 };
313
314 result == 0 }
316
317pub fn create_surface(
318 window: &winit::window::Window,
319 fb_info: FramebufferInfo,
320 gr_context: &mut skia_safe::gpu::DirectContext,
321 num_samples: usize,
322 stencil_size: usize,
323) -> Surface {
324 let size = window.inner_size();
325 let size = (
326 size.width.try_into().expect("Could not convert width"),
327 size.height.try_into().expect("Could not convert height"),
328 );
329
330 let backend_render_target =
331 backend_render_targets::make_gl(size, num_samples, stencil_size, fb_info);
332
333 let surface_props = SurfaceProps::new_with_text_properties(
334 SurfacePropsFlags::default(),
335 PixelGeometry::default(),
336 0.5,
337 0.0,
338 );
339
340 gpu::surfaces::wrap_backend_render_target(
341 gr_context,
342 &backend_render_target,
343 SurfaceOrigin::BottomLeft,
344 ColorType::RGBA8888,
345 ColorSpace::new_srgb(),
346 Some(surface_props).as_ref(),
347 )
349 .expect("Could not create skia surface")
350}
351
352type WindowCallback = Option<Box<dyn Fn(&mut EventContext)>>;
353
354pub struct Window {
355 pub window: Option<Arc<winit::window::Window>>,
356 pub on_close: WindowCallback,
357 pub on_create: WindowCallback,
358 pub should_close: bool,
359 pub(crate) custom_cursors: Arc<HashMap<CursorIcon, CustomCursor>>,
360}
361
362impl Window {
363 fn window(&self) -> &winit::window::Window {
364 self.window.as_ref().unwrap()
365 }
366
367 pub fn new(cx: &mut Context, content: impl 'static + Fn(&mut Context)) -> Handle<Self> {
368 Self {
369 window: None,
370 on_close: None,
371 on_create: None,
372 should_close: false,
373 custom_cursors: Default::default(),
374 }
375 .build(cx, |cx| {
376 cx.windows.insert(
377 cx.current(),
378 WindowState {
379 content: Some(Arc::new(content)),
380 window_description: WindowDescription::new(),
381 ..Default::default()
382 },
383 );
384 cx.tree.set_window(cx.current(), true);
385 })
386 .position_type(PositionType::Absolute)
387 .anchor_target(AnchorTarget::Window)
388 }
389
390 pub fn popup(
391 cx: &mut Context,
392 is_modal: bool,
393 content: impl 'static + Fn(&mut Context),
394 ) -> Handle<Self> {
395 Self {
396 window: None,
397 on_close: None,
398 on_create: None,
399 should_close: false,
400 custom_cursors: Default::default(),
401 }
402 .build(cx, |cx| {
403 let parent_window = cx.parent_window();
404 if is_modal {
405 cx.emit_to(parent_window, WindowEvent::SetEnabled(false));
406 }
407
408 cx.windows.insert(
409 cx.current(),
410 WindowState {
411 owner: Some(parent_window),
412 is_modal: true,
413 content: Some(Arc::new(content)),
414 window_description: WindowDescription::new(),
415 ..Default::default()
416 },
417 );
418 cx.tree.set_window(cx.current(), true);
419 })
420 .position_type(PositionType::Absolute)
421 .anchor_target(AnchorTarget::Window)
422 .lock_focus_to_within()
423 }
424}
425
426impl View for Window {
427 fn element(&self) -> Option<&'static str> {
428 Some("window")
429 }
430
431 fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
432 event.map(|window_event, meta| match window_event {
433 WindowEvent::Destroyed => {
434 let parent_window = cx.parent_window();
435 cx.emit_to(parent_window, WindowEvent::SetEnabled(true));
436 }
437
438 WindowEvent::GrabCursor(flag) => {
439 let grab_mode = if *flag { CursorGrabMode::Locked } else { CursorGrabMode::None };
440 self.window().set_cursor_grab(grab_mode).expect("Failed to set cursor grab");
441 }
442
443 WindowEvent::SetCursorPosition(x, y) => {
444 self.window()
445 .set_cursor_position(winit::dpi::Position::Physical(PhysicalPosition::new(
446 *x as i32, *y as i32,
447 )))
448 .expect("Failed to set cursor position");
449 }
450
451 WindowEvent::SetCursor(cursor) => {
452 let Some(icon) = cursor_icon_to_cursor_icon(*cursor) else {
453 self.window().set_cursor_visible(false);
454 return;
455 };
456
457 if let Some(custom_icon) = self.custom_cursors.get(&icon) {
458 self.window().set_cursor(custom_icon.clone());
459 } else {
460 self.window().set_cursor(icon);
461 }
462
463 self.window().set_cursor_visible(true);
464 }
465
466 WindowEvent::SetTitle(title) => {
467 self.window().set_title(title);
468 }
469
470 WindowEvent::SetSize(size) => {
471 let _ = self.window().request_inner_size(LogicalSize::new(size.width, size.height));
472 }
473
474 WindowEvent::SetMinSize(size) => {
475 self.window()
476 .set_min_inner_size(size.map(|size| LogicalSize::new(size.width, size.height)));
477 }
478
479 WindowEvent::SetMaxSize(size) => {
480 self.window()
481 .set_max_inner_size(size.map(|size| LogicalSize::new(size.width, size.height)));
482 }
483
484 WindowEvent::SetPosition(pos) => {
485 self.window().set_outer_position(LogicalPosition::new(pos.x, pos.y));
486 meta.consume();
487 }
488
489 WindowEvent::SetResizable(flag) => {
490 self.window().set_resizable(*flag);
491 }
492
493 WindowEvent::SetMinimized(flag) => {
494 self.window().set_minimized(*flag);
495 }
496
497 WindowEvent::SetMaximized(flag) => {
498 self.window().set_maximized(*flag);
499 }
500
501 WindowEvent::SetVisible(flag) => {
502 self.window().set_visible(*flag);
503
504 meta.consume();
505 }
506
507 WindowEvent::SetDecorations(flag) => {
508 self.window().set_decorations(*flag);
509 }
510
511 WindowEvent::ReloadStyles => {
512 cx.reload_styles().unwrap();
513 }
514
515 WindowEvent::WindowClose => {
516 self.should_close = true;
517
518 cx.close_window();
519
520 if let Some(callback) = &self.on_close {
521 callback(cx);
522 }
523
524 meta.consume();
525 }
526
527 WindowEvent::FocusNext => {
528 cx.focus_next();
529 }
530
531 WindowEvent::FocusPrev => {
532 cx.focus_prev();
533 }
534
535 WindowEvent::Redraw => {
536 self.window().request_redraw();
537 }
538
539 #[allow(unused_variables)]
540 WindowEvent::SetEnabled(flag) => {
541 #[cfg(target_os = "windows")]
542 self.window().set_enable(*flag);
543
544 self.window().focus_window();
545 }
546
547 WindowEvent::DragWindow => {
548 self.window().drag_window().expect("Failed to init drag window");
549 meta.consume();
550 }
551
552 WindowEvent::SetAlwaysOnTop(flag) => {
553 self.window().set_window_level(if *flag {
554 WindowLevel::AlwaysOnTop
555 } else {
556 WindowLevel::Normal
557 });
558 }
559
560 WindowEvent::SetImeCursorArea(position, size) => {
561 let position = PhysicalPosition::new(position.0 as i32, position.1 as i32);
562 let size = PhysicalSize::new(size.0, size.1);
563 self.window().set_ime_cursor_area(position, size);
564 }
565
566 _ => {}
567 })
568 }
569}
570
571impl WindowModifiers for Handle<'_, Window> {
572 fn on_close(self, callback: impl Fn(&mut EventContext) + 'static) -> Self {
573 self.modify(|window| window.on_close = Some(Box::new(callback)))
574 }
575
576 fn on_create(self, callback: impl Fn(&mut EventContext) + 'static) -> Self {
577 self.modify(|window| window.on_create = Some(Box::new(callback)))
578 }
579
580 fn title<T: ToString>(mut self, title: impl Res<T>) -> Self {
581 let entity = self.entity();
582 let title = title.get(&self).to_string();
583 if let Some(win_state) = self.context().windows.get_mut(&entity) {
584 win_state.window_description.title = title;
585 }
586
587 self
588 }
589
590 fn inner_size<S: Into<WindowSize>>(mut self, size: impl Res<S>) -> Self {
591 let entity = self.entity();
592 let size = size.get(&self).into();
593 if let Some(win_state) = self.context().windows.get_mut(&entity) {
594 win_state.window_description.inner_size = size;
595 }
596
597 self
598 }
599
600 fn min_inner_size<S: Into<WindowSize>>(mut self, size: impl Res<Option<S>>) -> Self {
601 let entity = self.entity();
602 let size = size.get(&self).map(|size| size.into());
603 if let Some(win_state) = self.context().windows.get_mut(&entity) {
604 win_state.window_description.min_inner_size = size;
605 }
606
607 self
608 }
609
610 fn max_inner_size<S: Into<WindowSize>>(mut self, size: impl Res<Option<S>>) -> Self {
611 let entity = self.entity();
612 let size = size.get(&self).map(|size| size.into());
613 if let Some(win_state) = self.context().windows.get_mut(&entity) {
614 win_state.window_description.max_inner_size = size;
615 }
616
617 self
618 }
619
620 fn position<P: Into<vizia_window::WindowPosition>>(mut self, position: impl Res<P>) -> Self {
621 let entity = self.entity();
622 let pos = Some(position.get(&self).into());
623 if let Some(win_state) = self.context().windows.get_mut(&entity) {
624 win_state.window_description.position = pos;
625 }
626
627 self
628 }
629
630 fn offset<P: Into<vizia_window::WindowPosition>>(mut self, offset: impl Res<P>) -> Self {
631 let entity = self.entity();
632 let offset = Some(offset.get(&self).into());
633 if let Some(win_state) = self.context().windows.get_mut(&entity) {
634 win_state.window_description.offset = offset;
635 }
636
637 self
638 }
639
640 fn anchor<P: Into<vizia_window::Anchor>>(mut self, anchor: impl Res<P>) -> Self {
641 let entity = self.entity();
642 let anchor = Some(anchor.get(&self).into());
643 if let Some(win_state) = self.context().windows.get_mut(&entity) {
644 win_state.window_description.anchor = anchor;
645 }
646
647 self
648 }
649
650 fn anchor_target<P: Into<vizia_window::AnchorTarget>>(
651 mut self,
652 anchor_target: impl Res<P>,
653 ) -> Self {
654 let entity = self.entity();
655 let anchor_target = Some(anchor_target.get(&self).into());
656 if let Some(win_state) = self.context().windows.get_mut(&entity) {
657 win_state.window_description.anchor_target = anchor_target;
658 }
659
660 self
661 }
662
663 fn parent_anchor<P: Into<Anchor>>(mut self, parent_anchor: impl Res<P>) -> Self {
664 let entity = self.entity();
665 let parent_anchor = Some(parent_anchor.get(&self).into());
666 if let Some(win_state) = self.context().windows.get_mut(&entity) {
667 win_state.window_description.parent_anchor = parent_anchor;
668 }
669
670 self
671 }
672
673 fn resizable(mut self, flag: impl Res<bool>) -> Self {
674 let entity = self.entity();
675 let flag = flag.get(&self);
676 if let Some(win_state) = self.context().windows.get_mut(&entity) {
677 win_state.window_description.resizable = flag;
678 }
679
680 self
681 }
682
683 fn minimized(mut self, flag: impl Res<bool>) -> Self {
684 let entity = self.entity();
685 let flag = flag.get(&self);
686 if let Some(win_state) = self.context().windows.get_mut(&entity) {
687 win_state.window_description.minimized = flag;
688 }
689
690 self
691 }
692
693 fn maximized(mut self, flag: impl Res<bool>) -> Self {
694 let entity = self.entity();
695 let flag = flag.get(&self);
696 if let Some(win_state) = self.context().windows.get_mut(&entity) {
697 win_state.window_description.maximized = flag;
698 }
699
700 self
701 }
702
703 fn visible(mut self, flag: impl Res<bool>) -> Self {
704 let entity = self.entity();
705 let flag = flag.get(&self);
706 if let Some(win_state) = self.context().windows.get_mut(&entity) {
707 win_state.window_description.visible = flag
708 }
709
710 self
711 }
712
713 fn transparent(mut self, flag: bool) -> Self {
714 let entity = self.entity();
715 if let Some(win_state) = self.context().windows.get_mut(&entity) {
716 win_state.window_description.transparent = flag
717 }
718
719 self
720 }
721
722 fn decorations(mut self, flag: bool) -> Self {
723 let entity = self.entity();
724 if let Some(win_state) = self.context().windows.get_mut(&entity) {
725 win_state.window_description.decorations = flag
726 }
727
728 self
729 }
730
731 fn always_on_top(mut self, flag: bool) -> Self {
732 let entity = self.entity();
733 if let Some(win_state) = self.context().windows.get_mut(&entity) {
734 win_state.window_description.always_on_top = flag
735 }
736
737 self
738 }
739
740 fn vsync(mut self, flag: bool) -> Self {
741 let entity = self.entity();
742 if let Some(win_state) = self.context().windows.get_mut(&entity) {
743 win_state.window_description.vsync = flag
744 }
745
746 self
747 }
748
749 fn icon(mut self, width: u32, height: u32, image: Vec<u8>) -> Self {
750 let entity = self.entity();
751 if let Some(win_state) = self.context().windows.get_mut(&entity) {
752 win_state.window_description.icon = Some(image);
753 win_state.window_description.icon_width = width;
754 win_state.window_description.icon_height = height;
755 }
756
757 self
758 }
759
760 fn enabled_window_buttons(mut self, window_buttons: WindowButtons) -> Self {
761 let entity = self.entity();
762 if let Some(win_state) = self.context().windows.get_mut(&entity) {
763 win_state.window_description.enabled_window_buttons = window_buttons;
764 }
765
766 self
767 }
768}