Skip to content

Commit 3863d6b

Browse files
committed
wip
1 parent 5e7b0e9 commit 3863d6b

41 files changed

Lines changed: 893 additions & 367 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ opengl = [
3737
keyboard-types = { version = "0.8.3" }
3838
raw-window-handle = "0.6.2"
3939
dpi = "0.1.2"
40+
tracing = { version = "0.1", optional = true }
4041

4142
[target.'cfg(target_os="linux")'.dependencies]
4243
x11rb = { version = "0.13.2", features = ["cursor", "resource_manager", "allow-unsafe-code", "dl-libxcb"], default-features = false }

examples/open_parented/src/main.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use baseview::dpi::LogicalSize;
22
use baseview::{
3-
Event, EventStatus, WindowContext, WindowHandle, WindowHandler, WindowOpenOptions, WindowSize,
3+
Event, EventStatus, HandlerError, WindowContext, WindowHandle, WindowHandler,
4+
WindowOpenOptions, WindowSize,
45
};
56
use std::cell::{Cell, RefCell};
67
use std::num::NonZeroU32;
@@ -13,22 +14,25 @@ struct ParentWindowHandler {
1314
}
1415

1516
impl ParentWindowHandler {
16-
pub fn new(window: WindowContext) -> Self {
17-
let ctx = softbuffer::Context::new(window.clone()).unwrap();
18-
let mut surface = softbuffer::Surface::new(&ctx, window.clone()).unwrap();
17+
pub fn new(window: WindowContext) -> Result<Self, HandlerError> {
18+
let ctx = softbuffer::Context::new(window.clone())?;
19+
let mut surface = softbuffer::Surface::new(&ctx, window.clone())?;
1920
let size = window.size().physical;
2021
surface
21-
.resize(NonZeroU32::new(size.width).unwrap(), NonZeroU32::new(size.height).unwrap())
22-
.unwrap();
22+
.resize(NonZeroU32::new(size.width).unwrap(), NonZeroU32::new(size.height).unwrap())?;
2323

2424
let window_open_options = WindowOpenOptions::new()
2525
.with_size(LogicalSize::new(256, 256))
2626
.with_parent(&window)
2727
.with_title("baseview child");
2828

29-
let child_window = baseview::create_window(window_open_options, ChildWindowHandler::new);
29+
let child_window = baseview::create_window(window_open_options, ChildWindowHandler::new)?;
3030

31-
Self { surface: surface.into(), damaged: true.into(), _child_window: Some(child_window) }
31+
Ok(Self {
32+
surface: surface.into(),
33+
damaged: true.into(),
34+
_child_window: Some(child_window),
35+
})
3236
}
3337
}
3438

@@ -72,15 +76,14 @@ struct ChildWindowHandler {
7276
}
7377

7478
impl ChildWindowHandler {
75-
pub fn new(window: WindowContext) -> Self {
76-
let ctx = softbuffer::Context::new(window.clone()).unwrap();
77-
let mut surface = softbuffer::Surface::new(&ctx, window.clone()).unwrap();
79+
pub fn new(window: WindowContext) -> Result<Self, HandlerError> {
80+
let ctx = softbuffer::Context::new(window.clone())?;
81+
let mut surface = softbuffer::Surface::new(&ctx, window.clone())?;
7882
let size = window.size().physical;
7983
surface
80-
.resize(NonZeroU32::new(size.width).unwrap(), NonZeroU32::new(size.height).unwrap())
81-
.unwrap();
84+
.resize(NonZeroU32::new(size.width).unwrap(), NonZeroU32::new(size.height).unwrap())?;
8285

83-
Self { surface: surface.into(), damaged: true.into() }
86+
Ok(Self { surface: surface.into(), damaged: true.into() })
8487
}
8588
}
8689

@@ -121,5 +124,7 @@ impl WindowHandler for ChildWindowHandler {
121124
fn main() {
122125
let window_open_options = WindowOpenOptions::new().with_size(LogicalSize::new(512.0, 512.0));
123126

124-
baseview::create_window(window_open_options, ParentWindowHandler::new).run_until_closed();
127+
baseview::create_window(window_open_options, ParentWindowHandler::new)
128+
.unwrap()
129+
.run_until_closed();
125130
}

examples/open_window/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,16 @@ fn main() {
155155
.resize(NonZeroU32::new(size.width).unwrap(), NonZeroU32::new(size.height).unwrap())
156156
.unwrap();
157157

158-
OpenWindowExample {
158+
Ok(OpenWindowExample {
159159
window_context: window,
160160
surface: surface.into(),
161161
rx: rx.into(),
162162
mouse_pos: PhysicalPosition::new(0., 0.).into(),
163163
is_cursor_inside: false.into(),
164164
damaged: true.into(),
165-
}
165+
})
166166
})
167+
.unwrap()
167168
.run_until_closed();
168169
}
169170

examples/render_femtovg/src/main.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use baseview::dpi::{LogicalSize, PhysicalPosition};
22
use baseview::gl::{GlConfig, GlContext};
33
use baseview::{
4-
Event, EventStatus, MouseEvent, WindowContext, WindowHandler, WindowOpenOptions, WindowSize,
4+
Event, EventStatus, HandlerError, MouseEvent, WindowContext, WindowHandler, WindowOpenOptions,
5+
WindowSize,
56
};
67
use femtovg::renderer::OpenGl;
78
use femtovg::{Canvas, Color};
@@ -17,28 +18,27 @@ struct FemtovgExample {
1718
}
1819

1920
impl FemtovgExample {
20-
fn new(window_context: WindowContext) -> Self {
21+
fn new(window_context: WindowContext) -> Result<Self, HandlerError> {
2122
let gl_context = window_context.gl_context().unwrap();
2223
unsafe { gl_context.make_current() };
2324

2425
let renderer = unsafe {
2526
OpenGl::new_from_function(|s| gl_context.get_proc_address(&CString::new(s).unwrap()))
26-
}
27-
.unwrap();
27+
}?;
2828

29-
let mut canvas = Canvas::new(renderer).unwrap();
29+
let mut canvas = Canvas::new(renderer)?;
3030
let size = window_context.size();
3131

3232
canvas.set_size(size.physical.width, size.physical.height, size.scale_factor as f32);
3333

3434
unsafe { gl_context.make_not_current() };
35-
Self {
35+
Ok(Self {
3636
gl_context,
3737
window_context,
3838
canvas: canvas.into(),
3939
damaged: true.into(),
4040
current_mouse_position: Cell::new(PhysicalPosition::default()),
41-
}
41+
})
4242
}
4343
}
4444

@@ -119,7 +119,7 @@ fn main() {
119119
.with_size(LogicalSize::new(512, 512))
120120
.with_gl_config(GlConfig { alpha_bits: 8, ..GlConfig::default() });
121121

122-
baseview::create_window(window_open_options, FemtovgExample::new).run_until_closed();
122+
baseview::create_window(window_open_options, FemtovgExample::new).unwrap().run_until_closed();
123123
}
124124

125125
fn log_event(event: &Event) {

examples/render_wgpu/src/main.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use baseview::dpi::{LogicalSize, PhysicalSize};
2-
use baseview::{Event, EventStatus, WindowContext, WindowHandler, WindowOpenOptions, WindowSize};
2+
use baseview::{
3+
Event, EventStatus, HandlerError, WindowContext, WindowHandler, WindowOpenOptions, WindowSize,
4+
};
35

46
use log::LevelFilter;
57
use std::cell::RefCell;
@@ -16,12 +18,12 @@ struct WgpuExample {
1618
}
1719

1820
impl WgpuExample {
19-
async fn new(context: WindowContext) -> Self {
21+
async fn new(context: WindowContext) -> Result<Self, HandlerError> {
2022
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_with_display_handle(
2123
Box::new(context.platform_handle()),
2224
));
2325

24-
let surface = instance.create_surface(context.platform_handle()).unwrap();
26+
let surface = instance.create_surface(context.platform_handle())?;
2527

2628
let adapter = instance
2729
.request_adapter(&wgpu::RequestAdapterOptions {
@@ -31,8 +33,7 @@ impl WgpuExample {
3133
compatible_surface: Some(&surface),
3234
..Default::default()
3335
})
34-
.await
35-
.expect("Failed to find an appropriate adapter");
36+
.await?;
3637

3738
// Create the logical device and command queue
3839
let (device, queue) = adapter
@@ -44,8 +45,7 @@ impl WgpuExample {
4445
memory_hints: wgpu::MemoryHints::MemoryUsage,
4546
..Default::default()
4647
})
47-
.await
48-
.expect("Failed to create device");
48+
.await?;
4949

5050
const SHADER: &str = "
5151
const VERTS = array(
@@ -116,7 +116,7 @@ impl WgpuExample {
116116
let surface_config = surface.get_default_config(&adapter, width, height).unwrap();
117117
surface.configure(&device, &surface_config);
118118

119-
Self {
119+
Ok(Self {
120120
window_context: context,
121121

122122
instance,
@@ -125,7 +125,7 @@ impl WgpuExample {
125125
queue,
126126
surface: surface.into(),
127127
surface_config: surface_config.into(),
128-
}
128+
})
129129
}
130130
}
131131

@@ -209,6 +209,7 @@ fn main() {
209209
.with_size(LogicalSize::new(512, 512));
210210

211211
baseview::create_window(window_open_options, |c| pollster::block_on(WgpuExample::new(c)))
212+
.unwrap()
212213
.run_until_closed();
213214
}
214215

src/error.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use std::fmt::{Debug, Display, Formatter};
2+
3+
pub type Result<T> = std::result::Result<T, Error>;
4+
5+
pub struct Error {
6+
inner: crate::platform::Error,
7+
}
8+
9+
impl From<crate::platform::Error> for Error {
10+
fn from(inner: crate::platform::Error) -> Error {
11+
Error { inner }
12+
}
13+
}
14+
15+
impl Debug for Error {
16+
#[inline]
17+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
18+
Debug::fmt(&self.inner, f)
19+
}
20+
}
21+
22+
impl Display for Error {
23+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
24+
Display::fmt(&self.inner, f)
25+
}
26+
}
27+
28+
impl std::error::Error for Error {}
29+
30+
pub struct HandlerError {
31+
inner: Box<dyn std::error::Error + 'static>,
32+
}
33+
34+
impl HandlerError {
35+
#[inline]
36+
pub fn cause(&self) -> &(dyn std::error::Error + 'static) {
37+
self.inner.as_ref()
38+
}
39+
40+
#[inline]
41+
pub fn into_inner(self) -> Box<dyn std::error::Error + 'static> {
42+
self.inner
43+
}
44+
}
45+
46+
impl Debug for HandlerError {
47+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
48+
Debug::fmt(&self.inner, f)
49+
}
50+
}
51+
52+
impl Display for HandlerError {
53+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
54+
Display::fmt(&self.inner, f)
55+
}
56+
}
57+
58+
impl<E: std::error::Error + 'static> From<E> for HandlerError {
59+
fn from(value: E) -> Self {
60+
Self { inner: Box::new(value) }
61+
}
62+
}

