tessera_ui_basic_components/
pos_misc.rs

1//! Contains some convenience functions for positioning and sizing.
2
3use tessera_ui::{ComputedData, Px, PxPosition};
4
5pub fn is_position_in_component(size: ComputedData, position: PxPosition) -> bool {
6    is_position_in_rect(position, PxPosition::ZERO, size.width, size.height)
7}
8
9pub fn is_position_in_rect(
10    position: PxPosition,
11    rect_pos: PxPosition,
12    rect_width: Px,
13    rect_height: Px,
14) -> bool {
15    let x = position.x;
16    let y = position.y;
17    let rect_x = rect_pos.x;
18    let rect_y = rect_pos.y;
19
20    x >= rect_x && x <= rect_x + rect_width && y >= rect_y && y <= rect_y + rect_height
21}
22
23#[test]
24fn test_is_position_in_component() {
25    let size = ComputedData {
26        width: Px(100),
27        height: Px(50),
28    };
29    assert!(is_position_in_component(
30        size,
31        PxPosition::new(Px(50), Px(25))
32    ));
33    assert!(!is_position_in_component(
34        size,
35        PxPosition::new(Px(150), Px(25))
36    ));
37    assert!(!is_position_in_component(
38        size,
39        PxPosition::new(Px(50), Px(75))
40    ));
41}