Skip to content

Truly independent web browser

3.3 KB
Raw Blame
/*
 * Copyright (c) 2024-2026, Tim Flynn <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/RefPtr.h>
#include <LibGC/Ptr.h>
#include <LibJS/Heap/Cell.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/DragDataStore.h>
#include <LibWebCommon/Page/DragEvent.h>
#include <LibWebCommon/Page/EventResult.h>
#include <LibWebCommon/PixelUnits.h>

namespace Web {

class DragAndDropEventHandler {
public:
    AK_ALLOC_WITH_KMALLOC;

    void visit_edges(JS::Cell::Visitor& visitor) const;

    bool has_ongoing_drag_and_drop_operation() const { return !m_drag_data_store.is_null(); }

    static GC::Ptr<HTML::HTMLElement> first_draggable_inclusive_ancestor(DOM::Node&);

    EventResult handle_drag_start(JS::Object const& relevant_global_object, GC::Ptr<DOM::Node> drag_target, CSSPixelPoint screen_position, CSSPixelPoint page_offset, CSSPixelPoint client_offset, CSSPixelPoint offset, unsigned button, unsigned buttons, unsigned modifiers, Vector<HTML::SelectedFile> files);
    EventResult handle_drag_move(JS::Object const& relevant_global_object, GC::Ref<DOM::Node>, CSSPixelPoint screen_position, CSSPixelPoint page_offset, CSSPixelPoint client_offset, CSSPixelPoint offset, unsigned button, unsigned buttons, unsigned modifiers);
    EventResult handle_drag_leave(JS::Object const& relevant_global_object, CSSPixelPoint screen_position, CSSPixelPoint page_offset, CSSPixelPoint client_offset, CSSPixelPoint offset, unsigned button, unsigned buttons, unsigned modifiers);
    EventResult handle_drag_cancel(JS::Object const& relevant_global_object, CSSPixelPoint screen_position, CSSPixelPoint page_offset, CSSPixelPoint client_offset, CSSPixelPoint offset, unsigned button, unsigned buttons, unsigned modifiers);
    EventResult handle_drop(JS::Object const& relevant_global_object, CSSPixelPoint screen_position, CSSPixelPoint page_offset, CSSPixelPoint client_offset, CSSPixelPoint offset, unsigned button, unsigned buttons, unsigned modifiers);

    void reset();

private:
    enum class Cancelled {
        No,
        Yes,
    };
    EventResult handle_drag_end(JS::Object const& relevant_global_object, Cancelled, CSSPixelPoint screen_position, CSSPixelPoint page_offset, CSSPixelPoint client_offset, CSSPixelPoint offset, unsigned button, unsigned buttons, unsigned modifiers);

    GC::Ref<HTML::DragEvent> fire_a_drag_and_drop_event(
        JS::Object const& relevant_global_object,
        GC::Ptr<DOM::EventTarget> target,
        Utf16FlyString const& name,
        CSSPixelPoint screen_position,
        CSSPixelPoint page_offset,
        CSSPixelPoint client_offset,
        CSSPixelPoint offset,
        unsigned button,
        unsigned buttons,
        unsigned modifiers,
        GC::Ptr<DOM::EventTarget> related_target = nullptr);

    bool allow_text_drop(GC::Ref<DOM::Node>) const;

    RefPtr<HTML::DragDataStore> m_drag_data_store;

    // https://html.spec.whatwg.org/multipage/dnd.html#source-node
    GC::Ptr<DOM::EventTarget> m_source_node;

    // https://html.spec.whatwg.org/multipage/dnd.html#immediate-user-selection
    GC::Ptr<DOM::Node> m_immediate_user_selection;

    // https://html.spec.whatwg.org/multipage/dnd.html#current-target-element
    GC::Ptr<DOM::Node> m_current_target_element;

    // https://html.spec.whatwg.org/multipage/dnd.html#current-drag-operation
    Utf16FlyString m_current_drag_operation;
};

}