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
use std::{
    collections::HashMap,
    sync::{Arc, Mutex},
};

use kludgine_core::{
    figures::{num_traits::One, Pixels, Point, Points},
    flume,
    math::{Scale, Scaled, Size},
    scene::Target,
    winit::{
        self,
        window::{Theme, WindowBuilder as WinitWindowBuilder, WindowId},
    },
};
use lazy_static::lazy_static;

use crate::{Error, Runtime};

/// Types for event handling.
pub mod event;
mod open;
mod runtime_window;

pub use open::{OpenWindow, RedrawRequester, RedrawStatus};
pub use runtime_window::{opened_first_window, RuntimeWindow, RuntimeWindowConfig, WindowHandle};
pub use winit::window::Icon;

use self::event::InputEvent;

/// How to react to a request to close a window
pub enum CloseResponse {
    /// Window should remain open
    RemainOpen,
    /// Window should close
    Close,
}

/// Trait to implement a Window
pub trait Window: Send + Sync + 'static {
    /// Called when the window is being initilaized.
    fn initialize(
        &mut self,
        _scene: &Target,
        _redrawer: RedrawRequester,
        _window: WindowHandle,
    ) -> crate::Result<()>
    where
        Self: Sized,
    {
        Ok(())
    }

    /// The window was requested to be closed, most likely from the Close
    /// Button. Override this implementation if you want logic in place to
    /// prevent a window from closing.
    fn close_requested(&mut self, _window: WindowHandle) -> crate::Result<CloseResponse> {
        Ok(CloseResponse::Close)
    }

    /// The window has received an input event.
    fn process_input(
        &mut self,
        _input: InputEvent,
        _status: &mut RedrawStatus,
        _scene: &Target,
        _window: WindowHandle,
    ) -> crate::Result<()>
    where
        Self: Sized,
    {
        Ok(())
    }

    /// A text input was received.
    fn receive_character(
        &mut self,
        _character: char,
        _status: &mut RedrawStatus,
        _scene: &Target,
        _window: WindowHandle,
    ) -> crate::Result<()>
    where
        Self: Sized,
    {
        Ok(())
    }

    /// Specify a target frames per second, which will force your window
    /// to redraw at this rate. If None is returned, the Window will only
    /// redraw when requested via methods on Context.
    fn target_fps(&self) -> Option<u16> {
        None
    }

    /// Renders the contents of the window. Called whenever the operating system
    /// needs the window's contents to be redrawn or when [`RedrawStatus`]
    /// indicates a new frame should be rendered in [`Window::update()`].
    #[allow(unused_variables)]
    fn render(
        &mut self,
        scene: &Target,
        status: &mut RedrawStatus,
        _window: WindowHandle,
    ) -> crate::Result<()> {
        Ok(())
    }

    /// Called on a regular basis as events come in. Use `status` to indicate
    /// when a redraw should happen.
    #[allow(unused_variables)]
    fn update(
        &mut self,
        scene: &Target,
        status: &mut RedrawStatus,
        _window: WindowHandle,
    ) -> crate::Result<()>
    where
        Self: Sized,
    {
        Ok(())
    }

    /// Called prior to rendering to allow setting a scaling amount that
    /// operates on top of the automatic DPI scaling. This can be used to offer
    /// a zoom setting to end-users.
    fn additional_scale(&self) -> Scale<f32, Scaled, Points> {
        Scale::one()
    }
}

/// Defines initial window properties.
pub trait WindowCreator: Window {
    /// Returns a [`WindowBuilder`] for this window.
    #[must_use]
    fn get_window_builder(&self) -> WindowBuilder {
        WindowBuilder::default()
            .with_title(self.window_title())
            .with_initial_system_theme(self.initial_system_theme())
            .with_size(self.initial_size())
            .with_resizable(self.resizable())
            .with_maximized(self.maximized())
            .with_visible(self.visible())
            .with_transparent(self.transparent())
            .with_decorations(self.decorations())
            .with_always_on_top(self.always_on_top())
    }

    /// The initial title of the window.
    #[must_use]
    fn window_title(&self) -> String {
        "Kludgine".to_owned()
    }

    /// The initial size of the window.
    #[must_use]
    fn initial_size(&self) -> Size<u32, Pixels> {
        Size::new(1024, 768)
    }

    /// Whether the window should be resizable or not.
    #[must_use]
    fn resizable(&self) -> bool {
        true
    }

    /// Whether the window should be maximized or not.
    #[must_use]
    fn maximized(&self) -> bool {
        false
    }

    /// Whether the window should be visible or not.
    #[must_use]
    fn visible(&self) -> bool {
        true
    }

    /// Whether the window should be transparent or not.
    #[must_use]
    fn transparent(&self) -> bool {
        false
    }

    /// Whether the window should be drawn with decorations or not.
    #[must_use]
    fn decorations(&self) -> bool {
        true
    }

    /// Whether the window should always be on top or not.
    #[must_use]
    fn always_on_top(&self) -> bool {
        false
    }

    /// The default [`Theme`] for the [`Window`] if `winit` is unable to
    /// determine the system theme.
    #[must_use]
    fn initial_system_theme(&self) -> Theme {
        Theme::Light
    }
}

