pub use crate::types::state::WidgetId;
#[derive(Clone, Debug, Default)]
pub struct FocusState {
pub focused: Option<WidgetId>,
pub pending_focus: Option<WidgetId>,
}
impl FocusState {
pub fn new() -> Self {
Self::default()
}
pub fn set_focus(&mut self, id: WidgetId) {
self.focused = Some(id);
}
pub fn clear_focus(&mut self) {
self.focused = None;
}
pub fn is_focused(&self, id: &WidgetId) -> bool {
self.focused.as_ref() == Some(id)
}
pub fn request_focus(&mut self, id: WidgetId) {
self.pending_focus = Some(id);
}
pub fn process_pending(&mut self) {
if let Some(id) = self.pending_focus.take() {
self.focused = Some(id);
}
}
}
#[derive(Clone, Debug, Default)]
pub struct HoverState {
pub hovered: Option<WidgetId>,
pub mouse_pos: (f64, f64),
pub mouse_pressed: bool,
pub press_origin: Option<WidgetId>,
}
impl HoverState {
pub fn new() -> Self {
Self::default()
}
pub fn update_mouse(&mut self, x: f64, y: f64) {
self.mouse_pos = (x, y);
}
pub fn set_hovered(&mut self, id: Option<WidgetId>) {
self.hovered = id;
}
pub fn is_hovered(&self, id: &WidgetId) -> bool {
self.hovered.as_ref() == Some(id)
}
pub fn set_pressed(&mut self, pressed: bool) {
self.mouse_pressed = pressed;
}
pub fn set_press_origin(&mut self, id: Option<WidgetId>) {
self.press_origin = id;
}
pub fn is_press_origin(&self, id: &WidgetId) -> bool {
self.press_origin.as_ref() == Some(id)
}
}
#[derive(Clone, Debug, Default)]
pub struct DragState {
pub dragging: Option<WidgetId>,
pub start_pos: (f64, f64),
pub current_pos: (f64, f64),
pub offset: (f64, f64),
pub initial_value: f64,
}
impl DragState {
pub fn new() -> Self {
Self::default()
}
pub fn start(&mut self, id: WidgetId, x: f64, y: f64, offset_x: f64, offset_y: f64) {
self.dragging = Some(id);
self.start_pos = (x, y);
self.current_pos = (x, y);
self.offset = (offset_x, offset_y);
}
pub fn start_with_value(&mut self, id: WidgetId, x: f64, y: f64, value: f64) {
self.dragging = Some(id);
self.start_pos = (x, y);
self.current_pos = (x, y);
self.offset = (0.0, 0.0);
self.initial_value = value;
}
pub fn update(&mut self, x: f64, y: f64) {
self.current_pos = (x, y);
}
pub fn end(&mut self) {
self.dragging = None;
}
pub fn is_dragging(&self, id: &WidgetId) -> bool {
self.dragging.as_ref() == Some(id)
}
pub fn delta(&self) -> (f64, f64) {
(
self.current_pos.0 - self.start_pos.0,
self.current_pos.1 - self.start_pos.1,
)
}
pub fn delta_from(&self, last_pos: (f64, f64)) -> (f64, f64) {
(
self.current_pos.0 - last_pos.0,
self.current_pos.1 - last_pos.1,
)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Default)]
pub enum WidgetInteraction {
#[default]
None,
Hover,
Press,
Drag,
Click,
DoubleClick,
TripleClick,
Focus,
}
#[derive(Clone, Debug, Default)]
pub struct WidgetInputState {
pub focus: FocusState,
pub hover: HoverState,
pub drag: DragState,
pub active: Option<WidgetId>,
}
impl WidgetInputState {
pub fn new() -> Self {
Self::default()
}
pub fn update_mouse(&mut self, x: f64, y: f64) {
self.hover.update_mouse(x, y);
if self.drag.dragging.is_some() {
self.drag.update(x, y);
}
}
pub fn start_drag(&mut self, id: WidgetId, x: f64, y: f64) {
self.drag.start(id, x, y, 0.0, 0.0);
}
pub fn start_drag_with_value(&mut self, id: WidgetId, x: f64, y: f64, value: f64) {
self.drag.start_with_value(id, x, y, value);
}
pub fn end_frame(&mut self) {
self.focus.process_pending();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_widget_id() {
let id1 = WidgetId::new("button1");
let id2: WidgetId = "button2".into();
assert_ne!(id1, id2);
}
#[test]
fn test_focus_state() {
let mut focus = FocusState::new();
let id = WidgetId::new("input1");
assert!(!focus.is_focused(&id));
focus.set_focus(id.clone());
assert!(focus.is_focused(&id));
focus.clear_focus();
assert!(!focus.is_focused(&id));
}
#[test]
fn test_hover_state() {
let mut hover = HoverState::new();
let id = WidgetId::new("button1");
hover.update_mouse(100.0, 50.0);
assert_eq!(hover.mouse_pos, (100.0, 50.0));
hover.set_hovered(Some(id.clone()));
assert!(hover.is_hovered(&id));
hover.set_hovered(None);
assert!(!hover.is_hovered(&id));
}
#[test]
fn test_drag_state() {
let mut drag = DragState::new();
let id = WidgetId::new("slider1");
drag.start(id.clone(), 100.0, 50.0, 5.0, 0.0);
assert!(drag.is_dragging(&id));
drag.update(150.0, 60.0);
assert_eq!(drag.delta(), (50.0, 10.0));
drag.end();
assert!(!drag.is_dragging(&id));
}
}