refactor: consolidate handle types

This commit is contained in:
drendog 2025-12-06 04:17:47 +01:00
parent 21e636aabf
commit f843d931e5
Signed by: dwenya
GPG key ID: 8DD77074645332D0
12 changed files with 147 additions and 69 deletions

View file

@ -49,7 +49,7 @@ impl PopupId {
#[must_use] #[must_use]
const fn to_handle(self) -> PopupHandle { const fn to_handle(self) -> PopupHandle {
PopupHandle::new(self.0) PopupHandle::from_raw(self.0)
} }
} }
@ -491,25 +491,25 @@ impl PopupManager {
#[must_use] #[must_use]
pub fn find_by_surface(&self, surface_id: &ObjectId) -> Option<PopupHandle> { pub fn find_by_surface(&self, surface_id: &ObjectId) -> Option<PopupHandle> {
self.find_popup_key_by_surface_id(surface_id) self.find_popup_key_by_surface_id(surface_id)
.map(PopupHandle::new) .map(PopupHandle::from_raw)
} }
#[must_use] #[must_use]
pub fn find_by_fractional_scale(&self, fractional_scale_id: &ObjectId) -> Option<PopupHandle> { pub fn find_by_fractional_scale(&self, fractional_scale_id: &ObjectId) -> Option<PopupHandle> {
self.find_popup_key_by_fractional_scale_id(fractional_scale_id) self.find_popup_key_by_fractional_scale_id(fractional_scale_id)
.map(PopupHandle::new) .map(PopupHandle::from_raw)
} }
#[must_use] #[must_use]
pub fn find_by_xdg_popup(&self, xdg_popup_id: &ObjectId) -> Option<PopupHandle> { pub fn find_by_xdg_popup(&self, xdg_popup_id: &ObjectId) -> Option<PopupHandle> {
self.find_popup_key_by_xdg_popup_id(xdg_popup_id) self.find_popup_key_by_xdg_popup_id(xdg_popup_id)
.map(PopupHandle::new) .map(PopupHandle::from_raw)
} }
#[must_use] #[must_use]
pub fn find_by_xdg_surface(&self, xdg_surface_id: &ObjectId) -> Option<PopupHandle> { pub fn find_by_xdg_surface(&self, xdg_surface_id: &ObjectId) -> Option<PopupHandle> {
self.find_popup_key_by_xdg_surface_id(xdg_surface_id) self.find_popup_key_by_xdg_surface_id(xdg_surface_id)
.map(PopupHandle::new) .map(PopupHandle::from_raw)
} }
#[must_use] #[must_use]
@ -526,7 +526,7 @@ impl PopupManager {
if let Some(popup_handle) = self if let Some(popup_handle) = self
.find_popup_key_by_surface_id(&surface_id) .find_popup_key_by_surface_id(&surface_id)
.map(PopupHandle::new) .map(PopupHandle::from_raw)
{ {
return ActiveWindow::Popup(popup_handle); return ActiveWindow::Popup(popup_handle);
} }

View file

@ -73,8 +73,3 @@ where
self(instance, surface); self(instance, surface);
} }
} }
#[derive(Debug, Clone)]
pub struct ShellSurfaceHandle {
pub name: String,
}

View file

