mirror of
https://codeberg.org/waydeer/layer-shika.git
synced 2025-10-28 23:04:22 +00:00
refactor: reduce ref count of window
This commit is contained in:
parent
0cc906c546
commit
ccf49145a0
2 changed files with 12 additions and 7 deletions
|
|
@ -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>)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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:?}"))
|
||||||
})?;
|
})?;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue