Skip to content

Commit

Permalink
cargo fmt (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
6543 authored Aug 22, 2024
1 parent f58c748 commit c9fe1be
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 61 deletions.
8 changes: 2 additions & 6 deletions src/core/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ impl<T: Send + Sync + ?Sized> Registry<T> {
Registry(const_mutex(None))
}
fn lock(&self) -> MappedMutexGuard<FxHashMap<usize, Weak<T>>> {
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<T>
where
Expand Down Expand Up @@ -118,9 +116,7 @@ impl<T: Send + Sync + ?Sized> OwnedRegistry<T> {
OwnedRegistry(const_mutex(None))
}
fn lock(&self) -> MappedMutexGuard<FxHashMap<usize, Arc<T>>> {
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<T>
where
Expand Down
131 changes: 77 additions & 54 deletions src/objects/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arc<InputHandler>>,
pub capture: Option<Arc<InputHandler>>,
}
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<Spatial>, &InputDataType, &Field) -> Option<f32>;

pub fn find_closest_capture(method: &InputMethod, distance_calculator: DistanceCalculator) -> Option<Arc<InputHandler>> {
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<Arc<InputHandler>> {
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<Arc<InputHandler>> {
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<Arc<InputHandler>> {
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()
}
2 changes: 1 addition & 1 deletion src/wayland/decoration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl KdeDecorationHandler for WaylandState {
decoration: &OrgKdeKwinServerDecoration,
mode: WEnum<KdeMode>,
) {
let Ok(mode) = mode.into_result() else {return};
let Ok(mode) = mode.into_result() else { return };
decoration.mode(mode);
}
}
Expand Down

0 comments on commit c9fe1be

Please sign in to comment.