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
use crate::{common::*, *};

use riddle_common::eventpub::*;

use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
use riddle_platform_common::traits::WindowCommon;
use std::borrow::Borrow;

/// A platform native window.
///
/// Construct windows using [`WindowBuilder`].
///
/// The window is kept open for the lifetime of this object, once it is
/// dropped the window will be closed during the next event loop pump.
///
/// # Example
///
/// ```no_run
/// # use riddle::{*, platform::*};
/// # fn main() -> Result<(), RiddleError> {
/// let rdl =  RiddleLib::new()?;
///
/// // Create a window
/// let mut window = Some(WindowBuilder::new().build(rdl.context())?);
///
/// rdl.run(move |rdl| {
///     if window.is_some() {
///         // Drop all handles to the window. It will be closed before the next invocation
///         // of this closure.
///         window.take();
///     } else {
///         // The window is closed. Exit.
///         rdl.quit();
///     }
/// #   std::thread::sleep(std::time::Duration::from_millis(500));
/// })
/// # }
/// ```
pub struct Window {
    weak_self: WindowWeak,
    window_system: PlatformSystemHandle,
    winit_window: winit::window::Window,
    event_sub: EventSub<PlatformEvent>,
    event_pub: EventPub<PlatformEvent>,

    id: WindowId,
}

define_handles!(<Window>::weak_self, pub WindowHandle, pub WindowWeak);

impl Window {
    fn new_shared(ctx: &PlatformContext, args: &WindowBuilder) -> Result<WindowHandle> {
        let system = ctx.system().clone_handle();

        #[cfg(target_os = "windows")]
        let winit_builder = {
            use winit::platform::windows::WindowBuilderExtWindows;
            winit::window::WindowBuilder::new()
                // Required to prevent cpal fighting with winit over ole threading model and panicing
                // Tracking Issues:
                // https://github.com/RustAudio/cpal/pull/348
                // https://github.com/rust-windowing/winit/pull/1524
                .with_drag_and_drop(false)
        };

        #[cfg(not(target_os = "windows"))]
        let winit_builder = winit::window::WindowBuilder::new();

        let winit_builder = winit_builder
            .with_inner_size(winit::dpi::LogicalSize::new(
                args.width as f32,
                args.height as f32,
            ))
            .with_title(args.title.clone())
            .with_resizable(args.resizeable);

        let winit_window = ctx.with_event_loop(|el| {
            winit_builder
                .build(el)
                .map_err(|_| PlatformError::WindowInitFailure)
        })?;

        args.configure_window(&winit_window);

        let event_sub = EventSub::new_with_filter(Self::event_filter);
        ctx.system().event_pub().attach(&event_sub);

        let window = WindowHandle::new(|weak_self| Self {
            weak_self,
            window_system: system.clone(),
            winit_window,
            event_sub,
            event_pub: EventPub::new(),
            id: system.with_window_map_mut(|wmap| wmap.take_next_window_id()),
        });

        ctx.system()
            .with_window_map_mut(|wmap| wmap.register_window(window.clone()));

        Ok(window)
    }

    /// Get the size of the drawable area of the window in pixels
    pub fn physical_size(&self) -> (u32, u32) {
        let size = self.winit_window.inner_size();
        (size.width, size.height)
    }

    /// Get the size of the drawable area of the window in logical units.
    pub fn logical_size(&self) -> LogicalSize {
        let physical_size = self.winit_window.inner_size();
        let logical_size: winit::dpi::LogicalSize<u32> =
            physical_size.to_logical(self.winit_window.scale_factor());
        dimensions::logical_size_from_winit(logical_size)
    }

    /// Get the scale factor of the window, based on how the window manager
    /// configures hidpi scaling
    pub fn scale_factor(&self) -> f64 {
        self.winit_window.scale_factor()
    }

    /// Set the window title
    pub fn set_title(&self, title: &str) {
        self.winit_window.set_title(title)
    }

    /// Attach a subscriber to the subset of platform events that relate to this window
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use riddle::{*, common::eventpub::*, platform::*};
    /// # fn main() -> Result<(), RiddleError> {
    /// let rdl =  RiddleLib::new()?;
    /// let subscriber: EventSub<PlatformEvent> = EventSub::new();
    ///
    /// let window = WindowBuilder::new().build(rdl.context())?;
    /// window.subscribe_to_events(&subscriber);
    /// # Ok(()) }
    /// ```
    pub fn subscribe_to_events(&self, sub: &EventSub<PlatformEvent>) {
        self.event_pub.attach(sub);
    }

