1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:37:35 +00:00

GTreeView: Switch to using GModelSelection

We don't support multi-select in GTreeView yet. Some day though :^)
This commit is contained in:
Andreas Kling 2019-09-07 20:15:33 +02:00
parent f8c0168adc
commit 56c360591c

View file

@ -69,9 +69,8 @@ void GTreeView::mousedown_event(GMouseEvent& event)
if (!index.is_valid()) if (!index.is_valid())
return; return;
if (model.selected_index() != index) { if (selection().first() != index) {
model.set_selected_index(index); selection().set(index);
update();
} }
if (is_toggle && model.row_count(index)) if (is_toggle && model.row_count(index))
@ -170,7 +169,7 @@ void GTreeView::paint_event(GPaintEvent& event)
icon_rect.right() + 1 + icon_spacing(), rect.y(), icon_rect.right() + 1 + icon_spacing(), rect.y(),
rect.width() - icon_size() - icon_spacing(), rect.height() rect.width() - icon_size() - icon_spacing(), rect.height()
}; };
if (index == model.selected_index()) { if (selection().contains(index)) {
background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060); background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060);
text_color = Color::from_rgb(0xffffff); text_color = Color::from_rgb(0xffffff);
painter.fill_rect(text_rect, background_color); painter.fill_rect(text_rect, background_color);
@ -232,8 +231,7 @@ void GTreeView::did_update_model()
void GTreeView::did_update_selection() void GTreeView::did_update_selection()
{ {
ASSERT(model()); ASSERT(model());
auto& model = *this->model(); auto index = selection().first();
auto index = model.selected_index();
if (!index.is_valid()) if (!index.is_valid())
return; return;
bool opened_any = false; bool opened_any = false;
@ -267,7 +265,7 @@ void GTreeView::keydown_event(GKeyEvent& event)
{ {
if (!model()) if (!model())
return; return;
auto cursor_index = model()->selected_index(); auto cursor_index = selection().first();
if (event.key() == KeyCode::Key_Space) { if (event.key() == KeyCode::Key_Space) {
if (model()->row_count(cursor_index)) if (model()->row_count(cursor_index))
@ -287,7 +285,7 @@ void GTreeView::keydown_event(GKeyEvent& event)
return IterationDecision::Continue; return IterationDecision::Continue;
}); });
if (found_index.is_valid()) { if (found_index.is_valid()) {
model()->set_selected_index(found_index); selection().set(found_index);
update(); update();
} }
return; return;
@ -304,7 +302,7 @@ void GTreeView::keydown_event(GKeyEvent& event)
return IterationDecision::Continue; return IterationDecision::Continue;
}); });
if (found_index.is_valid()) if (found_index.is_valid())
model()->set_selected_index(found_index); selection().set(found_index);
return; return;
} }
} }