use crate::binding::interfaces::ICreatesCommandBinding;
use ferroui_splat::AppLocator;
use std::any::TypeId;
use std::rc::Rc;
pub struct CommandBinderService;
impl CommandBinderService {
pub fn get_binder<T: ?Sized + 'static>(has_event_target: bool) -> Option<Rc<dyn ICreatesCommandBinding>> {
Self::get_binder_for(TypeId::of::<T>(), has_event_target)
}
pub fn get_binder_for(target_type: TypeId, has_event_target: bool) -> Option<Rc<dyn ICreatesCommandBinding>> {
let mut best_binder = None;
let mut best_affinity = 0;
for binder in AppLocator::get_services::<dyn ICreatesCommandBinding>() {
let affinity = binder.get_affinity_for_object(target_type, has_event_target);
if affinity <= best_affinity {
continue;
}
best_affinity = affinity;
best_binder = Some(binder);
}
best_binder
}
pub fn get_binder_above(target_type: TypeId, own_affinity: i32, has_event_target: bool) -> Option<Rc<dyn ICreatesCommandBinding>> {
let binder = Self::get_binder_for(target_type, has_event_target)?;
(binder.get_affinity_for_object(target_type, has_event_target) > own_affinity).then_some(binder)
}
}