1
//! Where the suggestion list opens: below or above its input, whichever
2
//! side of the visible area (the visual viewport, which an on-screen
3
//! keyboard shrinks) has room, never taller than that room.
4

            
5
use wasm_bindgen::prelude::*;
6
use web_sys::{
7
    EventTarget, HtmlElement, HtmlInputElement, ScrollIntoViewOptions, ScrollLogicalPosition,
8
    Window,
9
};
10

            
11
pub(crate) const MAX_HEIGHT: f64 = 200.0;
12
/// About three suggestions.
13
pub(crate) const MIN_HEIGHT: f64 = 108.0;
14
const MARGIN: f64 = 8.0;
15
const ABOVE: &str = "above";
16

            
17
/// A vertical extent in layout-viewport coordinates.
18
#[derive(Debug, Clone, Copy, PartialEq)]
19
pub(crate) struct Span {
20
    pub top: f64,
21
    pub bottom: f64,
22
}
23

            
24
#[derive(Debug, PartialEq)]
25
pub(crate) struct Placement {
26
    pub above: bool,
27
    pub max_height: f64,
28
}
29

            
30
8
fn rooms(input: Span, view: Span) -> (f64, f64) {
31
8
    (input.top - view.top, view.bottom - input.bottom)
32
8
}
33

            
34
/// Whether either side of `input` fits the smallest useful list.
35
2
pub(crate) fn roomy(input: Span, view: Span) -> bool {
36
2
    let (above, below) = rooms(input, view);
37
2
    above.max(below) - MARGIN >= MIN_HEIGHT
38
2
}
39

            
40
/// The side a list `list` pixels tall opens on, and its height cap.
41
6
pub(crate) fn placement(input: Span, view: Span, list: f64) -> Placement {
42
6
    let (above, below) = rooms(input, view);
43
6
    let opens_above = below < list.min(MAX_HEIGHT) && below < above;
44
6
    let room = if opens_above { above } else { below } - MARGIN;
45
6
    Placement {
46
6
        above: opens_above,
47
6
        max_height: room.clamp(MIN_HEIGHT, MAX_HEIGHT),
48
6
    }
49
6
}
50

            
51
fn input_span(input: &HtmlInputElement) -> Span {
52
    let rect = input.get_bounding_client_rect();
53
    Span {
54
        top: rect.top(),
55
        bottom: rect.bottom(),
56
    }
57
}
58

            
59
fn viewport(window: &Window) -> Span {
60
    match window.visual_viewport() {
61
        Some(view) => Span {
62
            top: view.offset_top(),
63
            bottom: view.offset_top() + view.height(),
64
        },
65
        None => Span {
66
            top: 0.0,
67
            bottom: window
68
                .inner_height()
69
                .ok()
70
                .and_then(|h| h.as_f64())
71
                .unwrap_or_default(),
72
        },
73
    }
74
}
75

            
76
fn center(input: &HtmlInputElement) {
77
    let options = ScrollIntoViewOptions::new();
78
    options.set_block(ScrollLogicalPosition::Center);
79
    input.scroll_into_view_with_scroll_into_view_options(&options);
80
}
81

            
82
fn apply(popup: &HtmlElement, input: &HtmlInputElement, at: &Placement) -> Result<(), JsValue> {
83
    let style = popup.style();
84
    style.set_property("max-height", &format!("{}px", at.max_height))?;
85
    match (at.above, popup.parent_element()) {
86
        (true, Some(field)) => {
87
            popup.class_list().add_1(ABOVE)?;
88
            let bottom = field.client_height() - input.offset_top();
89
            style.set_property("bottom", &format!("{bottom}px"))
90
        }
91
        _ => {
92
            popup.class_list().remove_1(ABOVE)?;
93
            style.remove_property("bottom").map(drop)
94
        }
95
    }
96
}
97

            
98
/// Fit the open `popup` of `input` into the visible area, first scrolling
99
/// the input to the middle when neither side has room.
100
pub(crate) fn fit(popup: &HtmlElement, input: &HtmlInputElement) -> Result<(), JsValue> {
101
    let window = web_sys::window().ok_or("no window")?;
102
    if popup.child_element_count() == 0 {
103
        return Ok(());
104
    }
105
    if !roomy(input_span(input), viewport(&window)) {
106
        center(input);
107
    }
108
    let at = placement(
109
        input_span(input),
110
        viewport(&window),
111
        f64::from(popup.scroll_height()),
112
    );
113
    apply(popup, input, &at)
114
}
115

            
116
/// The targets and events that move or shrink the visible area.
117
pub(crate) fn viewport_events() -> Vec<(EventTarget, &'static str)> {
118
    let Some(window) = web_sys::window() else {
119
        return Vec::new();
120
    };
121
    let mut events = vec![(EventTarget::from(window.clone()), "scroll")];
122
    if let Some(view) = window.visual_viewport() {
123
        let view = EventTarget::from(view);
124
        events.push((view.clone(), "resize"));
125
        events.push((view, "scroll"));
126
    }
127
    events
128
}
129

            
130
#[cfg(test)]
131
#[path = "place_tests.rs"]
132
mod tests;