Repository: servo/servo
File: components/script/textinput.rs (Rust)

Code before:
            SelectionDirection::Forward,
        );
        KeyReaction::DispatchInput(
            Some(insertion),
            IsComposing::Composing,
            InputType::InsertCompositionText,
        )
    }

    fn edit_point_for_mouse_event(&self, node: &Node, event: &MouseEvent) -> RopeIndex {
        node.owner_window()
            .text_index_query_on_node_for_event(node, event)
            .map(|grapheme_index| {
                self.rope.move_by(
                    Default::default(),
                    RopeMovement::Grapheme,
                    grapheme_index as isize,
                )
            })
            .unwrap_or_else(|| self.rope.last_index())
    }

    pub(crate) fn handle_mouse_event(
        &mut self,
        node: &Node,
        event: &Event,
        mouse_event: &MouseEvent,
    ) -> bool {
        // Cancel any ongoing drags if we see a mouseup of any kind or notice
        // that a button other than the primary button is pressed.
        if event.type_() == atom!("mouseup") || mouse_event.Buttons() & 1 != 1 {
            self.currently_dragging = false;
        }

        if event.type_() == atom!("mousedown") {
            return self.handle_mousedown(node, event, mouse_event);
        }

        if event.type_() == atom!("mousemove") && self.currently_dragging {
            self.edit_point = self.edit_point_for_mouse_event(node, mouse_event);
            self.update_selection_direction();
            return true;
        }

        false
    }

    /// Handle a "mousedown" event that happened on this [`TextInput`], belonging to the
    /// given [`Node`].
    ///
    /// Returns `true` if the [`TextInput`] changed at all or `false` otherwise.

Review comment (suggestion):
Passing the Event argument here is confusing. Let's upcast the MouseEvebt within the method instead.

Code after:
            SelectionDirection::Forward,
        );
        KeyReaction::DispatchInput(
            Some(insertion),
            IsComposing::Composing,
            InputType::InsertCompositionText,
        )
    }

    fn edit_point_for_mouse_event(&self, node: &Node, event: &MouseEvent) -> RopeIndex {
        node.owner_window()
            .text_index_query_on_node_for_event(node, event)
            .map(|grapheme_index| {
                self.rope.move_by(
                    Default::default(),
                    RopeMovement::Grapheme,
                    grapheme_index as isize,
                )
            })
            .unwrap_or_else(|| self.rope.last_index())
    }

    /// Handle a mouse even that has happened in this [`TextInput`]. Returns `true` if the selection
    /// in the input may have changed and `false` otherwise.
    pub(crate) fn handle_mouse_event(&mut self, node: &Node, mouse_event: &MouseEvent) -> bool {
        // Cancel any ongoing drags if we see a mouseup of any kind or notice
        // that a button other than the primary button is pressed.
        let event_type = mouse_event.upcast::<Event>().type_();
        if event_type == atom!("mouseup") || mouse_event.Buttons() & 1 != 1 {
            self.currently_dragging = false;
        }

        if event_type == atom!("mousedown") {
            return self.handle_mousedown(node, mouse_event);
        }

        if event_type == atom!("mousemove") && self.currently_dragging {
            self.edit_point = self.edit_point_for_mouse_event(node, mouse_event);
            self.update_selection_direction();
            return true;
        }

        false
    }

    /// Handle a "mousedown" event that happened on this [`TextInput`], belonging to the
    /// given [`Node`].
    ///
    /// Returns `true` if the [`TextInput`] changed at all or `false` otherwise.
    fn handle_mousedown(&mut self, node: &Node, mouse_event: &MouseEvent) -> bool {
        assert_eq!(mouse_event.upcast::<Event>().type_(), atom!("mousedown"));