use std::any::TypeId;
use kurbo::{Affine, Insets, Point, Rect, Size, Vec2};
use tracing::Span;
use crate::core::{
ClassSetDiff, PropertyCache, PropertyStackId, SubwindowId, WidgetId, WidgetOptions,
};
use crate::layout::MeasurementCache;
#[derive(Clone, Debug)]
pub(crate) struct WidgetState {
pub(crate) id: WidgetId,
pub(crate) subwindow_id: Option<SubwindowId>,
pub(crate) origin: Point,
pub(crate) end_point: Point,
pub(crate) layout_border_box_size: Size,
pub(crate) border_box_insets: Insets,
pub(crate) paint_box_insets: Insets,
pub(crate) bounding_box: Rect,
pub(crate) first_baseline: f64,
pub(crate) last_baseline: f64,
pub(crate) clip_path: Option<Rect>,
pub(crate) transform: Affine,
pub(crate) window_transform: Affine,
pub(crate) scroll_translation: Vec2,
pub(crate) transform_changed: bool,
pub(crate) action_type: TypeId,
pub(crate) accepts_pointer_interaction: bool,
pub(crate) accepts_tooltips: bool,
pub(crate) propagates_pointer_interaction: bool,
pub(crate) accepts_focus: bool,
pub(crate) accepts_text_input: bool,
pub(crate) ime_area: Option<Rect>,
pub(crate) is_new: bool,
pub(crate) is_expecting_place_child_call: bool,
pub(crate) request_layout: bool,
needs_layout: bool,
pub(crate) measurement_cache: MeasurementCache,
pub(crate) request_compose: bool,
pub(crate) needs_compose: bool,
pub(crate) request_pre_paint: bool,
pub(crate) request_paint: bool,
pub(crate) request_post_paint: bool,
pub(crate) needs_paint: bool,
pub(crate) request_accessibility: bool,
pub(crate) needs_accessibility: bool,
pub(crate) request_anim: bool,
pub(crate) needs_anim: bool,
pub(crate) needs_update_disabled: bool,
pub(crate) needs_update_stashed: bool,
pub(crate) descendant_is_focusable: bool,
pub(crate) needs_update_focusable: bool,
pub(crate) request_update_props: bool,
pub(crate) needs_update_props: bool,
pub(crate) children_changed: bool,
pub(crate) is_explicitly_disabled: bool,
pub(crate) is_disabled: bool,
pub(crate) is_explicitly_stashed: bool,
pub(crate) is_stashed: bool,
pub(crate) has_hovered: bool,
pub(crate) is_hovered: bool,
pub(crate) has_active: bool,
pub(crate) is_active: bool,
pub(crate) has_focus_target: bool,
pub(crate) property_stack_id: Option<PropertyStackId>,
pub(crate) class_diff: ClassSetDiff,
pub(crate) property_cache: PropertyCache,
pub(crate) trace_span: Span,
#[cfg(debug_assertions)]
pub(crate) widget_name: &'static str,
#[cfg(debug_assertions)]
pub(crate) action_type_name: &'static str,
}
impl WidgetState {
pub(crate) fn new(
id: WidgetId,
subwindow_id: Option<SubwindowId>,
widget_name: &'static str,
options: WidgetOptions,
action_type: TypeId,
property_stack_id: Option<PropertyStackId>,
#[cfg(debug_assertions)] action_type_name: &'static str,
) -> Self {
Self {
id,
subwindow_id,
origin: Point::ORIGIN,
end_point: Point::ORIGIN,
layout_border_box_size: Size::ZERO,
border_box_insets: Insets::ZERO,
paint_box_insets: Insets::ZERO,
bounding_box: Rect::ZERO,
first_baseline: f64::NAN,
last_baseline: f64::NAN,
clip_path: Option::default(),
transform: options.transform,
window_transform: Affine::IDENTITY,
scroll_translation: Vec2::ZERO,
transform_changed: false,
action_type,
accepts_pointer_interaction: true,
accepts_tooltips: false,
propagates_pointer_interaction: true,
accepts_focus: false,
accepts_text_input: false,
ime_area: None,
is_new: true,
is_expecting_place_child_call: false,
request_layout: true,
needs_layout: true,
measurement_cache: MeasurementCache::new(),
request_compose: true,
needs_compose: true,
request_pre_paint: true,
request_paint: true,
request_post_paint: true,
needs_paint: true,
request_accessibility: true,
needs_accessibility: true,
request_anim: true,
needs_anim: true,
needs_update_disabled: true,
needs_update_stashed: true,
descendant_is_focusable: false,
needs_update_focusable: true,
request_update_props: false,
needs_update_props: false,
children_changed: true,
is_explicitly_disabled: options.disabled,
is_disabled: false,
is_explicitly_stashed: false,
is_stashed: false,
has_hovered: false,
is_hovered: false,
has_active: false,
is_active: false,
has_focus_target: false,
property_stack_id,
class_diff: ClassSetDiff::default(),
property_cache: PropertyCache::default(),
trace_span: Span::none(),
#[cfg(debug_assertions)]
widget_name,
#[cfg(debug_assertions)]
action_type_name,
}
}
pub(crate) fn merge_up(&mut self, child_state: &mut Self) {
if child_state.needs_layout {
self.measurement_cache.clear();
}
self.needs_layout |= child_state.needs_layout;
self.needs_compose |= child_state.needs_compose;
self.needs_paint |= child_state.needs_paint;
self.needs_anim |= child_state.needs_anim;
self.needs_accessibility |= child_state.needs_accessibility;
self.needs_update_disabled |= child_state.needs_update_disabled;
self.children_changed |= child_state.children_changed;
self.needs_update_focusable |= child_state.needs_update_focusable;
self.needs_update_stashed |= child_state.needs_update_stashed;
self.needs_update_props |= child_state.needs_update_props;
}
pub(crate) fn needs_layout(&self) -> bool {
self.needs_layout
}
pub(crate) fn set_needs_layout(&mut self, needs_layout: bool) {
if needs_layout {
self.measurement_cache.clear();
}
self.needs_layout = needs_layout;
}
pub(crate) fn content_box(&self) -> Rect {
let border_box = self.border_box();
let x0 = border_box.x0 + self.border_box_insets.x0;
let y0 = border_box.y0 + self.border_box_insets.y0;
let x1 = (border_box.x1 - self.border_box_insets.x1).max(x0);
let y1 = (border_box.y1 - self.border_box_insets.y1).max(y0);
Rect::new(x0, y0, x1, y1)
}
pub(crate) fn border_box(&self) -> Rect {
(self.end_point - self.origin).to_size().to_rect()
}
pub(crate) fn paint_box(&self) -> Rect {
self.border_box() + self.paint_box_insets
}
pub(crate) fn border_box_translation(&self) -> Vec2 {
Vec2::new(self.border_box_insets.x0, self.border_box_insets.y0)
}
pub(crate) fn compose_local_transform(&self) -> Affine {
let local_translation = self.scroll_translation + self.origin.to_vec2();
self.transform.then_translate(local_translation)
}
pub(crate) fn layout_first_baseline(&self) -> f64 {
if self.first_baseline.is_nan() {
self.layout_border_box_size.height
} else {
self.first_baseline
}
}
pub(crate) fn layout_last_baseline(&self) -> f64 {
if self.last_baseline.is_nan() {
self.layout_border_box_size.height
} else {
self.last_baseline
}
}
pub(crate) fn aligned_first_baseline(&self) -> f64 {
if self.first_baseline.is_nan() {
self.end_point.y - self.origin.y
} else {
self.first_baseline
}
}
pub(crate) fn aligned_last_baseline(&self) -> f64 {
if self.last_baseline.is_nan() {
self.end_point.y - self.origin.y
} else {
self.last_baseline
}
}
pub(crate) fn get_ime_area(&self) -> Rect {
self.window_transform
.transform_rect_bbox(self.ime_area.unwrap_or(self.border_box()))
}
pub(crate) fn clip_child(&self, child_rect: Rect) -> Option<Rect> {
if let Some(clip_path) = self.clip_path {
let clip_path_global = self.window_transform.transform_rect_bbox(clip_path);
if clip_path_global.overlaps(child_rect) {
Some(clip_path_global.intersect(child_rect))
} else {
None
}
} else {
Some(child_rect)
}
}
pub(crate) fn needs_rewrite_passes(&self) -> bool {
self.needs_layout
|| self.needs_compose
|| self.needs_update_disabled
|| self.needs_update_stashed
|| self.needs_update_focusable
|| self.children_changed
|| self.needs_update_props
}
}