src/gl.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::platform::gl::*;
21
use std::ffi::{c_void, CStr};
32
use std::marker::PhantomData;
43

@@ -43,13 +42,6 @@ pub enum Profile {
4342
Core,
4443
}
4544

46-
#[derive(Debug)]
47-
#[non_exhaustive]
48-
pub enum GlError {
49-
VersionNotSupported,
50-
CreationFailed(CreationFailedError),
51-
}
52-
5345
#[derive(Clone)]
5446
pub struct GlContext {
5547
inner: crate::platform::gl::GlContext,

src/handler.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
use super::*;
2+
use crate::platform::Result;
23

34
pub trait WindowHandler: 'static {
45
fn on_frame(&self);
56
fn resized(&self, new_size: WindowSize);
67
fn on_event(&self, event: Event) -> EventStatus;
78
}
89

10+
type DynBuilderResult = core::result::Result<Box<dyn WindowHandler>, HandlerError>;
11+
912
#[allow(unused)]
1013
pub struct WindowHandlerBuilder {
11-
inner: Box<dyn FnOnce(WindowContext) -> Box<dyn WindowHandler> + Send + 'static>,
14+
inner: Box<dyn FnOnce(WindowContext) -> DynBuilderResult + Send + 'static>,
1215
}
1316

1417
impl WindowHandlerBuilder {
1518
pub fn new<H: WindowHandler>(
16-
f: impl FnOnce(WindowContext) -> H + Send + 'static,
19+
f: impl FnOnce(WindowContext) -> core::result::Result<H, HandlerError> + Send + 'static,
1720
) -> WindowHandlerBuilder {
18-
Self { inner: Box::new(|c| Box::new(f(c))) }
21+
Self { inner: Box::new(|c| Ok(Box::new(f(c)?))) }
1922
}
2023

21-
pub fn build(self, ctx: WindowContext) -> Box<dyn WindowHandler> {
22-
(self.inner)(ctx)
24+
pub fn build(self, ctx: WindowContext) -> Result<Box<dyn WindowHandler>> {
25+
match (self.inner)(ctx) {
26+
Ok(handle) => Ok(handle),
27+
Err(e) => Err(platform::Error::Handler(e)),
28+
}
2329
}
2430
}

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
mod clipboard;
22
mod context;
3+
mod error;
34
mod event;
45
mod handler;
56
mod keyboard;
67
mod mouse_cursor;
8+
mod tracing;
79
mod window;
810
mod window_open_options;
911

@@ -15,10 +17,13 @@ pub mod gl;
1517
pub use clipboard::*;
1618
pub use context::{PlatformHandle, WindowContext};
1719
pub use dpi;
20+
pub use error::*;
1821
pub use event::*;
1922
pub use handler::WindowHandler;
2023
pub use mouse_cursor::MouseCursor;
2124
pub use window::*;
2225
pub use window_open_options::*;
2326

27+
pub(crate) use tracing::*;
28+
2429
pub(crate) mod wrappers;

src/platform/macos/error.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use crate::HandlerError;
2+
use std::fmt::Display;
3+
4+
#[derive(Debug)]
5+
pub enum Error {
6+
Handler(HandlerError),
7+
#[cfg(feature = "opengl")]
8+
GlError(super::gl::GlError),
9+
}
10+
11+
impl Display for Error {
12+
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
13+
match self {
14+
#[cfg(feature = "opengl")]
15+
Error::GlError(e) => e.fmt(fmt),
16+
Error::Handler(e) => e.fmt(fmt),
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)