i-slint-backend-winit 1.0.0

OpenGL rendering backend for Slint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial

//! This module contains the GraphicsWindow that used to be within corelib.

// cspell:ignore borderless corelib nesw webgl winit winsys xlib

use core::cell::{Cell, RefCell};
use core::pin::Pin;
use std::rc::{Rc, Weak};

use crate::event_loop::WinitWindow;
use crate::renderer::WinitCompatibleRenderer;
use const_field_offset::FieldOffsets;
use corelib::component::ComponentRc;
use corelib::items::MouseCursor;
use corelib::layout::Orientation;
use corelib::lengths::{LogicalLength, LogicalPoint, LogicalSize};
use corelib::platform::PlatformError;
use corelib::window::{WindowAdapter, WindowAdapterSealed, WindowInner};
use corelib::Property;
use corelib::{graphics::*, Coord};
use i_slint_core as corelib;

fn position_to_winit(pos: &corelib::api::WindowPosition) -> winit::dpi::Position {
    match pos {
        corelib::api::WindowPosition::Logical(pos) => {
            winit::dpi::Position::new(winit::dpi::LogicalPosition::new(pos.x, pos.y))
        }
        corelib::api::WindowPosition::Physical(pos) => {
            winit::dpi::Position::new(winit::dpi::PhysicalPosition::new(pos.x, pos.y))
        }
    }
}

fn window_size_to_slint(size: &corelib::api::WindowSize) -> winit::dpi::Size {
    match size {
        corelib::api::WindowSize::Logical(size) => {
            winit::dpi::Size::new(winit::dpi::LogicalSize::new(size.width, size.height))
        }
        corelib::api::WindowSize::Physical(size) => {
            winit::dpi::Size::new(winit::dpi::PhysicalSize::new(size.width, size.height))
        }
    }
}

fn logical_size_to_winit(size: LogicalSize) -> winit::dpi::LogicalSize<f32> {
    winit::dpi::LogicalSize::new(size.width, size.height)
}

fn physical_size_to_slint(size: &winit::dpi::PhysicalSize<u32>) -> corelib::api::PhysicalSize {
    corelib::api::PhysicalSize::new(size.width, size.height)
}

fn icon_to_winit(icon: corelib::graphics::Image) -> Option<winit::window::Icon> {
    let image_inner: &ImageInner = (&icon).into();

    let pixel_buffer = match image_inner {
        ImageInner::EmbeddedImage { buffer, .. } => buffer.clone(),
        _ => return None,
    };

    // This could become a method in SharedPixelBuffer...
    let rgba_pixels: Vec<u8> = match &pixel_buffer {
        SharedImageBuffer::RGB8(pixels) => pixels
            .as_bytes()
            .chunks(3)
            .flat_map(|rgb| IntoIterator::into_iter([rgb[0], rgb[1], rgb[2], 255]))
            .collect(),
        SharedImageBuffer::RGBA8(pixels) => pixels.as_bytes().to_vec(),
        SharedImageBuffer::RGBA8Premultiplied(pixels) => pixels
            .as_bytes()
            .chunks(4)
            .flat_map(|rgba| {
                let alpha = rgba[3] as u32;
                IntoIterator::into_iter(rgba)
                    .take(3)
                    .map(move |component| (*component as u32 * alpha / 255) as u8)
                    .chain(std::iter::once(alpha as u8))
            })
            .collect(),
    };

    winit::window::Icon::from_rgba(rgba_pixels, pixel_buffer.width(), pixel_buffer.height()).ok()
}

fn window_is_resizable(min_size: Option<LogicalSize>, max_size: Option<LogicalSize>) -> bool {
    if let Some((
        LogicalSize { width: min_width, height: min_height, .. },
        LogicalSize { width: max_width, height: max_height, .. },
    )) = min_size.zip(max_size)
    {
        min_width < max_width || min_height < max_height
    } else {
        true
    }
}

