refactor: modularize windowing system

WIP/draft
drendog 2024-08-18 02:23:17 +02:00
parent 5cdf4d5af9
commit afe77990f8
Signed by: dwenya
GPG Key ID: 8DD77074645332D0
3 changed files with 128 additions and 112 deletions

92
src/windowing/builder.rs Normal file
View File

@ -0,0 +1,92 @@
use anyhow::Result;
use slint_interpreter::ComponentDefinition;
use smithay_client_toolkit::reexports::protocols_wlr::layer_shell::v1::client::{
zwlr_layer_shell_v1::{self},
zwlr_layer_surface_v1::{Anchor, KeyboardInteractivity},
};
use super::{config::WindowConfig, WindowingSystem};
pub struct WindowingSystemBuilder {
config: WindowConfig,
}
impl Default for WindowingSystemBuilder {
fn default() -> Self {
Self::new()
}
}
impl WindowingSystemBuilder {
#[inline]
#[must_use]
pub fn new() -> Self {
Self {
config: WindowConfig::default(),
}
}
#[must_use]
pub const fn with_height(mut self, height: u32) -> Self {
self.config.height = height;
self
}
#[must_use]
pub const fn with_layer(mut self, layer: zwlr_layer_shell_v1::Layer) -> Self {
self.config.layer = layer;
self
}
#[must_use]
pub const fn with_margin(mut self, top: i32, right: i32, bottom: i32, left: i32) -> Self {
self.config.margin = (top, right, bottom, left);
self
}
#[must_use]
pub const fn with_anchor(mut self, anchor: Anchor) -> Self {
self.config.anchor = anchor;
self
}
#[must_use]
pub const fn with_keyboard_interactivity(
mut self,
interactivity: KeyboardInteractivity,
) -> Self {
self.config.keyboard_interactivity = interactivity;
self
}
#[must_use]
pub const fn with_exclusive_zone(mut self, zone: i32) -> Self {
self.config.exclusive_zone = zone;
self
}
#[must_use]
pub fn with_namespace(mut self, namespace: String) -> Self {
self.config.namespace = namespace;
self
}
#[must_use]
pub const fn with_scale_factor(mut self, scale_factor: f32) -> Self {
self.config.scale_factor = scale_factor;
self
}
#[must_use]
pub fn with_component_definition(mut self, component: ComponentDefinition) -> Self {
self.config.component_definition = Some(component);
self
}
pub fn build(self) -> Result<WindowingSystem> {
match self.config.component_definition {
Some(_) => WindowingSystem::new(self.config),
None => Err(anyhow::anyhow!("Slint component not set")),
}
}
}

33
src/windowing/config.rs Normal file
View File

@ -0,0 +1,33 @@
use slint_interpreter::ComponentDefinition;
use smithay_client_toolkit::reexports::protocols_wlr::layer_shell::v1::client::{
zwlr_layer_shell_v1::{self},
zwlr_layer_surface_v1::{Anchor, KeyboardInteractivity},
};
pub struct WindowConfig {
pub height: u32,
pub layer: zwlr_layer_shell_v1::Layer,
pub margin: (i32, i32, i32, i32),
pub anchor: Anchor,
pub keyboard_interactivity: KeyboardInteractivity,
pub exclusive_zone: i32,
pub scale_factor: f32,
pub namespace: String,
pub component_definition: Option<ComponentDefinition>,
}
impl Default for WindowConfig {
fn default() -> Self {
Self {
height: 30,
layer: zwlr_layer_shell_v1::Layer::Top,
margin: (0, 0, 0, 0),
anchor: Anchor::Top | Anchor::Left | Anchor::Right,
keyboard_interactivity: KeyboardInteractivity::OnDemand,
exclusive_zone: -1,
namespace: "layer-shika".to_owned(),
scale_factor: 1.0,
component_definition: None,
}
}
}

View File

@ -6,6 +6,7 @@ use crate::{
},
};
use anyhow::{Context, Result};
use config::WindowConfig;
use log::{debug, info};
use slint::{platform::femtovg_renderer::FemtoVGRenderer, ComponentHandle, LogicalPosition};
use slint_interpreter::{ComponentDefinition, ComponentInstance};
@ -29,122 +30,12 @@ use wayland_client::{
Connection, EventQueue, Proxy, QueueHandle,
};
pub mod builder;
mod config;
mod event_loop;
mod macros;
mod state;
pub struct WindowConfig {
height: u32,
layer: zwlr_layer_shell_v1::Layer,
margin: (i32, i32, i32, i32),
anchor: Anchor,
keyboard_interactivity: KeyboardInteractivity,
exclusive_zone: i32,
scale_factor: f32,
namespace: String,
component_definition: Option<ComponentDefinition>,
}
impl Default for WindowConfig {
fn default() -> Self {
Self {
height: 30,
layer: zwlr_layer_shell_v1::Layer::Top,
margin: (0, 0, 0, 0),
anchor: Anchor::Top | Anchor::Left | Anchor::Right,
keyboard_interactivity: KeyboardInteractivity::OnDemand,
exclusive_zone: -1,
namespace: "layer-shika".to_owned(),
scale_factor: 1.0,
component_definition: None,
}
}
}
pub struct WindowingSystemBuilder {
config: WindowConfig,
}
impl Default for WindowingSystemBuilder {
fn default() -> Self {
Self::new()
}
}
impl WindowingSystemBuilder {
#[inline]
#[must_use]
pub fn new() -> Self {
Self {
config: WindowConfig::default(),
}
}
#[must_use]
pub const fn with_height(mut self, height: u32) -> Self {
self.config.height = height;
self
}
#[must_use]
pub const fn with_layer(mut self, layer: zwlr_layer_shell_v1::Layer) -> Self {
self.config.layer = layer;
self
}
#[must_use]
pub const fn with_margin(mut self, top: i32, right: i32, bottom: i32, left: i32) -> Self {
self.config.margin = (top, right, bottom, left);
self
}
#[must_use]
pub const fn with_anchor(mut self, anchor: Anchor) -> Self {
self.config.anchor = anchor;
self
}
#[must_use]
pub const fn with_keyboard_interactivity(
mut self,
interactivity: KeyboardInteractivity,
) -> Self {
self.config.keyboard_interactivity = interactivity;
self
}
#[must_use]
pub const fn with_exclusive_zone(mut self, zone: i32) -> Self {
self.config.exclusive_zone = zone;
self
}
#[must_use]
pub fn with_namespace(mut self, namespace: String) -> Self {
self.config.namespace = namespace;
self
}
#[must_use]
pub const fn with_scale_factor(mut self, scale_factor: f32) -> Self {
self.config.scale_factor = scale_factor;
self
}
#[must_use]
pub fn with_component_definition(mut self, component: ComponentDefinition) -> Self {
self.config.component_definition = Some(component);
self
}
pub fn build(self) -> Result<WindowingSystem> {
match self.config.component_definition {
Some(_) => WindowingSystem::new(self.config),
None => Err(anyhow::anyhow!("Slint component not set")),
}
}
}
pub struct WindowingSystem {
state: Rc<RefCell<WindowState>>,
connection: Rc<Connection>,