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 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<FemtoVGWindow>,
wayland_queue: Weak<RefCell<EventQueue<WindowState>>>,
connection: Weak<Connection>,
state: Weak<RefCell<WindowState>>,
wayland_queue: Rc<RefCell<EventQueue<WindowState>>>,
connection: Rc<Connection>,
state: Rc<RefCell<WindowState>>,
}
impl EventLoopHandler {
pub fn new(
window: Weak<FemtoVGWindow>,
wayland_queue: Weak<RefCell<EventQueue<WindowState>>>,
connection: Weak<Connection>,
state: Weak<RefCell<WindowState>>,
wayland_queue: Rc<RefCell<EventQueue<WindowState>>>,
connection: Rc<Connection>,
state: Rc<RefCell<WindowState>>,
) -> 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<PostAction, anyhow::Error> = (|| {
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();

View File

@ -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);