@ -18,6 +18,7 @@ pub use layer_shika_adapters::platform::{slint, slint_interpreter};
pub use layer_shika_domain::entities::output_registry::OutputRegistry; pub use layer_shika_domain::entities::output_registry::OutputRegistry;
pub use layer_shika_domain::prelude::AnchorStrategy; pub use layer_shika_domain::prelude::AnchorStrategy;
pub use layer_shika_domain::value_objects::anchor::AnchorEdges; pub use layer_shika_domain::value_objects::anchor::AnchorEdges;
pub use layer_shika_domain::value_objects::handle::{Handle, SurfaceHandle};
pub use layer_shika_domain::value_objects::keyboard_interactivity::KeyboardInteractivity; pub use layer_shika_domain::value_objects::keyboard_interactivity::KeyboardInteractivity;
pub use layer_shika_domain::value_objects::layer::Layer; pub use layer_shika_domain::value_objects::layer::Layer;
pub use layer_shika_domain::value_objects::output_handle::OutputHandle; pub use layer_shika_domain::value_objects::output_handle::OutputHandle;
@ -32,7 +33,7 @@ pub use shell_runtime::{DEFAULT_SURFACE_NAME, ShellRuntime};
pub use system::{EventContext, EventLoopHandle, ShellControl, SingleSurfaceShell}; pub use system::{EventContext, EventLoopHandle, ShellControl, SingleSurfaceShell};
pub use value_conversion::IntoValue; pub use value_conversion::IntoValue;
pub use layer_surface::{LayerSurfaceHandle, ShellSurfaceConfigHandler, ShellSurfaceHandle}; pub use layer_surface::{LayerSurfaceHandle, ShellSurfaceConfigHandler};
pub use shell::{ pub use shell::{
DEFAULT_COMPONENT_NAME, Shell, ShellBuilder, ShellEventContext, ShellEventLoopHandle, DEFAULT_COMPONENT_NAME, Shell, ShellBuilder, ShellEventContext, ShellEventLoopHandle,
@ -64,13 +65,13 @@ pub enum Error {
pub mod prelude { pub mod prelude {
pub use crate::{ pub use crate::{
AnchorEdges, AnchorStrategy, CompiledUiSource, DEFAULT_COMPONENT_NAME, AnchorEdges, AnchorStrategy, CompiledUiSource, DEFAULT_COMPONENT_NAME,
DEFAULT_SURFACE_NAME, EventContext, EventLoopHandle, IntoValue, KeyboardInteractivity, DEFAULT_SURFACE_NAME, EventContext, EventLoopHandle, Handle, IntoValue,
Layer, LayerSurfaceHandle, OutputGeometry, OutputHandle, OutputInfo, OutputPolicy, KeyboardInteractivity, Layer, LayerSurfaceHandle, OutputGeometry, OutputHandle, OutputInfo,
OutputRegistry, PopupBuilder, PopupHandle, PopupPlacement, PopupPositioningMode, OutputPolicy, OutputRegistry, PopupBuilder, PopupHandle, PopupPlacement,
PopupRequest, PopupSize, PopupWindow, Result, Shell, ShellBuilder, ShellConfig, PopupPositioningMode, PopupRequest, PopupSize, PopupWindow, Result, Shell, ShellBuilder,
ShellControl, ShellEventContext, ShellEventLoopHandle, ShellRuntime, ShellConfig, ShellControl, ShellEventContext, ShellEventLoopHandle, ShellRuntime,
ShellSurfaceConfigHandler, ShellSurfaceHandle, SingleSurfaceShell, SurfaceComponentConfig, ShellSurfaceConfigHandler, SingleSurfaceShell, SurfaceComponentConfig, SurfaceConfigBuilder,
SurfaceConfigBuilder, SurfaceDefinition, SurfaceDefinition, SurfaceHandle,
}; };
pub use crate::calloop::{Generic, Interest, Mode, PostAction, RegistrationToken, Timer}; pub use crate::calloop::{Generic, Interest, Mode, PostAction, RegistrationToken, Timer};

View file

@ -480,7 +480,7 @@ impl EventContext<'_> {
if control if control
.resize_popup( .resize_popup(
PopupHandle::new(popup_key), PopupHandle::from_raw(popup_key),
dimensions.width, dimensions.width,
dimensions.height, dimensions.height,
) )

View file

@ -10,10 +10,10 @@ pub use crate::surface_dimensions::SurfaceDimensions;
pub use crate::value_objects::anchor::AnchorEdges; pub use crate::value_objects::anchor::AnchorEdges;
pub use crate::value_objects::anchor_strategy::AnchorStrategy; pub use crate::value_objects::anchor_strategy::AnchorStrategy;
pub use crate::value_objects::dimensions::{PopupDimensions, SurfaceDimension}; pub use crate::value_objects::dimensions::{PopupDimensions, SurfaceDimension};
pub use crate::value_objects::handle::{Handle, OutputHandle, PopupHandle, SurfaceHandle};
pub use crate::value_objects::keyboard_interactivity::KeyboardInteractivity; pub use crate::value_objects::keyboard_interactivity::KeyboardInteractivity;
pub use crate::value_objects::layer::Layer; pub use crate::value_objects::layer::Layer;
pub use crate::value_objects::margins::Margins; pub use crate::value_objects::margins::Margins;
pub use crate::value_objects::output_handle::OutputHandle;
pub use crate::value_objects::output_info::{OutputGeometry, OutputInfo}; pub use crate::value_objects::output_info::{OutputGeometry, OutputInfo};
pub use crate::value_objects::output_policy::OutputPolicy; pub use crate::value_objects::output_policy::OutputPolicy;
pub use crate::value_objects::ui_source::UiSource; pub use crate::value_objects::ui_source::UiSource;

View file

@ -0,0 +1,116 @@
use std::fmt::{Debug, Formatter, Result as FmtResult};
use std::hash::Hash;
use std::hash::Hasher;
use std::marker::PhantomData;
use std::sync::atomic::{AtomicUsize, Ordering};
static NEXT_HANDLE_ID: AtomicUsize = AtomicUsize::new(1);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct HandleId(usize);
impl HandleId {
fn new() -> Self {
Self(NEXT_HANDLE_ID.fetch_add(1, Ordering::Relaxed))
}
const fn from_raw(id: usize) -> Self {
Self(id)
}
pub const fn as_usize(&self) -> usize {
self.0
}
}
impl Hash for HandleId {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Output;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Popup;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Surface;
pub struct Handle<T> {
id: HandleId,
_marker: PhantomData<T>,
}
impl<T> Debug for Handle<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.debug_struct("Handle").field("id", &self.id).finish()
}
}
impl<T> Handle<T> {
#[must_use]
pub fn new() -> Self {
Self {
id: HandleId::new(),
_marker: PhantomData,
}
}
#[must_use]
pub const fn from_raw(id: usize) -> Self {
Self {
id: HandleId::from_raw(id),
_marker: PhantomData,
}
}
#[must_use]
pub const fn id(&self) -> usize {
self.id.as_usize()
}
}
impl<T> Default for Handle<T> {
fn default() -> Self {
Self::new()
}
}
#[allow(clippy::non_canonical_clone_impl)]
impl<T> Clone for Handle<T> {
fn clone(&self) -> Self {
Self {
id: self.id,
_marker: PhantomData,
}
}
}
impl<T> Copy for Handle<T> {}
impl<T> PartialEq for Handle<T> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl<T> Eq for Handle<T> {}
impl<T> Hash for Handle<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
}
impl Handle<Popup> {
#[must_use]
pub const fn key(self) -> usize {
self.id()
}
}
pub type OutputHandle = Handle<Output>;
pub type PopupHandle = Handle<Popup>;
pub type SurfaceHandle = Handle<Surface>;

View file

@ -1,6 +1,7 @@
pub mod anchor; pub mod anchor;
pub mod anchor_strategy; pub mod anchor_strategy;
pub mod dimensions; pub mod dimensions;
pub mod handle;
pub mod keyboard_interactivity; pub mod keyboard_interactivity;
pub mod layer; pub mod layer;
pub mod margins; pub mod margins;

View file

@ -1,26 +1,3 @@
use std::sync::atomic::{AtomicUsize, Ordering}; #![allow(clippy::pub_use)]
static NEXT_OUTPUT_ID: AtomicUsize = AtomicUsize::new(1); pub use super::handle::OutputHandle;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct OutputHandle {
id: usize,
}
impl OutputHandle {
pub fn new() -> Self {
Self {
id: NEXT_OUTPUT_ID.fetch_add(1, Ordering::Relaxed),
}
}
pub const fn id(&self) -> usize {
self.id
}
}
impl Default for OutputHandle {
fn default() -> Self {
Self::new()
}
}

View file

@ -1,20 +1,8 @@
#![allow(clippy::pub_use)]
pub use super::handle::PopupHandle;
use super::popup_positioning_mode::PopupPositioningMode; use super::popup_positioning_mode::PopupPositioningMode;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct PopupHandle(usize);
impl PopupHandle {
#[must_use]
pub const fn new(key: usize) -> Self {
Self(key)
}
#[must_use]
pub const fn key(self) -> usize {
self.0
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct PopupRequest { pub struct PopupRequest {
pub component: String, pub component: String,

View file

@ -118,13 +118,13 @@ pub mod shell;
pub mod slint_integration; pub mod slint_integration;
pub mod window; pub mod window;
pub use layer_shika_composition::{Error, Result}; pub use layer_shika_composition::{Error, Handle, Result, SurfaceHandle};
pub use shell::{ pub use shell::{
CompiledUiSource, DEFAULT_COMPONENT_NAME, DEFAULT_SURFACE_NAME, LayerSurfaceHandle, Shell, CompiledUiSource, DEFAULT_COMPONENT_NAME, DEFAULT_SURFACE_NAME, LayerSurfaceHandle, Shell,
ShellBuilder, ShellConfig, ShellControl, ShellEventContext, ShellEventLoopHandle, ShellRuntime, ShellBuilder, ShellConfig, ShellControl, ShellEventContext, ShellEventLoopHandle, ShellRuntime,
ShellSurfaceConfigHandler, ShellSurfaceHandle, SingleSurfaceShell, SurfaceComponentConfig, ShellSurfaceConfigHandler, SingleSurfaceShell, SurfaceComponentConfig, SurfaceConfigBuilder,
SurfaceConfigBuilder, SurfaceDefinition, SurfaceDefinition,
}; };
pub use window::{ pub use window::{

View file

@ -11,8 +11,8 @@
pub use crate::shell::{ pub use crate::shell::{
CompiledUiSource, DEFAULT_COMPONENT_NAME, DEFAULT_SURFACE_NAME, LayerSurfaceHandle, Shell, CompiledUiSource, DEFAULT_COMPONENT_NAME, DEFAULT_SURFACE_NAME, LayerSurfaceHandle, Shell,
ShellBuilder, ShellConfig, ShellControl, ShellEventContext, ShellEventLoopHandle, ShellRuntime, ShellBuilder, ShellConfig, ShellControl, ShellEventContext, ShellEventLoopHandle, ShellRuntime,
ShellSurfaceConfigHandler, ShellSurfaceHandle, SingleSurfaceShell, SurfaceComponentConfig, ShellSurfaceConfigHandler, SingleSurfaceShell, SurfaceComponentConfig, SurfaceConfigBuilder,
SurfaceConfigBuilder, SurfaceDefinition, SurfaceDefinition,
}; };
pub use crate::window::{ pub use crate::window::{
@ -26,7 +26,7 @@ pub use crate::event::{EventContext, EventLoopHandle};
pub use crate::slint_integration::{PopupWindow, slint, slint_interpreter}; pub use crate::slint_integration::{PopupWindow, slint, slint_interpreter};
pub use crate::{Error, Result}; pub use crate::{Error, Handle, Result, SurfaceHandle};
pub use layer_shika_composition::prelude::{ pub use layer_shika_composition::prelude::{
Anchor, LogicalSize, Margins, PhysicalSize, ScaleFactor, SurfaceConfig, SurfaceDimension, Anchor, LogicalSize, Margins, PhysicalSize, ScaleFactor, SurfaceConfig, SurfaceDimension,

View file

@ -1,6 +1,6 @@
pub use layer_shika_composition::{ pub use layer_shika_composition::{
CompiledUiSource, DEFAULT_COMPONENT_NAME, DEFAULT_SURFACE_NAME, LayerSurfaceHandle, Shell, CompiledUiSource, DEFAULT_COMPONENT_NAME, DEFAULT_SURFACE_NAME, LayerSurfaceHandle, Shell,
ShellBuilder, ShellConfig, ShellControl, ShellEventContext, ShellEventLoopHandle, ShellRuntime, ShellBuilder, ShellConfig, ShellControl, ShellEventContext, ShellEventLoopHandle, ShellRuntime,
ShellSurfaceConfigHandler, ShellSurfaceHandle, SingleSurfaceShell, SurfaceComponentConfig, ShellSurfaceConfigHandler, SingleSurfaceShell, SurfaceComponentConfig, SurfaceConfigBuilder,
SurfaceConfigBuilder, SurfaceDefinition, SurfaceDefinition,
}; };