From 3554b1684d7fd1cebb6093488ae76ca16beb49a1 Mon Sep 17 00:00:00 2001 From: drendog Date: Wed, 14 Aug 2024 23:24:41 +0200 Subject: [PATCH] refactor: minor refactor --- src/common.rs | 4 +- src/lib.rs | 2 +- src/rendering/egl_context.rs | 27 +++++----- src/rendering/femtovg_window.rs | 4 +- src/windowing/event_handler.rs | 31 +++++------ src/windowing/event_loop.rs | 13 ++--- src/windowing/mod.rs | 95 ++++++++++++++++++--------------- src/windowing/state.rs | 14 ++--- 8 files changed, 96 insertions(+), 94 deletions(-) diff --git a/src/common.rs b/src/common.rs index 714bb0d..88c762b 100644 --- a/src/common.rs +++ b/src/common.rs @@ -6,13 +6,13 @@ pub struct LayerSize { } impl LayerSize { - pub fn new(width: u32, height: u32) -> Self { + pub const fn new(width: u32, height: u32) -> Self { Self { size: PhysicalSize::new(width, height), } } - pub fn physical_size(&self) -> PhysicalSize { + pub const fn physical_size(self) -> PhysicalSize { self.size } } diff --git a/src/lib.rs b/src/lib.rs index 5dcc92c..68dbebe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,4 +2,4 @@ mod common; mod rendering; mod windowing; -pub use windowing::WindowingSystemBuilder; +pub use windowing::WindowingSystemBuilder as LayerShika; diff --git a/src/rendering/egl_context.rs b/src/rendering/egl_context.rs index 4eeeb27..e25287b 100644 --- a/src/rendering/egl_context.rs +++ b/src/rendering/egl_context.rs @@ -36,7 +36,7 @@ pub struct EGLContextBuilder { #[allow(dead_code)] impl EGLContextBuilder { pub fn new() -> Self { - Default::default() + Self::default() } pub fn with_display_id(mut self, display_id: ObjectId) -> Self { @@ -49,17 +49,20 @@ impl EGLContextBuilder { self } - pub fn with_size(mut self, size: LayerSize) -> Self { + pub const fn with_size(mut self, size: LayerSize) -> Self { self.size = Some(size); self } - pub fn with_config_template(mut self, config_template: ConfigTemplateBuilder) -> Self { + pub const fn with_config_template(mut self, config_template: ConfigTemplateBuilder) -> Self { self.config_template = Some(config_template); self } - pub fn with_context_attributes(mut self, context_attributes: ContextAttributesBuilder) -> Self { + pub const fn with_context_attributes( + mut self, + context_attributes: ContextAttributesBuilder, + ) -> Self { self.context_attributes = Some(context_attributes); self } @@ -73,7 +76,7 @@ impl EGLContextBuilder { .ok_or_else(|| anyhow!("Surface ID is required"))?; let size = self.size.ok_or_else(|| anyhow!("Size is required"))?; - let display_handle = create_wayland_display_handle(display_id); + let display_handle = create_wayland_display_handle(&display_id); let glutin_display = unsafe { Display::new(display_handle) }?; let config_template = self.config_template.unwrap_or_default(); @@ -84,7 +87,7 @@ impl EGLContextBuilder { let context = create_context(&glutin_display, &config, context_attributes)?; - let surface_handle = create_surface_handle(surface_id); + let surface_handle = create_surface_handle(&surface_id); let surface = create_surface(&glutin_display, &config, surface_handle, size)?; let context = context @@ -108,9 +111,9 @@ impl EGLContext { } } -fn create_wayland_display_handle(display_id: ObjectId) -> RawDisplayHandle { - let display = - NonNull::new(display_id.as_ptr() as *mut c_void).expect("NonNull pointer creation failed"); +fn create_wayland_display_handle(display_id: &ObjectId) -> RawDisplayHandle { + let display = NonNull::new(display_id.as_ptr().cast::()) + .expect("NonNull pointer creation failed"); let handle = WaylandDisplayHandle::new(display); RawDisplayHandle::Wayland(handle) } @@ -133,9 +136,9 @@ fn create_context( .map_err(|e| anyhow!("Failed to create context: {}", e)) } -fn create_surface_handle(surface_id: ObjectId) -> RawWindowHandle { - let surface = - NonNull::new(surface_id.as_ptr() as *mut c_void).expect("NonNull pointer creation failed"); +fn create_surface_handle(surface_id: &ObjectId) -> RawWindowHandle { + let surface = NonNull::new(surface_id.as_ptr().cast::()) + .expect("NonNull pointer creation failed"); let handle = WaylandWindowHandle::new(surface); RawWindowHandle::Wayland(handle) } diff --git a/src/rendering/femtovg_window.rs b/src/rendering/femtovg_window.rs index 4b69585..6db3891 100644 --- a/src/rendering/femtovg_window.rs +++ b/src/rendering/femtovg_window.rs @@ -21,7 +21,7 @@ impl FemtoVGWindow { Self { window, renderer, - is_dirty: Default::default(), + is_dirty: Cell::default(), size: Cell::new(PhysicalSize::default()), scale_factor: Cell::new(1.), } @@ -31,7 +31,7 @@ impl FemtoVGWindow { pub fn render_frame_if_dirty(&self) { if self.is_dirty.get() { match self.renderer.render() { - Ok(_) => {} //log::debug!("Frame rendered successfully"), + Ok(()) => {} //log::debug!("Frame rendered successfully"), Err(e) => log::error!("Error rendering frame: {}", e), } self.is_dirty.set(false); diff --git a/src/windowing/event_handler.rs b/src/windowing/event_handler.rs index f606507..a9fede0 100644 --- a/src/windowing/event_handler.rs +++ b/src/windowing/event_handler.rs @@ -1,3 +1,4 @@ +use super::state::WindowState; use crate::impl_empty_dispatch; use log::info; use slint::platform::{PointerEventButton, WindowEvent}; @@ -7,6 +8,7 @@ use smithay_client_toolkit::reexports::protocols_wlr::layer_shell::v1::client::{ }; use std::rc::Rc; use std::{cell::RefCell, rc::Weak}; +use wayland_client::WEnum; use wayland_client::{ globals::GlobalListContents, protocol::{ @@ -17,12 +19,9 @@ use wayland_client::{ wl_seat::WlSeat, wl_surface::WlSurface, }, - Connection, Dispatch, QueueHandle, + Connection, Dispatch, Proxy, QueueHandle, }; -use super::state::WindowState; - -#[derive(Clone)] pub struct WindowEventHandler { state: Weak>, } @@ -36,7 +35,7 @@ impl WindowEventHandler { self.state.upgrade().unwrap() } - fn handle_pointer_enter(&mut self, surface_x: f64, surface_y: f64) { + fn handle_pointer_enter(&self, surface_x: f64, surface_y: f64) { if let Some(state) = self.state.upgrade() { state .borrow() @@ -50,7 +49,7 @@ impl WindowEventHandler { } } - fn handle_pointer_leave(&mut self) { + fn handle_pointer_leave(&self) { if let Some(state) = self.state.upgrade() { if let Some(window) = state.borrow().window() { window.dispatch_event(WindowEvent::PointerExited); @@ -58,7 +57,7 @@ impl WindowEventHandler { } } - fn handle_pointer_motion(&mut self, surface_x: f64, surface_y: f64) { + fn handle_pointer_motion(&self, surface_x: f64, surface_y: f64) { if let Some(state) = self.state.upgrade() { state .borrow() @@ -72,15 +71,9 @@ impl WindowEventHandler { } } - fn handle_pointer_button( - &mut self, - button_state: wayland_client::WEnum, - ) { + fn handle_pointer_button(&self, button_state: wayland_client::WEnum) { if let Some(state) = self.state.upgrade() { - let is_press = matches!( - button_state, - wayland_client::WEnum::Value(wl_pointer::ButtonState::Pressed) - ); + let is_press = matches!(button_state, WEnum::Value(wl_pointer::ButtonState::Pressed)); let current_position = state.borrow().current_pointer_position(); if let Some(window) = state.borrow().window() { let event = if is_press { @@ -140,7 +133,7 @@ impl Dispatch for WindowEventHandler { fn event( state: &mut Self, _proxy: &WlOutput, - event: ::Event, + event: ::Event, _data: &(), _conn: &Connection, _qhandle: &QueueHandle, @@ -150,7 +143,9 @@ impl Dispatch for WindowEventHandler { info!("WlOutput size changed to {}x{}", width, height); if let Some(state) = state.state.upgrade() { let state_borrow = state.borrow(); - state_borrow.set_output_size(width as u32, height as u32); + let width = width.try_into().unwrap_or_default(); + let height = height.try_into().unwrap_or_default(); + state_borrow.set_output_size(width, height); } } wl_output::Event::Description { ref description } => { @@ -186,7 +181,7 @@ impl Dispatch for WindowEventHandler { fn event( state: &mut Self, _proxy: &WlPointer, - event: ::Event, + event: ::Event, _data: &(), _conn: &Connection, _qhandle: &QueueHandle, diff --git a/src/windowing/event_loop.rs b/src/windowing/event_loop.rs index 5b80b43..aae4f03 100644 --- a/src/windowing/event_loop.rs +++ b/src/windowing/event_loop.rs @@ -1,6 +1,6 @@ use anyhow::{anyhow, Result}; -use log::{debug, error, info}; -use smithay_client_toolkit::reexports::calloop::{self, EventLoop, Interest, Mode, PostAction}; +use log::{debug, error}; +use smithay_client_toolkit::reexports::calloop::{self, Interest, Mode, PostAction}; use std::cell::RefCell; use std::rc::{Rc, Weak}; use wayland_client::{Connection, EventQueue}; @@ -44,7 +44,7 @@ impl EventLoopHandler { loop_handle .insert_source( calloop::generic::Generic::new(connection, Interest::READ, Mode::Level), - move |_, connection, _| { + move |_, connection, ()| { let result: Result = (|| { let wayland_queue = wayland_queue .upgrade() @@ -75,13 +75,6 @@ impl EventLoopHandler { Ok(()) } - pub fn run(&self, event_loop: &mut EventLoop<()>) -> Result<()> { - info!("Starting event loop"); - event_loop - .run(None, &mut (), |_| {}) - .map_err(|e| anyhow!("Failed to run event loop: {}", e)) - } - fn handle_wayland_events( connection: &Connection, wayland_queue: &Rc>>, diff --git a/src/windowing/mod.rs b/src/windowing/mod.rs index de9a01c..4582741 100644 --- a/src/windowing/mod.rs +++ b/src/windowing/mod.rs @@ -1,3 +1,11 @@ +use self::{event_handler::WindowEventHandler, event_loop::EventLoopHandler, state::WindowState}; +use crate::{ + bind_globals, + common::LayerSize, + rendering::{ + egl_context::EGLContext, femtovg_window::FemtoVGWindow, slint_platform::CustomSlintPlatform, + }, +}; use anyhow::{Context, Result}; use log::{debug, info}; use slint::{platform::femtovg_renderer::FemtoVGRenderer, ComponentHandle, LogicalPosition}; @@ -19,21 +27,11 @@ use wayland_client::{ Connection, EventQueue, Proxy, QueueHandle, }; -use crate::{ - bind_globals, - common::LayerSize, - rendering::{ - egl_context::EGLContext, femtovg_window::FemtoVGWindow, slint_platform::CustomSlintPlatform, - }, -}; - mod event_handler; mod event_loop; mod macros; mod state; -use self::{event_handler::WindowEventHandler, event_loop::EventLoopHandler, state::WindowState}; - pub struct WindowConfig { height: u32, layer: zwlr_layer_shell_v1::Layer, @@ -43,7 +41,7 @@ pub struct WindowConfig { exclusive_zone: i32, scale_factor: f32, namespace: String, - component_definition: Option>, + component_definition: Option, } impl Default for WindowConfig { @@ -55,7 +53,7 @@ impl Default for WindowConfig { anchor: Anchor::Top | Anchor::Left | Anchor::Right, keyboard_interactivity: KeyboardInteractivity::OnDemand, exclusive_zone: -1, - namespace: "layer-sheeka".to_string(), + namespace: "layer-shika".to_owned(), scale_factor: 1.0, component_definition: None, } @@ -73,53 +71,69 @@ impl Default for WindowingSystemBuilder { } impl WindowingSystemBuilder { + #[must_use] + #[inline] pub fn new() -> Self { Self { config: WindowConfig::default(), } } - pub fn with_height(mut self, height: u32) -> Self { + #[must_use] + pub const fn with_height(mut self, height: u32) -> Self { self.config.height = height; self } - pub fn with_layer(mut self, layer: zwlr_layer_shell_v1::Layer) -> Self { + #[must_use] + #[inline] + pub const fn with_layer(mut self, layer: zwlr_layer_shell_v1::Layer) -> Self { self.config.layer = layer; self } - pub fn with_margin(mut self, top: i32, right: i32, bottom: i32, left: i32) -> Self { + #[must_use] + #[inline] + pub const fn with_margin(mut self, top: i32, right: i32, bottom: i32, left: i32) -> Self { self.config.margin = (top, right, bottom, left); self } - - pub fn with_anchor(mut self, anchor: Anchor) -> Self { + #[must_use] + #[inline] + pub const fn with_anchor(mut self, anchor: Anchor) -> Self { self.config.anchor = anchor; self } - - pub fn with_keyboard_interactivity(mut self, interactivity: KeyboardInteractivity) -> Self { + #[must_use] + #[inline] + pub const fn with_keyboard_interactivity( + mut self, + interactivity: KeyboardInteractivity, + ) -> Self { self.config.keyboard_interactivity = interactivity; self } - - pub fn with_exclusive_zone(mut self, zone: i32) -> Self { + #[must_use] + #[inline] + pub const fn with_exclusive_zone(mut self, zone: i32) -> Self { self.config.exclusive_zone = zone; self } - + #[must_use] + #[inline] pub fn with_namespace(mut self, namespace: String) -> Self { self.config.namespace = namespace; self } - - pub fn with_scale_factor(mut self, scale_factor: f32) -> Self { + #[must_use] + #[inline] + pub const fn with_scale_factor(mut self, scale_factor: f32) -> Self { self.config.scale_factor = scale_factor; self } - - pub fn with_component_definition(mut self, component: Rc) -> Self { + #[must_use] + #[inline] + pub fn with_component_definition(mut self, component: ComponentDefinition) -> Self { self.config.component_definition = Some(component); self } @@ -185,7 +199,8 @@ impl<'a> WindowingSystem<'a> { system.wait_for_configure()?; system.initialize_renderer_and_ui()?; - system.initialize_event_loop_handler()?; + system.initialize_event_loop_handler(); + system.setup_event_sources()?; Ok(system) } @@ -253,14 +268,14 @@ impl<'a> WindowingSystem<'a> { config.margin.2, config.margin.3, ); - println!("Setting exclusive zone: {}", config.exclusive_zone); + layer_surface.set_exclusive_zone(config.exclusive_zone); layer_surface.set_keyboard_interactivity(config.keyboard_interactivity); layer_surface.set_size(1, config.height); surface.commit(); } - fn wait_for_configure(&mut self) -> Result<()> { + fn wait_for_configure(&self) -> Result<()> { info!("Waiting for surface to be configured..."); loop { self.connection.flush()?; @@ -288,10 +303,10 @@ impl<'a> WindowingSystem<'a> { .clone() .ok_or_else(|| anyhow::anyhow!("Component definition not set"))?; let (window, component_instance) = - self.initialize_slint_ui(renderer, component_definition)?; + self.initialize_slint_ui(renderer, &component_definition)?; self.window = Some(window.clone()); - self.state.borrow_mut().set_window(Rc::downgrade(&window)); + self.state.borrow_mut().set_window(window); self.component_instance = Some(component_instance); Ok(()) @@ -310,7 +325,7 @@ impl<'a> WindowingSystem<'a> { .with_surface_id(surface.id()) .with_size(LayerSize::new(size.width, size.height)) .build() - .context("Failed to build EGL context")?; + .map_err(|e| anyhow::anyhow!("Failed to create EGL context: {:?}", e))?; debug!("Creating FemtoVGRenderer"); FemtoVGRenderer::new(context).context("Failed to create FemtoVGRenderer") @@ -319,7 +334,7 @@ impl<'a> WindowingSystem<'a> { fn initialize_slint_ui( &self, renderer: FemtoVGRenderer, - component_definition: Rc, + component_definition: &ComponentDefinition, ) -> Result<(Rc, Rc)> { let femtovg_window = FemtoVGWindow::new(renderer); let size = self.state.borrow().size(); @@ -343,7 +358,7 @@ impl<'a> WindowingSystem<'a> { Ok((femtovg_window, slint_component)) } - pub fn initialize_event_loop_handler(&mut self) -> Result<()> { + pub fn initialize_event_loop_handler(&mut self) { let event_loop_handler = EventLoopHandler::new( Rc::downgrade(self.window.as_ref().unwrap()), Rc::downgrade(&self.event_queue), @@ -352,7 +367,6 @@ impl<'a> WindowingSystem<'a> { ); self.event_loop_handler = Some(event_loop_handler); - Ok(()) } pub fn setup_event_sources(&self) -> Result<()> { @@ -377,12 +391,9 @@ impl<'a> WindowingSystem<'a> { window.render_frame_if_dirty(); } - let event_loop_handler = self - .event_loop_handler - .as_ref() - .ok_or_else(|| anyhow::anyhow!("EventLoopHandler not initialized"))?; - - event_loop_handler.run(&mut self.event_loop) + self.event_loop + .run(None, &mut (), |()| {}) + .map_err(|e| anyhow::anyhow!("Failed to run event loop: {}", e)) } pub fn component_instance(&self) -> Option> { @@ -397,7 +408,7 @@ impl<'a> WindowingSystem<'a> { self.state.clone() } - pub fn display(&self) -> &WlDisplay { + pub const fn display(&self) -> &WlDisplay { &self.display } } diff --git a/src/windowing/state.rs b/src/windowing/state.rs index a22737a..d1dcab3 100644 --- a/src/windowing/state.rs +++ b/src/windowing/state.rs @@ -1,4 +1,4 @@ -use std::{cell::Cell, rc::Weak}; +use std::cell::Cell; use std::rc::Rc; use log::info; use slint::{LogicalPosition, PhysicalSize}; @@ -14,7 +14,7 @@ pub struct WindowState { size: Cell, output_size: Cell, pointer: Option, - window: Option>, + window: Option>, current_pointer_position: Cell, scale_factor: f32, height: u32, @@ -54,7 +54,7 @@ impl WindowState { } if let Some(s) = self.surface.as_ref() { - s.commit() + s.commit(); } } @@ -77,24 +77,24 @@ impl WindowState { self.current_pointer_position.get() } pub fn window(&self) -> Option> { - self.window.as_ref().and_then(|w| w.upgrade()) + self.window.clone() } pub fn layer_surface(&self) -> Option> { self.layer_surface.clone() } - pub fn surface(&self) -> Option<&WlSurface> { + pub const fn surface(&self) -> Option<&WlSurface> { self.surface.as_ref() } - pub fn height(&self) -> u32 { + pub const fn height(&self) -> u32 { self.height } pub fn set_output_size(&self, width: u32, height: u32) { self.output_size.set(PhysicalSize::new(width, height)); } - pub fn set_window(&mut self, window: Weak) { + pub fn set_window(&mut self, window: Rc) { self.window = Some(window); }