refactor: move slint component into window state

WIP/draft
drendog 2024-08-19 00:13:19 +02:00
parent 9a5817fc9a
commit 1e9b7f0e0e
Signed by: dwenya
GPG Key ID: 8DD77074645332D0
4 changed files with 49 additions and 43 deletions

View File

@ -1,32 +1,24 @@
use anyhow::Result;
use slint::{ use slint::{
platform::{Platform, WindowAdapter}, platform::{Platform, WindowAdapter},
PlatformError, PlatformError,
}; };
use std::rc::{Rc, Weak}; use std::rc::Rc;
use super::femtovg_window::FemtoVGWindow; use super::femtovg_window::FemtoVGWindow;
pub struct CustomSlintPlatform { pub struct CustomSlintPlatform {
window: Weak<FemtoVGWindow>, window: Rc<FemtoVGWindow>,
} }
impl CustomSlintPlatform { impl CustomSlintPlatform {
pub fn new(window: &Rc<FemtoVGWindow>) -> Self { pub fn new(window: Rc<FemtoVGWindow>) -> Self {
Self { Self { window }
window: Rc::downgrade(window),
}
} }
} }
impl Platform for CustomSlintPlatform { impl Platform for CustomSlintPlatform {
fn create_window_adapter(&self) -> Result<Rc<dyn WindowAdapter>, PlatformError> { fn create_window_adapter(&self) -> Result<Rc<(dyn WindowAdapter + 'static)>, PlatformError> {
self.window Result::Ok(Rc::clone(&self.window) as Rc<dyn WindowAdapter>)
.upgrade()
.map(|window| -> Rc<dyn WindowAdapter> { window })
.ok_or_else(|| {
PlatformError::Other(
"Failed to create window adapter: window no longer exists".into(),
)
})
} }
} }

View File

@ -83,9 +83,9 @@ impl WindowingSystemBuilder {
self self
} }
pub fn build(self) -> Result<WindowingSystem> { pub fn build(&mut self) -> Result<WindowingSystem> {
match self.config.component_definition { match self.config.component_definition {
Some(_) => WindowingSystem::new(&self.config), Some(_) => WindowingSystem::new(&mut self.config),
None => Err(anyhow::anyhow!("Slint component not set")), None => Err(anyhow::anyhow!("Slint component not set")),
} }
} }

View File

