cushy/widgets/
component_probe.rsuse std::fmt::Debug;
use super::Space;
use crate::styles::{ComponentDefinition, ContextFreeComponent};
use crate::value::{Destination, Dynamic};
use crate::widget::{MakeWidget, WidgetRef, WrapperWidget};
#[derive(Debug)]
pub struct ComponentProbe<Component>
where
Component: ComponentDefinition,
{
component: Component,
probed: Dynamic<Component::ComponentType>,
child: WidgetRef,
}
impl<Component> ComponentProbe<Component>
where
Component: ComponentDefinition,
{
pub fn new(component: Component, initial_value: Component::ComponentType) -> Self {
Self::new_wrapping(component, initial_value, Space::clear())
}
pub fn new_wrapping(
component: Component,
initial_value: Component::ComponentType,
child: impl MakeWidget,
) -> Self {
Self {
component,
probed: Dynamic::new(initial_value),
child: WidgetRef::new(child),
}
}
pub fn default_for(component: Component) -> Self
where
Component: ContextFreeComponent,
{
let default = component.default();
Self::new(component, default)
}
pub fn default_wrapping(component: Component, child: impl MakeWidget) -> Self
where
Component: ContextFreeComponent,
{
let default = component.default();
Self::new_wrapping(component, default, child)
}
pub const fn value(&self) -> &Dynamic<Component::ComponentType> {
&self.probed
}
}
impl<Component> WrapperWidget for ComponentProbe<Component>
where
Component: ComponentDefinition + Debug + Send + 'static,
Component::ComponentType: PartialEq + Debug + Send + 'static,
{
fn child_mut(&mut self) -> &mut crate::widget::WidgetRef {
&mut self.child
}
fn adjust_child_constraints(
&mut self,
available_space: figures::Size<crate::ConstraintLimit>,
context: &mut crate::context::LayoutContext<'_, '_, '_, '_>,
) -> figures::Size<crate::ConstraintLimit> {
self.probed.set(context.get(&self.component));
available_space
}
fn redraw_foreground(&mut self, context: &mut crate::context::GraphicsContext<'_, '_, '_, '_>) {
self.probed.set(context.get(&self.component));
}
}