use crate::reactive::focus::focus_path;
use crate::reactive::{Prop, signal::RwSignal};
use crate::tree::WidgetId;
use super::container::InteractionFlags;
use super::state_layer::StateWhen;
#[derive(Clone, Copy)]
pub struct Control {
id: WidgetId,
flags: RwSignal<InteractionFlags>,
enabled: Prop<bool>,
}
impl Control {
pub(crate) fn new(
id: WidgetId,
flags: RwSignal<InteractionFlags>,
enabled: Prop<bool>,
) -> Self {
Self { id, flags, enabled }
}
pub fn widget(&self) -> WidgetId {
self.id
}
pub fn is_hovered(&self) -> bool {
self.is_enabled() && self.flags.get().contains(InteractionFlags::HOVERED)
}
pub fn is_pressed(&self) -> bool {
self.is_enabled() && self.flags.get().contains(InteractionFlags::PRESSED)
}
pub fn has_focus(&self) -> bool {
self.is_enabled() && focus_path().contains(self.id)
}
pub fn is_enabled(&self) -> bool {
self.enabled.get_or(true)
}
pub fn is_disabled(&self) -> bool {
!self.is_enabled()
}
pub(crate) fn enabled_prop(&self) -> Prop<bool> {
self.enabled
}
pub(crate) fn is_active(&self, when: &StateWhen) -> bool {
match when {
StateWhen::Hovered => self.is_hovered(),
StateWhen::Pressed => self.is_pressed(),
StateWhen::Focused => self.has_focus(),
StateWhen::Disabled => self.is_disabled(),
StateWhen::When(condition) => condition.get(),
}
}
}