use waft_embedder::{Offset, Rect, clamp_double};
use waft_foundation::App;
use waft_rendering::{AnyRenderBox, TextSelectionPoint};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TextSelectionToolbarAnchors {
pub primary_anchor: Offset,
pub secondary_anchor: Option<Offset>,
}
impl TextSelectionToolbarAnchors {
pub const fn new(primary_anchor: Offset) -> TextSelectionToolbarAnchors {
TextSelectionToolbarAnchors {
primary_anchor,
secondary_anchor: None,
}
}
pub const fn secondary_anchor(
mut self,
secondary_anchor: Offset,
) -> TextSelectionToolbarAnchors {
self.secondary_anchor = Some(secondary_anchor);
self
}
pub fn from_selection(
app: &App,
render_box: AnyRenderBox,
start_glyph_height: f64,
end_glyph_height: f64,
selection_endpoints: &[TextSelectionPoint],
) -> TextSelectionToolbarAnchors {
let selection_rect = Self::get_selection_rect(
app,
render_box,
start_glyph_height,
end_glyph_height,
selection_endpoints,
);
if selection_rect == Rect::ZERO {
return TextSelectionToolbarAnchors::new(Offset::ZERO);
}
let editing_region = editing_region(app, render_box);
TextSelectionToolbarAnchors::new(Offset::new(
selection_rect.left + selection_rect.width() / 2.0,
clamp_double(
selection_rect.top,
editing_region.top,
editing_region.bottom,
),
))
.secondary_anchor(Offset::new(
selection_rect.left + selection_rect.width() / 2.0,
clamp_double(
selection_rect.bottom,
editing_region.top,
editing_region.bottom,
),
))
}
pub fn get_selection_rect(
app: &App,
render_box: AnyRenderBox,
start_glyph_height: f64,
end_glyph_height: f64,
selection_endpoints: &[TextSelectionPoint],
) -> Rect {
let editing_region = editing_region(app, render_box);
if editing_region.left.is_nan()
|| editing_region.top.is_nan()
|| editing_region.right.is_nan()
|| editing_region.bottom.is_nan()
{
return Rect::ZERO;
}
if selection_endpoints.is_empty() {
return Rect::ZERO;
}
let is_multiline = selection_endpoints.last().unwrap().point.dy()
- selection_endpoints.first().unwrap().point.dy()
> end_glyph_height / 2.0;
Rect::from_ltrb(
if is_multiline {
editing_region.left
} else {
editing_region.left + selection_endpoints.first().unwrap().point.dx()
},
editing_region.top + selection_endpoints.first().unwrap().point.dy()
- start_glyph_height,
if is_multiline {
editing_region.right
} else {
editing_region.left + selection_endpoints.last().unwrap().point.dx()
},
editing_region.top + selection_endpoints.last().unwrap().point.dy(),
)
}
}
fn editing_region(app: &App, render_box: AnyRenderBox) -> Rect {
Rect::from_points(
render_box.local_to_global(app, Offset::ZERO, None),
render_box.local_to_global(app, render_box.size(app).bottom_right(Offset::ZERO), None),
)
}