Skip to main content

hypersig_desktop/
frame.rs

1//! The window's frame, and the parts of a page that move a window without one.
2
3use hypersig::html::{Attr, El};
4
5/// What the system draws around a window, for [`App::frame`](crate::App::frame).
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
7#[non_exhaustive]
8pub enum Frame {
9    /// The system's title bar and border, with its close, minimize, and
10    /// maximize buttons.
11    #[default]
12    System,
13    /// No title bar and no border. The page draws its own, marks where the
14    /// person drags the window with [`MovesWindow::moves_window`], and closes
15    /// or places the window through [`Window`](crate::Window).
16    None,
17}
18
19/// The attribute [`MovesWindow::moves_window`] writes, which [`SCRIPT`] looks for.
20const MOVES: Attr = hypersig::attr!("data-hypersig-moves-window");
21
22/// What the page sends the application when the person presses a part that
23/// moves the window.
24pub(crate) const DRAG: &str = "drag";
25
26/// What the page sends when the person double-clicks such a part.
27pub(crate) const ZOOM: &str = "zoom";
28
29/// The script every page of a window runs before its own, which asks the
30/// application to move the window when the left button goes down on a part
31/// [`MovesWindow::moves_window`] marked, and to maximize or restore it on a
32/// double click there. A control inside that part, such as a button, keeps
33/// its own clicks.
34pub(crate) const SCRIPT: &str = r#"addEventListener("mousedown", (event) => {
35  if (event.button !== 0 || !(event.target instanceof Element)) return;
36  const part = event.target.closest("[data-hypersig-moves-window]");
37  if (part === null) return;
38  const control = event.target.closest("a, button, input, select, textarea, label, [contenteditable], [tabindex]");
39  if (control !== null && part.contains(control)) return;
40  event.preventDefault();
41  window.ipc.postMessage(event.detail === 2 ? "zoom" : "drag");
42});"#;
43
44/// A part of a page that moves the window when the person drags it.
45///
46/// It is implemented for hypersig's [`El`] alone.
47pub trait MovesWindow: Sized + sealed::Sealed {
48    /// Makes the person's drag on this element move the window.
49    ///
50    /// It is for a window of [`Frame::None`], whose page draws its own title
51    /// bar. A double click maximizes the window or restores it, as on the
52    /// system's title bar. A link, a button, a form control, or an element
53    /// with `tabindex` inside the element keeps its clicks, so the bar can
54    /// hold the window's own buttons. In a browser, the attribute does
55    /// nothing.
56    ///
57    /// ```
58    /// use hypersig::html::{div, span};
59    /// use hypersig_desktop::MovesWindow as _;
60    ///
61    /// let bar = div::<()>().moves_window().child(span().text("Notes"));
62    /// assert!(bar.render().contains("data-hypersig-moves-window"));
63    /// ```
64    #[must_use]
65    fn moves_window(self) -> Self;
66}
67
68mod sealed {
69    /// Keeps [`MovesWindow`](super::MovesWindow) to the types this crate implements it for.
70    pub trait Sealed {}
71
72    impl<S> Sealed for hypersig::html::El<S> {}
73}
74
75impl<S> MovesWindow for El<S> {
76    fn moves_window(self) -> Self {
77        self.attr(MOVES, "")
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use hypersig::html::div;
84
85    use super::{DRAG, MovesWindow as _, SCRIPT, ZOOM};
86
87    #[test]
88    fn the_script_sends_the_messages_the_application_answers() {
89        assert!(SCRIPT.contains(&format!("\"{ZOOM}\" : \"{DRAG}\"")));
90    }
91
92    #[test]
93    fn the_script_looks_for_the_attribute_the_method_writes() {
94        let html = div::<()>().moves_window().render();
95        assert_eq!(html, r#"<div data-hypersig-moves-window=""></div>"#);
96        assert!(SCRIPT.contains("[data-hypersig-moves-window]"));
97    }
98}