1
1
//! # Account manager module.
2
2
3
- use std:: collections:: BTreeMap ;
3
+ use std:: collections:: { BTreeMap , BTreeSet } ;
4
4
use std:: future:: Future ;
5
5
use std:: path:: { Path , PathBuf } ;
6
6
@@ -270,9 +270,51 @@ impl Accounts {
270
270
}
271
271
}
272
272
273
- /// Get a list of all account ids.
273
+ /// Gets a list of all account ids in the user-configured order .
274
274
pub fn get_all ( & self ) -> Vec < u32 > {
275
- self . accounts . keys ( ) . copied ( ) . collect ( )
275
+ let mut ordered_ids = Vec :: new ( ) ;
276
+ let mut all_ids: BTreeSet < u32 > = self . accounts . keys ( ) . copied ( ) . collect ( ) ;
277
+
278
+ // First, add accounts in the configured order
279
+ for & id in & self . config . inner . accounts_order {
280
+ if all_ids. remove ( & id) {
281
+ ordered_ids. push ( id) ;
282
+ }
283
+ }
284
+
285
+ // Then add any accounts not in the order list (newly added accounts)
286
+ for id in all_ids {
287
+ ordered_ids. push ( id) ;
288
+ }
289
+
290
+ ordered_ids
291
+ }
292
+
293
+ /// Sets the order of accounts.
294
+ ///
295
+ /// The provided list should contain all account IDs in the desired order.
296
+ /// If an account ID is missing from the list, it will be appended at the end.
297
+ /// If the list contains non-existent account IDs, they will be ignored.
298
+ pub async fn set_accounts_order ( & mut self , order : Vec < u32 > ) -> Result < ( ) > {
299
+ let existing_ids: BTreeSet < u32 > = self . accounts . keys ( ) . copied ( ) . collect ( ) ;
300
+
301
+ // Filter out non-existent account IDs
302
+ let mut filtered_order: Vec < u32 > = order
303
+ . into_iter ( )
304
+ . filter ( |id| existing_ids. contains ( id) )
305
+ . collect ( ) ;
306
+
307
+ // Add any missing account IDs at the end
308
+ for & id in & existing_ids {
309
+ if !filtered_order. contains ( & id) {
310
+ filtered_order. push ( id) ;
311
+ }
312
+ }
313
+
314
+ self . config . inner . accounts_order = filtered_order;
315
+ self . config . sync ( ) . await ?;
316
+ self . emit_event ( EventType :: AccountsChanged ) ;
317
+ Ok ( ( ) )
276
318
}
277
319
278
320
/// Starts background tasks such as IMAP and SMTP loops for all accounts.
@@ -415,6 +457,10 @@ struct InnerConfig {
415
457
pub selected_account : u32 ,
416
458
pub next_id : u32 ,
417
459
pub accounts : Vec < AccountConfig > ,
460
+ /// Ordered list of account IDs, representing the user's preferred order.
461
+ /// If an account ID is not in this list, it will be appended at the end.
462
+ #[ serde( default ) ]
463
+ pub accounts_order : Vec < u32 > ,
418
464
}
419
465
420
466
impl Drop for Config {
@@ -481,6 +527,7 @@ impl Config {
481
527
accounts : Vec :: new ( ) ,
482
528
selected_account : 0 ,
483
529
next_id : 1 ,
530
+ accounts_order : Vec :: new ( ) ,
484
531
} ;
485
532
if !lock {
486
533
let cfg = Self {
@@ -613,6 +660,10 @@ impl Config {
613
660
uuid,
614
661
} ) ;
615
662
self . inner . next_id += 1 ;
663
+
664
+ // Add new account to the end of the order list
665
+ self . inner . accounts_order . push ( id) ;
666
+
616
667
id
617
668
} ;
618
669
@@ -634,6 +685,10 @@ impl Config {
634
685
// remove account from the configs
635
686
self . inner . accounts . remove ( idx) ;
636
687
}
688
+
689
+ // Remove from order list as well
690
+ self . inner . accounts_order . retain ( |& x| x != id) ;
691
+
637
692
if self . inner . selected_account == id {
638
693
// reset selected account
639
694
self . inner . selected_account = self
0 commit comments