/// GraphicsWindow is an implementation of the [WindowAdapter][`crate::eventloop::WindowAdapter`] trait. This is
/// typically instantiated by entry factory functions of the different graphics back ends.
pub(crate) struct GLWindow<Renderer: WinitCompatibleRenderer + 'static> {
    window: corelib::api::Window,
    self_weak: Weak<Self>,
    map_state: RefCell<GraphicsWindowBackendState>,
    currently_pressed_key_code: std::cell::Cell<Option<winit::event::VirtualKeyCode>>,
    pending_redraw: Cell<bool>,
    in_resize_event: Cell<bool>,
    dark_color_scheme: once_cell::unsync::OnceCell<Pin<Box<Property<bool>>>>,

    renderer: Renderer,
    #[cfg(target_arch = "wasm32")]
    canvas_id: String,

    #[cfg(target_arch = "wasm32")]
    virtual_keyboard_helper: RefCell<Option<super::wasm_input_helper::WasmInputHelper>>,
}

impl<Renderer: WinitCompatibleRenderer + 'static> GLWindow<Renderer> {
    /// Creates a new reference-counted instance.
    ///
    /// Arguments:
    /// * `graphics_backend_factory`: The factor function stored in the GraphicsWindow that's called when the state
    ///   of the window changes to mapped. The event loop and window builder parameters can be used to create a
    ///   backing window.
    pub(crate) fn new(#[cfg(target_arch = "wasm32")] canvas_id: String) -> Rc<dyn WindowAdapter> {
        let self_rc = Rc::new_cyclic(|self_weak| Self {
            window: corelib::api::Window::new(self_weak.clone() as _),
            self_weak: self_weak.clone(),
            map_state: RefCell::new(GraphicsWindowBackendState::Unmapped {
                requested_position: None,
                requested_size: None,
            }),
            currently_pressed_key_code: Default::default(),
            pending_redraw: Cell::new(false),
            in_resize_event: Cell::new(false),
            dark_color_scheme: Default::default(),
            renderer: Renderer::new(&(self_weak.clone() as _)),
            #[cfg(target_arch = "wasm32")]
            canvas_id,
            #[cfg(target_arch = "wasm32")]
            virtual_keyboard_helper: Default::default(),
        });
        self_rc as _
    }

    fn is_mapped(&self) -> bool {
        matches!(&*self.map_state.borrow(), GraphicsWindowBackendState::Mapped { .. })
    }

    fn borrow_mapped_window(&self) -> Option<std::cell::Ref<MappedWindow>> {
        if self.is_mapped() {
            std::cell::Ref::map(self.map_state.borrow(), |state| match state {
                GraphicsWindowBackendState::Unmapped{..} => {
                    panic!("borrow_mapped_window must be called after checking if the window is mapped")
                }
                GraphicsWindowBackendState::Mapped(window) => window,
            }).into()
        } else {
            None
        }
    }

    fn unmap(&self) -> Result<(), PlatformError> {
        let old_mapped = match self.map_state.replace(GraphicsWindowBackendState::Unmapped {
            requested_position: None,
            requested_size: None,
        }) {
            GraphicsWindowBackendState::Unmapped { .. } => return Ok(()),
            GraphicsWindowBackendState::Mapped(old_mapped) => old_mapped,
        };

        crate::event_loop::unregister_window(old_mapped.winit_window.id());

        self.renderer.hide()
    }

    fn call_with_event_loop(
        &self,
        callback: fn(&Self) -> Result<(), PlatformError>,
    ) -> Result<(), PlatformError> {
        // With wasm, winit's `run()` consumes the event loop and access to it from outside the event handler yields
        // loop and thus ends up trying to create a new event loop instance, which panics in winit. Instead, forward
        // the call to be invoked from within the event loop
        #[cfg(target_arch = "wasm32")]
        return corelib::api::invoke_from_event_loop({
            let self_weak = send_wrapper::SendWrapper::new(self.self_weak.clone());

            move || {
                if let Some(this) = self_weak.take().upgrade() {
                    // Can't propagate the returned error because we're in an async callback, so throw.
                    callback(&this).unwrap()
                }
            }
        })
        .map_err(|_| {
            format!("internal error in winit backend: invoke_from_event_loop failed").into()
        });
        #[cfg(not(target_arch = "wasm32"))]
        return callback(self);
    }

    fn constraints(&self) -> (corelib::layout::LayoutInfo, corelib::layout::LayoutInfo) {
        self.borrow_mapped_window().map(|window| window.constraints.get()).unwrap_or_default()
    }

    fn set_constraints(
        &self,
        constraints: (corelib::layout::LayoutInfo, corelib::layout::LayoutInfo),
    ) {
        if let Some(window) = self.borrow_mapped_window() {
            window.constraints.set(constraints);
        }
    }
}

