mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 04:25:13 +00:00
LibGUI: Add GItemView::index_at_event_position()
This replaces GItemView::item_at_event_position(), which used to return a raw int, requiring callers to call model()->index() with it again.
This commit is contained in:
parent
2ccad40a16
commit
ff66101f9e
Notes:
sideshowbarker
2024-07-19 09:53:19 +09:00
Author: https://github.com/bugaevc Commit: https://github.com/SerenityOS/serenity/commit/ff66101f9e0 Pull-request: https://github.com/SerenityOS/serenity/pull/1115
2 changed files with 12 additions and 15 deletions
|
@ -115,7 +115,7 @@ Vector<int> GItemView::items_intersecting_rect(const Rect& rect) const
|
|||
return item_indexes;
|
||||
}
|
||||
|
||||
int GItemView::item_at_event_position(const Point& position) const
|
||||
GModelIndex GItemView::index_at_event_position(const Point& position) const
|
||||
{
|
||||
ASSERT(model());
|
||||
// FIXME: Since all items are the same size, just compute the clicked item index
|
||||
|
@ -127,21 +127,22 @@ int GItemView::item_at_event_position(const Point& position) const
|
|||
Rect item_rect;
|
||||
Rect icon_rect;
|
||||
Rect text_rect;
|
||||
auto item_text = model()->data(model()->index(item_index, model_column()));
|
||||
auto index = model()->index(item_index, model_column());
|
||||
auto item_text = model()->data(index);
|
||||
get_item_rects(item_index, font, item_text, item_rect, icon_rect, text_rect);
|
||||
if (icon_rect.contains(adjusted_position) || text_rect.contains(adjusted_position))
|
||||
return item_index;
|
||||
return index;
|
||||
}
|
||||
return -1;
|
||||
return {};
|
||||
}
|
||||
|
||||
void GItemView::mousedown_event(GMouseEvent& event)
|
||||
{
|
||||
int item_index = item_at_event_position(event.position());
|
||||
auto index = index_at_event_position(event.position());
|
||||
|
||||
if (event.button() == GMouseButton::Left) {
|
||||
m_left_mousedown_position = event.position();
|
||||
if (item_index == -1) {
|
||||
if (!index.is_valid()) {
|
||||
if (event.modifiers() & Mod_Ctrl) {
|
||||
selection().for_each_index([&](auto& index) {
|
||||
m_rubber_band_remembered_selection.append(index);
|
||||
|
@ -153,7 +154,6 @@ void GItemView::mousedown_event(GMouseEvent& event)
|
|||
m_rubber_band_origin = event.position();
|
||||
m_rubber_band_current = event.position();
|
||||
} else {
|
||||
auto index = model()->index(item_index, m_model_column);
|
||||
if (event.modifiers() & Mod_Ctrl)
|
||||
selection().toggle(index);
|
||||
else if (selection().size() > 1)
|
||||
|
@ -174,9 +174,8 @@ void GItemView::mouseup_event(GMouseEvent& event)
|
|||
update();
|
||||
return;
|
||||
}
|
||||
int item_index = item_at_event_position(event.position());
|
||||
if (item_index >= 0) {
|
||||
auto index = model()->index(item_index, m_model_column);
|
||||
auto index = index_at_event_position(event.position());
|
||||
if (index.is_valid()) {
|
||||
if ((selection().size() > 1) & m_might_drag) {
|
||||
selection().set(index);
|
||||
m_might_drag = false;
|
||||
|
@ -266,10 +265,8 @@ void GItemView::context_menu_event(GContextMenuEvent& event)
|
|||
{
|
||||
if (!model())
|
||||
return;
|
||||
auto item_index = item_at_event_position(event.position());
|
||||
GModelIndex index;
|
||||
if (item_index != -1) {
|
||||
index = model()->index(item_index, m_model_column);
|
||||
auto index = index_at_event_position(event.position());
|
||||
if (index.is_valid()) {
|
||||
if (!selection().contains(index))
|
||||
selection().set(index);
|
||||
} else {
|
||||
|
|
|
@ -64,7 +64,7 @@ private:
|
|||
|
||||
int item_count() const;
|
||||
Rect item_rect(int item_index) const;
|
||||
int item_at_event_position(const Point&) const;
|
||||
GModelIndex index_at_event_position(const Point&) const;
|
||||
Vector<int> items_intersecting_rect(const Rect&) const;
|
||||
void update_content_size();
|
||||
void get_item_rects(int item_index, const Font&, const GVariant& item_text, Rect& item_rect, Rect& icon_rect, Rect& text_rect) const;
|
||||
|
|
Loading…
Add table
Reference in a new issue