Skip to content

Commit ff2423b

Browse files
committed
wip
1 parent b207725 commit ff2423b

8 files changed

Lines changed: 104 additions & 73 deletions

File tree

examples/open_parented/src/main.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ impl ParentWindowHandler {
1818
let ctx = softbuffer::Context::new(window.clone())?;
1919
let mut surface = softbuffer::Surface::new(&ctx, window.clone())?;
2020
let size = window.size().physical;
21-
surface
22-
.resize(NonZeroU32::new(size.width).unwrap(), NonZeroU32::new(size.height).unwrap())?;
21+
surface.resize(size.width.try_into()?, size.height.try_into()?)?;
2322

2423
let window_open_options = WindowOpenOptions::new()
2524
.with_size(LogicalSize::new(256, 256))
@@ -37,25 +36,29 @@ impl ParentWindowHandler {
3736
}
3837

3938
impl WindowHandler for ParentWindowHandler {
40-
fn on_frame(&self) {
39+
fn on_frame(&self) -> Result<(), HandlerError> {
4140
let mut surface = self.surface.borrow_mut();
42-
let mut buf = surface.buffer_mut().unwrap();
41+
let mut buf = surface.buffer_mut()?;
4342
if self.damaged.get() {
4443
buf.fill(0xFFAAAAAA);
4544
self.damaged.set(false);
4645
}
47-
buf.present().unwrap();
46+
buf.present()?;
47+
48+
Ok(())
4849
}
4950

50-
fn resized(&self, new_size: WindowSize) {
51+
fn resized(&self, new_size: WindowSize) -> Result<(), HandlerError> {
5152
println!("Parent Resized: {new_size:?}");
5253

5354
if let (Some(width), Some(height)) =
5455
(NonZeroU32::new(new_size.physical.width), NonZeroU32::new(new_size.physical.height))
5556
{
56-
self.surface.borrow_mut().resize(width, height).unwrap();
57+
self.surface.borrow_mut().resize(width, height)?;
5758
self.damaged.set(true);
5859
}
60+
61+
Ok(())
5962
}
6063

6164
fn on_event(&self, event: Event) -> EventStatus {
@@ -80,33 +83,36 @@ impl ChildWindowHandler {
8083
let ctx = softbuffer::Context::new(window.clone())?;
8184
let mut surface = softbuffer::Surface::new(&ctx, window.clone())?;
8285
let size = window.size().physical;
83-
surface
84-
.resize(NonZeroU32::new(size.width).unwrap(), NonZeroU32::new(size.height).unwrap())?;
86+
surface.resize(size.width.try_into()?, size.height.try_into()?)?;
8587

8688
Ok(Self { surface: surface.into(), damaged: true.into() })
8789
}
8890
}
8991

9092
impl WindowHandler for ChildWindowHandler {
91-
fn on_frame(&self) {
93+
fn on_frame(&self) -> Result<(), HandlerError> {
9294
let mut surface = self.surface.borrow_mut();
93-
let mut buf = surface.buffer_mut().unwrap();
95+
let mut buf = surface.buffer_mut()?;
9496
if self.damaged.get() {
9597
buf.fill(0xFFAA0000);
9698
self.damaged.set(false);
9799
}
98-
buf.present().unwrap();
100+
buf.present()?;
101+
102+
Ok(())
99103
}
100104

101-
fn resized(&self, new_size: WindowSize) {
105+
fn resized(&self, new_size: WindowSize) -> Result<(), HandlerError> {
102106
println!("Child Resized: {new_size:?}");
103107

104108
if let (Some(width), Some(height)) =
105109
(NonZeroU32::new(new_size.physical.width), NonZeroU32::new(new_size.physical.height))
106110
{
107-
self.surface.borrow_mut().resize(width, height).unwrap();
111+
self.surface.borrow_mut().resize(width, height)?;
108112
self.damaged.set(true);
109113
}
114+
115+
Ok(())
110116
}
111117

112118
fn on_event(&self, event: Event) -> EventStatus {
@@ -121,10 +127,10 @@ impl WindowHandler for ChildWindowHandler {
121127
}
122128
}
123129

124-
fn main() {
130+
fn main() -> Result<(), baseview::Error> {
125131
let window_open_options = WindowOpenOptions::new().with_size(LogicalSize::new(512.0, 512.0));
126132

127-
baseview::create_window(window_open_options, ParentWindowHandler::new)
128-
.unwrap()
129-
.run_until_closed();
133+
baseview::create_window(window_open_options, ParentWindowHandler::new)?.run_until_closed();
134+
135+
Ok(())
130136
}

examples/open_window/src/main.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use rtrb::{Consumer, RingBuffer};
88
use baseview::copy_to_clipboard;
99
use baseview::dpi::{LogicalSize, PhysicalPosition};
1010
use baseview::{
11-
Event, EventStatus, MouseEvent, WindowContext, WindowHandler, WindowOpenOptions, WindowSize,
11+
Event, EventStatus, HandlerError, MouseEvent, WindowContext, WindowHandler, WindowOpenOptions,
12+
WindowSize,
1213
};
1314

1415
#[derive(Debug, Clone)]
@@ -27,24 +28,26 @@ struct OpenWindowExample {
2728
}
2829

2930
impl WindowHandler for OpenWindowExample {
30-
fn resized(&self, new_size: WindowSize) {
31+
fn resized(&self, new_size: WindowSize) -> Result<(), HandlerError> {
3132
println!("Resized: {new_size:?}");
3233

3334
if let (Some(width), Some(height)) =
3435
(NonZeroU32::new(new_size.physical.width), NonZeroU32::new(new_size.physical.height))
3536
{
36-
self.surface.borrow_mut().resize(width, height).unwrap();
37+
self.surface.borrow_mut().resize(width, height)?;
3738
self.damaged.set(true);
3839
}
40+
41+
Ok(())
3942
}
4043

41-
fn on_frame(&self) {
44+
fn on_frame(&self) -> Result<(), HandlerError> {
4245
if !self.damaged.get() {
43-
return;
46+
return Ok(());
4447
}
4548

4649
let mut surface = self.surface.borrow_mut();
47-
let mut pixels = surface.buffer_mut().unwrap();
50+
let mut pixels = surface.buffer_mut()?;
4851
let size = self.window_context.size();
4952
let scale_factor = self.window_context.scale_factor();
5053
let (width, height) = (size.physical.width, size.physical.height);
@@ -101,12 +104,14 @@ impl WindowHandler for OpenWindowExample {
101104
}
102105
}
103106

104-
pixels.present().unwrap();
107+
pixels.present()?;
105108
self.damaged.set(false);
106109

107110
while let Ok(message) = self.rx.borrow_mut().pop() {
108111
println!("Message: {:?}", message);
109112
}
113+
114+
Ok(())
110115
}
111116

112117
fn on_event(&self, event: Event) -> EventStatus {
@@ -134,7 +139,7 @@ impl WindowHandler for OpenWindowExample {
134139
}
135140
}
136141

137-
fn main() {
142+
fn main() -> Result<(), baseview::Error> {
138143
let window_open_options = WindowOpenOptions::new().with_size(LogicalSize::new(512.0, 512.0));
139144

140145
let (mut tx, rx) = RingBuffer::new(128);
@@ -148,12 +153,10 @@ fn main() {
148153
});
149154

150155
baseview::create_window(window_open_options, |window| {
151-
let ctx = softbuffer::Context::new(window.clone()).unwrap();
152-
let mut surface = softbuffer::Surface::new(&ctx, window.clone()).unwrap();
156+
let ctx = softbuffer::Context::new(window.clone())?;
157+
let mut surface = softbuffer::Surface::new(&ctx, window.clone())?;
153158
let size = window.size().physical;
154-
surface
155-
.resize(NonZeroU32::new(size.width).unwrap(), NonZeroU32::new(size.height).unwrap())
156-
.unwrap();
159+
surface.resize(size.width.try_into()?, size.height.try_into()?)?;
157160

158161
Ok(OpenWindowExample {
159162
window_context: window,
@@ -163,9 +166,10 @@ fn main() {
163166
is_cursor_inside: false.into(),
164167
damaged: true.into(),
165168
})
166-
})
167-
.unwrap()
169+
})?
168170
.run_until_closed();
171+
172+
Ok(())
169173
}
170174

171175
fn log_event(event: &Event) {

examples/plugin_clack/src/window_handler.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,26 @@ pub struct OpenWindowExample {
1515
}
1616

1717
impl WindowHandler for OpenWindowExample {
18-
fn resized(&self, new_size: WindowSize) {
18+
fn resized(&self, new_size: WindowSize) -> Result<(), HandlerError> {
1919
println!("Resized: {new_size:?}");
2020

2121
if let (Some(width), Some(height)) =
2222
(NonZeroU32::new(new_size.physical.width), NonZeroU32::new(new_size.physical.height))
2323
{
24-
self.surface.borrow_mut().resize(width, height).unwrap();
24+
self.surface.borrow_mut().resize(width, height)?;
2525
self.damaged.set(true);
2626
}
27+
28+
Ok(())
2729
}
2830

29-
fn on_frame(&self) {
31+
fn on_frame(&self) -> Result<(), HandlerError> {
3032
if !self.damaged.get() {
31-
return;
33+
return Ok(());
3234
}
3335

3436
let mut surface = self.surface.borrow_mut();
35-
let mut pixels = surface.buffer_mut().unwrap();
37+
let mut pixels = surface.buffer_mut()?;
3638
let size = self.window_context.size();
3739
let scale_factor = self.window_context.scale_factor();
3840
let (width, height) = (size.physical.width, size.physical.height);
@@ -89,8 +91,10 @@ impl WindowHandler for OpenWindowExample {
8991
}
9092
}
9193

92-
pixels.present().unwrap();
94+
pixels.present()?;
9395
self.damaged.set(false);
96+
97+
Ok(())
9498
}
9599

96100
fn on_event(&self, event: Event) -> EventStatus {
@@ -121,8 +125,7 @@ impl OpenWindowExample {
121125
let ctx = softbuffer::Context::new(window.clone())?;
122126
let mut surface = softbuffer::Surface::new(&ctx, window.clone())?;
123127
let size = window.size().physical;
124-
surface
125-
.resize(NonZeroU32::new(size.width).unwrap(), NonZeroU32::new(size.height).unwrap())?;
128+
surface.resize(size.width.try_into()?, size.height.try_into()?)?;
126129

127130
Ok(OpenWindowExample {
128131
window_context: window,

examples/render_femtovg/src/main.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use baseview::{
77
use femtovg::renderer::OpenGl;
88
use femtovg::{Canvas, Color};
99
use std::cell::{Cell, RefCell};
10-
use std::ffi::CString;
1110

1211
struct FemtovgExample {
1312
window_context: WindowContext,
@@ -19,19 +18,18 @@ struct FemtovgExample {
1918

2019
impl FemtovgExample {
2120
fn new(window_context: WindowContext) -> Result<Self, HandlerError> {
22-
let gl_context = window_context.gl_context().unwrap();
23-
unsafe { gl_context.make_current() };
21+
let Some(gl_context) = window_context.gl_context() else { unreachable!() };
22+
unsafe { gl_context.make_current()? };
2423

25-
let renderer = unsafe {
26-
OpenGl::new_from_function(|s| gl_context.get_proc_address(&CString::new(s).unwrap()))
27-
}?;
24+
let renderer =
25+
unsafe { OpenGl::new_from_function(|s| gl_context.get_proc_address_from_str(s)) }?;
2826

2927
let mut canvas = Canvas::new(renderer)?;
3028
let size = window_context.size();
3129

3230
canvas.set_size(size.physical.width, size.physical.height, size.scale_factor as f32);
3331

34-
unsafe { gl_context.make_not_current() };
32+
unsafe { gl_context.make_not_current()? };
3533
Ok(Self {
3634
gl_context,
3735
window_context,
@@ -43,9 +41,9 @@ impl FemtovgExample {
4341
}
4442

4543
impl WindowHandler for FemtovgExample {
46-
fn on_frame(&self) {
44+
fn on_frame(&self) -> Result<(), HandlerError> {
4745
if !self.damaged.get() {
48-
return;
46+
return Ok(());
4947
}
5048

5149
let context = &self.gl_context;
@@ -84,12 +82,16 @@ impl WindowHandler for FemtovgExample {
8482
context.swap_buffers();
8583
unsafe { context.make_not_current() };
8684
self.damaged.set(false);
85+
86+
Ok(())
8787
}
8888

89-
fn resized(&self, new_size: WindowSize) {
89+
fn resized(&self, new_size: WindowSize) -> Result<(), HandlerError> {
9090
let size = new_size.physical;
9191
self.canvas.borrow_mut().set_size(size.width, size.height, new_size.scale_factor as f32);
9292
self.damaged.set(true);
93+
94+
Ok(())
9395
}
9496

9597
fn on_event(&self, event: Event) -> EventStatus {
@@ -113,13 +115,14 @@ impl WindowHandler for FemtovgExample {
113115
}
114116
}
115117

116-
fn main() {
118+
fn main() -> Result<(), baseview::Error> {
117119
let window_open_options = WindowOpenOptions::new()
118120
.with_title("Femtovg on Baseview")
119121
.with_size(LogicalSize::new(512, 512))
120122
.with_gl_config(GlConfig { alpha_bits: 8, ..GlConfig::default() });
121123

122-
baseview::create_window(window_open_options, FemtovgExample::new).unwrap().run_until_closed();
124+
baseview::create_window(window_open_options, FemtovgExample::new)?.run_until_closed();
125+
Ok(())
123126
}
124127

125128
fn log_event(event: &Event) {

0 commit comments

Comments
 (0)