use std::any::Any;
use std::fmt::Debug;
use std::rc::Rc;
use crate::reactive::Signal;
use crate::render_object::RenderObject;
use crate::{GestureState, Point, Rect, WidgetEvent};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct EventContext {
handled: bool,
}
impl EventContext {
pub fn stop(&mut self) {
self.handled = true;
}
pub fn is_handled(&self) -> bool {
self.handled
}
}
pub use overlay_host::{OverlayAnchor, OverlayAware, OverlayEvent, OverlayFocus, OverlayIntent, OverlayRef};
#[derive(Clone)]
pub struct WidgetNode(pub Rc<dyn Widget>);
impl WidgetNode {
pub fn new<W: Widget + 'static>(widget: W) -> Self {
Self(Rc::new(widget))
}
pub fn as_any(&self) -> &dyn Any {
self.0.as_any()
}
pub fn type_id(&self) -> std::any::TypeId {
self.0.as_any().type_id()
}
pub fn is<T: 'static>(&self) -> bool {
self.0.as_any().is::<T>()
}
pub fn ptr_eq(&self, other: &WidgetNode) -> bool {
Rc::ptr_eq(&self.0, &other.0)
}
pub fn is_equivalent(&self, old: &WidgetNode) -> bool {
if widget_eq_disabled() {
return false;
}
if self.0.is_stateful() || old.0.is_stateful() {
return false;
}
if self.0.is_repaint_boundary() || old.0.is_repaint_boundary() {
return false;
}
if self.0.key() != old.0.key() {
return false;
}
if self.0.dynamic_signal().is_some() || old.0.dynamic_signal().is_some() {
return false; }
self.0.can_update(&*old.0) && self.0.is_equivalent(&*old.0)
}
}
impl std::fmt::Debug for WidgetNode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("WidgetNode").field("type", &self.0.type_name()).finish()
}
}
#[allow(unused_variables)]
#[deny(missing_docs)]
pub trait Widget: Any + Debug + 'static {
fn as_any(&self) -> &dyn Any;
fn type_name(&self) -> &'static str {
std::any::type_name::<Self>()
}
fn is_stateful(&self) -> bool {
false
}
fn is_focusable(&self) -> bool {
false
}
fn wants_ime(&self) -> bool {
false
}
fn captures_tab(&self) -> bool {
false
}
fn create_state(&self) -> Option<Box<dyn State>> {
None
}
fn create_render_object(&self) -> Box<dyn RenderObject>;
fn children(&self) -> Vec<WidgetNode> {
Vec::new()
}
fn children_with_positions(&self) -> Vec<(Point, WidgetNode)> {
self.children().into_iter().map(|c| (Point::ZERO, c)).collect()
}
fn handle_event(&self, event: &WidgetEvent, local_pos: Point) {}
fn handle_event_ctx(&self, event: &WidgetEvent, local_pos: Point, _ctx: &mut EventContext) {
self.handle_event(event, local_pos);
}
fn accepts_scroll(&self) -> bool {
false
}
fn key(&self) -> Option<u64> {
None
}
fn gesture_state(&self) -> Option<GestureState> {
None
}
fn dynamic_signal(&self) -> Option<Signal<Point>> {
None
}
fn semantics(&self) -> Option<crate::semantics::WidgetSemantics> {
None
}
fn as_inherited(&self) -> Option<&dyn crate::inherited::InheritedWidget> {
None
}
fn can_update(&self, old: &dyn Widget) -> bool {
self.as_any().type_id() == old.as_any().type_id()
}
fn is_equivalent(&self, _old: &dyn Widget) -> bool {
false
}
fn is_repaint_boundary(&self) -> bool {
false
}
fn visible_children(&self) -> Option<Vec<bool>> {
None
}
fn parent_data(&self) -> Option<crate::layout::ParentData> {
None
}
}
impl Widget for WidgetNode {
fn as_any(&self) -> &dyn Any {
self.0.as_any()
}
fn type_name(&self) -> &'static str {
self.0.type_name()
}
fn is_stateful(&self) -> bool {
self.0.is_stateful()
}
fn is_focusable(&self) -> bool {
self.0.is_focusable()
}
fn wants_ime(&self) -> bool {
self.0.wants_ime()
}
fn captures_tab(&self) -> bool {
self.0.captures_tab()
}
fn create_state(&self) -> Option<Box<dyn State>> {
self.0.create_state()
}
fn create_render_object(&self) -> Box<dyn RenderObject> {
self.0.create_render_object()
}
fn children(&self) -> Vec<WidgetNode> {
self.0.children()
}
fn children_with_positions(&self) -> Vec<(Point, WidgetNode)> {
self.0.children_with_positions()
}
fn handle_event(&self, event: &WidgetEvent, local_pos: Point) {
self.0.handle_event(event, local_pos)
}
fn handle_event_ctx(&self, event: &WidgetEvent, local_pos: Point, ctx: &mut EventContext) {
self.0.handle_event_ctx(event, local_pos, ctx)
}
fn accepts_scroll(&self) -> bool {
self.0.accepts_scroll()
}
fn key(&self) -> Option<u64> {
self.0.key()
}
fn gesture_state(&self) -> Option<GestureState> {
self.0.gesture_state()
}
fn dynamic_signal(&self) -> Option<Signal<Point>> {
self.0.dynamic_signal()
}
fn semantics(&self) -> Option<crate::semantics::WidgetSemantics> {
self.0.semantics()
}
fn as_inherited(&self) -> Option<&dyn crate::inherited::InheritedWidget> {
self.0.as_inherited()
}
fn can_update(&self, old: &dyn Widget) -> bool {
self.0.can_update(old)
}
fn is_equivalent(&self, old: &dyn Widget) -> bool {
self.0.is_equivalent(old)
}
fn is_repaint_boundary(&self) -> bool {
self.0.is_repaint_boundary()
}
fn visible_children(&self) -> Option<Vec<bool>> {
self.0.visible_children()
}
fn parent_data(&self) -> Option<crate::layout::ParentData> {
self.0.parent_data()
}
}
pub trait StatefulWidget: Widget {}
fn state_apply_disabled() -> bool {
static DISABLED: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*DISABLED.get_or_init(|| std::env::var_os("MGUI_DISABLE_STATE_APPLY").is_some())
}
pub fn state_apply_enabled() -> bool {
!state_apply_disabled()
}
fn widget_eq_disabled() -> bool {
static DISABLED: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*DISABLED.get_or_init(|| std::env::var_os("MGUI_DISABLE_WIDGET_EQ").is_some())
}
pub fn children_equivalent(a: &[WidgetNode], b: &[WidgetNode]) -> bool {
a.len() == b.len() && a.iter().zip(b.iter()).all(|(x, y)| x.is_equivalent(y))
}
pub fn optional_child_equivalent(a: &Option<WidgetNode>, b: &Option<WidgetNode>) -> bool {
match (a, b) {
(Some(a), Some(b)) => a.is_equivalent(b),
(None, None) => true,
_ => false,
}
}
#[allow(unused_variables)]
#[deny(missing_docs)]
pub trait State: Any {
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
fn widget(&self) -> &WidgetNode;
fn set_widget(&mut self, widget: WidgetNode);
fn init_state(&mut self) {}
fn did_update_widget(&mut self) {}
fn dispose(&mut self) {}
fn build(&self, ctx: &mut crate::inherited::BuildContext<'_>) -> Vec<WidgetNode> {
let _ = ctx;
self.widget().children()
}
fn create_render_object(&self) -> Box<dyn RenderObject> {
self.widget().create_render_object()
}
fn apply_to_ro(&mut self, ro: &mut dyn RenderObject) -> crate::render_object::StateApply {
if state_apply_disabled() {
return crate::render_object::StateApply::Unsupported;
}
ro.apply_state(self.as_any_mut())
}
fn semantics(&self) -> Option<crate::semantics::WidgetSemantics> {
None
}
fn handle_event(&mut self, event: &WidgetEvent, local_pos: Point) {}
fn handle_event_ctx(&mut self, event: &WidgetEvent, local_pos: Point, _ctx: &mut EventContext) {
self.handle_event(event, local_pos);
}
fn scroll_into_view(&mut self, _target: Rect) -> bool {
false
}
fn tick(&mut self, _now_ms: u64) {}
fn handle_overlay_key(&mut self, _key: crate::KeyCode, _mods: crate::KeyModifiers) -> bool {
false
}
fn is_animating(&self) -> bool {
false
}
fn next_animation_delay(&self, _now_ms: u64) -> Option<u64> {
None
}
fn take_needs_build(&mut self) -> bool {
false
}
fn gesture_state(&self) -> Option<GestureState> {
None
}
fn reject_gestures(&mut self) {}
fn handles_build_error(&self) -> bool {
false
}
fn on_build_error(&mut self, msg: &str) {
let _ = msg;
}
fn ime_cursor_area(&self) -> Option<(f32, f32, f32)> {
None
}
fn as_overlay_aware(&mut self) -> Option<&mut dyn OverlayAware> {
None
}
}
pub mod overlay_host;
pub mod primitives;
pub use primitives::{RRectWidget, StackWidget};
pub use overlay_host::AnchorPlacement as TooltipPlacement;