~buzzert/Kordophone#9: gtk v2: Conversation selected state lost when reloading
This commit is contained in:
@@ -11,6 +11,9 @@ public class ConversationListView : Adw.Bin
|
||||
private Adw.HeaderBar header_bar;
|
||||
private ConversationListModel conversation_model;
|
||||
|
||||
private string? selected_conversation_guid = null;
|
||||
private bool selection_update_queued = false;
|
||||
|
||||
public ConversationListView () {
|
||||
container = new Adw.ToolbarView ();
|
||||
set_child (container);
|
||||
@@ -25,7 +28,10 @@ public class ConversationListView : Adw.Bin
|
||||
|
||||
list_box.row_selected.connect ((row) => {
|
||||
var conversation_row = (ConversationRow?) row;
|
||||
conversation_selected(conversation_row != null ? conversation_row.conversation.guid : null);
|
||||
if (conversation_row != null) {
|
||||
selected_conversation_guid = conversation_row.conversation.guid;
|
||||
conversation_selected(selected_conversation_guid);
|
||||
}
|
||||
});
|
||||
|
||||
header_bar = new Adw.HeaderBar ();
|
||||
@@ -44,9 +50,43 @@ public class ConversationListView : Adw.Bin
|
||||
|
||||
// Set up model and bind to list
|
||||
conversation_model = new ConversationListModel ();
|
||||
conversation_model.items_changed.connect (on_items_changed);
|
||||
list_box.bind_model (conversation_model, create_conversation_row);
|
||||
}
|
||||
|
||||
private void on_items_changed (uint position, uint removed, uint added) {
|
||||
enqueue_selection_update();
|
||||
}
|
||||
|
||||
private void enqueue_selection_update() {
|
||||
if (selection_update_queued) {
|
||||
return;
|
||||
}
|
||||
|
||||
selection_update_queued = true;
|
||||
GLib.Idle.add(() => {
|
||||
update_selection();
|
||||
selection_update_queued = false;
|
||||
return false;
|
||||
}, GLib.Priority.HIGH);
|
||||
}
|
||||
|
||||
private void update_selection() {
|
||||
// Re-select selected_conversation_guid, if it has changed.
|
||||
if (selected_conversation_guid != null) {
|
||||
for (uint i = 0; i < conversation_model.get_n_items(); i++) {
|
||||
var conversation = (Conversation) conversation_model.get_item(i);
|
||||
if (conversation.guid == selected_conversation_guid) {
|
||||
var row = list_box.get_row_at_index((int)i);
|
||||
if (row != null) {
|
||||
list_box.select_row(row);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Widget create_conversation_row (Object item) {
|
||||
Conversation conversation = (Conversation) item;
|
||||
return new ConversationRow (conversation);
|
||||
|
||||
Reference in New Issue
Block a user