use super::state::ToolbarState;
use crate::layout::{ChevronStepDirection, DispatchEvent};
use crate::types::{Rect, WidgetId};
pub struct ConsumeEventCtx {
pub cursor: (f64, f64),
pub frame_rect: Rect,
pub viewport: (f64, f64),
}
pub fn consume_event(
event: DispatchEvent,
state: &mut ToolbarState,
host_id: &WidgetId,
ctx: ConsumeEventCtx,
) -> Option<DispatchEvent> {
match event {
DispatchEvent::ChevronStepRequested {
ref chevron_id,
direction,
} => {
let is_own = chevron_id.0 == format!("{}:chevron_back", host_id.0)
|| chevron_id.0 == format!("{}:chevron_fwd", host_id.0);
if is_own {
let step = 80.0_f64;
let signed = match direction {
ChevronStepDirection::Up | ChevronStepDirection::Left => -step,
_ => step,
};
state.scroll_offset = (state.scroll_offset + signed).max(0.0);
None
} else {
Some(event)
}
}
DispatchEvent::ResizeHandleDragStarted {
host_id: ref hid,
edge,
} => {
if hid == host_id {
let min_size = 24.0_f64;
let cap_size = (ctx.viewport.0.max(ctx.viewport.1) * 0.20).max(60.0);
state.start_resize(edge, ctx.frame_rect, ctx.cursor, min_size, cap_size);
None
} else {
Some(event)
}
}
_ => Some(event),
}
}
pub fn drag_outcome_toolbar(
state: &ToolbarState,
which: &'static str,
) -> Option<crate::layout::DragOutcome> {
if state.resize_drag.is_some() {
return Some(crate::layout::DragOutcome::ToolbarResize { which });
}
None
}