From 130059b97945384416509d56e67f50671116cf51 Mon Sep 17 00:00:00 2001 From: drendog Date: Sat, 17 Aug 2024 18:55:03 +0200 Subject: [PATCH] refactor: remove window field on event loop handler and minor refactor --- src/windowing/event_loop.rs | 43 ++++++++++++++----------------------- src/windowing/mod.rs | 7 +++--- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/src/windowing/event_loop.rs b/src/windowing/event_loop.rs index fe8d068..b2fe174 100644 --- a/src/windowing/event_loop.rs +++ b/src/windowing/event_loop.rs @@ -2,7 +2,7 @@ use anyhow::{anyhow, Result}; use log::{debug, error}; use smithay_client_toolkit::reexports::calloop::{self, Interest, Mode, PostAction}; use std::cell::RefCell; -use std::rc::{Rc, Weak}; +use std::rc::Rc; use wayland_client::{Connection, EventQueue}; use crate::rendering::femtovg_window::FemtoVGWindow; @@ -10,22 +10,19 @@ use crate::rendering::femtovg_window::FemtoVGWindow; use super::state::WindowState; pub struct EventLoopHandler { - window: Weak, - wayland_queue: Weak>>, - connection: Weak, - state: Weak>, + wayland_queue: Rc>>, + connection: Rc, + state: Rc>, } impl EventLoopHandler { pub fn new( - window: Weak, - wayland_queue: Weak>>, - connection: Weak, - state: Weak>, + wayland_queue: Rc>>, + connection: Rc, + state: Rc>, ) -> Self { debug!("Creating EventLoopHandler"); Self { - window, wayland_queue, connection, state, @@ -35,28 +32,20 @@ impl EventLoopHandler { pub fn setup_wayland_event_source(&self, loop_handle: &calloop::LoopHandle<()>) -> Result<()> { debug!("Setting up Wayland event source"); - let wayland_queue = Weak::clone(&self.wayland_queue); - let state = Weak::clone(&self.state); - let connection = self.connection.upgrade().ok_or_else(|| { - anyhow!("Failed to get Wayland connection reference in Wayland event source") - })?; - let window = Weak::clone(&self.window); + let wayland_queue = Rc::clone(&self.wayland_queue); + let state = Rc::clone(&self.state); + let connection = Rc::clone(&self.connection); loop_handle .insert_source( calloop::generic::Generic::new(connection, Interest::READ, Mode::Level), move |_, connection, ()| { let result: Result = (|| { - let wayland_queue = wayland_queue - .upgrade() - .ok_or_else(|| anyhow!("Failed to get Wayland queue reference"))?; - let state = state - .upgrade() - .ok_or_else(|| anyhow!("Failed to get event handler reference"))?; - let window = window - .upgrade() - .ok_or_else(|| anyhow!("Failed to get window reference"))?; - Self::handle_wayland_events(connection, &wayland_queue, &state, &window)?; + let binding = state.borrow().window(); + let window = binding.as_ref().ok_or_else(|| { + anyhow!("Window not initialized in Wayland event source") + })?; + Self::handle_wayland_events(connection, &wayland_queue, &state, window)?; Ok(PostAction::Continue) })(); @@ -89,7 +78,7 @@ impl EventLoopHandler { } event_queue - .dispatch_pending(&mut *state.borrow_mut()) + .blocking_dispatch(&mut state.borrow_mut()) .map_err(|e| anyhow!("Failed to dispatch Wayland events: {}", e))?; slint::platform::update_timers_and_animations(); diff --git a/src/windowing/mod.rs b/src/windowing/mod.rs index 8a5e873..8befd3b 100644 --- a/src/windowing/mod.rs +++ b/src/windowing/mod.rs @@ -347,10 +347,9 @@ impl WindowingSystem { pub fn initialize_event_loop_handler(&mut self) { let event_loop_handler = EventLoopHandler::new( - Rc::downgrade(self.state.borrow().window().as_ref().unwrap()), - Rc::downgrade(&self.event_queue), - Rc::downgrade(&self.connection), - Rc::downgrade(&self.state), + Rc::clone(&self.event_queue), + Rc::clone(&self.connection), + Rc::clone(&self.state), ); self.event_loop_handler = Some(event_loop_handler);