diff --git a/src/core/registry.rs b/src/core/registry.rs index ba34c73..711d6f2 100644 --- a/src/core/registry.rs +++ b/src/core/registry.rs @@ -13,9 +13,7 @@ impl Registry { Registry(const_mutex(None)) } fn lock(&self) -> MappedMutexGuard>> { - MutexGuard::map(self.0.lock(), |r| { - r.get_or_insert_with(FxHashMap::default) - }) + MutexGuard::map(self.0.lock(), |r| r.get_or_insert_with(FxHashMap::default)) } pub fn add(&self, t: T) -> Arc where @@ -118,9 +116,7 @@ impl OwnedRegistry { OwnedRegistry(const_mutex(None)) } fn lock(&self) -> MappedMutexGuard>> { - MutexGuard::map(self.0.lock(), |r| { - r.get_or_insert_with(FxHashMap::default) - }) + MutexGuard::map(self.0.lock(), |r| r.get_or_insert_with(FxHashMap::default)) } pub fn add(&self, t: T) -> Arc where diff --git a/src/objects/input/mod.rs b/src/objects/input/mod.rs index b0cc9f6..65f0c48 100644 --- a/src/objects/input/mod.rs +++ b/src/objects/input/mod.rs @@ -4,72 +4,95 @@ pub mod sk_controller; pub mod sk_hand; use crate::nodes::{ - fields::{Field, FieldTrait, Ray}, input::{InputDataTrait, InputDataType, InputHandler, InputMethod, INPUT_HANDLER_REGISTRY}, spatial::Spatial + fields::{Field, FieldTrait, Ray}, + input::{InputDataTrait, InputDataType, InputHandler, InputMethod, INPUT_HANDLER_REGISTRY}, + spatial::Spatial, }; use glam::vec3; use std::sync::Arc; - #[derive(Default)] pub struct CaptureManager { - pub capture: Option>, + pub capture: Option>, } impl CaptureManager { - pub fn update_capture(&mut self, pointer: &InputMethod) { - if let Some(capture) = &self.capture { - if !pointer.internal_capture_requests.get_valid_contents().contains(capture) { - self.capture.take(); - } - } - } - pub fn set_new_capture(&mut self, pointer: &InputMethod, distance_calculator: DistanceCalculator) { - if self.capture.is_none() { - self.capture = find_closest_capture(pointer, distance_calculator); - } - } - pub fn apply_capture(&self, method: &InputMethod) { - method.captures.clear(); - if let Some(capture) = &self.capture { - method.set_handler_order([capture].into_iter()); - method.captures.add_raw(capture); - } - } + pub fn update_capture(&mut self, pointer: &InputMethod) { + if let Some(capture) = &self.capture { + if !pointer + .internal_capture_requests + .get_valid_contents() + .contains(capture) + { + self.capture.take(); + } + } + } + pub fn set_new_capture( + &mut self, + pointer: &InputMethod, + distance_calculator: DistanceCalculator, + ) { + if self.capture.is_none() { + self.capture = find_closest_capture(pointer, distance_calculator); + } + } + pub fn apply_capture(&self, method: &InputMethod) { + method.captures.clear(); + if let Some(capture) = &self.capture { + method.set_handler_order([capture].into_iter()); + method.captures.add_raw(capture); + } + } } - type DistanceCalculator = fn(&Arc, &InputDataType, &Field) -> Option; -pub fn find_closest_capture(method: &InputMethod, distance_calculator: DistanceCalculator) -> Option> { - method - .internal_capture_requests - .get_valid_contents() - .into_iter() - .filter_map(|h| distance_calculator(&method.spatial, &method.data.lock(), &h.field).map(|dist| (h.clone(), dist))) - .min_by(|(_, dist_a), (_, dist_b)| dist_a.partial_cmp(dist_b).unwrap()) - .map(|(handler, _)| handler) +pub fn find_closest_capture( + method: &InputMethod, + distance_calculator: DistanceCalculator, +) -> Option> { + method + .internal_capture_requests + .get_valid_contents() + .into_iter() + .filter_map(|h| { + distance_calculator(&method.spatial, &method.data.lock(), &h.field) + .map(|dist| (h.clone(), dist)) + }) + .min_by(|(_, dist_a), (_, dist_b)| dist_a.partial_cmp(dist_b).unwrap()) + .map(|(handler, _)| handler) } -pub fn get_sorted_handlers(method: &InputMethod, distance_calculator: DistanceCalculator) -> Vec> { - INPUT_HANDLER_REGISTRY - .get_valid_contents() - .into_iter() - .filter(|handler| handler.spatial.node().map_or(false, |node| node.enabled())) - .filter(|handler| handler.field.spatial.node().map_or(false, |node| node.enabled())) - .filter_map(|handler| { - distance_calculator(&method.spatial, &method.data.lock(), &handler.field) - .map(|distance| (vec![handler], distance)) - }) - .filter(|(_, distance)| *distance > 0.0) - .reduce(|(mut handlers_a, distance_a), (handlers_b, distance_b)| { - if (distance_a - distance_b).abs() < 0.001 { - handlers_a.extend(handlers_b); - (handlers_a, distance_a) - } else if distance_a < distance_b { - (handlers_a, distance_a) - } else { - (handlers_b, distance_b) - } - }) - .map(|(handlers, _)| handlers) - .unwrap_or_default() +pub fn get_sorted_handlers( + method: &InputMethod, + distance_calculator: DistanceCalculator, +) -> Vec> { + INPUT_HANDLER_REGISTRY + .get_valid_contents() + .into_iter() + .filter(|handler| handler.spatial.node().map_or(false, |node| node.enabled())) + .filter(|handler| { + handler + .field + .spatial + .node() + .map_or(false, |node| node.enabled()) + }) + .filter_map(|handler| { + distance_calculator(&method.spatial, &method.data.lock(), &handler.field) + .map(|distance| (vec![handler], distance)) + }) + .filter(|(_, distance)| *distance > 0.0) + .reduce(|(mut handlers_a, distance_a), (handlers_b, distance_b)| { + if (distance_a - distance_b).abs() < 0.001 { + handlers_a.extend(handlers_b); + (handlers_a, distance_a) + } else if distance_a < distance_b { + (handlers_a, distance_a) + } else { + (handlers_b, distance_b) + } + }) + .map(|(handlers, _)| handlers) + .unwrap_or_default() } diff --git a/src/wayland/decoration.rs b/src/wayland/decoration.rs index c8ba89b..92be6c5 100644 --- a/src/wayland/decoration.rs +++ b/src/wayland/decoration.rs @@ -90,7 +90,7 @@ impl KdeDecorationHandler for WaylandState { decoration: &OrgKdeKwinServerDecoration, mode: WEnum, ) { - let Ok(mode) = mode.into_result() else {return}; + let Ok(mode) = mode.into_result() else { return }; decoration.mode(mode); } }