mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:17:44 +00:00
LibGUI/TreeView: Select parent on collapse
When collapsing a tree that contains the current selection, the parent node becomes selected instead.
This commit is contained in:
parent
67a5e9f018
commit
7a4445a1fe
3 changed files with 16 additions and 0 deletions
|
@ -19,6 +19,17 @@ Variant ModelIndex::data(ModelRole role) const
|
||||||
return model()->data(*this, role);
|
return model()->data(*this, role);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ModelIndex::is_parent_of(const ModelIndex& child) const
|
||||||
|
{
|
||||||
|
auto current_index = child.parent();
|
||||||
|
while (current_index.is_valid()) {
|
||||||
|
if (current_index == *this)
|
||||||
|
return true;
|
||||||
|
current_index = current_index.parent();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
ModelIndex ModelIndex::sibling(int row, int column) const
|
ModelIndex ModelIndex::sibling(int row, int column) const
|
||||||
{
|
{
|
||||||
if (!is_valid())
|
if (!is_valid())
|
||||||
|
|
|
@ -26,6 +26,7 @@ public:
|
||||||
void* internal_data() const { return m_internal_data; }
|
void* internal_data() const { return m_internal_data; }
|
||||||
|
|
||||||
ModelIndex parent() const;
|
ModelIndex parent() const;
|
||||||
|
bool is_parent_of(const ModelIndex&) const;
|
||||||
|
|
||||||
bool operator==(const ModelIndex& other) const
|
bool operator==(const ModelIndex& other) const
|
||||||
{
|
{
|
||||||
|
|
|
@ -148,6 +148,10 @@ void TreeView::toggle_index(const ModelIndex& index)
|
||||||
VERIFY(model()->row_count(index));
|
VERIFY(model()->row_count(index));
|
||||||
auto& metadata = ensure_metadata_for_index(index);
|
auto& metadata = ensure_metadata_for_index(index);
|
||||||
metadata.open = !metadata.open;
|
metadata.open = !metadata.open;
|
||||||
|
|
||||||
|
if (!metadata.open && index.is_parent_of(cursor_index()))
|
||||||
|
set_cursor(index, SelectionUpdate::Set);
|
||||||
|
|
||||||
if (on_toggle)
|
if (on_toggle)
|
||||||
on_toggle(index, metadata.open);
|
on_toggle(index, metadata.open);
|
||||||
update_column_sizes();
|
update_column_sizes();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue