refactor: remove window field on event loop handler and minor refactor

WIP/draft
drendog 2024-08-17 18:55:03 +02:00
parent f89eea5f77
commit 130059b979
Signed by: dwenya
GPG Key ID: 8DD77074645332D0
2 changed files with 19 additions and 31 deletions

View File

@ -2,7 +2,7 @@ use anyhow::{anyhow, Result};
use log::{debug, error}; use log::{debug, error};
use smithay_client_toolkit::reexports::calloop::{self, Interest, Mode, PostAction}; use smithay_client_toolkit::reexports::calloop::{self, Interest, Mode, PostAction};
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::{Rc, Weak}; use std::rc::Rc;
use wayland_client::{Connection, EventQueue}; use wayland_client::{Connection, EventQueue};
use crate::rendering::femtovg_window::FemtoVGWindow; use crate::rendering::femtovg_window::FemtoVGWindow;
@ -10,22 +10,19 @@ use crate::rendering::femtovg_window::FemtoVGWindow;
use super::state::WindowState; use super::state::WindowState;
pub struct EventLoopHandler { pub struct EventLoopHandler {
window: Weak<FemtoVGWindow>, wayland_queue: Rc<RefCell<EventQueue<WindowState>>>,
wayland_queue: Weak<RefCell<EventQueue<WindowState>>>, connection: Rc<Connection>,
connection: Weak<Connection>, state: Rc<RefCell<WindowState>>,
state: Weak<RefCell<WindowState>>,
} }
impl EventLoopHandler { impl EventLoopHandler {
pub fn new( pub fn new(
window: Weak<FemtoVGWindow>, wayland_queue: Rc<RefCell<EventQueue<WindowState>>>,
wayland_queue: Weak<RefCell<EventQueue<WindowState>>>, connection: Rc<Connection>,
connection: Weak<Connection>, state: Rc<RefCell<WindowState>>,
state: Weak<RefCell<WindowState>>,
) -> Self { ) -> Self {
debug!("Creating EventLoopHandler"); debug!("Creating EventLoopHandler");
Self { Self {
window,
wayland_queue, wayland_queue,
connection, connection,
state, state,
@ -35,28 +32,20 @@ impl EventLoopHandler {
pub fn setup_wayland_event_source(&self, loop_handle: &calloop::LoopHandle<()>) -> Result<()> { pub fn setup_wayland_event_source(&self, loop_handle: &calloop::LoopHandle<()>) -> Result<()> {
debug!("Setting up Wayland event source"); debug!("Setting up Wayland event source");
let wayland_queue = Weak::clone(&self.wayland_queue); let wayland_queue = Rc::clone(&self.wayland_queue);
let state = Weak::clone(&self.state); let state = Rc::clone(&self.state);
let connection = self.connection.upgrade().ok_or_else(|| { let connection = Rc::clone(&self.connection);
anyhow!("Failed to get Wayland connection reference in Wayland event source")
})?;
let window = Weak::clone(&self.window);
loop_handle loop_handle
.insert_source( .insert_source(
calloop::generic::Generic::new(connection, Interest::READ, Mode::Level), calloop::generic::Generic::new(connection, Interest::READ, Mode::Level),
move |_, connection, ()| { move |_, connection, ()| {
let result: Result<PostAction, anyhow::Error> = (|| { let result: Result<PostAction, anyhow::Error> = (|| {
let wayland_queue = wayland_queue let binding = state.borrow().window();
.upgrade() let window = binding.as_ref().ok_or_else(|| {
.ok_or_else(|| anyhow!("Failed to get Wayland queue reference"))?; anyhow!("Window not initialized in Wayland event source")
let state = state })?;
.upgrade() Self::handle_wayland_events(connection, &wayland_queue, &state, window)?;
.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)?;
Ok(PostAction::Continue) Ok(PostAction::Continue)
})(); })();
@ -89,7 +78,7 @@ impl EventLoopHandler {
} }
event_queue event_queue
.dispatch_pending(&mut *state.borrow_mut()) .blocking_dispatch(&mut state.borrow_mut())
.map_err(|e| anyhow!("Failed to dispatch Wayland events: {}", e))?; .map_err(|e| anyhow!("Failed to dispatch Wayland events: {}", e))?;
slint::platform::update_timers_and_animations(); slint::platform::update_timers_and_animations();

View File

@ -347,10 +347,9 @@ impl WindowingSystem {
pub fn initialize_event_loop_handler(&mut self) { pub fn initialize_event_loop_handler(&mut self) {
let event_loop_handler = EventLoopHandler::new( let event_loop_handler = EventLoopHandler::new(
Rc::downgrade(self.state.borrow().window().as_ref().unwrap()), Rc::clone(&self.event_queue),
Rc::downgrade(&self.event_queue), Rc::clone(&self.connection),
Rc::downgrade(&self.connection), Rc::clone(&self.state),
Rc::downgrade(&self.state),
); );
self.event_loop_handler = Some(event_loop_handler); self.event_loop_handler = Some(event_loop_handler);