Skip to content

Commit 01fa89d

Browse files
committed
[add] Start conversion to Builder pattern in winit
1. Add a LoopBuilder for consistency. 2. Add a WindowHandleBuilder 3. Add from method in LoopPublisher 4. Add TODOS to remove/review changes, and older functions.
1 parent a178504 commit 01fa89d

1 file changed

Lines changed: 93 additions & 2 deletions

File tree

  • crates/lambda-platform/src/winit

crates/lambda-platform/src/winit/mod.rs

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use winit::{
1919

2020
/// Embedded module for exporting data/types from winit as minimally/controlled
2121
/// as possible. The exports from this module are not guaranteed to be stable.
22+
// TODO(ahlawat) = Remove all these except WindowEvent since we're abstracting them already? Double check
2223
pub mod winit_exports {
2324
pub use winit::{
2425
event::{
@@ -34,11 +35,22 @@ pub mod winit_exports {
3435
};
3536
}
3637

38+
/// LoopBuilder - Putting this here for consistency.
39+
pub struct LoopBuilder;
40+
41+
impl LoopBuilder {
42+
pub fn build<Events: 'static>() -> Loop<Events> {
43+
let event_loop = EventLoop::<Events>::with_user_event();
44+
return Loop { event_loop };
45+
}
46+
}
47+
3748
/// Loop wrapping for the winit event loop.
3849
pub struct Loop<E: 'static> {
3950
event_loop: EventLoop<E>,
4051
}
4152

53+
/// TODO(ahlawat) = Remove this and refactor the code depending directly on it.
4254
pub fn create_event_loop<Events: 'static>() -> Loop<Events> {
4355
let event_loop = EventLoop::<Events>::with_user_event();
4456
return Loop { event_loop };
@@ -51,7 +63,7 @@ pub struct WindowProperties {
5163
pub monitor_handle: MonitorHandle,
5264
}
5365

54-
/// Metadata for Lambda window sizing that supports Copy and move operations.
66+
/// Metadata for Lambda window sizing that supports Copy and Move operations.
5567
#[derive(Clone, Copy)]
5668
pub struct WindowSize {
5769
pub width: u32,
@@ -66,6 +78,77 @@ pub struct WindowHandle {
6678
pub monitor_handle: MonitorHandle,
6779
}
6880

81+
// Should we take the loop as a field right here? Probably a ref or something? IDK
82+
pub struct WindowHandleBuilder {
83+
window_handle: Option<Window>,
84+
size: Option<WindowSize>,
85+
monitor_handle: Option<MonitorHandle>,
86+
}
87+
88+
impl WindowHandleBuilder {
89+
/// Instantiate an empty builder
90+
pub fn new() -> Self {
91+
return Self;
92+
}
93+
94+
/// Set the window size for the WindowHandle
95+
fn with_window_size(
96+
&mut self,
97+
window_size: [u32; 2],
98+
scale_factor: f64,
99+
) -> self {
100+
let logical: LogicalSize<u32> = window_size.into();
101+
let physical: PhysicalSize<u32> = logical_size.to_physical(scale_factor);
102+
103+
let window_size = WindowSize {
104+
width: window_size[0],
105+
height: window_size[1],
106+
logical,
107+
physical,
108+
};
109+
110+
self.size = Some(window_size);
111+
return self;
112+
}
113+
114+
/// Probably the function that'll be used the most
115+
pub fn with_window_properties<E: 'static>(
116+
&mut self,
117+
window_properties: WindowProperties,
118+
lambda_loop: &Loop<E>,
119+
) -> self {
120+
let WindowProperties {
121+
name,
122+
dimensions,
123+
monitor_handle,
124+
} = window_properties;
125+
126+
self.with_window_size(dimensions, monitor_handle.scale_factor());
127+
128+
let window_handle = WindowBuilder::new()
129+
.with_title(name)
130+
.with_inner_size(self.size.expect("No window size found.").logical)
131+
.build(&lambda_loop.event_loop)
132+
.expect("Failed creation of window handle");
133+
134+
self.monitor_handle = Some(monitor_handle);
135+
self.window_handle = Some(window_handle);
136+
return self;
137+
}
138+
139+
/// Build the WindowHandle
140+
pub fn build(&self) -> WindowHandle {
141+
return WindowHandle {
142+
monitor_handle: self
143+
.monitor_handle
144+
.expect("Unable to find a MonitorHandle."),
145+
size: self.size.expect("Unable to find WindowSize."),
146+
window_handle: self.window_handle.expect("Unable to find WindowHandle."),
147+
};
148+
}
149+
}
150+
151+
// TODO(ahlawat) = Remove this as well?
69152
/// Construct WindowSize metdata from the window dimensions and scale factor of
70153
/// the monitor being rendered to.
71154
#[inline]
@@ -89,12 +172,18 @@ pub struct LoopPublisher<E: 'static> {
89172
}
90173

91174
impl<E: 'static> LoopPublisher<E> {
92-
/// Instantiate a new EventLoopPublisher from an event loop proxy.
175+
/// Instantiate a new LoopPublisher from an event loop proxy.
93176
#[inline]
94177
pub fn new(winit_proxy: EventLoopProxy<E>) -> Self {
95178
return LoopPublisher { winit_proxy };
96179
}
97180

181+
/// Instantiate a new LoopPublisher from a loop
182+
pub fn from<E: 'static>(lambda_loop: &Loop<E>) -> Self {
183+
let winit_proxy = lambda_loop.event_loop.create_proxy();
184+
return LoopPublisher { winit_proxy };
185+
}
186+
98187
/// Send an event
99188
#[inline]
100189
pub fn send_event(&self, event: E) {
@@ -103,6 +192,7 @@ impl<E: 'static> LoopPublisher<E> {
103192
}
104193

105194
impl<E: 'static> Loop<E> {
195+
// TODO(ahlawat) = Possibly remove this?
106196
pub fn create_publisher(&mut self) -> LoopPublisher<E> {
107197
let proxy = self.event_loop.create_proxy();
108198
return LoopPublisher::new(proxy);
@@ -134,6 +224,7 @@ impl<E: 'static> Loop<E> {
134224
self.event_loop.run(callback);
135225
}
136226

227+
// TODO(ahlawat) = Should this be here?
137228
pub fn create_window_handle(
138229
&mut self,
139230
window_properties: WindowProperties,

0 commit comments

Comments
 (0)