/// A builder for a [`Window`].
#[derive(Default)]
pub struct WindowBuilder {
    title: Option<String>,
    position: Option<Point<i32, Pixels>>,
    size: Option<Size<u32, Pixels>>,
    resizable: Option<bool>,
    maximized: Option<bool>,
    visible: Option<bool>,
    transparent: Option<bool>,
    decorations: Option<bool>,
    always_on_top: Option<bool>,
    pub(crate) initial_system_theme: Option<Theme>,
    icon: Option<winit::window::Icon>,
}

impl WindowBuilder {
    /// Builder-style function. Sets `title` and returns self.
    #[must_use]
    pub fn with_title<T: Into<String>>(mut self, title: T) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Builder-style function. Sets `size` and returns self.
    #[must_use]
    pub const fn with_position(mut self, position: Point<i32, Pixels>) -> Self {
        self.position = Some(position);
        self
    }

    /// Builder-style function. Sets `size` and returns self.
    #[must_use]
    pub const fn with_size(mut self, size: Size<u32, Pixels>) -> Self {
        self.size = Some(size);
        self
    }

    /// Builder-style function. Sets `resizable` and returns self.
    #[must_use]
    pub const fn with_resizable(mut self, resizable: bool) -> Self {
        self.resizable = Some(resizable);
        self
    }

    /// Builder-style function. Sets `maximized` and returns self.
    #[must_use]
    pub const fn with_maximized(mut self, maximized: bool) -> Self {
        self.maximized = Some(maximized);
        self
    }

    /// Builder-style function. Sets `visible` and returns self.
    #[must_use]
    pub const fn with_visible(mut self, visible: bool) -> Self {
        self.visible = Some(visible);
        self
    }

    /// Builder-style function. Sets `transparent` and returns self.
    #[must_use]
    pub const fn with_transparent(mut self, transparent: bool) -> Self {
        self.transparent = Some(transparent);
        self
    }

    /// Builder-style function. Sets `decorations` and returns self.
    #[must_use]
    pub const fn with_decorations(mut self, decorations: bool) -> Self {
        self.decorations = Some(decorations);
        self
    }

    /// Builder-style function. Sets `alawys_on_top` and returns self.
    #[must_use]
    pub const fn with_always_on_top(mut self, always_on_top: bool) -> Self {
        self.always_on_top = Some(always_on_top);
        self
    }

    /// Builder-style function. Sets `icon` and returns self.
    #[must_use]
    #[allow(clippy::missing_const_for_fn)] // unsupported
    pub fn with_icon(mut self, icon: Icon) -> Self {
        self.icon = Some(icon);
        self
    }

    /// Builder-style function. Sets `initial_system_theme` and returns self.
    #[must_use]
    pub const fn with_initial_system_theme(mut self, system_theme: Theme) -> Self {
        self.initial_system_theme = Some(system_theme);
        self
    }
}

impl From<WindowBuilder> for WinitWindowBuilder {
    fn from(wb: WindowBuilder) -> Self {
        let mut builder = Self::new();
        if let Some(title) = wb.title {
            builder = builder.with_title(title);
        }
        if let Some(position) = wb.position {
            builder = builder.with_position(winit::dpi::Position::Physical(
                winit::dpi::PhysicalPosition {
                    x: position.x,
                    y: position.y,
                },
            ));
        }
        if let Some(size) = wb.size {
            builder =
                builder.with_inner_size(winit::dpi::Size::Physical(winit::dpi::PhysicalSize {
                    width: size.width,
                    height: size.height,
                }));
        }
        if let Some(resizable) = wb.resizable {
            builder = builder.with_resizable(resizable);
        }
        if let Some(maximized) = wb.maximized {
            builder = builder.with_maximized(maximized);
        }
        if let Some(visible) = wb.visible {
            builder = builder.with_visible(visible);
        }
        if let Some(transparent) = wb.transparent {
            builder = builder.with_transparent(transparent);
        }
        if let Some(decorations) = wb.decorations {
            builder = builder.with_decorations(decorations);
        }
        if let Some(always_on_top) = wb.always_on_top {
            builder = builder.with_always_on_top(always_on_top);
        }

        builder = builder.with_window_icon(wb.icon);

        builder
    }
}

/// A window that can be opened.
#[cfg(feature = "multiwindow")]
pub trait OpenableWindow {
    /// Opens `self` as a [`Window`].
    fn open(self);
}

#[cfg(feature = "multiwindow")]
impl<T> OpenableWindow for T
where
    T: Window + WindowCreator,
{
    fn open(self) {
        crate::runtime::Runtime::open_window(self.get_window_builder(), self);
    }
}

lazy_static! {
    static ref WINDOW_CHANNELS: Arc<Mutex<HashMap<WindowId, flume::Sender<WindowMessage>>>> =
        Arc::default();
}

pub enum WindowMessage {
    Close,
    RequestClose,
    SetAdditionalScale(Scale<f32, Scaled, Points>),
}

impl WindowMessage {
    pub fn send_to(self, id: WindowId) -> crate::Result<()> {
        let sender = {
            let mut channels = WINDOW_CHANNELS.lock().unwrap();
            if let Some(sender) = channels.get_mut(&id) {
                sender.clone()
            } else {
                return Err(Error::InternalWindowMessageSend(
                    "Channel not found for id".to_owned(),
                ));
            }
        };

        sender.send(self).unwrap_or_default();
        Runtime::try_process_window_events(None);
        Ok(())
    }
}