impl<Renderer: WinitCompatibleRenderer + 'static> WinitWindow for GLWindow<Renderer> {
    fn take_pending_redraw(&self) -> bool {
        self.pending_redraw.take()
    }

    fn currently_pressed_key_code(&self) -> &Cell<Option<winit::event::VirtualKeyCode>> {
        &self.currently_pressed_key_code
    }

    /// Draw the items of the specified `component` in the given window.
    fn draw(&self) -> Result<bool, PlatformError> {
        let window = match self.borrow_mapped_window() {
            Some(window) => window,
            None => return Ok(false), // caller bug, doesn't make sense to call draw() when not mapped
        };

        self.pending_redraw.set(false);

        self.renderer.render(physical_size_to_slint(&window.winit_window.inner_size()))?;

        Ok(self.pending_redraw.get())
    }

    fn with_window_handle(&self, callback: &mut dyn FnMut(&winit::window::Window)) {
        if let Some(mapped_window) = self.borrow_mapped_window() {
            callback(&mapped_window.winit_window);
        }
    }

    fn winit_window(&self) -> Option<Rc<winit::window::Window>> {
        self.borrow_mapped_window().map(|mapped_window| mapped_window.winit_window.clone())
    }

    #[cfg(target_arch = "wasm32")]
    fn input_method_focused(&self) -> bool {
        match self.virtual_keyboard_helper.try_borrow() {
            Ok(vkh) => vkh.as_ref().map_or(false, |h| h.has_focus()),
            // the only location in which the virtual_keyboard_helper is mutably borrowed is from
            // show_virtual_keyboard, which means we have the focus
            Err(_) => true,
        }
    }

    fn resize_event(&self, size: winit::dpi::PhysicalSize<u32>) -> Result<(), PlatformError> {
        // slint::Window::set_size will call set_size() on this type, which would call
        // set_inner_size on the winit Window. On Windows that triggers an new resize event
        // in the next event loop iteration for mysterious reasons, with slightly different sizes.
        // I suspect a bug in the way the frame decorations are added and subtracted from the size
        // we provide.
        // Work around it with this guard that prevents us from calling set_inner_size again.
        assert!(!self.in_resize_event.get());
        self.in_resize_event.set(true);
        scopeguard::defer! { self.in_resize_event.set(false); }

        // When a window is minimized on Windows, we get a move event to an off-screen position
        // and a resize even with a zero size. Don't forward that, especially not to the renderer,
        // which might panic when trying to create a zero-sized surface.
        if size.width > 0 && size.height > 0 {
            let physical_size = physical_size_to_slint(&size);
            self.window.set_size(physical_size);
            self.renderer.resize_event(physical_size)
        } else {
            Ok(())
        }
    }

    fn set_dark_color_scheme(&self, dark_mode: bool) {
        self.dark_color_scheme
            .get_or_init(|| Box::pin(Property::new(false)))
            .as_ref()
            .set(dark_mode)
    }
}

impl<Renderer: WinitCompatibleRenderer + 'static> WindowAdapter for GLWindow<Renderer> {
    fn window(&self) -> &corelib::api::Window {
        &self.window
    }
}

