# window.rs - source
Source of the Rust file `maplibre/src/window.rs`.

    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

    //! Utilities for the window system.

    use std::num::NonZeroU32;

    use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};

    /// Window of a certain [`PhysicalSize`]. This can either be a proper window or a headless one.
    pub trait MapWindow {
        fn size(&self) -> PhysicalSize;
    }

    /// Window which references a physical `RawWindow`. This is only implemented by headed windows and
    /// not by headless windows.
    pub trait HeadedMapWindow: MapWindow {
        type RawWindow: HasRawWindowHandle + HasRawDisplayHandle;

        fn raw(&self) -> &Self::RawWindow;

        // TODO: Can we avoid this?
        fn request_redraw(&self);

        fn scale_factor(&self) -> f64;

        fn id(&self) -> u64;
    }

    /// A configuration for a window which determines the corresponding implementation of a
    /// [`MapWindow`] and is able to create it.
    pub trait MapWindowConfig: 'static + Clone {
        type MapWindow: MapWindow;

        fn create(&self) -> Self::MapWindow;
    }

    /// Window size with a width and an height in pixels.
    #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
    pub struct PhysicalSize {
        width: NonZeroU32,
        height: NonZeroU32,
    }

    impl PhysicalSize {
        pub fn new(width: u32, height: u32) -> Option<Self> {
            Some(Self {
                width: NonZeroU32::new(width)?,
                height: NonZeroU32::new(height)?,
            })
        }

        pub fn width(&self) -> u32 {
            self.width.get()
        }

        pub fn width_non_zero(&self) -> NonZeroU32 {
            self.width
        }

        pub fn height(&self) -> u32 {
            self.height.get()
        }

        pub fn height_non_zero(&self) -> NonZeroU32 {
            self.height
        }
    }

    #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
    pub struct LogicalSize {
        width: NonZeroU32,
        height: NonZeroU32,
    }

    impl LogicalSize {
        pub fn new(width: u32, height: u32) -> Option<Self> {
            Some(Self {
                width: NonZeroU32::new(width)?,
                height: NonZeroU32::new(height)?,
            })
        }

        pub fn width(&self) -> u32 {
            self.width.get()
        }

        pub fn width_non_zero(&self) -> NonZeroU32 {
            self.width
        }

        pub fn height(&self) -> u32 {
            self.height.get()
        }

        pub fn height_non_zero(&self) -> NonZeroU32 {
            self.height
        }
    }

    impl PhysicalSize {
        pub fn to_logical(&self, scale_factor: f64) -> LogicalSize {
            let width = self.width.get() as f64 / scale_factor;
            let height = self.height.get() as f64 / scale_factor;
            LogicalSize {
                width: NonZeroU32::new(width as u32).expect("impossible to reach"),
                height: NonZeroU32::new(height as u32).expect("impossible to reach"),
            }
        }
    }