use crate::core::{Action, FocusHandle};
use crate::view::div::Div;
use crate::view::interactivity::{
ActionEvent, ClickEvent, DragEvent, EventContext, KeyDownEvent, ScrollEvent, TextInputEvent,
};
use crate::view::key_context::KeyContext;
use crate::view::style::Style;
use std::rc::Rc;
impl Div {
pub fn id(mut self, id: impl Into<String>) -> Self {
self.interactivity.id = Some(id.into());
self
}
pub fn hover(mut self, refine: impl FnOnce(Style) -> Style) -> Self {
self.interactivity.hover_style = Some(refine(Style::default()));
self
}
pub fn active(mut self, refine: impl FnOnce(Style) -> Style) -> Self {
self.interactivity.active_style = Some(refine(Style::default()));
self
}
pub fn focus(mut self, refine: impl FnOnce(Style) -> Style) -> Self {
self.interactivity.focus_style = Some(refine(Style::default()));
self
}
pub fn on_click(mut self, handler: impl Fn(&ClickEvent, &mut EventContext) + 'static) -> Self {
self.interactivity.on_click = Some(Rc::new(handler));
self
}
pub fn on_mouse_down(mut self, handler: impl Fn(&ClickEvent, &mut EventContext) + 'static) -> Self {
self.interactivity.on_mouse_down = Some(Rc::new(handler));
self
}
pub fn on_right_click(mut self, handler: impl Fn(&ClickEvent, &mut EventContext) + 'static) -> Self {
self.interactivity.on_right_click = Some(Rc::new(handler));
self
}
pub fn on_scroll(mut self, handler: impl Fn(&ScrollEvent, &mut EventContext) + 'static) -> Self {
self.interactivity.on_scroll = Some(Rc::new(handler));
self
}
pub fn on_drag(mut self, handler: impl Fn(&DragEvent, &mut EventContext) + 'static) -> Self {
self.interactivity.on_drag = Some(Rc::new(handler));
self
}
pub fn on_mouse_move(mut self, handler: impl Fn(&DragEvent, &mut EventContext) + 'static) -> Self {
self.interactivity.on_mouse_move = Some(Rc::new(handler));
self
}
pub fn record_bounds(mut self, cell: Rc<std::cell::Cell<crate::core::Bounds>>) -> Self {
self.interactivity.record_bounds = Some(cell);
self
}
pub fn tooltip(mut self, text: impl Into<String>) -> Self {
let text: String = text.into();
self.interactivity.tooltip = Some(Rc::from(text));
self
}
pub fn track_focus(mut self, handle: &FocusHandle) -> Self {
self.interactivity.focus = Some(*handle);
self
}
pub fn tab_index(mut self, index: i32) -> Self {
self.interactivity.tab_index = Some(index);
self
}
pub fn key_context(mut self, context: &str) -> Self {
self.interactivity.key_context = Some(KeyContext::parse(context));
self
}
pub fn on_action(mut self, action: Action, handler: impl Fn(&ActionEvent, &mut EventContext) + 'static) -> Self {
self.interactivity.actions.push((action, Rc::new(handler)));
self
}
pub fn on_key_down(mut self, handler: impl Fn(&KeyDownEvent, &mut EventContext) + 'static) -> Self {
self.interactivity.on_key_down = Some(Rc::new(handler));
self
}
pub fn on_text_input(mut self, handler: impl Fn(&TextInputEvent, &mut EventContext) + 'static) -> Self {
self.interactivity.on_text_input = Some(Rc::new(handler));
self
}
pub fn refine_style(mut self, style: &Style) -> Self {
self.style.refine(style);
self
}
pub fn occlude(mut self) -> Self {
self.interactivity.blocks_mouse = true;
self
}
pub fn when(self, condition: bool, build: impl FnOnce(Self) -> Self) -> Self {
if condition { build(self) } else { self }
}
pub fn when_some<T>(self, value: Option<T>, build: impl FnOnce(Self, T) -> Self) -> Self {
match value {
Some(value) => build(self, value),
None => self,
}
}
}