From 1e9b7f0e0efd5ad478dd49687fb2bf3b4f7ec398 Mon Sep 17 00:00:00 2001 From: drendog Date: Mon, 19 Aug 2024 00:13:19 +0200 Subject: [PATCH] refactor: move slint component into window state --- src/rendering/slint_platform.rs | 22 +++++++-------------- src/windowing/builder.rs | 4 ++-- src/windowing/mod.rs | 35 ++++++++++++--------------------- src/windowing/state/mod.rs | 31 +++++++++++++++++++++++++---- 4 files changed, 49 insertions(+), 43 deletions(-) diff --git a/src/rendering/slint_platform.rs b/src/rendering/slint_platform.rs index edac8e2..a4627ca 100644 --- a/src/rendering/slint_platform.rs +++ b/src/rendering/slint_platform.rs @@ -1,32 +1,24 @@ +use anyhow::Result; use slint::{ platform::{Platform, WindowAdapter}, PlatformError, }; -use std::rc::{Rc, Weak}; +use std::rc::Rc; use super::femtovg_window::FemtoVGWindow; pub struct CustomSlintPlatform { - window: Weak, + window: Rc, } impl CustomSlintPlatform { - pub fn new(window: &Rc) -> Self { - Self { - window: Rc::downgrade(window), - } + pub fn new(window: Rc) -> Self { + Self { window } } } impl Platform for CustomSlintPlatform { - fn create_window_adapter(&self) -> Result, PlatformError> { - self.window - .upgrade() - .map(|window| -> Rc { window }) - .ok_or_else(|| { - PlatformError::Other( - "Failed to create window adapter: window no longer exists".into(), - ) - }) + fn create_window_adapter(&self) -> Result, PlatformError> { + Result::Ok(Rc::clone(&self.window) as Rc) } } diff --git a/src/windowing/builder.rs b/src/windowing/builder.rs index bf58eec..8d7d104 100644 --- a/src/windowing/builder.rs +++ b/src/windowing/builder.rs @@ -83,9 +83,9 @@ impl WindowingSystemBuilder { self } - pub fn build(self) -> Result { + pub fn build(&mut self) -> Result { match self.config.component_definition { - Some(_) => WindowingSystem::new(&self.config), + Some(_) => WindowingSystem::new(&mut self.config), None => Err(anyhow::anyhow!("Slint component not set")), } } diff --git a/src/windowing/mod.rs b/src/windowing/mod.rs index cf98711..25f88c4 100644 --- a/src/windowing/mod.rs +++ b/src/windowing/mod.rs @@ -35,12 +35,11 @@ pub struct WindowingSystem { state: WindowState, connection: Rc, event_queue: EventQueue, - component_instance: Rc, event_loop: EventLoop<'static, WindowState>, } impl WindowingSystem { - fn new(config: &WindowConfig) -> Result { + fn new(config: &mut WindowConfig) -> Result { info!("Initializing WindowingSystem"); let connection = Rc::new(Connection::connect_to_env()?); @@ -65,7 +64,7 @@ impl WindowingSystem { Self::wait_for_configure(&mut event_queue, &mut state)?; let display = connection.display(); - let component_instance = Self::initialize_renderer_and_ui(&mut state, &display, config)?; + Self::initialize_renderer(&mut state, &display, config)?; let event_loop = EventLoop::try_new().context("Failed to create event loop")?; @@ -73,7 +72,6 @@ impl WindowingSystem { state, connection, event_queue, - component_instance, event_loop, }) } @@ -180,16 +178,12 @@ impl WindowingSystem { FemtoVGRenderer::new(context).context("Failed to create FemtoVGRenderer") } - fn initialize_renderer_and_ui( + fn initialize_renderer( state: &mut WindowState, display: &WlDisplay, config: &WindowConfig, - ) -> Result> { + ) -> Result<()> { let renderer = Self::create_renderer(state, display)?; - let component_definition = config - .component_definition - .as_ref() - .ok_or_else(|| anyhow::anyhow!("Component definition not set"))?; let femtovg_window = FemtoVGWindow::new(renderer); let size = state.size(); @@ -198,21 +192,13 @@ impl WindowingSystem { femtovg_window.set_scale_factor(config.scale_factor); femtovg_window.set_position(LogicalPosition::new(0., 0.)); - debug!("Setting up custom Slint platform"); - let platform = CustomSlintPlatform::new(&femtovg_window); - slint::platform::set_platform(Box::new(platform)) - .map_err(|e| anyhow::anyhow!("Failed to set platform: {:?}", e))?; + //debug!("Setting up custom Slint platform"); debug!("Creating Slint component instance"); - let slint_component: Rc = Rc::new(component_definition.create()?); - - slint_component - .show() - .map_err(|e| anyhow::anyhow!("Failed to show component: {:?}", e))?; state.set_window(Rc::clone(&femtovg_window)); - Ok(slint_component) + Ok(()) } pub fn event_loop_handle(&self) -> LoopHandle<'static, WindowState> { @@ -222,8 +208,13 @@ impl WindowingSystem { pub fn run(&mut self) -> Result<()> { info!("Starting WindowingSystem main loop"); if let Some(window) = &self.state.window() { + /*let platform = CustomSlintPlatform::new(window); + slint::platform::set_platform(Box::new(platform)) + .map_err(|e| anyhow::anyhow!("Failed to set platform: {:?}", e))?; + */ window.render_frame_if_dirty(); } + self.state.show_component(); self.setup_wayland_event_source(); self.event_loop @@ -256,8 +247,8 @@ impl WindowingSystem { ); } - pub fn component_instance(&self) -> Rc { - Rc::clone(&self.component_instance) + pub fn component_instance(&self) -> &ComponentInstance { + self.state.component_instance() } pub fn window(&self) -> Rc { diff --git a/src/windowing/state/mod.rs b/src/windowing/state/mod.rs index c006d78..31e91da 100644 --- a/src/windowing/state/mod.rs +++ b/src/windowing/state/mod.rs @@ -1,14 +1,18 @@ -use std::rc::Rc; +use std::{borrow::{Borrow, BorrowMut}, rc::Rc}; use log::info; -use slint::{LogicalPosition, PhysicalSize}; +use slint::{LogicalPosition, PhysicalSize, ComponentHandle}; +use slint_interpreter::{ComponentDefinition, ComponentInstance}; use smithay_client_toolkit::reexports::protocols_wlr::layer_shell::v1::client::zwlr_layer_surface_v1::ZwlrLayerSurfaceV1; use wayland_client::protocol::{wl_pointer::WlPointer, wl_surface::WlSurface}; -use crate::rendering::femtovg_window::FemtoVGWindow; +use crate::rendering::{femtovg_window::FemtoVGWindow, slint_platform::CustomSlintPlatform}; use super::WindowConfig; +use anyhow::Result; pub mod dispatches; pub struct WindowState { + component_definition: ComponentDefinition, + component_instance: Option, surface: Option>, layer_surface: Option>, size: PhysicalSize, @@ -22,7 +26,7 @@ pub struct WindowState { } impl WindowState { - pub fn new(config: &WindowConfig) -> Self { + pub fn new(config: &mut WindowConfig) -> Self { Self { surface: None, layer_surface: None, @@ -34,9 +38,24 @@ impl WindowState { scale_factor: config.scale_factor, height: config.height, exclusive_zone: config.exclusive_zone, + component_definition: config.component_definition.take().unwrap(), + component_instance: None, } } + pub fn show_component(&mut self) -> Result<()> { + if let Some(window) = &self.window { + let platform = CustomSlintPlatform::new(Rc::clone(window)); + slint::platform::set_platform(Box::new(platform)) + .map_err(|e| anyhow::anyhow!("Failed to set platform: {:?}", e))?; + } + + self.component_instance = Some(self.component_definition.create()?); + + self.component_instance.as_ref().unwrap().show()?; + Ok(()) + } + pub fn update_size(&mut self, width: u32, height: u32) { let new_size = PhysicalSize::new(width, height); if let Some(window) = &self.window() { @@ -110,4 +129,8 @@ impl WindowState { pub fn set_pointer(&mut self, pointer: Rc) { self.pointer = Some(pointer); } + + pub fn component_instance(&self) -> &ComponentInstance { + &self.component_instance.as_ref().unwrap() + } }