refactor: reduce ref count of window

This commit is contained in:
drendog 2025-10-25 22:56:23 +02:00
parent 0cc906c546
commit ccf49145a0
Signed by: dwenya
GPG key ID: 8DD77074645332D0
2 changed files with 12 additions and 7 deletions

View file

@ -2,22 +2,27 @@ use slint::{
platform::{Platform, WindowAdapter}, platform::{Platform, WindowAdapter},
PlatformError, PlatformError,
}; };
use std::rc::Rc; use std::rc::{Rc, Weak};
use super::femtovg_window::FemtoVGWindow; use super::femtovg_window::FemtoVGWindow;
pub struct CustomSlintPlatform { pub struct CustomSlintPlatform {
window: Rc<FemtoVGWindow>, window: Weak<FemtoVGWindow>,
} }
impl CustomSlintPlatform { impl CustomSlintPlatform {
pub const fn new(window: Rc<FemtoVGWindow>) -> Self { pub fn new(window: &Rc<FemtoVGWindow>) -> Self {
Self { window } Self {
window: Rc::downgrade(window),
}
} }
} }
impl Platform for CustomSlintPlatform { impl Platform for CustomSlintPlatform {
fn create_window_adapter(&self) -> Result<Rc<dyn WindowAdapter + 'static>, PlatformError> { fn create_window_adapter(&self) -> Result<Rc<dyn WindowAdapter + 'static>, PlatformError> {
Result::Ok(Rc::clone(&self.window) as Rc<dyn WindowAdapter>) self.window
.upgrade()
.ok_or(PlatformError::NoPlatform)
.map(|w| w as Rc<dyn WindowAdapter>)
} }
} }

View file

@ -110,11 +110,11 @@ impl WindowStateBuilder {
} }
pub fn build(self) -> Result<WindowState> { pub fn build(self) -> Result<WindowState> {
let platform = CustomSlintPlatform::new(Rc::clone( let platform = CustomSlintPlatform::new(
self.window self.window
.as_ref() .as_ref()
.ok_or_else(|| LayerShikaError::InvalidInput("Window is required".into()))?, .ok_or_else(|| LayerShikaError::InvalidInput("Window is required".into()))?,
)); );
set_platform(Box::new(platform)).map_err(|e| { set_platform(Box::new(platform)).map_err(|e| {
LayerShikaError::PlatformSetup(format!("Failed to set platform: {e:?}")) LayerShikaError::PlatformSetup(format!("Failed to set platform: {e:?}"))
})?; })?;