Refactor to avoid use of deprecated method

This commit is contained in:
Hanno Braun 2024-05-06 14:31:39 +02:00
parent 157e7e92e7
commit 6ada107e28
2 changed files with 10 additions and 10 deletions

View File

@ -21,12 +21,11 @@ use crate::window::{self, Window};
/// Display the provided mesh in a window that processes input /// Display the provided mesh in a window that processes input
pub fn display(model: Model, invert_zoom: bool) -> Result<(), Error> { pub fn display(model: Model, invert_zoom: bool) -> Result<(), Error> {
let event_loop = EventLoop::new()?; let event_loop = EventLoop::new()?;
let window = Window::new(&event_loop)?;
let mut display_state = DisplayState { let mut display_state = DisplayState {
model: Some(model), model: Some(model),
invert_zoom, invert_zoom,
window, window: None,
viewer: None, viewer: None,
held_mouse_button: None, held_mouse_button: None,
new_size: None, new_size: None,
@ -57,7 +56,7 @@ pub enum Error {
struct DisplayState { struct DisplayState {
model: Option<Model>, model: Option<Model>,
invert_zoom: bool, invert_zoom: bool,
window: Window, window: Option<Window>,
viewer: Option<Viewer>, viewer: Option<Viewer>,
held_mouse_button: Option<MouseButton>, held_mouse_button: Option<MouseButton>,
new_size: Option<ScreenSize>, new_size: Option<ScreenSize>,
@ -65,8 +64,10 @@ struct DisplayState {
} }
impl ApplicationHandler for DisplayState { impl ApplicationHandler for DisplayState {
fn resumed(&mut self, _: &ActiveEventLoop) { fn resumed(&mut self, event_loop: &ActiveEventLoop) {
let window = &self.window; let window = self
.window
.get_or_insert_with(|| Window::new(event_loop).unwrap());
let viewer = self let viewer = self
.viewer .viewer
@ -83,7 +84,7 @@ impl ApplicationHandler for DisplayState {
_: WindowId, _: WindowId,
event: WindowEvent, event: WindowEvent,
) { ) {
let window = &self.window; let Some(window) = &self.window else { return };
let Some(viewer) = &mut self.viewer else { let Some(viewer) = &mut self.viewer else {
return; return;
}; };
@ -159,7 +160,7 @@ impl ApplicationHandler for DisplayState {
} }
fn about_to_wait(&mut self, _: &ActiveEventLoop) { fn about_to_wait(&mut self, _: &ActiveEventLoop) {
let window = &self.window; let Some(window) = &self.window else { return };
window.window().request_redraw(); window.window().request_redraw();
} }
} }

View File

@ -1,7 +1,7 @@
use std::sync::Arc; use std::sync::Arc;
use fj_viewer::{Screen, ScreenSize}; use fj_viewer::{Screen, ScreenSize};
use winit::event_loop::EventLoop; use winit::event_loop::ActiveEventLoop;
/// A window that can be used with `fj-viewer` /// A window that can be used with `fj-viewer`
pub struct Window { pub struct Window {
@ -10,8 +10,7 @@ pub struct Window {
impl Window { impl Window {
/// Create an instance of `Window` from the given `EventLoop` /// Create an instance of `Window` from the given `EventLoop`
pub fn new<T>(event_loop: &EventLoop<T>) -> Result<Self, WindowError> { pub fn new(event_loop: &ActiveEventLoop) -> Result<Self, WindowError> {
#[allow(deprecated)] // only for the transition to winit 0.30
let window = event_loop.create_window( let window = event_loop.create_window(
winit::window::Window::default_attributes() winit::window::Window::default_attributes()
.with_title("Fornjot") .with_title("Fornjot")