From 8c1b4c9c999eaf95cffbe4e98c47a0a5fbb6cc2f Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 25 May 2022 13:58:14 +0200 Subject: [PATCH] Improve error handling in `Window::new` --- crates/fj-viewer/src/graphics/renderer.rs | 2 +- crates/fj-viewer/src/run.rs | 2 +- crates/fj-viewer/src/window.rs | 12 ++++++++---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/crates/fj-viewer/src/graphics/renderer.rs b/crates/fj-viewer/src/graphics/renderer.rs index 2d35629d7..670bc76ae 100644 --- a/crates/fj-viewer/src/graphics/renderer.rs +++ b/crates/fj-viewer/src/graphics/renderer.rs @@ -46,7 +46,7 @@ impl Renderer { /// /// // Create window /// let event_loop = winit::event_loop::EventLoop::new(); - /// let window = window::Window::new(&event_loop); + /// let window = window::Window::new(&event_loop).unwrap(); /// /// // Attach renderer to the window /// let mut renderer = graphics::Renderer::new(&window); diff --git a/crates/fj-viewer/src/run.rs b/crates/fj-viewer/src/run.rs index 8986480eb..1b3d5731a 100644 --- a/crates/fj-viewer/src/run.rs +++ b/crates/fj-viewer/src/run.rs @@ -27,7 +27,7 @@ pub fn run( shape_processor: ShapeProcessor, ) -> Result<(), graphics::InitError> { let event_loop = EventLoop::new(); - let window = Window::new(&event_loop); + let window = Window::new(&event_loop).unwrap(); let mut previous_time = Instant::now(); diff --git a/crates/fj-viewer/src/window.rs b/crates/fj-viewer/src/window.rs index e9aaab995..b65e09415 100644 --- a/crates/fj-viewer/src/window.rs +++ b/crates/fj-viewer/src/window.rs @@ -13,16 +13,15 @@ impl Window { /// let event_loop = winit::event_loop::EventLoop::new(); /// let window = fj_viewer::window::Window::new(&event_loop); /// ``` - pub fn new(event_loop: &EventLoop<()>) -> Self { + pub fn new(event_loop: &EventLoop<()>) -> Result { let window = WindowBuilder::new() .with_title("Fornjot") .with_maximized(true) .with_decorations(true) .with_transparent(false) - .build(event_loop) - .unwrap(); + .build(event_loop)?; - Self(window) + Ok(Self(window)) } /// Returns a shared reference to the wrapped window @@ -40,3 +39,8 @@ impl Window { self.0.inner_size().height } } + +/// Error initializing window +#[derive(Debug, thiserror::Error)] +#[error("Error initializing window")] +pub struct Error(#[from] pub winit::error::OsError);