Skip to content
This repository was archived by the owner on Jul 8, 2025. It is now read-only.

Commit 0bcd66f

Browse files
Merge pull request #1 from 3k2ng/main
Some Opinionated Changes + Resize Implementation
2 parents 3a44f6c + 9134adc commit 0bcd66f

File tree

1 file changed

+21
-36
lines changed

1 file changed

+21
-36
lines changed

src/lib.rs

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,34 @@ use std::rc::Rc;
33

44
use softbuffer::Surface;
55
use winit::application::ApplicationHandler;
6+
use winit::dpi::PhysicalSize;
67
use winit::error::EventLoopError;
78
use winit::event::WindowEvent;
89
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
910
use winit::window::{Window, WindowId};
1011

1112
/// Contains a few potential properties to set for a SoftbufferWindow when it is created.
1213
pub struct WindowProperties {
13-
width: u32,
14-
height: u32,
15-
title: String,
14+
pub size: PhysicalSize<u32>,
15+
pub title: Box<str>,
1616
}
1717

18-
impl std::default::Default for WindowProperties {
18+
impl Default for WindowProperties {
1919
fn default() -> WindowProperties {
2020
WindowProperties {
21-
width: 800,
22-
height: 600,
23-
title: String::from("Softbuffer window"),
21+
size: PhysicalSize::new(800, 600),
22+
title: "Softbuffer window".into(),
2423
}
2524
}
2625
}
2726

2827
impl WindowProperties {
2928
pub fn new(width: u32, height: u32, title: &str) -> WindowProperties {
3029
WindowProperties {
31-
width,
32-
height,
33-
title: String::from(title),
30+
size: PhysicalSize::new(width, height),
31+
title: title.into(),
3432
}
3533
}
36-
pub fn get_size(&self) -> (u32, u32) {
37-
(self.width, self.height)
38-
}
39-
pub fn get_title(&self) -> &str {
40-
self.title.as_str()
41-
}
4234
}
4335

4436
/// Wrapper for Softbuffer and a Winit window
@@ -60,44 +52,37 @@ where
6052
let window = {
6153
let window = event_loop.create_window(
6254
Window::default_attributes()
63-
.with_title(self.properties.get_title())
64-
.with_inner_size(winit::dpi::LogicalSize::new(
65-
self.properties.get_size().0,
66-
self.properties.get_size().1,
67-
)),
55+
.with_title(self.properties.title.clone())
56+
.with_inner_size(self.properties.size),
6857
);
6958
Rc::new(window.unwrap())
7059
};
7160
let context = softbuffer::Context::new(window.clone()).unwrap();
7261
self.window = Some(window.clone());
73-
self.surface = Some(softbuffer::Surface::new(&context, window.clone()).unwrap());
62+
self.surface = Some(Surface::new(&context, window.clone()).unwrap());
7463
}
7564

7665
fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
7766
match event {
7867
WindowEvent::CloseRequested => {
7968
event_loop.exit();
8069
}
81-
WindowEvent::RedrawRequested => {
82-
let window = self.window.clone().unwrap();
83-
let surface = self.surface.as_mut().unwrap();
84-
let (width, height) = {
85-
let size = window.inner_size();
86-
(size.width, size.height)
87-
};
88-
surface
70+
WindowEvent::Resized(new_size) => {
71+
self.properties.size = new_size;
72+
let (width, height) = (new_size.width, new_size.height);
73+
self.surface
74+
.as_mut()
75+
.unwrap()
8976
.resize(
9077
NonZeroU32::new(width).unwrap(),
9178
NonZeroU32::new(height).unwrap(),
9279
)
9380
.unwrap();
94-
95-
let mut buffer = surface.buffer_mut().unwrap();
96-
97-
(self.loop_fn)(window, buffer.as_mut());
98-
81+
}
82+
WindowEvent::RedrawRequested => {
83+
let mut buffer = self.surface.as_mut().unwrap().buffer_mut().unwrap();
84+
(self.loop_fn)(self.window.clone().unwrap(), buffer.as_mut());
9985
buffer.present().unwrap();
100-
10186
self.window.as_ref().unwrap().request_redraw();
10287
}
10388
_ => (),

0 commit comments

Comments
 (0)