    /// The window id which is used to identify this window in [`PlatformEvent`]s
    pub fn id(&self) -> WindowId {
        self.id
    }

    pub(crate) fn update(&self) {
        for event in self.event_sub.collect() {
            self.event_pub.dispatch(event);
        }
    }

    pub(crate) fn winit_window_id(&self) -> winit::window::WindowId {
        self.winit_window.id()
    }

    fn event_filter(event: &PlatformEvent) -> bool {
        matches!(event, PlatformEvent::WindowResize(_))
    }
}

impl winit_ext::WinitWindowExt for Window {
    fn winit_window(&self) -> &winit::window::Window {
        &self.winit_window
    }
}

impl WindowCommon for Window {
    fn logical_to_physical<L: Into<LogicalVec2>>(&self, vec2: L) -> (u32, u32) {
        let winit_pos = dimensions::logical_vec_to_winit(vec2.into());
        let physical_size = winit_pos.to_physical(self.scale_factor());
        (physical_size.x, physical_size.y)
    }
}

impl std::cmp::PartialEq for Window {
    fn eq(&self, other: &Self) -> bool {
        self.id() == other.id()
    }
}

impl std::cmp::Eq for Window {}

impl Drop for Window {
    fn drop(&mut self) {
        self.window_system
            .with_window_map_mut(|wmap| wmap.unregister_window(&self));
    }
}

unsafe impl HasRawWindowHandle for Window {
    fn raw_window_handle(&self) -> RawWindowHandle {
        self.winit_window.raw_window_handle()
    }
}

/// Builder for [`Window`] instances.
///
/// Default values:
///
/// * Width: `800`
/// * Height: `600`
/// * Title: `Riddle Window`
/// * Resizeable: `true`
/// * Cursor Visible: `true`
///
/// # Example
///
/// ```no_run
/// use riddle::{*, platform::*};
///
/// fn main() -> Result<(), RiddleError> {
///     let rdl =  RiddleLib::new()?;
///
///     let window: WindowHandle = WindowBuilder::new()
///         .title("A Sample Title")
///         .dimensions(320, 240)
///         .build(rdl.context())?;
///
///     // The window has been created, and is visible now.
///     // [.. the rest of the application ..]
/// # Ok(())
/// }
/// ```
pub struct WindowBuilder {
    width: u32,
    height: u32,
    title: String,
    resizeable: bool,
    cursor_visible: bool,
}

impl Default for WindowBuilder {
    fn default() -> Self {
        WindowBuilder {
            width: 800,
            height: 600,
            title: String::from("Riddle Window"),
            resizeable: true,
            cursor_visible: true,
        }
    }
}

impl WindowBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the dimensions of the window, in logical units.
    ///
    /// The pixel dimensions of the window may be different if the display's scale factor
    /// does not equal `1`.
    pub fn dimensions(&mut self, width: u32, height: u32) -> &mut Self {
        self.width = width;
        self.height = height;
        self
    }

    /// Set whether the window is resizeable.
    pub fn resizeable(&mut self, resizeable: bool) -> &mut Self {
        self.resizeable = resizeable;
        self
    }

    /// Set the window's title.
    pub fn title<S: Into<String>>(&mut self, title: S) -> &mut Self {
        self.title = title.into();
        self
    }

    /// Set whether the system cursor will be displayed for the created window.
    pub fn cursor_visible(&mut self, cursor_visible: bool) -> &mut Self {
        self.cursor_visible = cursor_visible;
        self
    }

    /// Build the new window, returning a handle to the new window.
    ///
    /// The window will be visible as long as the handle, or a clone of it, is alive.
    pub fn build<'a, C>(&self, ctx: C) -> Result<WindowHandle>
    where
        C: Borrow<PlatformContext<'a>>,
    {
        Window::new_shared(ctx.borrow(), self)
    }

    fn configure_window(&self, window: &winit::window::Window) {
        window.set_cursor_visible(self.cursor_visible);
    }
}