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
//! Window implementation for rendering applications.

use lambda_platform::winit::{
  Loop,
  WindowHandle,
  WindowHandleBuilder,
  WindowProperties,
};

use crate::events::Events;

/// Builder for windows that are used to render a frame within the
/// RenderContext.
pub struct WindowBuilder {
  name: String,
  dimensions: (u32, u32),
  vsync: bool,
}

impl WindowBuilder {
  /// A new window builder will be 480x360 by default and have the name
  /// "Window". After customizing the window with whatever properties your
  /// application needs, you can supply it an event loop to process the
  /// events that will be generated by the window.
  pub fn new() -> Self {
    return Self {
      name: String::from("Window"),
      dimensions: (480, 360),
      vsync: false,
    };
  }

  /// The name to be displayed in the title bar of the window.
  pub fn with_name(mut self, name: &str) -> Self {
    self.name = name.to_string();
    return self;
  }

  /// Specify the dimensions for the window (Defaults to 480 x 360).
  pub fn with_dimensions(mut self, width: u32, height: u32) -> Self {
    self.dimensions = (width, height);
    return self;
  }

  pub fn with_vsync(mut self, vsync: bool) -> Self {
    return self;
  }

  // TODO(vmarcella): Remove new call for window and construct the window directly.
  pub fn build(self, event_loop: &mut Loop<Events>) -> Window {
    return Window::new(self.name.as_str(), self.dimensions, event_loop);
  }
}

/// Window implementation for rendering applications.
pub struct Window {
  window_handle: WindowHandle,
}

impl Window {
  fn new(
    name: &str,
    dimensions: (u32, u32),
    event_loop: &mut Loop<Events>,
  ) -> Self {
    // Attempt to get the primary monitor first and then falls back to the first
    // available monitor if that isn't found.
    let monitor_handle = event_loop.get_primary_monitor().unwrap_or(
      event_loop
        .get_any_available_monitors()
        .expect("No monitors available"),
    );

    let window_properties = WindowProperties {
      name: name.to_string(),
      dimensions,
      monitor_handle,
    };

    let window_handle = WindowHandleBuilder::new()
      .with_window_properties(window_properties, event_loop)
      .build();

    return Self { window_handle };
  }

  /// Redraws the window.
  pub fn redraw(&self) {
    self.window_handle.window_handle.request_redraw();
  }

  /// Returns the window handle.
  pub fn window_handle(&self) -> &WindowHandle {
    return &self.window_handle;
  }

  /// Returns the dimensions of the current window. (width, height)
  pub fn dimensions(&self) -> (u32, u32) {
    return (
      self.window_handle.size.width,
      self.window_handle.size.height,
    );
  }
}