impl<Renderer: WinitCompatibleRenderer + 'static> WindowAdapterSealed for GLWindow<Renderer> {
    fn request_redraw(&self) {
        self.pending_redraw.set(true);
        self.with_window_handle(&mut |window| window.request_redraw())
    }

    fn request_window_properties_update(&self) {
        self.call_with_event_loop(|self_| {
            self_.with_window_handle(&mut |window| {
                let window_id = window.id();
                crate::event_loop::with_window_target(|event_loop| {
                    event_loop.event_loop_proxy().send_event(
                        crate::event_loop::CustomEvent::UpdateWindowProperties(window_id),
                    )
                })
                .ok();
            });
            Ok(()) // Doesn't matter if the eventloop is already closed, nothing to update then.
        })
        .ok();
    }

    fn apply_window_properties(&self, window_item: Pin<&i_slint_core::items::WindowItem>) {
        let winit_window = match self.winit_window() {
            Some(handle) => handle,
            None => return,
        };

        let mut width = window_item.width().get() as f32;
        let mut height = window_item.height().get() as f32;

        let mut must_resize = false;

        winit_window.set_window_icon(icon_to_winit(window_item.icon()));
        winit_window.set_title(&window_item.title());
        winit_window
            .set_decorations(!window_item.no_frame() || winit_window.fullscreen().is_some());

        if width <= 0. || height <= 0. {
            must_resize = true;

            let winit_size =
                winit_window.inner_size().to_logical(self.window.scale_factor() as f64);

            if width <= 0. {
                width = winit_size.width;
            }
            if height <= 0. {
                height = winit_size.height;
            }
        }

        let existing_size: winit::dpi::LogicalSize<f32> =
            winit_window.inner_size().to_logical(self.window.scale_factor().into());

        if (existing_size.width - width).abs() > 1. || (existing_size.height - height).abs() > 1. {
            // If we're in fullscreen state, don't try to resize the window but maintain the surface
            // size we've been assigned to from the windowing system. Weston/Wayland don't like it
            // when we create a surface that's bigger than the screen due to constraints (#532).
            if winit_window.fullscreen().is_none() {
                winit_window.set_inner_size(winit::dpi::LogicalSize::new(width, height));
            }
        }

        if must_resize {
            self.window.set_size(i_slint_core::api::LogicalSize::new(width, height));
        }
    }

    fn apply_geometry_constraint(
        &self,
        constraints_horizontal: corelib::layout::LayoutInfo,
        constraints_vertical: corelib::layout::LayoutInfo,
    ) {
        self.with_window_handle(&mut |winit_window| {
            // If we're in fullscreen state, don't try to resize the window but maintain the surface
            // size we've been assigned to from the windowing system. Weston/Wayland don't like it
            // when we create a surface that's bigger than the screen due to constraints (#532).
            if winit_window.fullscreen().is_some() {
                return;
            }

            if (constraints_horizontal, constraints_vertical) != self.constraints() {
                // Use our scale factor instead of winit's logical size to take a scale factor override into account.
                let sf = self.window.scale_factor();

                let (min_size, max_size) =
                    i_slint_core::layout::min_max_size_for_layout_constraints(
                        constraints_horizontal,
                        constraints_vertical,
                    );

                let resizable = window_is_resizable(min_size, max_size);

                winit_window.set_min_inner_size(min_size.map(|logical_size| {
                    winit::dpi::PhysicalSize::new(logical_size.width * sf, logical_size.height * sf)
                }));
                winit_window.set_max_inner_size(max_size.map(|logical_size| {
                    winit::dpi::PhysicalSize::new(
                        (logical_size.width * sf).min(65535.),
                        (logical_size.height * sf).min(65535.),
                    )
                }));
                self.set_constraints((constraints_horizontal, constraints_vertical));
                winit_window.set_resizable(resizable);

                #[cfg(target_arch = "wasm32")]
                if let Some((
                    LogicalSize { width: min_width, height: min_height, .. },
                    LogicalSize { width: max_width, height: max_height, .. },
                )) = min_size.zip(max_size)
                {
                    // set_max_inner_size / set_min_inner_size don't work on wasm, so apply the size manually
                    let existing_size: winit::dpi::LogicalSize<f32> =
                        winit_window.inner_size().to_logical(sf as f64);
                    if !(min_width..=max_width).contains(&(existing_size.width))
                        || !(min_height..=max_height).contains(&(existing_size.height))
                    {
                        let new_size = winit::dpi::LogicalSize::new(
                            existing_size.width.min(max_width).max(min_width),
                            existing_size.height.min(max_height).max(min_height),
                        );
                        winit_window.set_inner_size(new_size);
                    }
                }

                // Auto-resize to the preferred size if users (SlintPad) requests it
                #[cfg(target_arch = "wasm32")]
                {
                    let canvas = self.renderer.html_canvas_element();

                    if canvas
                        .dataset()
                        .get("slint-auto-resize-to-preferred")
                        .and_then(|val_str| val_str.parse().ok())
                        .unwrap_or_default()
                    {
                        let pref_width = constraints_horizontal.preferred_bounded();
                        let pref_height = constraints_vertical.preferred_bounded();
                        if pref_width > 0 as Coord || pref_height > 0 as Coord {
                            winit_window.set_inner_size(winit::dpi::LogicalSize::new(
                                pref_width,
                                pref_height,
                            ));
                        };
                    }
                }
            }
        });
    }

    fn show(&self) -> Result<(), PlatformError> {
        self.call_with_event_loop(|self_| {
            let (requested_position, requested_size) = match &*self_.map_state.borrow() {
                GraphicsWindowBackendState::Unmapped { requested_position, requested_size } => {
                    (requested_position.clone(), requested_size.clone())
                }
                GraphicsWindowBackendState::Mapped(_) => return Ok(()),
            };

            let mut window_builder = winit::window::WindowBuilder::new();

            let runtime_window = WindowInner::from_pub(&self_.window);
            let component_rc = runtime_window.component();
            let component = ComponentRc::borrow_pin(&component_rc);

            if let Some(window_item) = runtime_window.window_item().as_ref().map(|i| i.as_pin_ref())
            {
                window_builder = window_builder
                    .with_title(window_item.title().to_string())
                    .with_decorations(!window_item.no_frame())
                    .with_window_icon(icon_to_winit(window_item.icon()));
            } else {
                window_builder = window_builder.with_title("Slint Window".to_string());
            };

            let scale_factor_override = runtime_window.scale_factor();
            // If the scale factor was already set programmatically, use that
            // else, use the SLINT_SCALE_FACTOR if set, otherwise use the one from winit
            let scale_factor_override = if scale_factor_override > 1. {
                Some(scale_factor_override as f64)
            } else {
                std::env::var("SLINT_SCALE_FACTOR")
                    .ok()
                    .and_then(|x| x.parse::<f64>().ok())
                    .filter(|f| *f > 0.)
            };

            let into_size = |s: winit::dpi::LogicalSize<Coord>| -> winit::dpi::Size {
                if let Some(f) = scale_factor_override {
                    s.to_physical::<f32>(f).into()
                } else {
                    s.into()
                }
            };

            let layout_info_h = component.as_ref().layout_info(Orientation::Horizontal);
            if let Some(window_item) = runtime_window.window_item() {
                // Setting the width to its preferred size before querying the vertical layout info
                // is important in case the height depends on the width
                window_item.width.set(LogicalLength::new(layout_info_h.preferred_bounded()));
            }
            let layout_info_v = component.as_ref().layout_info(Orientation::Vertical);
            #[allow(unused_mut)]
            let mut s = winit::dpi::LogicalSize::new(
                layout_info_h.preferred_bounded(),
                layout_info_v.preferred_bounded(),
            );

            #[cfg(target_arch = "wasm32")]
            let html_canvas = {
                use wasm_bindgen::JsCast;

                web_sys::window()
                    .ok_or_else(|| "winit backend: Could not retrieve DOM window".to_string())?
                    .document()
                    .ok_or_else(|| "winit backend: Could not retrieve DOM document".to_string())?
                    .get_element_by_id(&self_.canvas_id)
                    .ok_or_else(|| {
                        format!(
                            "winit backend: Could not retrieve existing HTML Canvas element '{}'",
                            self_.canvas_id
                        )
                    })?
                    .dyn_into::<web_sys::HtmlCanvasElement>()
                    .map_err(|_| {
                        format!(
                            "winit backend: Specified DOM element '{}' is not a HTML Canvas",
                            self_.canvas_id
                        )
                    })?
            };

            #[cfg(target_arch = "wasm32")]
            {
                let existing_canvas_size = winit::dpi::LogicalSize::new(
                    html_canvas.client_width() as f32,
                    html_canvas.client_height() as f32,
                );

                // Try to maintain the existing size of the canvas element. A window created with winit
                // on the web will always have 1024x768 as size otherwise.
                if s.width <= 0. {
                    s.width = existing_canvas_size.width;
                }
                if s.height <= 0. {
                    s.height = existing_canvas_size.height;
                }
            }

            if std::env::var("SLINT_FULLSCREEN").is_ok() {
                window_builder = window_builder
                    .with_fullscreen(Some(winit::window::Fullscreen::Borderless(None)));
            } else {
                let (min_inner_size, max_inner_size) =
                    i_slint_core::layout::min_max_size_for_layout_constraints(
                        layout_info_h,
                        layout_info_v,
                    );

                if let Some(min_inner_size) = min_inner_size {
                    window_builder = window_builder
                        .with_min_inner_size(into_size(logical_size_to_winit(min_inner_size)))
                }
                if let Some(max_inner_size) = max_inner_size {
                    window_builder = window_builder
                        .with_max_inner_size(into_size(logical_size_to_winit(max_inner_size)))
                }

                window_builder = window_builder
                    .with_resizable(window_is_resizable(min_inner_size, max_inner_size));

                if let Some(requested_size) = &requested_size {
                    // It would be nice to bound this with our constraints, but those are in logical coordinates
                    // and we don't know the scale factor yet...
                    if let Some(sf) = scale_factor_override {
                        let physical_size = requested_size.to_physical(sf as f32);
                        window_builder = window_builder.with_inner_size(winit::dpi::Size::new(
                            winit::dpi::PhysicalSize::new(
                                physical_size.width,
                                physical_size.height,
                            ),
                        ));
                    } else {
                        window_builder =
                            window_builder.with_inner_size(window_size_to_slint(requested_size));
                    }
                } else if s.width > 0 as Coord && s.height > 0 as Coord {
                    window_builder = window_builder.with_inner_size(into_size(s));
                }
            };

            if let Some(pos) = &requested_position {
                window_builder = window_builder.with_position(position_to_winit(pos))
            };

            #[cfg(target_arch = "wasm32")]
            {
                use winit::platform::web::WindowBuilderExtWebSys;
                window_builder = window_builder.with_canvas(Some(html_canvas.clone()))
            };

            let winit_window = self_.renderer.show(
                window_builder,
                #[cfg(target_arch = "wasm32")]
                &self_.canvas_id,
            )?;

            let scale_factor = scale_factor_override.unwrap_or_else(|| winit_window.scale_factor());
            WindowInner::from_pub(&self_.window).set_scale_factor(scale_factor as _);
            let s = winit_window.inner_size().to_logical(scale_factor);
            // Make sure that the window's inner size is in sync with the root window item's
            // width/height.
            runtime_window.set_window_item_geometry(LogicalSize::new(s.width, s.height));
            let id = winit_window.id();

            self_.map_state.replace(GraphicsWindowBackendState::Mapped(MappedWindow {
                constraints: Default::default(),
                winit_window,
            }));

            crate::event_loop::register_window(id, self_.self_weak.upgrade().unwrap());
            Ok(())
        })
    }

    fn hide(&self) -> Result<(), PlatformError> {
        self.call_with_event_loop(|self_| {
            self_.unmap()?;

            /* FIXME:
            if let Some(existing_blinker) = self.cursor_blinker.borrow().upgrade() {
                existing_blinker.stop();
            }*/
            crate::event_loop::with_window_target(|event_loop| {
                event_loop
                    .event_loop_proxy()
                    .send_event(crate::event_loop::CustomEvent::WindowHidden)
            })
            .ok(); // It's okay to call hide() even after the event loop is closed. We don't need the logic for quitting the event loop anymore at this point.
            Ok(())
        })
    }

    fn set_mouse_cursor(&self, cursor: MouseCursor) {
        let winit_cursor = match cursor {
            MouseCursor::Default => winit::window::CursorIcon::Default,
            MouseCursor::None => winit::window::CursorIcon::Default,
            MouseCursor::Help => winit::window::CursorIcon::Help,
            MouseCursor::Pointer => winit::window::CursorIcon::Hand,
            MouseCursor::Progress => winit::window::CursorIcon::Progress,
            MouseCursor::Wait => winit::window::CursorIcon::Wait,
            MouseCursor::Crosshair => winit::window::CursorIcon::Crosshair,
            MouseCursor::Text => winit::window::CursorIcon::Text,
            MouseCursor::Alias => winit::window::CursorIcon::Alias,
            MouseCursor::Copy => winit::window::CursorIcon::Copy,
            MouseCursor::Move => winit::window::CursorIcon::Move,
            MouseCursor::NoDrop => winit::window::CursorIcon::NoDrop,
            MouseCursor::NotAllowed => winit::window::CursorIcon::NotAllowed,
            MouseCursor::Grab => winit::window::CursorIcon::Grab,
            MouseCursor::Grabbing => winit::window::CursorIcon::Grabbing,
            MouseCursor::ColResize => winit::window::CursorIcon::ColResize,
            MouseCursor::RowResize => winit::window::CursorIcon::RowResize,
            MouseCursor::NResize => winit::window::CursorIcon::NResize,
            MouseCursor::EResize => winit::window::CursorIcon::EResize,
            MouseCursor::SResize => winit::window::CursorIcon::SResize,
            MouseCursor::WResize => winit::window::CursorIcon::WResize,
            MouseCursor::NeResize => winit::window::CursorIcon::NeResize,
            MouseCursor::NwResize => winit::window::CursorIcon::NwResize,
            MouseCursor::SeResize => winit::window::CursorIcon::SeResize,
            MouseCursor::SwResize => winit::window::CursorIcon::SwResize,
            MouseCursor::EwResize => winit::window::CursorIcon::EwResize,
            MouseCursor::NsResize => winit::window::CursorIcon::NsResize,
            MouseCursor::NeswResize => winit::window::CursorIcon::NeswResize,
            MouseCursor::NwseResize => winit::window::CursorIcon::NwseResize,
        };
        self.with_window_handle(&mut |winit_window| {
            winit_window.set_cursor_visible(cursor != MouseCursor::None);
            winit_window.set_cursor_icon(winit_cursor);
        });
    }

    fn renderer(&self) -> &dyn i_slint_core::renderer::Renderer {
        self.renderer.as_core_renderer()
    }

    fn enable_input_method(&self, _it: corelib::items::InputType) {
        #[cfg(target_arch = "wasm32")]
        {
            let mut vkh = self.virtual_keyboard_helper.borrow_mut();
            let h = vkh.get_or_insert_with(|| {
                let canvas = self.renderer.html_canvas_element();
                super::wasm_input_helper::WasmInputHelper::new(self.self_weak.clone(), canvas)
            });
            h.show();
        }
        #[cfg(not(target_arch = "wasm32"))]
        self.with_window_handle(&mut |winit_window| {
            winit_window.set_ime_allowed(matches!(_it, corelib::items::InputType::Text))
        });
    }

    fn disable_input_method(&self) {
        #[cfg(target_arch = "wasm32")]
        if let Some(h) = &*self.virtual_keyboard_helper.borrow() {
            h.hide()
        }
        #[cfg(not(target_arch = "wasm32"))]
        self.with_window_handle(&mut |winit_window| winit_window.set_ime_allowed(false));
    }

    fn set_ime_position(&self, ime_pos: LogicalPoint) {
        self.with_window_handle(&mut |winit_window| {
            winit_window.set_ime_position(winit::dpi::LogicalPosition::new(ime_pos.x, ime_pos.y))
        })
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn position(&self) -> corelib::api::PhysicalPosition {
        match &*self.map_state.borrow() {
            GraphicsWindowBackendState::Unmapped { requested_position, .. } => requested_position
                .as_ref()
                .map(|p| p.to_physical(self.window.scale_factor()))
                .unwrap_or_default(),
            GraphicsWindowBackendState::Mapped(mapped_window) => {
                match mapped_window.winit_window.outer_position() {
                    Ok(outer_position) => {
                        corelib::api::PhysicalPosition::new(outer_position.x, outer_position.y)
                    }
                    Err(_) => Default::default(),
                }
            }
        }
    }

    fn set_position(&self, position: corelib::api::WindowPosition) {
        let w = match &mut *self.map_state.borrow_mut() {
            GraphicsWindowBackendState::Unmapped { requested_position, .. } => {
                *requested_position = Some(position);
                return;
            }
            GraphicsWindowBackendState::Mapped(mapped_window) => mapped_window.winit_window.clone(),
        };
        w.set_outer_position(position_to_winit(&position))
    }

    fn set_size(&self, size: corelib::api::WindowSize) {
        if self.in_resize_event.get() {
            return;
        }
        let Ok(mut map_state) = self.map_state.try_borrow_mut() else { return };
        let w = match &mut *map_state {
            GraphicsWindowBackendState::Unmapped { requested_size, .. } => {
                *requested_size = Some(size);
                return;
            }
            GraphicsWindowBackendState::Mapped(mapped_window) => mapped_window.winit_window.clone(),
        };
        drop(map_state);
        w.set_inner_size(window_size_to_slint(&size))
    }

    fn dark_color_scheme(&self) -> bool {
        self.dark_color_scheme
            .get_or_init(|| {
                Box::pin(Property::new({
                    self.borrow_mapped_window()
                        .and_then(|mapped_window| {
                            mapped_window
                                .winit_window
                                .theme()
                                .map(|theme| theme == winit::window::Theme::Dark)
                        })
                        .unwrap_or_default()
                }))
            })
            .as_ref()
            .get()
    }

    fn is_visible(&self) -> bool {
        if let Some(mapped_window) = self.borrow_mapped_window() {
            mapped_window.winit_window.is_visible().unwrap_or(true)
        } else {
            false
        }
    }
}

impl<Renderer: WinitCompatibleRenderer + 'static> Drop for GLWindow<Renderer> {
    fn drop(&mut self) {
        self.unmap().expect("winit backend: error unmapping window");
    }
}

struct MappedWindow {
    constraints: Cell<(corelib::layout::LayoutInfo, corelib::layout::LayoutInfo)>,
    winit_window: Rc<winit::window::Window>,
}

enum GraphicsWindowBackendState {
    Unmapped {
        requested_position: Option<corelib::api::WindowPosition>,
        requested_size: Option<corelib::api::WindowSize>,
    },
    Mapped(MappedWindow),
}

#[derive(FieldOffsets)]
#[repr(C)]
#[pin]
struct WindowProperties {
    scale_factor: Property<f32>,
}

impl Default for WindowProperties {
    fn default() -> Self {
        Self { scale_factor: Property::new(1.0) }
    }
}