use std::any::TypeId;
use accesskit::{Node, Role};
use tracing::{Span, trace_span};
use crate::core::{
AccessCtx, AccessEvent, ChildrenIds, EventCtx, LayoutCtx, MeasureCtx, NoAction, PaintCtx,
PointerEvent, PropertiesMut, PropertiesRef, RegisterCtx, TextEvent, Update, UpdateCtx, Widget,
WidgetId, WidgetMut, WidgetPod,
};
use crate::imaging::Painter;
use crate::kurbo::{Axis, Size};
use crate::layout::{LayoutSize, LenReq, Length, SizeDef};
use crate::widgets::Label;
pub struct SelectorItem {
child: WidgetPod<Label>,
}
impl SelectorItem {
pub fn new(text: String) -> Self {
Self {
child: WidgetPod::new(Label::new(text)),
}
}
}
impl SelectorItem {
pub fn child_mut<'t>(this: &'t mut WidgetMut<'_, Self>) -> WidgetMut<'t, Label> {
this.ctx.get_mut(&mut this.widget.child)
}
}
impl Widget for SelectorItem {
type Action = NoAction;
fn on_pointer_event(
&mut self,
ctx: &mut EventCtx<'_>,
_props: &mut PropertiesMut<'_>,
event: &PointerEvent,
) {
match event {
PointerEvent::Down(..) => {
ctx.request_focus();
ctx.capture_pointer();
ctx.request_paint_only();
}
_ => (),
}
}
fn on_text_event(
&mut self,
_ctx: &mut EventCtx<'_>,
_props: &mut PropertiesMut<'_>,
_event: &TextEvent,
) {
}
fn on_access_event(
&mut self,
_ctx: &mut EventCtx<'_>,
_props: &mut PropertiesMut<'_>,
_event: &AccessEvent,
) {
}
fn update(&mut self, ctx: &mut UpdateCtx<'_>, _props: &mut PropertiesMut<'_>, event: &Update) {
match event {
Update::HoveredChanged(_)
| Update::ActiveChanged(_)
| Update::FocusChanged(_)
| Update::DisabledChanged(_) => {
ctx.request_paint_only();
}
_ => {}
}
}
fn register_children(&mut self, ctx: &mut RegisterCtx<'_>) {
ctx.register_child(&mut self.child);
}
fn property_changed(&mut self, _ctx: &mut UpdateCtx<'_>, _property_type: TypeId) {}
fn measure(
&mut self,
ctx: &mut MeasureCtx<'_>,
_props: &PropertiesRef<'_>,
axis: Axis,
len_req: LenReq,
cross_length: Option<Length>,
) -> Length {
let auto_length = len_req.into();
let context_size = LayoutSize::maybe(axis.cross(), cross_length);
ctx.compute_length(
&mut self.child,
auto_length,
context_size,
axis,
cross_length,
)
}
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, _props: &PropertiesRef<'_>, size: Size) {
let child_size = ctx.compute_size(&mut self.child, SizeDef::fit(size), size.into());
ctx.run_layout(&mut self.child, child_size);
let child_origin = ((size - child_size).to_vec2() * 0.5).to_point();
ctx.place_child(&mut self.child, child_origin);
ctx.derive_baselines(&self.child);
}
fn paint(
&mut self,
_ctx: &mut PaintCtx<'_>,
_props: &PropertiesRef<'_>,
_painter: &mut Painter<'_>,
) {
}
fn accessibility_role(&self) -> Role {
Role::MenuItem
}
fn accessibility(
&mut self,
_ctx: &mut AccessCtx<'_>,
_props: &PropertiesRef<'_>,
node: &mut Node,
) {
node.add_action(accesskit::Action::Click);
}
fn children_ids(&self) -> ChildrenIds {
ChildrenIds::from_slice(&[self.child.id()])
}
fn make_trace_span(&self, id: WidgetId) -> Span {
trace_span!("SelectorItem", id = id.trace())
}
}