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 ColorSpace, ColorType, PixelGeometry, Surface, SurfaceProps, SurfacePropsFlags,
33 gpu::{
34 self, ContextOptions, SurfaceOrigin, backend_render_targets, ganesh::context_options,
35 gl::FramebufferInfo,
36 },
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::{DWMWA_CLOAK, DwmSetWindowAttribute},
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 if let Some(window_state) = cx.windows.get_mut(&cx.current()) {
468 window_state.window_description.title = title.clone();
469 }
470
471 if let Some(window) = &self.window {
472 window.set_title(title);
473 }
474 }
475
476 WindowEvent::SetSize(size) => {
477 let _ = self.window().request_inner_size(LogicalSize::new(size.width, size.height));
478 }
479
480 WindowEvent::SetMinSize(size) => {
481 self.window()
482 .set_min_inner_size(size.map(|size| LogicalSize::new(size.width, size.height)));
483 }
484
485 WindowEvent::SetMaxSize(size) => {
486 self.window()
487 .set_max_inner_size(size.map(|size| LogicalSize::new(size.width, size.height)));
488 }
489
490 WindowEvent::SetPosition(pos) => {
491 self.window().set_outer_position(LogicalPosition::new(pos.x, pos.y));
492 meta.consume();
493 }
494
495 WindowEvent::SetResizable(flag) => {
496 self.window().set_resizable(*flag);
497 }
498
499 WindowEvent::SetMinimized(flag) => {
500 self.window().set_minimized(*flag);
501 }
502
503 WindowEvent::SetMaximized(flag) => {
504 self.window().set_maximized(*flag);
505 }
506
507 WindowEvent::SetVisible(flag) => {
508 self.window().set_visible(*flag);
509
510 meta.consume();
511 }
512
513 WindowEvent::SetDecorations(flag) => {
514 self.window().set_decorations(*flag);
515 }
516
517 WindowEvent::ReloadStyles => {
518 cx.reload_styles().unwrap();
519 }
520
521 WindowEvent::WindowClose => {
522 self.should_close = true;
523
524 cx.close_window();
525
526 if let Some(callback) = &self.on_close {
527 callback(cx);
528 }
529
530 meta.consume();
531 }
532
533 WindowEvent::FocusNext => {
534 cx.focus_next();
535 }
536
537 WindowEvent::FocusPrev => {
538 cx.focus_prev();
539 }
540
541 WindowEvent::Redraw => {
542 self.window().request_redraw();
543 }
544
545 #[allow(unused_variables)]
546 WindowEvent::SetEnabled(flag) => {
547 #[cfg(target_os = "windows")]
548 self.window().set_enable(*flag);
549
550 self.window().focus_window();
551 }
552
553 WindowEvent::DragWindow => {
554 self.window().drag_window().expect("Failed to init drag window");
555 meta.consume();
556 }
557
558 WindowEvent::SetAlwaysOnTop(flag) => {
559 self.window().set_window_level(if *flag {
560 WindowLevel::AlwaysOnTop
561 } else {
562 WindowLevel::Normal
563 });
564 }
565
566 WindowEvent::SetImeCursorArea(position, size) => {
567 let position = PhysicalPosition::new(position.0 as i32, position.1 as i32);
568 let size = PhysicalSize::new(size.0, size.1);
569 self.window().set_ime_cursor_area(position, size);
570 }
571
572 _ => {}
573 })
574 }
575}
576
577impl WindowModifiers for Handle<'_, Window> {
578 fn on_close(self, callback: impl Fn(&mut EventContext) + 'static) -> Self {
579 self.modify(|window| window.on_close = Some(Box::new(callback)))
580 }
581
582 fn on_create(self, callback: impl Fn(&mut EventContext) + 'static) -> Self {
583 self.modify(|window| window.on_create = Some(Box::new(callback)))
584 }
585
586 fn title<T: ToStringLocalized>(mut self, title: impl Res<T> + Clone + 'static) -> Self {
587 let entity = self.entity();
588 let initial_title = title.get_value(&self).to_string_local(&self);
589 if let Some(win_state) = self.context().windows.get_mut(&entity) {
590 win_state.window_description.title = initial_title;
591 }
592
593 let getter_for_locale = title.clone();
594
595 self.context().with_current(entity, |cx| {
596 title.set_or_bind(cx, move |cx, val| {
597 let title_str = val.get_value(cx).to_string_local(cx);
598 cx.emit(WindowEvent::SetTitle(title_str));
599 });
600
601 let locale = cx.environment().locale;
602 locale.set_or_bind(cx, move |cx, _| {
603 let title = getter_for_locale.get_value(cx).to_string_local(cx);
604 cx.emit(WindowEvent::SetTitle(title));
605 });
606 });
607
608 self
609 }
610
611 fn inner_size<S: Into<WindowSize>>(mut self, size: impl Res<S>) -> Self {
612 let entity = self.entity();
613 let size = size.get_value(&self).into();
614 if let Some(win_state) = self.context().windows.get_mut(&entity) {
615 win_state.window_description.inner_size = size;
616 }
617
618 self
619 }
620
621 fn min_inner_size<S: Into<WindowSize>>(mut self, size: impl Res<Option<S>>) -> Self {
622 let entity = self.entity();
623 let size = size.get_value(&self).map(|size| size.into());
624 if let Some(win_state) = self.context().windows.get_mut(&entity) {
625 win_state.window_description.min_inner_size = size;
626 }
627
628 self
629 }
630
631 fn max_inner_size<S: Into<WindowSize>>(mut self, size: impl Res<Option<S>>) -> Self {
632 let entity = self.entity();
633 let size = size.get_value(&self).map(|size| size.into());
634 if let Some(win_state) = self.context().windows.get_mut(&entity) {
635 win_state.window_description.max_inner_size = size;
636 }
637
638 self
639 }
640
641 fn position<P: Into<vizia_window::WindowPosition>>(mut self, position: impl Res<P>) -> Self {
642 let entity = self.entity();
643 let pos = Some(position.get_value(&self).into());
644 if let Some(win_state) = self.context().windows.get_mut(&entity) {
645 win_state.window_description.position = pos;
646 }
647
648 self
649 }
650
651 fn offset<P: Into<vizia_window::WindowPosition>>(mut self, offset: impl Res<P>) -> Self {
652 let entity = self.entity();
653 let offset = Some(offset.get_value(&self).into());
654 if let Some(win_state) = self.context().windows.get_mut(&entity) {
655 win_state.window_description.offset = offset;
656 }
657
658 self
659 }
660
661 fn anchor<P: Into<vizia_window::Anchor>>(mut self, anchor: impl Res<P>) -> Self {
662 let entity = self.entity();
663 let anchor = Some(anchor.get_value(&self).into());
664 if let Some(win_state) = self.context().windows.get_mut(&entity) {
665 win_state.window_description.anchor = anchor;
666 }
667
668 self
669 }
670
671 fn anchor_target<P: Into<vizia_window::AnchorTarget>>(
672 mut self,
673 anchor_target: impl Res<P>,
674 ) -> Self {
675 let entity = self.entity();
676 let anchor_target = Some(anchor_target.get_value(&self).into());
677 if let Some(win_state) = self.context().windows.get_mut(&entity) {
678 win_state.window_description.anchor_target = anchor_target;
679 }
680
681 self
682 }
683
684 fn parent_anchor<P: Into<Anchor>>(mut self, parent_anchor: impl Res<P>) -> Self {
685 let entity = self.entity();
686 let parent_anchor = Some(parent_anchor.get_value(&self).into());
687 if let Some(win_state) = self.context().windows.get_mut(&entity) {
688 win_state.window_description.parent_anchor = parent_anchor;
689 }
690
691 self
692 }
693
694 fn resizable(mut self, flag: impl Res<bool>) -> Self {
695 let entity = self.entity();
696 let flag = flag.get_value(&self);
697 if let Some(win_state) = self.context().windows.get_mut(&entity) {
698 win_state.window_description.resizable = flag;
699 }
700
701 self
702 }
703
704 fn minimized(mut self, flag: impl Res<bool>) -> Self {
705 let entity = self.entity();
706 let flag = flag.get_value(&self);
707 if let Some(win_state) = self.context().windows.get_mut(&entity) {
708 win_state.window_description.minimized = flag;
709 }
710
711 self
712 }
713
714 fn maximized(mut self, flag: impl Res<bool>) -> Self {
715 let entity = self.entity();
716 let flag = flag.get_value(&self);
717 if let Some(win_state) = self.context().windows.get_mut(&entity) {
718 win_state.window_description.maximized = flag;
719 }
720
721 self
722 }
723
724 fn visible(mut self, flag: impl Res<bool>) -> Self {
725 let entity = self.entity();
726 let flag = flag.get_value(&self);
727 if let Some(win_state) = self.context().windows.get_mut(&entity) {
728 win_state.window_description.visible = flag
729 }
730
731 self
732 }
733
734 fn transparent(mut self, flag: bool) -> Self {
735 let entity = self.entity();
736 if let Some(win_state) = self.context().windows.get_mut(&entity) {
737 win_state.window_description.transparent = flag
738 }
739
740 self
741 }
742
743 fn decorations(mut self, flag: bool) -> Self {
744 let entity = self.entity();
745 if let Some(win_state) = self.context().windows.get_mut(&entity) {
746 win_state.window_description.decorations = flag
747 }
748
749 self
750 }
751
752 fn always_on_top(mut self, flag: bool) -> Self {
753 let entity = self.entity();
754 if let Some(win_state) = self.context().windows.get_mut(&entity) {
755 win_state.window_description.always_on_top = flag
756 }
757
758 self
759 }
760
761 fn vsync(mut self, flag: bool) -> Self {
762 let entity = self.entity();
763 if let Some(win_state) = self.context().windows.get_mut(&entity) {
764 win_state.window_description.vsync = flag
765 }
766
767 self
768 }
769
770 fn icon(mut self, width: u32, height: u32, image: Vec<u8>) -> Self {
771 let entity = self.entity();
772 if let Some(win_state) = self.context().windows.get_mut(&entity) {
773 win_state.window_description.icon = Some(image);
774 win_state.window_description.icon_width = width;
775 win_state.window_description.icon_height = height;
776 }
777
778 self
779 }
780
781 fn enabled_window_buttons(mut self, window_buttons: WindowButtons) -> Self {
782 let entity = self.entity();
783 if let Some(win_state) = self.context().windows.get_mut(&entity) {
784 win_state.window_description.enabled_window_buttons = window_buttons;
785 }
786
787 self
788 }
789}