fix(input): dropped input handlers properly release methods

This commit is contained in:
Nova
2025-04-07 09:50:34 -07:00
committed by Nova
parent 173b033963
commit fe6ed81255
5 changed files with 15 additions and 13 deletions

View File

@@ -9,21 +9,21 @@ use crate::nodes::{
spatial::Spatial,
};
use glam::vec3;
use std::sync::Arc;
use std::sync::{Arc, Weak};
#[derive(Default)]
pub struct CaptureManager {
pub capture: Option<Arc<InputHandler>>,
pub capture: Weak<InputHandler>,
}
impl CaptureManager {
pub fn update_capture(&mut self, method: &InputMethod) {
if let Some(capture) = &self.capture {
if let Some(capture) = &self.capture.upgrade() {
if !method
.capture_attempts
.get_valid_contents()
.contains(capture)
{
self.capture.take();
self.capture = Weak::new();
}
}
}
@@ -32,13 +32,13 @@ impl CaptureManager {
method: &InputMethod,
distance_calculator: DistanceCalculator,
) {
if self.capture.is_none() {
if self.capture.upgrade().is_none() {
self.capture = find_closest_capture(method, distance_calculator);
}
}
pub fn apply_capture(&self, method: &InputMethod) {
method.captures.clear();
if let Some(capture) = &self.capture {
if let Some(capture) = &self.capture.upgrade() {
method.set_handler_order([capture].into_iter());
method.captures.add_raw(capture);
}
@@ -50,7 +50,7 @@ type DistanceCalculator = fn(&Arc<Spatial>, &InputDataType, &Field) -> Option<f3
pub fn find_closest_capture(
method: &InputMethod,
distance_calculator: DistanceCalculator,
) -> Option<Arc<InputHandler>> {
) -> Weak<InputHandler> {
method
.capture_attempts
.get_valid_contents()
@@ -60,7 +60,8 @@ pub fn find_closest_capture(
.map(|dist| (h.clone(), dist))
})
.min_by(|(_, dist_a), (_, dist_b)| dist_a.partial_cmp(dist_b).unwrap())
.map(|(handler, _)| handler)
.map(|(handler, _)| Arc::downgrade(&handler))
.unwrap_or_default()
}
pub fn get_sorted_handlers(