lambda/render/
window.rs

1//! Window implementation for rendering applications.
2
3use lambda_platform::winit::{
4  Loop,
5  WindowHandle,
6  WindowHandleBuilder,
7  WindowProperties,
8};
9
10use crate::events::Events;
11
12/// Builder for windows that are used to render a frame within the
13/// RenderContext.
14pub struct WindowBuilder {
15  name: String,
16  dimensions: (u32, u32),
17  vsync: bool,
18}
19
20impl WindowBuilder {
21  /// A new window builder will be 480x360 by default and have the name
22  /// "Window". After customizing the window with whatever properties your
23  /// application needs, you can supply it an event loop to process the
24  /// events that will be generated by the window.
25  pub fn new() -> Self {
26    return Self {
27      name: String::from("Window"),
28      dimensions: (480, 360),
29      vsync: false,
30    };
31  }
32
33  /// The name to be displayed in the title bar of the window.
34  pub fn with_name(mut self, name: &str) -> Self {
35    self.name = name.to_string();
36    return self;
37  }
38
39  /// Specify the dimensions for the window (Defaults to 480 x 360).
40  pub fn with_dimensions(mut self, width: u32, height: u32) -> Self {
41    self.dimensions = (width, height);
42    return self;
43  }
44
45  pub fn with_vsync(mut self, vsync: bool) -> Self {
46    return self;
47  }
48
49  // TODO(vmarcella): Remove new call for window and construct the window directly.
50  pub fn build(self, event_loop: &mut Loop<Events>) -> Window {
51    return Window::new(self.name.as_str(), self.dimensions, event_loop);
52  }
53}
54
55/// Window implementation for rendering applications.
56pub struct Window {
57  window_handle: WindowHandle,
58}
59
60impl Window {
61  fn new(
62    name: &str,
63    dimensions: (u32, u32),
64    event_loop: &mut Loop<Events>,
65  ) -> Self {
66    // Attempt to get the primary monitor first and then falls back to the first
67    // available monitor if that isn't found.
68    let monitor_handle = event_loop.get_primary_monitor().unwrap_or(
69      event_loop
70        .get_any_available_monitors()
71        .expect("No monitors available"),
72    );
73
74    let window_properties = WindowProperties {
75      name: name.to_string(),
76      dimensions,
77      monitor_handle,
78    };
79
80    let window_handle = WindowHandleBuilder::new()
81      .with_window_properties(window_properties, event_loop)
82      .build();
83
84    logging::debug!("Created window: {}", name);
85    return Self { window_handle };
86  }
87
88  /// Redraws the window.
89  pub fn redraw(&self) {
90    self.window_handle.window_handle.request_redraw();
91  }
92
93  /// Returns the window handle.
94  pub fn window_handle(&self) -> &WindowHandle {
95    return &self.window_handle;
96  }
97
98  /// Returns the dimensions of the current window. (width, height)
99  pub fn dimensions(&self) -> (u32, u32) {
100    return (
101      self.window_handle.size.width,
102      self.window_handle.size.height,
103    );
104  }
105}