use std::any::Any;
use waft_embedder::{Offset, Rect, Size};
use waft_rendering::{BoxConstraints, SingleChildLayoutDelegate};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum SuggestionListPosition {
Below,
Above,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) struct SuggestionListLayout {
pub(super) position: SuggestionListPosition,
pub(super) left: f64,
pub(super) width: f64,
pub(super) max_height: f64,
pub(super) content_element: Rect,
}
pub(super) fn arrange(
text_box: Rect,
content_element: Rect,
bounds: Size,
max_suggestion_list_height: f64,
) -> SuggestionListLayout {
let delta_top = text_box.top;
let delta_bottom = bounds.height() - text_box.bottom;
let (position, available) = if delta_top < delta_bottom {
(SuggestionListPosition::Below, delta_bottom.abs())
} else {
(SuggestionListPosition::Above, delta_top)
};
let available = available.max(0.0);
let max_height = if (available > 0.0 && max_suggestion_list_height > available)
|| max_suggestion_list_height < 0.0
{
available
} else {
max_suggestion_list_height
};
SuggestionListLayout {
position,
left: text_box.left,
width: text_box.width(),
max_height,
content_element,
}
}
#[derive(Clone, Debug, PartialEq)]
pub(super) struct SuggestionsPosition(pub(super) SuggestionListLayout);
impl SingleChildLayoutDelegate for SuggestionsPosition {
fn get_constraints_for_child(&self, _constraints: BoxConstraints) -> BoxConstraints {
let layout = self.0;
BoxConstraints::new()
.min_width(layout.width)
.max_width(layout.width)
.max_height(layout.max_height)
}
fn get_position_for_child(&self, _size: Size, child_size: Size) -> Offset {
let layout = self.0;
let top = match layout.position {
SuggestionListPosition::Below => layout.content_element.bottom,
SuggestionListPosition::Above => layout.content_element.top - child_size.height(),
};
Offset::new(layout.left, top)
}
fn should_relayout(&self, old_delegate: &dyn SingleChildLayoutDelegate) -> bool {
old_delegate
.as_any()
.downcast_ref::<Self>()
.is_none_or(|old| old != self)
}
fn as_any(&self) -> &dyn Any {
self
}
}
#[cfg(test)]
mod tests {
use super::*;
fn content(text_box: Rect) -> Rect {
Rect::from_ltrb(
text_box.left + 1.0,
text_box.top + 1.0,
text_box.right - 1.0,
text_box.bottom - 1.0,
)
}
#[test]
fn the_list_opens_on_the_side_with_more_room_and_takes_at_most_that_room() {
let window = Size::new(800.0, 600.0);
let high = Rect::from_ltwh(10.0, 100.0, 300.0, 32.0);
let below = arrange(high, content(high), window, f64::INFINITY);
assert_eq!(below.position, SuggestionListPosition::Below);
assert_eq!(below.max_height, 600.0 - 132.0);
assert_eq!((below.left, below.width), (10.0, 300.0));
let low = Rect::from_ltwh(10.0, 500.0, 300.0, 32.0);
let above = arrange(low, content(low), window, 200.0);
assert_eq!(above.position, SuggestionListPosition::Above);
assert_eq!(above.max_height, 200.0);
}
#[test]
fn a_negative_max_height_asks_for_all_the_room() {
let window = Size::new(800.0, 600.0);
let text_box = Rect::from_ltwh(0.0, 0.0, 300.0, 32.0);
let layout = arrange(text_box, content(text_box), window, -1.0);
assert_eq!(layout.max_height, 568.0);
}
}