session-list: Make sure sessions are always listed in the same order

This commit is contained in:
Kévin Commaille 2023-11-29 18:54:58 +01:00
parent 589ae9b840
commit 6754e9653f
No known key found for this signature in database
GPG key ID: 29A48C1F03620416
2 changed files with 26 additions and 3 deletions

View file

@ -1,3 +1,5 @@
use std::cmp::Ordering;
use gettextrs::gettext; use gettextrs::gettext;
use gtk::{gio, glib, glib::clone, prelude::*, subclass::prelude::*}; use gtk::{gio, glib, glib::clone, prelude::*, subclass::prelude::*};
use indexmap::map::IndexMap; use indexmap::map::IndexMap;
@ -233,8 +235,24 @@ impl SessionList {
let handle = spawn_tokio!(secret::restore_sessions()); let handle = spawn_tokio!(secret::restore_sessions());
match handle.await.unwrap() { match handle.await.unwrap() {
Ok(sessions) => { Ok(mut sessions) => {
self.settings().load(); let settings = self.settings();
settings.load();
let session_ids = settings.session_ids();
// Keep the order from the settings.
sessions.sort_by(|a, b| {
let pos_a = session_ids.get_index_of(a.id());
let pos_b = session_ids.get_index_of(b.id());
match (pos_a, pos_b) {
(Some(pos_a), Some(pos_b)) => pos_a.cmp(&pos_b),
// Keep unknown sessions at the end.
(Some(_), None) => Ordering::Greater,
(None, Some(_)) => Ordering::Less,
_ => Ordering::Equal,
}
});
for stored_session in sessions { for stored_session in sessions {
info!( info!(

View file

@ -1,5 +1,5 @@
use gtk::{glib, prelude::*, subclass::prelude::*}; use gtk::{glib, prelude::*, subclass::prelude::*};
use indexmap::IndexMap; use indexmap::{IndexMap, IndexSet};
use tracing::error; use tracing::error;
use crate::{ use crate::{
@ -105,6 +105,11 @@ impl SessionListSettings {
self.imp().sessions.borrow_mut().remove(session_id); self.imp().sessions.borrow_mut().remove(session_id);
self.save(); self.save();
} }
/// Get the list of session IDs stored in these settings.
pub fn session_ids(&self) -> IndexSet<String> {
self.imp().sessions.borrow().keys().cloned().collect()
}
} }
impl Default for SessionListSettings { impl Default for SessionListSettings {