uzor 1.5.40

Core UI engine — geometry, interaction, input state
Documentation
//! Popup event consumption.
//!
//! Level-1 hit registration stays in [`super::input`].
//!
//! ```
//! use uzor::layout::DispatchEvent;
//! use uzor::ui::widgets::composite::popup::consume::{
//!     consume_event, drag_outcome_popup, ConsumeEventCtx,
//! };
//! use uzor::ui::widgets::composite::popup::PopupState;
//! use uzor::{Rect, WidgetId};
//!
//! let mut state = PopupState::default();
//! let host = WidgetId::from("popup-widget");
//! let ctx = ConsumeEventCtx {
//!     cursor: (0.0, 0.0),
//!     frame_rect: Rect::new(0.0, 0.0, 160.0, 90.0),
//!     viewport: (800.0, 600.0),
//! };
//! let event = DispatchEvent::Unhandled(WidgetId::from("other"));
//! assert!(consume_event(event, &mut state, &host, ctx).is_some());
//! assert!(drag_outcome_popup(&state).is_none());
//! ```

use super::state::PopupState;
use crate::layout::DispatchEvent;
use crate::types::{Rect, WidgetId};

/// Cursor position and view metadata for events that need spatial context
/// (scrollbar drag start, track click).
pub struct ConsumeEventCtx {
    /// Current pointer position in screen coordinates.
    pub cursor: (f64, f64),
    /// Resolved frame rect of the popup this frame.
    pub frame_rect: Rect,
    /// Viewport size used for resize cap computation.
    pub viewport: (f64, f64),
}

/// Consume a `DispatchEvent` if it belongs to this popup. Returns:
/// - `None` — the event was consumed (composite mutated its state).
/// - `Some(event)` — the event is not for this popup; pass it through.
///
/// `host_id` is the popup composite's WidgetId (e.g. `"popup-widget"`). Only
/// events whose carried id starts with `{host_id}:` are consumed.
pub fn consume_event(
    event: DispatchEvent,
    state: &mut PopupState,
    host_id: &WidgetId,
    ctx: ConsumeEventCtx,
) -> Option<DispatchEvent> {
    match event {
        DispatchEvent::ChevronStepRequested {
            ref chevron_id,
            direction,
        } => {
            let is_own = chevron_id.0 == format!("{}:chevron_up", host_id.0)
                || chevron_id.0 == format!("{}:chevron_down", host_id.0);
            if is_own {
                state.body_chevron_step(direction);
                None
            } else {
                Some(event)
            }
        }
        DispatchEvent::ScrollbarTrackClicked { ref track_id } => {
            if track_id.0 == format!("{}:scrollbar_track", host_id.0) {
                state.body_scroll_track_click(ctx.cursor.1);
                None
            } else {
                Some(event)
            }
        }
        DispatchEvent::ScrollbarThumbDragStarted { ref thumb_id } => {
            if thumb_id.0 == format!("{}:scrollbar_handle", host_id.0) {
                state.start_body_scroll_drag(ctx.cursor.1);
                None
            } else {
                Some(event)
            }
        }
        _ => Some(event),
    }
}

/// Inspect popup state after `consume_event` returned `None` (consumed) to
/// determine what drag was started.
pub fn drag_outcome_popup(state: &PopupState) -> Option<crate::layout::DragOutcome> {
    if state.scroll.is_dragging {
        return Some(crate::layout::DragOutcome::PopupBodyScroll);
    }
    if state.resize_drag.is_some() {
        return Some(crate::layout::DragOutcome::PopupResize);
    }
    None
}