use super::state::{SidebarState, MAX_SIDEBAR_WIDTH, MIN_SIDEBAR_WIDTH};
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 SidebarState,
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 {
let step = 40.0_f64;
let signed = match direction {
ChevronStepDirection::Up | ChevronStepDirection::Left => -step,
_ => step,
};
let scroll = state.get_or_insert_scroll("default");
scroll.offset = (scroll.offset + signed).max(0.0);
None
} else {
Some(event)
}
}
DispatchEvent::ResizeHandleDragStarted {
host_id: ref hid,
edge,
} => {
if hid == host_id {
let min_size = MIN_SIDEBAR_WIDTH;
let cap_size = (ctx.viewport.0.max(ctx.viewport.1)).max(MAX_SIDEBAR_WIDTH);
state.start_resize(edge, ctx.frame_rect, ctx.cursor, min_size, cap_size);
None
} else {
Some(event)
}
}
DispatchEvent::ScrollbarTrackClicked { ref track_id } => {
if track_id.0 == format!("{}:scrollbar_track", host_id.0) {
Some(event)
} else {
Some(event)
}
}
DispatchEvent::ScrollbarThumbDragStarted { ref thumb_id } => {
if thumb_id.0 == format!("{}:scrollbar_handle", host_id.0) {
state
.get_or_insert_scroll("default")
.start_drag(ctx.cursor.1);
None
} else {
Some(event)
}
}
_ => Some(event),
}
}
pub fn drag_outcome_sidebar(
state: &SidebarState,
which: &'static str,
sidebar_rect: crate::types::Rect,
est_content_h: f64,
) -> Option<crate::layout::DragOutcome> {
if state.resize_drag.is_some() {
return Some(crate::layout::DragOutcome::SidebarResize { which });
}
if let Some(scroll) = state.scroll_per_panel.get("default") {
if scroll.is_dragging {
let track_rect = SidebarState::scrollbar_track_rect(sidebar_rect);
let viewport_h = track_rect.height;
return Some(crate::layout::DragOutcome::SidebarScrollbar {
track_rect,
content_h: est_content_h,
viewport_h,
});
}
}
None
}