Skip to main content

script/drag/
drag_gesture.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use js::context::JSContext;
6use script_traits::ConstellationInputEvent;
7
8use crate::dom::inputevent::HitTestResult;
9use crate::dom::text_input::TextInputSelectionDragHandler;
10use crate::drag::document_selection_drag::DocumentSelectionDragHandler;
11
12#[derive(JSTraceable, MallocSizeOf)]
13#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
14pub(crate) struct DragGesture {
15    handler: DragHandler,
16}
17
18impl DragGesture {
19    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
20    pub(crate) fn new(handler: DragHandler) -> Self {
21        Self { handler }
22    }
23
24    pub(crate) fn handle_mouse_button_event(&self, event: &ConstellationInputEvent) -> bool {
25        event.primary_button_is_pressed() &&
26            match &self.handler {
27                DragHandler::TextInputSelection(handler) => handler.still_connected(),
28                DragHandler::DocumentSelection(handler) => handler.still_connected(),
29            }
30    }
31
32    /// Handle the a mouse move event.
33    ///
34    /// Returns `true` if the `DragGesture` should continue and `false` otherwise.
35    pub(crate) fn handle_mouse_move_event(
36        &self,
37        cx: &mut JSContext,
38        event: &ConstellationInputEvent,
39        hit_test_result: &HitTestResult,
40    ) -> bool {
41        if !event.primary_button_is_pressed() {
42            return false;
43        }
44        match &self.handler {
45            DragHandler::TextInputSelection(handler) => handler.moved(hit_test_result),
46            DragHandler::DocumentSelection(handler) => handler.moved(cx, hit_test_result),
47        }
48    }
49
50    pub(crate) fn need_dom_position_from_hit_test(&self) -> bool {
51        matches!(self.handler, DragHandler::DocumentSelection(..))
52    }
53}
54
55#[derive(JSTraceable, MallocSizeOf)]
56#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
57pub(crate) enum DragHandler {
58    TextInputSelection(TextInputSelectionDragHandler),
59    DocumentSelection(DocumentSelectionDragHandler),
60}