1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-16 19:15:07 +00:00

LibGUI: Make AbstractView accept drags (and delegate to model)

This commit is contained in:
Andreas Kling 2021-01-09 11:36:49 +01:00
parent 7baaa34490
commit b08ed1b560
4 changed files with 50 additions and 21 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -74,6 +74,7 @@ void AbstractView::model_did_update(unsigned int flags)
m_edit_index = {};
m_hovered_index = {};
m_cursor_index = {};
m_drop_candidate_index = {};
clear_selection();
} else {
// FIXME: These may no longer point to whatever they did before,
@ -86,6 +87,8 @@ void AbstractView::model_did_update(unsigned int flags)
m_hovered_index = {};
if (!model()->is_valid(m_cursor_index))
m_cursor_index = {};
if (!model()->is_valid(m_drop_candidate_index))
m_drop_candidate_index = {};
selection().remove_matching([this](auto& index) { return !model()->is_valid(index); });
}
}
@ -720,4 +723,43 @@ void AbstractView::focusin_event(FocusEvent& event)
}
}
void AbstractView::drag_enter_event(DragEvent& event)
{
if (!model())
return;
// NOTE: Right now, AbstractView always accepts drags since we won't get "drag move" events
// unless we accept the "drag enter" event.
// We might be able to reduce event traffic by communicating the set of drag-accepting
// rects in this widget to the windowing system somehow.
event.accept();
dbgln("accepting drag of {}", event.mime_types().first());
}
void AbstractView::drag_move_event(DragEvent& event)
{
if (!model())
return;
auto index = index_at_event_position(event.position());
ModelIndex new_drop_candidate_index;
if (index.is_valid()) {
bool acceptable = model()->accepts_drag(index, event.mime_types());
if (acceptable)
new_drop_candidate_index = index;
}
if (m_drop_candidate_index != new_drop_candidate_index) {
m_drop_candidate_index = new_drop_candidate_index;
update();
}
if (m_drop_candidate_index.is_valid())
event.accept();
}
void AbstractView::drag_leave_event(Event&)
{
if (m_drop_candidate_index.is_valid()) {
m_drop_candidate_index = {};
update();
}
}
}