mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-23 08:00:20 +00:00
LibGUI: Preserve selection when resorting a GSortingProxyTableModel.
This commit is contained in:
parent
0b32ab12f1
commit
48e4b3e751
Notes:
sideshowbarker
2024-07-19 15:05:49 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/48e4b3e7515
1 changed files with 10 additions and 1 deletions
|
@ -28,7 +28,6 @@ int GSortingProxyTableModel::column_count() const
|
|||
|
||||
GModelIndex GSortingProxyTableModel::map_to_target(const GModelIndex& index) const
|
||||
{
|
||||
ASSERT(!m_row_mappings.is_empty());
|
||||
if (!index.is_valid())
|
||||
return { };
|
||||
if (index.row() >= row_count() || index.column() >= column_count())
|
||||
|
@ -79,6 +78,7 @@ void GSortingProxyTableModel::set_key_column_and_sort_order(int column, GSortOrd
|
|||
|
||||
void GSortingProxyTableModel::resort()
|
||||
{
|
||||
int previously_selected_target_row = map_to_target(selected_index()).row();
|
||||
int row_count = target().row_count();
|
||||
m_row_mappings.resize(row_count);
|
||||
for (int i = 0; i < row_count; ++i)
|
||||
|
@ -93,6 +93,15 @@ void GSortingProxyTableModel::resort()
|
|||
bool is_less_than = data1 < data2;
|
||||
return m_sort_order == GSortOrder::Ascending ? is_less_than : !is_less_than;
|
||||
});
|
||||
if (previously_selected_target_row != -1) {
|
||||
// Preserve selection.
|
||||
for (int i = 0; i < row_count; ++i) {
|
||||
if (m_row_mappings[i] == previously_selected_target_row) {
|
||||
set_selected_index({ i, 0 });
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
did_update();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue