use waft_embedder::{Offset, Rect, clamp_double};
use waft_foundation::App;
use crate::framework::{BuildContext, InheritedWidget, IntoWidget, KeyRef, WidgetRef};
pub trait DragBoundaryDelegate<T> {
fn is_within_boundary(&self, dragged_object: T) -> bool;
fn nearest_position_within_boundary(&self, dragged_object: T) -> T;
}
struct DragBoundaryDelegateForRect {
boundary: Option<Rect>,
}
impl DragBoundaryDelegateForRect {
fn new(boundary: Option<Rect>) -> DragBoundaryDelegateForRect {
DragBoundaryDelegateForRect { boundary }
}
}
impl DragBoundaryDelegate<Rect> for DragBoundaryDelegateForRect {
fn is_within_boundary(&self, dragged_object: Rect) -> bool {
let Some(boundary) = self.boundary else {
return true;
};
boundary.contains(dragged_object.top_left())
&& boundary.contains(dragged_object.bottom_right())
}
fn nearest_position_within_boundary(&self, dragged_object: Rect) -> Rect {
let Some(boundary) = self.boundary else {
return dragged_object;
};
if boundary.right - dragged_object.width() < boundary.left
|| boundary.bottom - dragged_object.height() < boundary.top
{
panic!(
"The rect is larger than the boundary. The rect width must be less than the \
boundary width, and the rect height must be less than the boundary height."
);
}
let left = clamp_double(
dragged_object.left,
boundary.left,
boundary.right - dragged_object.width(),
);
let top = clamp_double(
dragged_object.top,
boundary.top,
boundary.bottom - dragged_object.height(),
);
Rect::from_ltwh(left, top, dragged_object.width(), dragged_object.height())
}
}
#[derive(Debug)]
pub struct DragBoundary {
pub key: Option<KeyRef>,
pub child: WidgetRef,
}
impl DragBoundary {
#[cfg_attr(debug_assertions, track_caller)]
pub fn new<K>(child: impl IntoWidget<K>) -> DragBoundary {
DragBoundary {
key: None,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> DragBoundary {
self.key = Some(key);
self
}
pub fn for_rect_of(
app: &App,
context: BuildContext,
use_global_position: bool,
) -> Box<dyn DragBoundaryDelegate<Rect>> {
DragBoundary::for_rect_maybe_of(app, context, use_global_position)
.unwrap_or_else(|| Box::new(DragBoundaryDelegateForRect::new(None)))
}
pub fn for_rect_maybe_of(
app: &App,
context: BuildContext,
use_global_position: bool,
) -> Option<Box<dyn DragBoundaryDelegate<Rect>>> {
let element =
context.get_element_for_inherited_widget_of_exact_type::<DragBoundary>(app)?;
let rb = element
.find_render_object(app)
.and_then(|render_object| render_object.as_box());
debug_assert!(
rb.is_some_and(|rb| rb.has_size(app)),
"DragBoundary is not available"
);
let rb = rb.expect("DragBoundary is not available");
let size = rb.size(app);
let boundary = if use_global_position {
Rect::from_points(
rb.local_to_global(app, Offset::ZERO, None),
rb.local_to_global(app, size.bottom_right(Offset::ZERO), None),
)
} else {
Offset::ZERO & size
};
Some(Box::new(DragBoundaryDelegateForRect::new(Some(boundary))))
}
}
impl InheritedWidget for DragBoundary {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, _old_widget: &DragBoundary) -> bool {
true
}
}
#[cfg(test)]
mod tests {
use waft_painting::{AlignmentGeometry, EdgeInsetsGeometry};
use super::*;
use crate::framework::GlobalKey;
use crate::testing::{WidgetTester, test_widgets};
use crate::widgets::basic::SizedBox;
use crate::widgets::container::Container;
fn pump_box(tester: &mut WidgetTester, key: &GlobalKey, boundary: bool) {
let sized_box = SizedBox::new()
.key(key.clone().into())
.width(100.0)
.height(100.0);
let container = Container::new()
.margin(EdgeInsetsGeometry::only(100.0, 100.0, 0.0, 0.0))
.alignment(AlignmentGeometry::TOP_LEFT);
if boundary {
tester.pump_widget(container.child(DragBoundary::new(sized_box)));
} else {
tester.pump_widget(container.child(sized_box));
}
}
fn for_rect_of(
tester: &WidgetTester,
key: &GlobalKey,
use_global_position: bool,
) -> Box<dyn DragBoundaryDelegate<Rect>> {
tester.update(|app| {
let context = key.current_context(app).expect("mounted");
DragBoundary::for_rect_of(app, context, use_global_position)
})
}
#[test]
fn test_drag_boundary_with_use_global_position() {
test_widgets(|tester| {
let key = GlobalKey::new();
pump_box(tester, &key, true);
let boundary = for_rect_of(tester, &key, true);
assert!(!boundary.is_within_boundary(Rect::from_ltwh(50.0, 50.0, 20.0, 20.0)));
assert!(boundary.is_within_boundary(Rect::from_ltwh(100.0, 100.0, 20.0, 20.0)));
assert_eq!(
boundary.nearest_position_within_boundary(Rect::from_ltwh(50.0, 50.0, 20.0, 20.0)),
Rect::from_ltwh(100.0, 100.0, 20.0, 20.0)
);
assert_eq!(
boundary
.nearest_position_within_boundary(Rect::from_ltwh(150.0, 150.0, 20.0, 20.0)),
Rect::from_ltwh(150.0, 150.0, 20.0, 20.0)
);
});
}
#[test]
fn test_drag_boundary_without_use_global_position() {
test_widgets(|tester| {
let key = GlobalKey::new();
pump_box(tester, &key, true);
let boundary = for_rect_of(tester, &key, false);
assert!(boundary.is_within_boundary(Rect::from_ltwh(50.0, 50.0, 20.0, 20.0)));
assert!(!boundary.is_within_boundary(Rect::from_ltwh(90.0, 90.0, 20.0, 20.0)));
assert_eq!(
boundary.nearest_position_within_boundary(Rect::from_ltwh(50.0, 50.0, 20.0, 20.0)),
Rect::from_ltwh(50.0, 50.0, 20.0, 20.0)
);
assert_eq!(
boundary.nearest_position_within_boundary(Rect::from_ltwh(90.0, 90.0, 20.0, 20.0)),
Rect::from_ltwh(80.0, 80.0, 20.0, 20.0)
);
});
}
#[test]
fn for_rect_of_returns_a_free_boundary_when_no_ancestor_has_a_drag_boundary() {
test_widgets(|tester| {
let key = GlobalKey::new();
pump_box(tester, &key, false);
let boundary = for_rect_of(tester, &key, true);
assert!(boundary.is_within_boundary(Rect::from_ltwh(50.0, 50.0, 20.0, 20.0)));
assert!(boundary.is_within_boundary(Rect::from_ltwh(100.0, 100.0, 20.0, 20.0)));
assert!(boundary.is_within_boundary(Rect::from_ltwh(300.0, 300.0, 300.0, 300.0)));
assert_eq!(
boundary.nearest_position_within_boundary(Rect::from_ltwh(50.0, 50.0, 20.0, 20.0)),
Rect::from_ltwh(50.0, 50.0, 20.0, 20.0)
);
assert_eq!(
boundary
.nearest_position_within_boundary(Rect::from_ltwh(150.0, 150.0, 20.0, 20.0)),
Rect::from_ltwh(150.0, 150.0, 20.0, 20.0)
);
assert_eq!(
boundary
.nearest_position_within_boundary(Rect::from_ltwh(300.0, 300.0, 300.0, 300.0)),
Rect::from_ltwh(300.0, 300.0, 300.0, 300.0)
);
});
}
}