@ -35,12 +35,11 @@ pub struct WindowingSystem {
state: WindowState, state: WindowState,
connection: Rc<Connection>, connection: Rc<Connection>,
event_queue: EventQueue<WindowState>, event_queue: EventQueue<WindowState>,
component_instance: Rc<ComponentInstance>,
event_loop: EventLoop<'static, WindowState>, event_loop: EventLoop<'static, WindowState>,
} }
impl WindowingSystem { impl WindowingSystem {
fn new(config: &WindowConfig) -> Result<Self> { fn new(config: &mut WindowConfig) -> Result<Self> {
info!("Initializing WindowingSystem"); info!("Initializing WindowingSystem");
let connection = Rc::new(Connection::connect_to_env()?); let connection = Rc::new(Connection::connect_to_env()?);
@ -65,7 +64,7 @@ impl WindowingSystem {
Self::wait_for_configure(&mut event_queue, &mut state)?; Self::wait_for_configure(&mut event_queue, &mut state)?;
let display = connection.display(); 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")?; let event_loop = EventLoop::try_new().context("Failed to create event loop")?;
@ -73,7 +72,6 @@ impl WindowingSystem {
state, state,
connection, connection,
event_queue, event_queue,
component_instance,
event_loop, event_loop,
}) })
} }
@ -180,16 +178,12 @@ impl WindowingSystem {
FemtoVGRenderer::new(context).context("Failed to create FemtoVGRenderer") FemtoVGRenderer::new(context).context("Failed to create FemtoVGRenderer")
} }
fn initialize_renderer_and_ui( fn initialize_renderer(
state: &mut WindowState, state: &mut WindowState,
display: &WlDisplay, display: &WlDisplay,
config: &WindowConfig, config: &WindowConfig,
) -> Result<Rc<ComponentInstance>> { ) -> Result<()> {
let renderer = Self::create_renderer(state, display)?; 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 femtovg_window = FemtoVGWindow::new(renderer);
let size = state.size(); let size = state.size();
@ -198,21 +192,13 @@ impl WindowingSystem {
femtovg_window.set_scale_factor(config.scale_factor); femtovg_window.set_scale_factor(config.scale_factor);
femtovg_window.set_position(LogicalPosition::new(0., 0.)); femtovg_window.set_position(LogicalPosition::new(0., 0.));
debug!("Setting up custom Slint platform"); //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!("Creating Slint component instance"); debug!("Creating Slint component instance");
let slint_component: Rc<ComponentInstance> = 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)); state.set_window(Rc::clone(&femtovg_window));
Ok(slint_component) Ok(())
} }
pub fn event_loop_handle(&self) -> LoopHandle<'static, WindowState> { pub fn event_loop_handle(&self) -> LoopHandle<'static, WindowState> {
@ -222,8 +208,13 @@ impl WindowingSystem {
pub fn run(&mut self) -> Result<()> { pub fn run(&mut self) -> Result<()> {
info!("Starting WindowingSystem main loop"); info!("Starting WindowingSystem main loop");
if let Some(window) = &self.state.window() { 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(); window.render_frame_if_dirty();
} }
self.state.show_component();
self.setup_wayland_event_source(); self.setup_wayland_event_source();
self.event_loop self.event_loop
@ -256,8 +247,8 @@ impl WindowingSystem {
); );
} }
pub fn component_instance(&self) -> Rc<ComponentInstance> { pub fn component_instance(&self) -> &ComponentInstance {
Rc::clone(&self.component_instance) self.state.component_instance()
} }
pub fn window(&self) -> Rc<FemtoVGWindow> { pub fn window(&self) -> Rc<FemtoVGWindow> {

View File

@ -1,14 +1,18 @@
use std::rc::Rc; use std::{borrow::{Borrow, BorrowMut}, rc::Rc};
use log::info; 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 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 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 super::WindowConfig;
use anyhow::Result;
pub mod dispatches; pub mod dispatches;
pub struct WindowState { pub struct WindowState {
component_definition: ComponentDefinition,
component_instance: Option<ComponentInstance>,
surface: Option<Rc<WlSurface>>, surface: Option<Rc<WlSurface>>,
layer_surface: Option<Rc<ZwlrLayerSurfaceV1>>, layer_surface: Option<Rc<ZwlrLayerSurfaceV1>>,
size: PhysicalSize, size: PhysicalSize,
@ -22,7 +26,7 @@ pub struct WindowState {
} }
impl WindowState { impl WindowState {
pub fn new(config: &WindowConfig) -> Self { pub fn new(config: &mut WindowConfig) -> Self {
Self { Self {
surface: None, surface: None,
layer_surface: None, layer_surface: None,
@ -34,9 +38,24 @@ impl WindowState {
scale_factor: config.scale_factor, scale_factor: config.scale_factor,
height: config.height, height: config.height,
exclusive_zone: config.exclusive_zone, 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) { pub fn update_size(&mut self, width: u32, height: u32) {
let new_size = PhysicalSize::new(width, height); let new_size = PhysicalSize::new(width, height);
if let Some(window) = &self.window() { if let Some(window) = &self.window() {
@ -110,4 +129,8 @@ impl WindowState {
pub fn set_pointer(&mut self, pointer: Rc<WlPointer>) { pub fn set_pointer(&mut self, pointer: Rc<WlPointer>) {
self.pointer = Some(pointer); self.pointer = Some(pointer);
} }
pub fn component_instance(&self) -> &ComponentInstance {
&self.component_instance.as_ref().unwrap()
}
} }