refactor: remove unnecessary options and config

WIP/draft
drendog 2024-08-18 02:40:28 +02:00
parent afe77990f8
commit e607e24633
Signed by: dwenya
GPG Key ID: 8DD77074645332D0
2 changed files with 61 additions and 67 deletions

View File

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

View File

@ -9,12 +9,11 @@ use anyhow::{Context, Result};
use config::WindowConfig; use config::WindowConfig;
use log::{debug, info}; use log::{debug, info};
use slint::{platform::femtovg_renderer::FemtoVGRenderer, ComponentHandle, LogicalPosition}; use slint::{platform::femtovg_renderer::FemtoVGRenderer, ComponentHandle, LogicalPosition};
use slint_interpreter::{ComponentDefinition, ComponentInstance}; use slint_interpreter::ComponentInstance;
use smithay_client_toolkit::reexports::{ use smithay_client_toolkit::reexports::{
calloop::{EventLoop, LoopHandle}, calloop::{EventLoop, LoopHandle},
protocols_wlr::layer_shell::v1::client::{ protocols_wlr::layer_shell::v1::client::{
zwlr_layer_shell_v1::{self, ZwlrLayerShellV1}, zwlr_layer_shell_v1::ZwlrLayerShellV1, zwlr_layer_surface_v1::ZwlrLayerSurfaceV1,
zwlr_layer_surface_v1::{Anchor, KeyboardInteractivity, ZwlrLayerSurfaceV1},
}, },
}; };
use std::{ use std::{
@ -40,18 +39,17 @@ pub struct WindowingSystem {
state: Rc<RefCell<WindowState>>, state: Rc<RefCell<WindowState>>,
connection: Rc<Connection>, connection: Rc<Connection>,
event_queue: Rc<RefCell<EventQueue<WindowState>>>, event_queue: Rc<RefCell<EventQueue<WindowState>>>,
component_instance: Option<Rc<ComponentInstance>>, component_instance: Rc<ComponentInstance>,
display: WlDisplay, display: WlDisplay,
config: WindowConfig,
event_loop: EventLoop<'static, ()>, event_loop: EventLoop<'static, ()>,
event_loop_handler: Option<EventLoopHandler>, event_loop_handler: EventLoopHandler,
} }
impl WindowingSystem { impl WindowingSystem {
fn new(config: WindowConfig) -> Result<Self> { fn new(config: &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()?);
let state = Rc::new(RefCell::new(WindowState::new(&config))); let state = Rc::new(RefCell::new(WindowState::new(config)));
let display = connection.display(); let display = connection.display();
let event_queue = Rc::new(RefCell::new(connection.new_event_queue())); let event_queue = Rc::new(RefCell::new(connection.new_event_queue()));
@ -66,26 +64,29 @@ impl WindowingSystem {
&seat, &seat,
&event_queue.borrow().handle(), &event_queue.borrow().handle(),
&state, &state,
&config, config,
); );
let event_loop = EventLoop::try_new().context("Failed to create event loop")?; Self::wait_for_configure(&event_queue, &state)?;
let mut system = Self { let component_instance = Self::initialize_renderer_and_ui(&state, &display, config)?;
let event_loop = EventLoop::try_new().context("Failed to create event loop")?;
let event_loop_handler = EventLoopHandler::new(
Rc::clone(&event_queue),
Rc::clone(&connection),
Rc::clone(&state),
);
Ok(Self {
state, state,
connection, connection,
event_queue, event_queue,
component_instance: None, component_instance,
display, display,
config,
event_loop, event_loop,
event_loop_handler: None, event_loop_handler,
}; })
system.wait_for_configure()?;
system.initialize_renderer_and_ui()?;
Ok(system)
} }
fn initialize_registry(connection: &Connection) -> Result<GlobalList> { fn initialize_registry(connection: &Connection) -> Result<GlobalList> {
@ -156,49 +157,38 @@ impl WindowingSystem {
surface.commit(); surface.commit();
} }
fn wait_for_configure(&self) -> Result<()> { fn wait_for_configure(
event_queue: &Rc<RefCell<EventQueue<WindowState>>>,
state: &Rc<RefCell<WindowState>>,
) -> Result<()> {
info!("Waiting for surface to be configured..."); info!("Waiting for surface to be configured...");
let mut state = self.state.borrow_mut(); let mut state = state.borrow_mut();
self.event_queue event_queue
.borrow_mut() .borrow_mut()
.blocking_dispatch(&mut state)?; .blocking_dispatch(&mut state)
.context("Failed to dispatch events")?;
info!("Blocking dispatch completed"); info!("Blocking dispatch completed");
let size = state.output_size(); let size = state.output_size();
match (size.width, size.height) { if size.width > 1 && size.height > 1 {
(w, h) if w > 1 && h > 1 => {
info!("Configured output size: {:?}", size); info!("Configured output size: {:?}", size);
Ok(()) } else {
return Err(anyhow::anyhow!("Invalid output size: {:?}", size));
} }
_ => Err(anyhow::anyhow!("Invalid output size: {:?}", size)), debug!("Surface configuration complete");
}
}
fn initialize_renderer_and_ui(&mut self) -> Result<()> {
let renderer = self.create_renderer()?;
let component_definition = self
.config
.component_definition
.as_ref()
.ok_or_else(|| anyhow::anyhow!("Component definition not set"))?;
let (window, component_instance) =
self.initialize_slint_ui(renderer, component_definition)?;
self.state.borrow_mut().set_window(window);
self.component_instance = Some(component_instance);
Ok(()) Ok(())
} }
fn create_renderer(&self) -> Result<FemtoVGRenderer> { fn create_renderer(
let state_borrow = self.state.borrow(); state: &Rc<RefCell<WindowState>>,
display: &WlDisplay,
) -> Result<FemtoVGRenderer> {
let state_borrow = state.borrow();
let size = state_borrow.size(); let size = state_borrow.size();
let surface = state_borrow.surface().unwrap(); let surface = state_borrow.surface().unwrap();
debug!("Creating EGL context with size: {:?}", size); debug!("Creating EGL context with size: {:?}", size);
let context = EGLContext::builder() let context = EGLContext::builder()
.with_display_id(self.display.id()) .with_display_id(display.id())
.with_surface_id(surface.id()) .with_surface_id(surface.id())
.with_size(size) .with_size(size)
.build() .build()
@ -208,16 +198,22 @@ impl WindowingSystem {
FemtoVGRenderer::new(context).context("Failed to create FemtoVGRenderer") FemtoVGRenderer::new(context).context("Failed to create FemtoVGRenderer")
} }
fn initialize_slint_ui( fn initialize_renderer_and_ui(
&self, state: &Rc<RefCell<WindowState>>,
renderer: FemtoVGRenderer, display: &WlDisplay,
component_definition: &ComponentDefinition, config: &WindowConfig,
) -> Result<(Rc<FemtoVGWindow>, Rc<ComponentInstance>)> { ) -> Result<Rc<ComponentInstance>> {
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 = self.state.borrow().size(); let size = state.borrow().size();
info!("Initializing UI with size: {:?}", size); info!("Initializing UI with size: {:?}", size);
femtovg_window.set_size(slint::WindowSize::Physical(size)); femtovg_window.set_size(slint::WindowSize::Physical(size));
femtovg_window.set_scale_factor(self.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");
@ -232,7 +228,9 @@ impl WindowingSystem {
.show() .show()
.map_err(|e| anyhow::anyhow!("Failed to show component: {:?}", e))?; .map_err(|e| anyhow::anyhow!("Failed to show component: {:?}", e))?;
Ok((femtovg_window, slint_component)) state.borrow_mut().set_window(Rc::clone(&femtovg_window));
Ok(slint_component)
} }
pub fn initialize_event_loop_handler(&mut self) { pub fn initialize_event_loop_handler(&mut self) {
@ -242,17 +240,13 @@ impl WindowingSystem {
Rc::clone(&self.state), Rc::clone(&self.state),
); );
self.event_loop_handler = Some(event_loop_handler); self.event_loop_handler = event_loop_handler;
} }
pub fn setup_event_sources(&self) -> Result<()> { pub fn setup_event_sources(&self) -> Result<()> {
let loop_handle = self.event_loop.handle(); let loop_handle = self.event_loop.handle();
let event_loop_handler = self self.event_loop_handler
.event_loop_handler .setup_wayland_event_source(&loop_handle)?;
.as_ref()
.ok_or_else(|| anyhow::anyhow!("EventLoopHandler not initialized"))?;
event_loop_handler.setup_wayland_event_source(&loop_handle)?;
Ok(()) Ok(())
} }
@ -275,7 +269,7 @@ impl WindowingSystem {
} }
pub fn component_instance(&self) -> Rc<ComponentInstance> { pub fn component_instance(&self) -> Rc<ComponentInstance> {
Rc::clone(self.component_instance.as_ref().unwrap()) Rc::clone(&self.component_instance)
} }
pub fn window(&self) -> Rc<FemtoVGWindow> { pub fn window(&self) -> Rc<FemtoVGWindow> {