mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
LibGUI: Add granular ColumnView helpers for column and index at events
This patch extracts logic for getting colum at an event position and for getting an index in a column at an event position from index_at_event_position into separate functions.
This commit is contained in:
parent
707b4f83eb
commit
f0a20fc902
Notes:
sideshowbarker
2024-07-17 07:31:24 +09:00
Author: https://github.com/networkException Commit: https://github.com/SerenityOS/serenity/commit/f0a20fc902 Pull-request: https://github.com/SerenityOS/serenity/pull/15128 Reviewed-by: https://github.com/linusg
2 changed files with 30 additions and 9 deletions
|
@ -206,7 +206,7 @@ void ColumnsView::update_column_sizes()
|
||||||
set_content_size({ total_width, total_height });
|
set_content_size({ total_width, total_height });
|
||||||
}
|
}
|
||||||
|
|
||||||
ModelIndex ColumnsView::index_at_event_position(Gfx::IntPoint const& a_position) const
|
Optional<ColumnsView::Column> ColumnsView::column_at_event_position(Gfx::IntPoint const& a_position) const
|
||||||
{
|
{
|
||||||
if (!model())
|
if (!model())
|
||||||
return {};
|
return {};
|
||||||
|
@ -215,7 +215,7 @@ ModelIndex ColumnsView::index_at_event_position(Gfx::IntPoint const& a_position)
|
||||||
|
|
||||||
int column_x = 0;
|
int column_x = 0;
|
||||||
|
|
||||||
for (auto& column : m_columns) {
|
for (auto const& column : m_columns) {
|
||||||
if (position.x() < column_x)
|
if (position.x() < column_x)
|
||||||
break;
|
break;
|
||||||
if (position.x() > column_x + column.width) {
|
if (position.x() > column_x + column.width) {
|
||||||
|
@ -223,12 +223,7 @@ ModelIndex ColumnsView::index_at_event_position(Gfx::IntPoint const& a_position)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
int row = position.y() / item_height();
|
return column;
|
||||||
int row_count = model()->row_count(column.parent_index);
|
|
||||||
if (row >= row_count)
|
|
||||||
return {};
|
|
||||||
|
|
||||||
return model()->index(row, m_model_column, column.parent_index);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
|
@ -248,6 +243,25 @@ void ColumnsView::select_range(ModelIndex const& index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ModelIndex ColumnsView::index_at_event_position_in_column(Gfx::IntPoint const& position, Column const& column) const
|
||||||
|
{
|
||||||
|
int row = position.y() / item_height();
|
||||||
|
int row_count = model()->row_count(column.parent_index);
|
||||||
|
if (row >= row_count)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
return model()->index(row, m_model_column, column.parent_index);
|
||||||
|
}
|
||||||
|
|
||||||
|
ModelIndex ColumnsView::index_at_event_position(Gfx::IntPoint const& position) const
|
||||||
|
{
|
||||||
|
auto const& column = column_at_event_position(position);
|
||||||
|
if (!column.has_value())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
return index_at_event_position_in_column(position, *column);
|
||||||
|
}
|
||||||
|
|
||||||
void ColumnsView::mousedown_event(MouseEvent& event)
|
void ColumnsView::mousedown_event(MouseEvent& event)
|
||||||
{
|
{
|
||||||
AbstractView::mousedown_event(event);
|
AbstractView::mousedown_event(event);
|
||||||
|
@ -258,7 +272,11 @@ void ColumnsView::mousedown_event(MouseEvent& event)
|
||||||
if (event.button() != MouseButton::Primary)
|
if (event.button() != MouseButton::Primary)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
auto index = index_at_event_position(event.position());
|
auto column = column_at_event_position(event.position());
|
||||||
|
if (!column.has_value())
|
||||||
|
return;
|
||||||
|
|
||||||
|
auto index = index_at_event_position_in_column(event.position(), *column);
|
||||||
if (index.is_valid() && !(event.modifiers() & Mod_Ctrl)) {
|
if (index.is_valid() && !(event.modifiers() & Mod_Ctrl)) {
|
||||||
if (model()->row_count(index))
|
if (model()->row_count(index))
|
||||||
push_column(index);
|
push_column(index);
|
||||||
|
|
|
@ -48,6 +48,9 @@ private:
|
||||||
// TODO: per-column vertical scroll?
|
// TODO: per-column vertical scroll?
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Optional<Column> column_at_event_position(Gfx::IntPoint const&) const;
|
||||||
|
ModelIndex index_at_event_position_in_column(Gfx::IntPoint const&, Column const&) const;
|
||||||
|
|
||||||
Vector<Column> m_columns;
|
Vector<Column> m_columns;
|
||||||
int m_model_column { 0 };
|
int m_model_column { 0 };
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue